Token导航 LogoToken导航TokenDH.com
研究检索操作浏览器github未标认证来源可访问许可证需确认审计通过

pretextpretext 搜索

Agent Skill

pretext 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

1,978

周安装

80

GitHub Stars

12

下载量

621
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:pretext(pretext 搜索)
来源仓库:https://github.com/asharibali/pretext-skills
仓库路径:skills/pretext
安装命令:
npx skills add https://github.com/asharibali/pretext-skills --skill pretext
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/asharibali/pretext-skills --skill pretext

简介

pretext 是纯 JavaScript 文本测量与排版库,无需 DOM 即可计算多行文本布局。

  • 它使用 canvas measureText 作为基准,适用于服务端渲染与性能敏感场景。
  • 支持 DOM、Canvas、SVG 输出,减少浏览器重排开销,提升渲染效率。
  • 安装来自 GitHub,注意浏览器兼容性,避免在旧版环境中使用高级特性。
  • pretext 属于研究检索类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Pretext – DOM-Free Text Measurement & Layout

Pretext is a pure-JavaScript/TypeScript library that measures and lays out multiline text without touching the DOM. It uses the browser's own font engine (via canvas measureText) as ground truth, then does all line-breaking and height calculation in pure arithmetic. Supports rendering to DOM, Canvas, SVG, and (eventually) server-side.

Why This Matters

DOM-based text measurement (getBoundingClientRect, offsetHeight) triggers layout reflow — one of the most expensive operations in a browser. Every call forces the engine to recalculate styles and geometry for potentially the entire document. In a virtualized list with 10,000 rows, that means 10,000 reflows just to get row heights.

Pretext replaces that with a two-phase approach: one prepare() call does text analysis, segmentation, glue rules, and canvas measurement (the expensive part, done once), then layout() computes height from cached widths using pure math (the cheap part, called on every resize). This is 300-600x faster than DOM measurement, and it eliminates layout thrashing entirely.

The result is pixel-perfect across browsers (7680/7680 on Chrome, Safari, and Firefox accuracy sweeps), supports all languages including emoji, mixed-bidi, CJK, Thai, Arabic, Khmer, Myanmar, Urdu, and more.

Installation

npm install @chenglou/pretext

Two Paths: Pick the Simpler One

Fast Path (90% of cases — you just need the height)

import { prepare, layout } from '@chenglou/pretext'

const prepared = prepare('AGI 春天到了. بدأت الرحلة 🚀‎', '16px Inter')
const { height, lineCount } = layout(prepared, maxWidth, 20)

This covers virtualized lists, dynamic containers, scroll anchoring — anything where you need to know how tall text will be at a given width. The prepared handle is reusable: on resize, just call layout() again with the new width.

For textarea-like text where spaces, tabs, and \n hard breaks stay visible:

const prepared = prepare(textareaValue, '16px Inter', { whiteSpace: 'pre-wrap' })
const { height } = layout(prepared, textareaWidth, 20)

For CJK/Hangul text that should avoid breaking within words:

const prepared = prepare(text, '16px Inter', { wordBreak: 'keep-all' })

Rich Path (you need the actual lines)

When you need to render text yourself (Canvas, SVG, WebGL) or do variable-width layout (flowing around images), switch to the rich APIs:

import { prepareWithSegments, layoutWithLines, walkLineRanges, layoutNextLine, layoutNextLineRange, materializeLineRange, measureLineStats, measureNaturalWidth } from '@chenglou/pretext'
  • layoutWithLines() — all lines at a fixed width (Canvas/SVG rendering)
  • walkLineRanges() — line ranges without building strings (shrink-wrap, binary search for optimal width, balanced text layout)
  • measureLineStats() — line count and widest line width without any allocations
  • measureNaturalWidth() — widest forced line when width is not the thing causing wraps
  • layoutNextLine() — one line at a time with varying width (text flowing around obstacles), returns full line with text
  • layoutNextLineRange() — same as above but without building the text string; use materializeLineRange() to get the text when needed

Rich-Text Inline Flow (multi-font, chips, mentions)

For rich-text inline flow with mixed fonts, code spans, mentions, chips, and browser-like boundary whitespace collapse:

import { prepareRichInline, walkRichInlineLineRanges, materializeRichInlineLineRange, measureRichInlineStats } from '@chenglou/pretext/rich-inline'

const prepared = prepareRichInline([
  { text: 'Ship ', font: '500 17px Inter' },
  { text: '@maya', font: '700 12px Inter', break: 'never', extraWidth: 22 },
  { text: "'s rich-note", font: '500 17px Inter' },
])

walkRichInlineLineRanges(prepared, 320, range => {
  const line = materializeRichInlineLineRange(prepared, range)
  // each fragment keeps its source item index, text slice, gapBefore, and cursors
})

The rich-inline helper is intentionally narrow:

  • Raw inline text in, including boundary spaces
  • Caller-owned extraWidth for pill chrome (padding + border width)
  • break: 'never' for atomic items like chips and mentions
  • white-space: normal only
  • Not a nested markup tree and not a general CSS inline formatting engine

API Reference

Fast-path APIs

prepare(text: string, font: string, options?: {
  whiteSpace?: 'normal' | 'pre-wrap',
  wordBreak?: 'normal' | 'keep-all'
}): PreparedText

layout(prepared: PreparedText, maxWidth: number, lineHeight: number): {
  height: number,
  lineCount: number
}

Rich-path APIs

prepareWithSegments(text: string, font: string, options?: {
  whiteSpace?: 'normal' | 'pre-wrap',
  wordBreak?: 'normal' | 'keep-all'
}): PreparedTextWithSegments

layoutWithLines(prepared: PreparedTextWithSegments, maxWidth: number, lineHeight: number): {
  height: number, lineCount: number, lines: LayoutLine[]
}

walkLineRanges(prepared: PreparedTextWithSegments, maxWidth: number,
  onLine: (line: LayoutLineRange) => void): number

measureLineStats(prepared: PreparedTextWithSegments, maxWidth: number): {
  lineCount: number, maxLineWidth: number
}

measureNaturalWidth(prepared: PreparedTextWithSegments): number

layoutNextLineRange(prepared: PreparedTextWithSegments, start: LayoutCursor, maxWidth: number): LayoutLineRange | null

layoutNextLine(prepared: PreparedTextWithSegments, start: LayoutCursor, maxWidth: number): LayoutLine | null

materializeLineRange(prepared: PreparedTextWithSegments, line: LayoutLineRange): LayoutLine

Rich-inline APIs (@chenglou/pretext/rich-inline)

prepareRichInline(items: RichInlineItem[]): PreparedRichInline

layoutNextRichInlineLineRange(prepared: PreparedRichInline, maxWidth: number, start?: RichInlineCursor): RichInlineLineRange | null

walkRichInlineLineRanges(prepared: PreparedRichInline, maxWidth: number,
  onLine: (line: RichInlineLineRange) => void): number

materializeRichInlineLineRange(prepared: PreparedRichInline, line: RichInlineLineRange): RichInlineLine

measureRichInlineStats(prepared: PreparedRichInline, maxWidth: number): {
  lineCount: number, maxLineWidth: number
}

Utility APIs

clearCache(): void   // release internal caches used by prepare/prepareWithSegments
setLocale(locale?: string): void  // retarget word segmenter for future prepare() calls; also clears cache

Key Types

type LayoutLine = {
  text: string         // full text of the line
  width: number        // measured pixel width
  start: LayoutCursor  // inclusive start cursor
  end: LayoutCursor    // exclusive end cursor
}

type LayoutLineRange = {
  width: number
  start: LayoutCursor
  end: LayoutCursor
}

type LayoutCursor = {
  segmentIndex: number   // position in prepared segments
  graphemeIndex: number  // grapheme within that segment
}

type RichInlineItem = {
  text: string            // raw text including boundary spaces
  font: string            // canvas font shorthand
  break?: 'normal' | 'never'  // 'never' = atomic (chips, mentions)
  extraWidth?: number     // horizontal chrome width (padding + border)
}

type RichInlineFragment = {
  itemIndex: number       // index into original items array
  text: string
  gapBefore: number       // collapsed boundary gap
  occupiedWidth: number   // text width + extraWidth
  start: LayoutCursor
  end: LayoutCursor
}

type RichInlineLine = {
  fragments: RichInlineFragment[]
  width: number
  end: RichInlineCursor
}

Key Things to Get Right

  • Cache prepared handles. Same text + same font = reuse the handle. Only call prepare() again if the text or font actually changed. On resize, just re-call layout().
  • Match your CSS exactly. The font string must match your CSS font shorthand (size, weight, style, family). The lineHeight must match your CSS line-height. Mismatches produce wrong heights.
  • Use {whiteSpace: 'pre-wrap'} for textareas where spaces, tabs, and newlines should be preserved. Tabs follow default browser-style tab-size: 8 stops.
  • Use {wordBreak: 'keep-all'} for CJK/Hangul when you want to avoid breaking within words.
  • Avoid system-ui on macOS — canvas and DOM can resolve to different fonts (SF Pro Text vs SF Pro Display), breaking accuracy. Use a named font.
  • clearCache() is rarely needed — only when cycling through many different fonts and you want to release memory.
  • setLocale() is optional — by default Pretext uses the current locale. Call it before prepare() if you need a specific Intl.Segmenter locale.

Supported CSS Target

  • white-space: normal (default) and pre-wrap
  • word-break: normal (default) and keep-all
  • overflow-wrap: break-word — narrow widths can break inside words, but only at grapheme boundaries
  • line-break: auto
  • Tabs follow default browser-style tab-size: 8

What Pretext Handles Under the Hood

  • Segmentation: text analysis, normalization, Unicode-aware word/grapheme segmentation via Intl.Segmenter
  • CJK: astral ideographs, compatibility blocks, extension blocks, kinsoku (line-start/end prohibited punctuation)
  • Arabic/RTL: no-space punctuation cluster merging, punctuation-plus-mark clusters, bidi metadata on the rich path
  • Emoji: auto-detected per-font correction for canvas/DOM width discrepancy, ZWJ sequences
  • Soft hyphens: invisible when unbroken, visible trailing - when the break is chosen
  • NBSP/NNBSP/WJ: preserved as visible content, prevents word-boundary wrapping
  • ZWSP: preserved as zero-width break opportunity
  • URL-like runs: modeled as structured breakable units (path + query) to avoid bad mid-path breaks
  • Mixed script: handles mixed-bidi, CJK-leading no-space runs, contextual ASCII quotes, numeric/time-range expressions

Ready-to-Use Examples

See the references/examples/ directory for complete, copy-paste-ready code:

Fast-path (height measurement):

  • basic-height.ts — measure text height for a container, with wordBreak: 'keep-all'
  • pre-wrap-textarea.ts — auto-size a textarea with preserved whitespace and tabs
  • resize-handler.ts — canonical resize pattern: prepare() once, layout() on every resize
  • virtualized-list.ts — variable row heights for virtualized lists with cached handles
  • scroll-anchor.ts — prevent layout shift by predicting height before DOM insertion
  • label-overflow-check.ts — CI/dev-time check that labels fit without wrapping
  • multilingual-layout.ts — locale-aware measurement with setLocale() for CJK, Arabic, Thai

Rich-path (manual line layout):

  • canvas-manual-layout.ts — render wrapped text on a canvas with layoutWithLines
  • svg-text-render.ts — render wrapped text into SVG <tspan> elements
  • flow-around-image.ts — route text around a floated image using layoutNextLineRange
  • two-column-flow.ts — continuous two-column editorial flow with layoutNextLineRange
  • shrink-wrap.ts — find tightest container width with walkLineRanges and measureLineStats
  • balanced-text.ts — binary-search for balanced line widths to avoid orphan lines
  • natural-width.ts — tooltip/popover sizing with measureNaturalWidth
  • bubble-chat.ts — chat bubble shrink-wrap: find tightest width, then render lines

Rich-inline (multi-font):

  • rich-inline.ts — multi-font inline flow with atomic chips and mentions

Deep Reference

  • references/README.md — Full API surface from the library repo: installation, complete type definitions, all function signatures, usage examples, and caveats.
  • references/AGENTS.md — Agent-specific guidance: important source files, implementation notes, practical patterns, gotchas, open questions, and decision heuristics for building with Pretext.
  • references/STATUS.md — Current accuracy dashboard and benchmark snapshot pointers (browser sweep results, corpus canaries).
  • references/RESEARCH.md — Research log: everything tried, measured, and learned — system-ui discovery, word-sum accuracy, Arabic frontier, soft hyphens, emoji correction, CJK/Thai/Khmer/Myanmar/Japanese/Chinese corpus canaries, and rejected approaches.
  • references/DEVELOPMENT.md — Development setup, day-to-day commands, packaging/release checks, browser accuracy and benchmarking commands, corpus tooling, and deep profiling workflow.
  • references/CHANGELOG.md — Version history from 0.0.0 (2026-03-26) through unreleased changes including keep-all support and rich-inline APIs.
  • references/TODO.md — Current priorities: canary maintenance, next engine work, demo direction, and open design questions.
  • references/SECURITY.md — Security policy and private vulnerability reporting via GitHub advisories.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

34.98%
按下载量换算217

Claude

29.3%
按下载量换算182

Cursor

21.3%
按下载量换算132

Gemini CLI

10.16%
按下载量换算63

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

操作浏览器

该 Skill 可能涉及浏览器控制能力,使用时可能读取或操作网页内容,需要在受控环境中确认权限边界。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills