@adea-ai/ui 0.161.7 → 0.161.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/ui/message/parse-message-text.d.ts.map +1 -1
- package/dist/components/ui/message/parse-message-text.js +7 -1
- package/dist/components/ui/message/parse-message-text.js.map +1 -1
- package/dist/lib/observe-resize.d.ts.map +1 -1
- package/dist/lib/observe-resize.js +16 -7
- package/dist/lib/observe-resize.js.map +1 -1
- package/dist/r/src/components/ui/message/parse-message-text.ts +9 -1
- package/dist/r/src/lib/observe-resize.ts +21 -7
- package/package.json +1 -1
- package/src/components/ui/message/parse-message-text.ts +9 -1
- package/src/lib/observe-resize.ts +21 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-message-text.d.ts","sourceRoot":"","sources":["../../../../src/components/ui/message/parse-message-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC,OAAO,EAAE,QAAQ,EAAE,CAAA;AAEnB,MAAM,MAAM,aAAa,GACrB,QAAQ,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,GACxC,QAAQ,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,GACxC,QAAQ,CAAC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAA;CAAE,CAAC,GAChE,QAAQ,CAAC;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAA;CAAE,CAAC,GAC5D,QAAQ,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,SAAS,aAAa,EAAE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YAAY,EAAE,CAAA;IAC/B,iEAAiE;IACjE,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;CAC1B,CAAC,CAAA;AAEF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,YAAY,GAAG,SAAS,EAClC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,mBAAwB,GAChC,YAAY,CAmCd"}
|
|
@@ -239,7 +239,13 @@ function parseInline(text, depth = 0, options = {}) {
|
|
|
239
239
|
continue;
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
|
-
if (depth < MAX_INLINE_DEPTH && char === "[") {
|
|
242
|
+
if (depth < MAX_INLINE_DEPTH && char === "[" && paren !== -1) {
|
|
243
|
+
if (paren < index) paren = text.indexOf(")", index + 1);
|
|
244
|
+
if (paren === -1) {
|
|
245
|
+
buffer += char;
|
|
246
|
+
index += 1;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
243
249
|
brackets ??= bracketMatches(text);
|
|
244
250
|
const labelEnd = brackets.get(index) ?? -1;
|
|
245
251
|
if (labelEnd > index && text[labelEnd + 1] === "(") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-message-text.js","names":[],"sources":["../../../../src/components/ui/message/parse-message-text.ts"],"sourcesContent":["/**\n * A small, safe reading of model and user text.\n *\n * Assistant output is untrusted: it can contain anything a model, a tool result\n * or a pasted web page put there. This parser therefore produces *data* —\n * blocks and inline spans — and the renderer turns that data into text nodes and\n * a fixed set of elements. There is no HTML pass, no `innerHTML`, and no\n * attribute built from the text other than a link `href` that has passed\n * `safeHref`.\n *\n * It reads a deliberately small subset of Markdown: fenced code, headings,\n * flat lists, block quotes, paragraphs, and inline code, bold, italic and links.\n * Anything it does not recognise stays as paragraph text with its line breaks\n * (the paragraph renders `white-space: pre-wrap`), so unsupported syntax\n * degrades to readable source instead of vanishing.\n *\n * A citation marker, `[^1]` or `[^1,2]`, is read only when the caller says how\n * many sources it has (`citations`) and every number names one of them. A marker\n * the host cannot resolve stays literal text: it is never a citation by shape\n * alone, and `arr[1]` or a footnote the model invented is not one either.\n *\n * Streaming is a first-class input. `streaming: true` means the text may end\n * mid-construct, so an unterminated fence is a *partial* code block — shown as\n * code from its first line, flagged incomplete — and an unterminated inline\n * span stays literal until its closing delimiter arrives. Once streaming stops,\n * an unterminated fence is just a code block that ends at the end of the text.\n */\n\nimport { safeHref } from '#lib/url'\n\nexport { safeHref }\n\nexport type MessageInline =\n | Readonly<{ kind: 'text'; text: string }>\n | Readonly<{ kind: 'code'; text: string }>\n | Readonly<{ kind: 'strong'; children: readonly MessageInline[] }>\n | Readonly<{ kind: 'em'; children: readonly MessageInline[] }>\n | Readonly<{ kind: 'link'; href: string; children: readonly MessageInline[] }>\n /** A resolved citation marker: 1-based source numbers, each within `citations`. */\n | Readonly<{ kind: 'cite'; numbers: readonly number[]; raw: string }>\n\nexport type MessageBlock =\n | Readonly<{ kind: 'paragraph'; inline: readonly MessageInline[]; raw: string }>\n | Readonly<{\n kind: 'heading'\n level: 1 | 2 | 3 | 4\n inline: readonly MessageInline[]\n raw: string\n }>\n | Readonly<{\n kind: 'list'\n ordered: boolean\n start: number\n items: readonly (readonly MessageInline[])[]\n raw: string\n }>\n | Readonly<{ kind: 'quote'; inline: readonly MessageInline[]; raw: string }>\n | Readonly<{\n kind: 'code'\n language: string | undefined\n code: string\n /** False while the fence is still open and the text may still grow. */\n complete: boolean\n raw: string\n }>\n\nconst WHITESPACE = /\\s/\n\n/**\n * An opening fence: up to three spaces, a run of three or more backticks or of\n * three or more tildes, then an info string that holds no backtick. The first\n * word of the info string is the language. This is a scan, not one pattern,\n * because a pattern whose classes overlap (`[^\\s`]*` then `[^`]*`) retries every\n * split of a long info string when a stray backtick ends it: three backticks, a\n * long run of `a` and a trailing backtick took quadratic time.\n */\nfunction fenceOpen(line: string): Readonly<{ marker: string; language: string }> | undefined {\n let start = 0\n while (start < 3 && line.charCodeAt(start) === 32) start += 1\n const mark = line[start]\n if (mark !== '`' && mark !== '~') return undefined\n let end = start + 1\n while (line[end] === mark) end += 1\n if (end - start < 3) return undefined\n if (line.includes('`', end)) return undefined\n let wordStart = end\n while (line[wordStart] === ' ' || line[wordStart] === '\\t') wordStart += 1\n let wordEnd = wordStart\n while (wordEnd < line.length && !WHITESPACE.test(line[wordEnd]!)) wordEnd += 1\n return { marker: line.slice(start, end), language: line.slice(wordStart, wordEnd) }\n}\ntype FenceOpen = NonNullable<ReturnType<typeof fenceOpen>>\n\n/**\n * The body of a fence that opened on `lines[start - 1]`: every line up to a\n * closing run of the same mark at least as long as the opening one, or to the end\n * of the text when none comes.\n */\nfunction readFence(\n lines: readonly string[],\n start: number,\n fence: FenceOpen\n): Readonly<{ code: string; closed: boolean; next: number }> {\n const body: string[] = []\n let index = start\n while (index < lines.length) {\n const candidate = lines[index]!\n const close = /^ {0,3}(`{3,}|~{3,})[ \\t]*$/.exec(candidate)\n if (close && close[1]![0] === fence.marker[0] && close[1]!.length >= fence.marker.length)\n return { code: body.join('\\n'), closed: true, next: index + 1 }\n body.push(candidate)\n index += 1\n }\n return { code: body.join('\\n'), closed: false, next: index }\n}\n\nexport type FenceSegment =\n | Readonly<{ kind: 'prose'; text: string }>\n | Readonly<{\n kind: 'code'\n language: string | undefined\n code: string\n /** False while the fence is still open and the text may still grow. */\n complete: boolean\n }>\n\n/**\n * Text split into fenced code and the prose between, by the same fence grammar\n * `parseMessageText` reads. For a renderer that draws code blocks but leaves the\n * prose as written (`MessageBody`).\n */\nexport function splitCodeFences(\n text: string,\n options: { streaming?: boolean } = {}\n): readonly FenceSegment[] {\n const lines = text.replace(/\\r\\n?/g, '\\n').split('\\n')\n const segments: FenceSegment[] = []\n let prose: string[] = []\n const flushProse = () => {\n if (prose.length > 0) segments.push({ kind: 'prose', text: prose.join('\\n') })\n prose = []\n }\n let index = 0\n while (index < lines.length) {\n const line = lines[index]!\n const fence = fenceOpen(line)\n if (!fence) {\n prose.push(line)\n index += 1\n continue\n }\n flushProse()\n const block = readFence(lines, index + 1, fence)\n segments.push({\n kind: 'code',\n language: fence.language || undefined,\n code: block.code,\n complete: block.closed || !options.streaming,\n })\n index = block.next\n }\n flushProse()\n return segments\n}\n\n// The lookahead after each marker's spaces makes that run maximal, so a line\n// that fails later (`.` stops at U+2028) cannot retry every shorter run.\nconst BULLET = /^ {0,3}[-*+][ \\t]+(?![ \\t])(.*)$/\nconst ORDERED = /^ {0,3}(\\d{1,9})[.)][ \\t]+(?![ \\t])(.*)$/\nconst QUOTE = /^ {0,3}>[ \\t]?(.*)$/\n\nconst isBlank = (char: string | undefined) => char === ' ' || char === '\\t'\nconst LINE_TERMINATOR = /[\\n\\r\\u2028\\u2029]/\n\n/**\n * An ATX heading: up to three spaces, one to four `#`, spaces or tabs, then the\n * text, with any trailing spaces and closing `#` run removed. This is a scan for\n * the reason `fenceOpen` is: the equivalent pattern (`(.+?)[ \\t]*#*[ \\t]*$`)\n * retries the closing check from every position of a long run of spaces.\n */\nfunction headingOf(line: string): Readonly<{ level: 1 | 2 | 3 | 4; text: string }> | undefined {\n let start = 0\n while (start < 3 && line.charCodeAt(start) === 32) start += 1\n let marks = start\n while (line[marks] === '#') marks += 1\n const level = marks - start\n if (level < 1 || level > 4 || !isBlank(line[marks])) return undefined\n let textStart = marks\n while (isBlank(line[textStart])) textStart += 1\n if (textStart === line.length) {\n // Only blanks follow the marker: the text is the last of them, which needs a second one.\n return textStart - marks >= 2\n ? { level: level as 1 | 2 | 3 | 4, text: line[line.length - 1]! }\n : undefined\n }\n if (LINE_TERMINATOR.test(line.slice(textStart))) return undefined\n // Drop the longest tail of blanks, a run of `#`, and blanks again.\n let textEnd = line.length\n while (textEnd > textStart && isBlank(line[textEnd - 1])) textEnd -= 1\n while (textEnd > textStart && line[textEnd - 1] === '#') textEnd -= 1\n while (textEnd > textStart && isBlank(line[textEnd - 1])) textEnd -= 1\n // Text that is nothing but a closing run keeps its first character.\n return {\n level: level as 1 | 2 | 3 | 4,\n text: line.slice(textStart, Math.max(textEnd, textStart + 1)),\n }\n}\n\nconst MAX_INLINE_DEPTH = 4\n\n/** A marker may cite at most this many sources, so one cannot flood a sentence. */\nconst MAX_CITED_SOURCES = 6\nconst CITATION = /\\[\\^(\\d{1,4}(?:[ \\t]*,[ \\t]*\\d{1,4}){0,5})\\]/y\n\nexport type ParseInlineOptions = {\n /** How many sources the host has. Without it no marker is a citation. */\n citations?: number\n /** Inside a link label, where a citation (itself a button) must not nest. */\n inLink?: boolean\n}\n\nfunction citationAt(text: string, index: number, count: number): MessageInline | undefined {\n CITATION.lastIndex = index\n const match = CITATION.exec(text)\n if (!match) return undefined\n const numbers = [...new Set(match[1]!.split(',').map((part) => Number(part.trim())))]\n if (numbers.length > MAX_CITED_SOURCES) return undefined\n if (numbers.some((number) => number < 1 || number > count)) return undefined\n return { kind: 'cite', numbers, raw: match[0] }\n}\n\n/** Inline spans. Delimiters must close on the same block, or they stay literal. */\nexport function parseInline(\n text: string,\n depth = 0,\n options: ParseInlineOptions = {}\n): readonly MessageInline[] {\n const out: MessageInline[] = []\n let brackets: ReadonlyMap<number, number> | undefined\n let paren = -2\n let buffer = ''\n const flush = () => {\n if (buffer) out.push({ kind: 'text', text: buffer })\n buffer = ''\n }\n let index = 0\n while (index < text.length) {\n const char = text[index]!\n if (char === '`') {\n let ticks = 1\n while (text[index + ticks] === '`') ticks += 1\n const fence = '`'.repeat(ticks)\n const close = text.indexOf(fence, index + ticks)\n if (close > index + ticks) {\n flush()\n out.push({ kind: 'code', text: text.slice(index + ticks, close).replace(/^ (.*) $/, '$1') })\n index = close + ticks\n continue\n }\n buffer += fence\n index += ticks\n continue\n }\n if (depth < MAX_INLINE_DEPTH && (char === '*' || char === '_')) {\n const double = text[index + 1] === char\n const delimiter = double ? char + char : char\n const start = index + delimiter.length\n // Word-internal underscores cannot open emphasis. Reject before scanning,\n // and consume both marks so the second cannot rescan the remaining pairs.\n if (char === '_' && index > 0 && /[A-Za-z0-9]/.test(text[index - 1] ?? '')) {\n buffer += delimiter\n index = start\n continue\n }\n const next = text[start]\n // A delimiter opens only before a non-space, so `a * b * c` and `2 * 3` stay arithmetic.\n if (next !== undefined && !/\\s/.test(next)) {\n const close = findClosing(text, delimiter, start)\n if (double && !(close > start && !/\\s/.test(text[close - 1] ?? ' '))) {\n // An opener that does not close stays literal as a pair. Retrying its\n // second mark as emphasis would scan to the end of the line again, so a\n // line of unclosed `**a ` would be quadratic.\n buffer += delimiter\n index = start\n continue\n }\n if (close > start && !/\\s/.test(text[close - 1] ?? ' ')) {\n flush()\n out.push({\n kind: double ? 'strong' : 'em',\n children: parseInline(text.slice(start, close), depth + 1, options),\n })\n index = close + delimiter.length\n continue\n }\n }\n }\n if (char === '[' && text[index + 1] === '^' && options.citations && !options.inLink) {\n const cite = citationAt(text, index, options.citations)\n if (cite && cite.kind === 'cite') {\n flush()\n out.push(cite)\n index += cite.raw.length\n continue\n }\n }\n if (depth < MAX_INLINE_DEPTH && char === '[') {\n brackets ??= bracketMatches(text)\n const labelEnd = brackets.get(index) ?? -1\n if (labelEnd > index && text[labelEnd + 1] === '(') {\n // The nearest `)` is remembered: a run of `[a](` with no `)` after it\n // would otherwise search to the end of the text from every opener.\n if (paren !== -1 && paren < labelEnd + 2) paren = text.indexOf(')', labelEnd + 2)\n const hrefEnd = paren\n if (hrefEnd > labelEnd + 1) {\n const href = safeHref(text.slice(labelEnd + 2, hrefEnd))\n if (href) {\n flush()\n out.push({\n kind: 'link',\n href,\n children: parseInline(text.slice(index + 1, labelEnd), depth + 1, {\n ...options,\n inLink: true,\n }),\n })\n index = hrefEnd + 1\n continue\n }\n }\n }\n }\n buffer += char\n index += 1\n }\n flush()\n return out\n}\n\nfunction findClosing(text: string, delimiter: string, from: number): number {\n let index = from\n while (index < text.length) {\n if (text[index] === '`') {\n // Skip a code span so `**a `**` b**` closes where the code ends.\n const close = text.indexOf('`', index + 1)\n if (close < 0) return -1\n index = close + 1\n continue\n }\n if (text[index] === delimiter[0]) {\n let end = index + 1\n while (text[end] === delimiter[0]) end += 1\n const run = end - index\n if (delimiter.length === 1) {\n // Emphasis closes on a run of one (or an odd run, whose last mark closes).\n // An even run is `**` pairs: strong spans inside the emphasis, which the\n // inner parse reads. One pass, and the scan stops at the next odd run, so\n // a line of unclosed delimiters is still linear.\n if (run % 2 === 1) return end - 1\n } else if (run >= delimiter.length) {\n // Strong closes on the last two marks of a run.\n return end - delimiter.length\n }\n index = end\n continue\n }\n index += 1\n }\n return -1\n}\n\n/**\n * The `]` that closes each `[`, found in one pass. A match never crosses a\n * newline, and an unmatched `[` has no entry. Scanning forward from every `[`\n * instead is quadratic on a long line of unclosed brackets, which a pasted page\n * or a hostile model can use to hang the renderer.\n */\nfunction bracketMatches(text: string): ReadonlyMap<number, number> {\n const matches = new Map<number, number>()\n const open: number[] = []\n for (let index = 0; index < text.length; index += 1) {\n const char = text[index]\n if (char === '[') open.push(index)\n else if (char === ']') {\n const start = open.pop()\n if (start !== undefined) matches.set(start, index)\n } else if (char === '\\n') open.length = 0\n }\n return matches\n}\n\n/** Parse text into blocks. Pure, total, and linear in the length of `text`. */\nexport function parseMessageText(\n text: string,\n options: { streaming?: boolean; citations?: number } = {}\n): readonly MessageBlock[] {\n return parseLines(text.replace(/\\r\\n?/g, '\\n'), options).blocks\n}\n\nexport type MessageParseOptions = { streaming?: boolean; citations?: number }\n\n/**\n * A parse that the next, longer text can continue from. See\n * {@link parseMessageTextIncremental}.\n */\nexport type MessageParse = Readonly<{\n /** The normalised text that was parsed. */\n text: string\n streaming: boolean\n citations: number\n blocks: readonly MessageBlock[]\n /** The offset in `text` where each block's first line starts. */\n starts: readonly number[]\n}>\n\n/**\n * `parseMessageText`, resumed.\n *\n * A streaming reply grows by appending, and reparsing the whole text per token\n * makes a long reply quadratic. When `text` extends `previous.text` under the same\n * options, only its final line can still change, since it may be unfinished. A block\n * that ends before that line is terminated by a finished line (a blank line, a\n * closing fence, or the line that starts the next block), so appending cannot\n * change it. Only the tail from the last block that starts before the final line\n * is parsed again, and the earlier blocks are returned as the same objects. The\n * parser keeps no state across a block boundary, so the tail parsed alone gives\n * exactly the blocks a full parse gives there. Anything else (an edit, a\n * different option) is a full parse.\n */\nexport function parseMessageTextIncremental(\n previous: MessageParse | undefined,\n text: string,\n options: MessageParseOptions = {}\n): MessageParse {\n const normalized = text.replace(/\\r\\n?/g, '\\n')\n const streaming = options.streaming ?? false\n const citations = options.citations ?? 0\n if (\n previous &&\n previous.streaming === streaming &&\n previous.citations === citations &&\n previous.blocks.length > 0 &&\n normalized.length >= previous.text.length &&\n normalized.startsWith(previous.text)\n ) {\n // Only the final line can still change (it may not be finished), and with it\n // the block it belongs to or the one it ends. Resume from the last block that\n // starts before that line: every block before it ends at a finished line.\n const lastLine = previous.text.lastIndexOf('\\n') + 1\n let keep = previous.starts.length - 1\n while (keep >= 0 && previous.starts[keep]! >= lastLine) keep -= 1\n if (keep > 0) {\n const resumeAt = previous.starts[keep]!\n const tail = parseLines(normalized.slice(resumeAt), options)\n return {\n text: normalized,\n streaming,\n citations,\n blocks: [...previous.blocks.slice(0, keep), ...tail.blocks],\n starts: [\n ...previous.starts.slice(0, keep),\n ...tail.starts.map((start) => start + resumeAt),\n ],\n }\n }\n }\n const full = parseLines(normalized, options)\n return { text: normalized, streaming, citations, ...full }\n}\n\nfunction parseLines(\n text: string,\n options: MessageParseOptions\n): { blocks: MessageBlock[]; starts: number[] } {\n const inline = (source: string) => parseInline(source, 0, { citations: options.citations })\n const lines = text.split('\\n')\n // The offset where each line starts, so a block can record where it began.\n const lineStarts: number[] = []\n for (let line = 0, offset = 0; line < lines.length; line++) {\n lineStarts.push(offset)\n offset += lines[line]!.length + 1\n }\n const blocks: MessageBlock[] = []\n const starts: number[] = []\n let paragraph: string[] = []\n let paragraphStart = 0\n const flushParagraph = () => {\n if (paragraph.length === 0) return\n const raw = paragraph.join('\\n')\n blocks.push({ kind: 'paragraph', inline: inline(raw), raw })\n starts.push(lineStarts[paragraphStart]!)\n paragraph = []\n }\n\n let index = 0\n while (index < lines.length) {\n const line = lines[index]!\n const fence = fenceOpen(line)\n if (fence) {\n flushParagraph()\n const marker = fence.marker\n const language = fence.language || undefined\n const { code, closed, next } = readFence(lines, index + 1, fence)\n starts.push(lineStarts[index]!)\n index = next\n blocks.push({\n kind: 'code',\n language,\n code,\n complete: closed || !options.streaming,\n raw: `${marker}${language ?? ''}\\n${code}${closed ? `\\n${marker}` : ''}`,\n })\n continue\n }\n if (line.trim() === '') {\n flushParagraph()\n index += 1\n continue\n }\n const heading = headingOf(line)\n if (heading) {\n flushParagraph()\n starts.push(lineStarts[index]!)\n blocks.push({\n kind: 'heading',\n level: heading.level,\n inline: inline(heading.text),\n raw: line,\n })\n index += 1\n continue\n }\n const bullet = BULLET.exec(line)\n const ordered = bullet ? null : ORDERED.exec(line)\n if (bullet || ordered) {\n flushParagraph()\n starts.push(lineStarts[index]!)\n const items: MessageInline[][] = []\n const rawLines: string[] = []\n const isOrdered = ordered !== null\n const start = ordered ? Number(ordered[1]) : 1\n while (index < lines.length) {\n const current = lines[index]!\n const match = isOrdered ? ORDERED.exec(current) : BULLET.exec(current)\n if (match) {\n items.push([...inline(isOrdered ? match[2]! : match[1]!)])\n rawLines.push(current)\n index += 1\n continue\n }\n // An indented continuation belongs to the previous item.\n if (/^ {2,}\\S/.test(current) && items.length > 0) {\n // Append in place: copying the accumulated spans on every continuation\n // makes a long item quadratic in its number of lines. Push one span at\n // a time, because spreading a line of many spans into `push` exceeds\n // the engine's argument limit and throws.\n const item = items[items.length - 1]!\n item.push({ kind: 'text', text: ' ' })\n for (const span of inline(current.trim())) item.push(span)\n rawLines.push(current)\n index += 1\n continue\n }\n break\n }\n blocks.push({ kind: 'list', ordered: isOrdered, start, items, raw: rawLines.join('\\n') })\n continue\n }\n const quote = QUOTE.exec(line)\n if (quote) {\n flushParagraph()\n starts.push(lineStarts[index]!)\n const quoted: string[] = []\n while (index < lines.length) {\n const match = QUOTE.exec(lines[index]!)\n if (!match) break\n quoted.push(match[1]!)\n index += 1\n }\n const raw = quoted.join('\\n')\n blocks.push({ kind: 'quote', inline: inline(raw), raw })\n continue\n }\n if (paragraph.length === 0) paragraphStart = index\n paragraph.push(line)\n index += 1\n }\n flushParagraph()\n return { blocks, starts }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAM,aAAa;;;;;;;;;AAUnB,SAAS,UAAU,MAA0E;CAC3F,IAAI,QAAQ;CACZ,OAAO,QAAQ,KAAK,KAAK,WAAW,KAAK,MAAM,IAAI,SAAS;CAC5D,MAAM,OAAO,KAAK;CAClB,IAAI,SAAS,OAAO,SAAS,KAAK,OAAO,KAAA;CACzC,IAAI,MAAM,QAAQ;CAClB,OAAO,KAAK,SAAS,MAAM,OAAO;CAClC,IAAI,MAAM,QAAQ,GAAG,OAAO,KAAA;CAC5B,IAAI,KAAK,SAAS,KAAK,GAAG,GAAG,OAAO,KAAA;CACpC,IAAI,YAAY;CAChB,OAAO,KAAK,eAAe,OAAO,KAAK,eAAe,KAAM,aAAa;CACzE,IAAI,UAAU;CACd,OAAO,UAAU,KAAK,UAAU,CAAC,WAAW,KAAK,KAAK,QAAS,GAAG,WAAW;CAC7E,OAAO;EAAE,QAAQ,KAAK,MAAM,OAAO,GAAG;EAAG,UAAU,KAAK,MAAM,WAAW,OAAO;CAAE;AACpF;;;;;;AAQA,SAAS,UACP,OACA,OACA,OAC2D;CAC3D,MAAM,OAAiB,CAAC;CACxB,IAAI,QAAQ;CACZ,OAAO,QAAQ,MAAM,QAAQ;EAC3B,MAAM,YAAY,MAAM;EACxB,MAAM,QAAQ,8BAA8B,KAAK,SAAS;EAC1D,IAAI,SAAS,MAAM,EAAE,CAAE,OAAO,MAAM,OAAO,MAAM,MAAM,EAAE,CAAE,UAAU,MAAM,OAAO,QAChF,OAAO;GAAE,MAAM,KAAK,KAAK,IAAI;GAAG,QAAQ;GAAM,MAAM,QAAQ;EAAE;EAChE,KAAK,KAAK,SAAS;EACnB,SAAS;CACX;C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AAQ,OAAO,QAAQ,KAAK,CAAC,GAAG,KAAK,KAAK,IAAI;KACzD,SAAS,KAAK,OAAO;KACrB,SAAS;KACT;IACF;IACA;GACF;GACA,OAAO,KAAK;IAAE,MAAM;IAAQ,SAAS;IAAW;IAAO;IAAO,KAAK,SAAS,KAAK,IAAI;GAAE,CAAC;GACxF;EACF;EAEA,IADc,MAAM,KAAK,IACrB,GAAO;GACT,eAAe;GACf,OAAO,KAAK,WAAW,MAAO;GAC9B,MAAM,SAAmB,CAAC;GAC1B,OAAO,QAAQ,MAAM,QAAQ;IAC3B,MAAM,QAAQ,MAAM,KAAK,MAAM,MAAO;IACtC,IAAI,CAAC,OAAO;IACZ,OAAO,KAAK,MAAM,EAAG;IACrB,SAAS;GACX;GACA,MAAM,MAAM,OAAO,KAAK,IAAI;GAC5B,OAAO,KAAK;IAAE,MAAM;IAAS,QAAQ,OAAO,GAAG;IAAG;GAAI,CAAC;GACvD;EACF;EACA,IAAI,UAAU,WAAW,GAAG,iBAAiB;EAC7C,UAAU,KAAK,IAAI;EACnB,SAAS;CACX;CACA,eAAe;CACf,OAAO;EAAE;EAAQ;CAAO;AAC1B"}
|
|
1
|
+
{"version":3,"file":"parse-message-text.js","names":[],"sources":["../../../../src/components/ui/message/parse-message-text.ts"],"sourcesContent":["/**\n * A small, safe reading of model and user text.\n *\n * Assistant output is untrusted: it can contain anything a model, a tool result\n * or a pasted web page put there. This parser therefore produces *data* —\n * blocks and inline spans — and the renderer turns that data into text nodes and\n * a fixed set of elements. There is no HTML pass, no `innerHTML`, and no\n * attribute built from the text other than a link `href` that has passed\n * `safeHref`.\n *\n * It reads a deliberately small subset of Markdown: fenced code, headings,\n * flat lists, block quotes, paragraphs, and inline code, bold, italic and links.\n * Anything it does not recognise stays as paragraph text with its line breaks\n * (the paragraph renders `white-space: pre-wrap`), so unsupported syntax\n * degrades to readable source instead of vanishing.\n *\n * A citation marker, `[^1]` or `[^1,2]`, is read only when the caller says how\n * many sources it has (`citations`) and every number names one of them. A marker\n * the host cannot resolve stays literal text: it is never a citation by shape\n * alone, and `arr[1]` or a footnote the model invented is not one either.\n *\n * Streaming is a first-class input. `streaming: true` means the text may end\n * mid-construct, so an unterminated fence is a *partial* code block — shown as\n * code from its first line, flagged incomplete — and an unterminated inline\n * span stays literal until its closing delimiter arrives. Once streaming stops,\n * an unterminated fence is just a code block that ends at the end of the text.\n */\n\nimport { safeHref } from '#lib/url'\n\nexport { safeHref }\n\nexport type MessageInline =\n | Readonly<{ kind: 'text'; text: string }>\n | Readonly<{ kind: 'code'; text: string }>\n | Readonly<{ kind: 'strong'; children: readonly MessageInline[] }>\n | Readonly<{ kind: 'em'; children: readonly MessageInline[] }>\n | Readonly<{ kind: 'link'; href: string; children: readonly MessageInline[] }>\n /** A resolved citation marker: 1-based source numbers, each within `citations`. */\n | Readonly<{ kind: 'cite'; numbers: readonly number[]; raw: string }>\n\nexport type MessageBlock =\n | Readonly<{ kind: 'paragraph'; inline: readonly MessageInline[]; raw: string }>\n | Readonly<{\n kind: 'heading'\n level: 1 | 2 | 3 | 4\n inline: readonly MessageInline[]\n raw: string\n }>\n | Readonly<{\n kind: 'list'\n ordered: boolean\n start: number\n items: readonly (readonly MessageInline[])[]\n raw: string\n }>\n | Readonly<{ kind: 'quote'; inline: readonly MessageInline[]; raw: string }>\n | Readonly<{\n kind: 'code'\n language: string | undefined\n code: string\n /** False while the fence is still open and the text may still grow. */\n complete: boolean\n raw: string\n }>\n\nconst WHITESPACE = /\\s/\n\n/**\n * An opening fence: up to three spaces, a run of three or more backticks or of\n * three or more tildes, then an info string that holds no backtick. The first\n * word of the info string is the language. This is a scan, not one pattern,\n * because a pattern whose classes overlap (`[^\\s`]*` then `[^`]*`) retries every\n * split of a long info string when a stray backtick ends it: three backticks, a\n * long run of `a` and a trailing backtick took quadratic time.\n */\nfunction fenceOpen(line: string): Readonly<{ marker: string; language: string }> | undefined {\n let start = 0\n while (start < 3 && line.charCodeAt(start) === 32) start += 1\n const mark = line[start]\n if (mark !== '`' && mark !== '~') return undefined\n let end = start + 1\n while (line[end] === mark) end += 1\n if (end - start < 3) return undefined\n if (line.includes('`', end)) return undefined\n let wordStart = end\n while (line[wordStart] === ' ' || line[wordStart] === '\\t') wordStart += 1\n let wordEnd = wordStart\n while (wordEnd < line.length && !WHITESPACE.test(line[wordEnd]!)) wordEnd += 1\n return { marker: line.slice(start, end), language: line.slice(wordStart, wordEnd) }\n}\ntype FenceOpen = NonNullable<ReturnType<typeof fenceOpen>>\n\n/**\n * The body of a fence that opened on `lines[start - 1]`: every line up to a\n * closing run of the same mark at least as long as the opening one, or to the end\n * of the text when none comes.\n */\nfunction readFence(\n lines: readonly string[],\n start: number,\n fence: FenceOpen\n): Readonly<{ code: string; closed: boolean; next: number }> {\n const body: string[] = []\n let index = start\n while (index < lines.length) {\n const candidate = lines[index]!\n const close = /^ {0,3}(`{3,}|~{3,})[ \\t]*$/.exec(candidate)\n if (close && close[1]![0] === fence.marker[0] && close[1]!.length >= fence.marker.length)\n return { code: body.join('\\n'), closed: true, next: index + 1 }\n body.push(candidate)\n index += 1\n }\n return { code: body.join('\\n'), closed: false, next: index }\n}\n\nexport type FenceSegment =\n | Readonly<{ kind: 'prose'; text: string }>\n | Readonly<{\n kind: 'code'\n language: string | undefined\n code: string\n /** False while the fence is still open and the text may still grow. */\n complete: boolean\n }>\n\n/**\n * Text split into fenced code and the prose between, by the same fence grammar\n * `parseMessageText` reads. For a renderer that draws code blocks but leaves the\n * prose as written (`MessageBody`).\n */\nexport function splitCodeFences(\n text: string,\n options: { streaming?: boolean } = {}\n): readonly FenceSegment[] {\n const lines = text.replace(/\\r\\n?/g, '\\n').split('\\n')\n const segments: FenceSegment[] = []\n let prose: string[] = []\n const flushProse = () => {\n if (prose.length > 0) segments.push({ kind: 'prose', text: prose.join('\\n') })\n prose = []\n }\n let index = 0\n while (index < lines.length) {\n const line = lines[index]!\n const fence = fenceOpen(line)\n if (!fence) {\n prose.push(line)\n index += 1\n continue\n }\n flushProse()\n const block = readFence(lines, index + 1, fence)\n segments.push({\n kind: 'code',\n language: fence.language || undefined,\n code: block.code,\n complete: block.closed || !options.streaming,\n })\n index = block.next\n }\n flushProse()\n return segments\n}\n\n// The lookahead after each marker's spaces makes that run maximal, so a line\n// that fails later (`.` stops at U+2028) cannot retry every shorter run.\nconst BULLET = /^ {0,3}[-*+][ \\t]+(?![ \\t])(.*)$/\nconst ORDERED = /^ {0,3}(\\d{1,9})[.)][ \\t]+(?![ \\t])(.*)$/\nconst QUOTE = /^ {0,3}>[ \\t]?(.*)$/\n\nconst isBlank = (char: string | undefined) => char === ' ' || char === '\\t'\nconst LINE_TERMINATOR = /[\\n\\r\\u2028\\u2029]/\n\n/**\n * An ATX heading: up to three spaces, one to four `#`, spaces or tabs, then the\n * text, with any trailing spaces and closing `#` run removed. This is a scan for\n * the reason `fenceOpen` is: the equivalent pattern (`(.+?)[ \\t]*#*[ \\t]*$`)\n * retries the closing check from every position of a long run of spaces.\n */\nfunction headingOf(line: string): Readonly<{ level: 1 | 2 | 3 | 4; text: string }> | undefined {\n let start = 0\n while (start < 3 && line.charCodeAt(start) === 32) start += 1\n let marks = start\n while (line[marks] === '#') marks += 1\n const level = marks - start\n if (level < 1 || level > 4 || !isBlank(line[marks])) return undefined\n let textStart = marks\n while (isBlank(line[textStart])) textStart += 1\n if (textStart === line.length) {\n // Only blanks follow the marker: the text is the last of them, which needs a second one.\n return textStart - marks >= 2\n ? { level: level as 1 | 2 | 3 | 4, text: line[line.length - 1]! }\n : undefined\n }\n if (LINE_TERMINATOR.test(line.slice(textStart))) return undefined\n // Drop the longest tail of blanks, a run of `#`, and blanks again.\n let textEnd = line.length\n while (textEnd > textStart && isBlank(line[textEnd - 1])) textEnd -= 1\n while (textEnd > textStart && line[textEnd - 1] === '#') textEnd -= 1\n while (textEnd > textStart && isBlank(line[textEnd - 1])) textEnd -= 1\n // Text that is nothing but a closing run keeps its first character.\n return {\n level: level as 1 | 2 | 3 | 4,\n text: line.slice(textStart, Math.max(textEnd, textStart + 1)),\n }\n}\n\nconst MAX_INLINE_DEPTH = 4\n\n/** A marker may cite at most this many sources, so one cannot flood a sentence. */\nconst MAX_CITED_SOURCES = 6\nconst CITATION = /\\[\\^(\\d{1,4}(?:[ \\t]*,[ \\t]*\\d{1,4}){0,5})\\]/y\n\nexport type ParseInlineOptions = {\n /** How many sources the host has. Without it no marker is a citation. */\n citations?: number\n /** Inside a link label, where a citation (itself a button) must not nest. */\n inLink?: boolean\n}\n\nfunction citationAt(text: string, index: number, count: number): MessageInline | undefined {\n CITATION.lastIndex = index\n const match = CITATION.exec(text)\n if (!match) return undefined\n const numbers = [...new Set(match[1]!.split(',').map((part) => Number(part.trim())))]\n if (numbers.length > MAX_CITED_SOURCES) return undefined\n if (numbers.some((number) => number < 1 || number > count)) return undefined\n return { kind: 'cite', numbers, raw: match[0] }\n}\n\n/** Inline spans. Delimiters must close on the same block, or they stay literal. */\nexport function parseInline(\n text: string,\n depth = 0,\n options: ParseInlineOptions = {}\n): readonly MessageInline[] {\n const out: MessageInline[] = []\n let brackets: ReadonlyMap<number, number> | undefined\n let paren = -2\n let buffer = ''\n const flush = () => {\n if (buffer) out.push({ kind: 'text', text: buffer })\n buffer = ''\n }\n let index = 0\n while (index < text.length) {\n const char = text[index]!\n if (char === '`') {\n let ticks = 1\n while (text[index + ticks] === '`') ticks += 1\n const fence = '`'.repeat(ticks)\n const close = text.indexOf(fence, index + ticks)\n if (close > index + ticks) {\n flush()\n out.push({ kind: 'code', text: text.slice(index + ticks, close).replace(/^ (.*) $/, '$1') })\n index = close + ticks\n continue\n }\n buffer += fence\n index += ticks\n continue\n }\n if (depth < MAX_INLINE_DEPTH && (char === '*' || char === '_')) {\n const double = text[index + 1] === char\n const delimiter = double ? char + char : char\n const start = index + delimiter.length\n // Word-internal underscores cannot open emphasis. Reject before scanning,\n // and consume both marks so the second cannot rescan the remaining pairs.\n if (char === '_' && index > 0 && /[A-Za-z0-9]/.test(text[index - 1] ?? '')) {\n buffer += delimiter\n index = start\n continue\n }\n const next = text[start]\n // A delimiter opens only before a non-space, so `a * b * c` and `2 * 3` stay arithmetic.\n if (next !== undefined && !/\\s/.test(next)) {\n const close = findClosing(text, delimiter, start)\n if (double && !(close > start && !/\\s/.test(text[close - 1] ?? ' '))) {\n // An opener that does not close stays literal as a pair. Retrying its\n // second mark as emphasis would scan to the end of the line again, so a\n // line of unclosed `**a ` would be quadratic.\n buffer += delimiter\n index = start\n continue\n }\n if (close > start && !/\\s/.test(text[close - 1] ?? ' ')) {\n flush()\n out.push({\n kind: double ? 'strong' : 'em',\n children: parseInline(text.slice(start, close), depth + 1, options),\n })\n index = close + delimiter.length\n continue\n }\n }\n }\n if (char === '[' && text[index + 1] === '^' && options.citations && !options.inLink) {\n const cite = citationAt(text, index, options.citations)\n if (cite && cite.kind === 'cite') {\n flush()\n out.push(cite)\n index += cite.raw.length\n continue\n }\n }\n if (depth < MAX_INLINE_DEPTH && char === '[' && paren !== -1) {\n // Don't allocate bracket pairs for text that cannot contain a complete\n // link. Remember the last miss so later literal openers are O(1).\n if (paren < index) paren = text.indexOf(')', index + 1)\n if (paren === -1) {\n buffer += char\n index += 1\n continue\n }\n brackets ??= bracketMatches(text)\n const labelEnd = brackets.get(index) ?? -1\n if (labelEnd > index && text[labelEnd + 1] === '(') {\n // The nearest `)` is remembered: a run of `[a](` with no `)` after it\n // would otherwise search to the end of the text from every opener.\n if (paren !== -1 && paren < labelEnd + 2) paren = text.indexOf(')', labelEnd + 2)\n const hrefEnd = paren\n if (hrefEnd > labelEnd + 1) {\n const href = safeHref(text.slice(labelEnd + 2, hrefEnd))\n if (href) {\n flush()\n out.push({\n kind: 'link',\n href,\n children: parseInline(text.slice(index + 1, labelEnd), depth + 1, {\n ...options,\n inLink: true,\n }),\n })\n index = hrefEnd + 1\n continue\n }\n }\n }\n }\n buffer += char\n index += 1\n }\n flush()\n return out\n}\n\nfunction findClosing(text: string, delimiter: string, from: number): number {\n let index = from\n while (index < text.length) {\n if (text[index] === '`') {\n // Skip a code span so `**a `**` b**` closes where the code ends.\n const close = text.indexOf('`', index + 1)\n if (close < 0) return -1\n index = close + 1\n continue\n }\n if (text[index] === delimiter[0]) {\n let end = index + 1\n while (text[end] === delimiter[0]) end += 1\n const run = end - index\n if (delimiter.length === 1) {\n // Emphasis closes on a run of one (or an odd run, whose last mark closes).\n // An even run is `**` pairs: strong spans inside the emphasis, which the\n // inner parse reads. One pass, and the scan stops at the next odd run, so\n // a line of unclosed delimiters is still linear.\n if (run % 2 === 1) return end - 1\n } else if (run >= delimiter.length) {\n // Strong closes on the last two marks of a run.\n return end - delimiter.length\n }\n index = end\n continue\n }\n index += 1\n }\n return -1\n}\n\n/**\n * The `]` that closes each `[`, found in one pass. A match never crosses a\n * newline, and an unmatched `[` has no entry. Scanning forward from every `[`\n * instead is quadratic on a long line of unclosed brackets, which a pasted page\n * or a hostile model can use to hang the renderer.\n */\nfunction bracketMatches(text: string): ReadonlyMap<number, number> {\n const matches = new Map<number, number>()\n const open: number[] = []\n for (let index = 0; index < text.length; index += 1) {\n const char = text[index]\n if (char === '[') open.push(index)\n else if (char === ']') {\n const start = open.pop()\n if (start !== undefined) matches.set(start, index)\n } else if (char === '\\n') open.length = 0\n }\n return matches\n}\n\n/** Parse text into blocks. Pure, total, and linear in the length of `text`. */\nexport function parseMessageText(\n text: string,\n options: { streaming?: boolean; citations?: number } = {}\n): readonly MessageBlock[] {\n return parseLines(text.replace(/\\r\\n?/g, '\\n'), options).blocks\n}\n\nexport type MessageParseOptions = { streaming?: boolean; citations?: number }\n\n/**\n * A parse that the next, longer text can continue from. See\n * {@link parseMessageTextIncremental}.\n */\nexport type MessageParse = Readonly<{\n /** The normalised text that was parsed. */\n text: string\n streaming: boolean\n citations: number\n blocks: readonly MessageBlock[]\n /** The offset in `text` where each block's first line starts. */\n starts: readonly number[]\n}>\n\n/**\n * `parseMessageText`, resumed.\n *\n * A streaming reply grows by appending, and reparsing the whole text per token\n * makes a long reply quadratic. When `text` extends `previous.text` under the same\n * options, only its final line can still change, since it may be unfinished. A block\n * that ends before that line is terminated by a finished line (a blank line, a\n * closing fence, or the line that starts the next block), so appending cannot\n * change it. Only the tail from the last block that starts before the final line\n * is parsed again, and the earlier blocks are returned as the same objects. The\n * parser keeps no state across a block boundary, so the tail parsed alone gives\n * exactly the blocks a full parse gives there. Anything else (an edit, a\n * different option) is a full parse.\n */\nexport function parseMessageTextIncremental(\n previous: MessageParse | undefined,\n text: string,\n options: MessageParseOptions = {}\n): MessageParse {\n const normalized = text.replace(/\\r\\n?/g, '\\n')\n const streaming = options.streaming ?? false\n const citations = options.citations ?? 0\n if (\n previous &&\n previous.streaming === streaming &&\n previous.citations === citations &&\n previous.blocks.length > 0 &&\n normalized.length >= previous.text.length &&\n normalized.startsWith(previous.text)\n ) {\n // Only the final line can still change (it may not be finished), and with it\n // the block it belongs to or the one it ends. Resume from the last block that\n // starts before that line: every block before it ends at a finished line.\n const lastLine = previous.text.lastIndexOf('\\n') + 1\n let keep = previous.starts.length - 1\n while (keep >= 0 && previous.starts[keep]! >= lastLine) keep -= 1\n if (keep > 0) {\n const resumeAt = previous.starts[keep]!\n const tail = parseLines(normalized.slice(resumeAt), options)\n return {\n text: normalized,\n streaming,\n citations,\n blocks: [...previous.blocks.slice(0, keep), ...tail.blocks],\n starts: [\n ...previous.starts.slice(0, keep),\n ...tail.starts.map((start) => start + resumeAt),\n ],\n }\n }\n }\n const full = parseLines(normalized, options)\n return { text: normalized, streaming, citations, ...full }\n}\n\nfunction parseLines(\n text: string,\n options: MessageParseOptions\n): { blocks: MessageBlock[]; starts: number[] } {\n const inline = (source: string) => parseInline(source, 0, { citations: options.citations })\n const lines = text.split('\\n')\n // The offset where each line starts, so a block can record where it began.\n const lineStarts: number[] = []\n for (let line = 0, offset = 0; line < lines.length; line++) {\n lineStarts.push(offset)\n offset += lines[line]!.length + 1\n }\n const blocks: MessageBlock[] = []\n const starts: number[] = []\n let paragraph: string[] = []\n let paragraphStart = 0\n const flushParagraph = () => {\n if (paragraph.length === 0) return\n const raw = paragraph.join('\\n')\n blocks.push({ kind: 'paragraph', inline: inline(raw), raw })\n starts.push(lineStarts[paragraphStart]!)\n paragraph = []\n }\n\n let index = 0\n while (index < lines.length) {\n const line = lines[index]!\n const fence = fenceOpen(line)\n if (fence) {\n flushParagraph()\n const marker = fence.marker\n const language = fence.language || undefined\n const { code, closed, next } = readFence(lines, index + 1, fence)\n starts.push(lineStarts[index]!)\n index = next\n blocks.push({\n kind: 'code',\n language,\n code,\n complete: closed || !options.streaming,\n raw: `${marker}${language ?? ''}\\n${code}${closed ? `\\n${marker}` : ''}`,\n })\n continue\n }\n if (line.trim() === '') {\n flushParagraph()\n index += 1\n continue\n }\n const heading = headingOf(line)\n if (heading) {\n flushParagraph()\n starts.push(lineStarts[index]!)\n blocks.push({\n kind: 'heading',\n level: heading.level,\n inline: inline(heading.text),\n raw: line,\n })\n index += 1\n continue\n }\n const bullet = BULLET.exec(line)\n const ordered = bullet ? null : ORDERED.exec(line)\n if (bullet || ordered) {\n flushParagraph()\n starts.push(lineStarts[index]!)\n const items: MessageInline[][] = []\n const rawLines: string[] = []\n const isOrdered = ordered !== null\n const start = ordered ? Number(ordered[1]) : 1\n while (index < lines.length) {\n const current = lines[index]!\n const match = isOrdered ? ORDERED.exec(current) : BULLET.exec(current)\n if (match) {\n items.push([...inline(isOrdered ? match[2]! : match[1]!)])\n rawLines.push(current)\n index += 1\n continue\n }\n // An indented continuation belongs to the previous item.\n if (/^ {2,}\\S/.test(current) && items.length > 0) {\n // Append in place: copying the accumulated spans on every continuation\n // makes a long item quadratic in its number of lines. Push one span at\n // a time, because spreading a line of many spans into `push` exceeds\n // the engine's argument limit and throws.\n const item = items[items.length - 1]!\n item.push({ kind: 'text', text: ' ' })\n for (const span of inline(current.trim())) item.push(span)\n rawLines.push(current)\n index += 1\n continue\n }\n break\n }\n blocks.push({ kind: 'list', ordered: isOrdered, start, items, raw: rawLines.join('\\n') })\n continue\n }\n const quote = QUOTE.exec(line)\n if (quote) {\n flushParagraph()\n starts.push(lineStarts[index]!)\n const quoted: string[] = []\n while (index < lines.length) {\n const match = QUOTE.exec(lines[index]!)\n if (!match) break\n quoted.push(match[1]!)\n index += 1\n }\n const raw = quoted.join('\\n')\n blocks.push({ kind: 'quote', inline: inline(raw), raw })\n continue\n }\n if (paragraph.length === 0) paragraphStart = index\n paragraph.push(line)\n index += 1\n }\n flushParagraph()\n return { blocks, starts }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAM,aAAa;;;;;;;;;AAUnB,SAAS,UAAU,MAA0E;CAC3F,IAAI,QAAQ;CACZ,OAAO,QAAQ,KAAK,KAAK,WAAW,KAAK,MAAM,IAAI,SAAS;CAC5D,MAAM,OAAO,KAAK;CAClB,IAAI,SAAS,OAAO,SAAS,KAAK,OAAO,KAAA;CACzC,IAAI,MAAM,QAAQ;CAClB,OAAO,KAAK,SAAS,MAAM,OAAO;CAClC,IAAI,MAAM,QAAQ,GAAG,OAAO,KAAA;CAC5B,IAAI,KAAK,SAAS,KAAK,GAAG,GAAG,OAAO,KAAA;CACpC,IAAI,YAAY;CAChB,OAAO,KAAK,eAAe,OAAO,KAAK,eAAe,KAAM,aAAa;CACzE,IAAI,UAAU;CACd,OAAO,UAAU,KAAK,UAAU,CAAC,WAAW,KAAK,KAAK,QAAS,GAAG,WAAW;CAC7E,OAAO;EAAE,QAAQ,KAAK,MAAM,OAAO,GAAG;EAAG,UAAU,KAAK,MAAM,WAAW,OAAO;CAAE;AACpF;;;;;;AAQA,SAAS,UACP,OACA,OACA,OAC2D;CAC3D,MAAM,OAAiB,CAAC;CACxB,IAAI,QAAQ;CACZ,OAAO,QAAQ,MAAM,QAAQ;EAC3B,MAAM,YAAY,MAAM;EACxB,MAAM,QAAQ,8BAA8B,KAAK,SAAS;EAC1D,IAAI,SAAS,MAAM,EAAE,CAAE,OAAO,MAAM,OAAO,MAAM,MAAM,EAAE,CAAE,UAAU,M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GAAG,OAAO,YAAY,MAAM,KAAM,MAAM,EAAG,CAAC,CAAC;KACzD,SAAS,KAAK,OAAO;KACrB,SAAS;KACT;IACF;IAEA,IAAI,WAAW,KAAK,OAAO,KAAK,MAAM,SAAS,GAAG;KAKhD,MAAM,OAAO,MAAM,MAAM,SAAS;KAClC,KAAK,KAAK;MAAE,MAAM;MAAQ,MAAM;KAAI,CAAC;KACrC,KAAK,MAAM,QAAQ,OAAO,QAAQ,KAAK,CAAC,GAAG,KAAK,KAAK,IAAI;KACzD,SAAS,KAAK,OAAO;KACrB,SAAS;KACT;IACF;IACA;GACF;GACA,OAAO,KAAK;IAAE,MAAM;IAAQ,SAAS;IAAW;IAAO;IAAO,KAAK,SAAS,KAAK,IAAI;GAAE,CAAC;GACxF;EACF;EAEA,IADc,MAAM,KAAK,IACrB,GAAO;GACT,eAAe;GACf,OAAO,KAAK,WAAW,MAAO;GAC9B,MAAM,SAAmB,CAAC;GAC1B,OAAO,QAAQ,MAAM,QAAQ;IAC3B,MAAM,QAAQ,MAAM,KAAK,MAAM,MAAO;IACtC,IAAI,CAAC,OAAO;IACZ,OAAO,KAAK,MAAM,EAAG;IACrB,SAAS;GACX;GACA,MAAM,MAAM,OAAO,KAAK,IAAI;GAC5B,OAAO,KAAK;IAAE,MAAM;IAAS,QAAQ,OAAO,GAAG;IAAG;GAAI,CAAC;GACvD;EACF;EACA,IAAI,UAAU,WAAW,GAAG,iBAAiB;EAC7C,UAAU,KAAK,IAAI;EACnB,SAAS;CACX;CACA,eAAe;CACf,OAAO;EAAE;EAAQ;CAAO;AAC1B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observe-resize.d.ts","sourceRoot":"","sources":["../../src/lib/observe-resize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,KAAK,cAAc,GAAG,CAAC,KAAK,EAAE,mBAAmB,KAAK,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"observe-resize.d.ts","sourceRoot":"","sources":["../../src/lib/observe-resize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,KAAK,cAAc,GAAG,CAAC,KAAK,EAAE,mBAAmB,KAAK,IAAI,CAAA;AAW1D,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,cAAc,GAAG,MAAM,IAAI,CAoBpF"}
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
// src/lib/observe-resize.ts
|
|
2
|
-
var
|
|
3
|
-
var callbacks = /* @__PURE__ */ new WeakMap();
|
|
2
|
+
var watchers = /* @__PURE__ */ new WeakMap();
|
|
4
3
|
function observeResize(element, callback) {
|
|
5
4
|
if (typeof ResizeObserver === "undefined") return () => {};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const Constructor = ResizeObserver;
|
|
6
|
+
let watcher = watchers.get(Constructor);
|
|
7
|
+
if (!watcher) {
|
|
8
|
+
const callbacks = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
watcher = {
|
|
10
|
+
observer: new Constructor((entries) => {
|
|
11
|
+
for (const entry of entries) callbacks.get(entry.target)?.(entry);
|
|
12
|
+
}),
|
|
13
|
+
callbacks
|
|
14
|
+
};
|
|
15
|
+
watchers.set(Constructor, watcher);
|
|
16
|
+
}
|
|
17
|
+
const { observer, callbacks } = watcher;
|
|
9
18
|
callbacks.set(element, callback);
|
|
10
|
-
|
|
19
|
+
observer.observe(element);
|
|
11
20
|
return () => {
|
|
12
21
|
if (callbacks.get(element) !== callback) return;
|
|
13
22
|
callbacks.delete(element);
|
|
14
|
-
|
|
23
|
+
observer.unobserve(element);
|
|
15
24
|
};
|
|
16
25
|
}
|
|
17
26
|
export { observeResize };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observe-resize.js","names":[],"sources":["../../src/lib/observe-resize.ts"],"sourcesContent":["/**\n * Watch an element's size through one ResizeObserver shared by every caller.\n *\n * A component that creates its own observer per instance pays for one observer\n * object, one browser registration and one delivery callback per mount; a reply\n * with a dozen code blocks made a dozen. The shared observer delivers every\n * changed element in one batch per frame and calls each element's callback.\n *\n * Returns a function that stops watching. Outside a browser it does nothing.\n */\ntype ResizeCallback = (entry: ResizeObserverEntry) => void\n\
|
|
1
|
+
{"version":3,"file":"observe-resize.js","names":[],"sources":["../../src/lib/observe-resize.ts"],"sourcesContent":["/**\n * Watch an element's size through one ResizeObserver shared by every caller.\n *\n * A component that creates its own observer per instance pays for one observer\n * object, one browser registration and one delivery callback per mount; a reply\n * with a dozen code blocks made a dozen. The shared observer delivers every\n * changed element in one batch per frame and calls each element's callback.\n *\n * Returns a function that stops watching. Outside a browser it does nothing.\n */\ntype ResizeCallback = (entry: ResizeObserverEntry) => void\n\ntype Watcher = {\n observer: ResizeObserver\n callbacks: WeakMap<Element, ResizeCallback>\n}\n\n// Reuse a single observer for a platform constructor. Replacing the constructor\n// (for example, installing a polyfill) must not leave new callers using the old observer.\nconst watchers = new WeakMap<typeof ResizeObserver, Watcher>()\n\nexport function observeResize(element: Element, callback: ResizeCallback): () => void {\n if (typeof ResizeObserver === 'undefined') return () => {}\n const Constructor = ResizeObserver\n let watcher = watchers.get(Constructor)\n if (!watcher) {\n const callbacks = new WeakMap<Element, ResizeCallback>()\n const observer = new Constructor((entries) => {\n for (const entry of entries) callbacks.get(entry.target)?.(entry)\n })\n watcher = { observer, callbacks }\n watchers.set(Constructor, watcher)\n }\n const { observer, callbacks } = watcher\n callbacks.set(element, callback)\n observer.observe(element)\n return () => {\n if (callbacks.get(element) !== callback) return\n callbacks.delete(element)\n observer.unobserve(element)\n }\n}\n"],"mappings":";AAmBA,IAAM,2BAAW,IAAI,QAAwC;AAE7D,SAAgB,cAAc,SAAkB,UAAsC;CACpF,IAAI,OAAO,mBAAmB,aAAa,aAAa,CAAC;CACzD,MAAM,cAAc;CACpB,IAAI,UAAU,SAAS,IAAI,WAAW;CACtC,IAAI,CAAC,SAAS;EACZ,MAAM,4BAAY,IAAI,QAAiC;EAIvD,UAAU;GAAE,UAAA,IAHS,aAAa,YAAY;IAC5C,KAAK,MAAM,SAAS,SAAS,UAAU,IAAI,MAAM,MAAM,CAAC,GAAG,KAAK;GAClE,CACY;GAAU;EAAU;EAChC,SAAS,IAAI,aAAa,OAAO;CACnC;CACA,MAAM,EAAE,UAAU,cAAc;CAChC,UAAU,IAAI,SAAS,QAAQ;CAC/B,SAAS,QAAQ,OAAO;CACxB,aAAa;EACX,IAAI,UAAU,IAAI,OAAO,MAAM,UAAU;EACzC,UAAU,OAAO,OAAO;EACxB,SAAS,UAAU,OAAO;CAC5B;AACF"}
|
|
@@ -304,7 +304,15 @@ export function parseInline(
|
|
|
304
304
|
continue
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
-
if (depth < MAX_INLINE_DEPTH && char === '[') {
|
|
307
|
+
if (depth < MAX_INLINE_DEPTH && char === '[' && paren !== -1) {
|
|
308
|
+
// Don't allocate bracket pairs for text that cannot contain a complete
|
|
309
|
+
// link. Remember the last miss so later literal openers are O(1).
|
|
310
|
+
if (paren < index) paren = text.indexOf(')', index + 1)
|
|
311
|
+
if (paren === -1) {
|
|
312
|
+
buffer += char
|
|
313
|
+
index += 1
|
|
314
|
+
continue
|
|
315
|
+
}
|
|
308
316
|
brackets ??= bracketMatches(text)
|
|
309
317
|
const labelEnd = brackets.get(index) ?? -1
|
|
310
318
|
if (labelEnd > index && text[labelEnd + 1] === '(') {
|
|
@@ -10,19 +10,33 @@
|
|
|
10
10
|
*/
|
|
11
11
|
type ResizeCallback = (entry: ResizeObserverEntry) => void
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
type Watcher = {
|
|
14
|
+
observer: ResizeObserver
|
|
15
|
+
callbacks: WeakMap<Element, ResizeCallback>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Reuse a single observer for a platform constructor. Replacing the constructor
|
|
19
|
+
// (for example, installing a polyfill) must not leave new callers using the old observer.
|
|
20
|
+
const watchers = new WeakMap<typeof ResizeObserver, Watcher>()
|
|
15
21
|
|
|
16
22
|
export function observeResize(element: Element, callback: ResizeCallback): () => void {
|
|
17
23
|
if (typeof ResizeObserver === 'undefined') return () => {}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
const Constructor = ResizeObserver
|
|
25
|
+
let watcher = watchers.get(Constructor)
|
|
26
|
+
if (!watcher) {
|
|
27
|
+
const callbacks = new WeakMap<Element, ResizeCallback>()
|
|
28
|
+
const observer = new Constructor((entries) => {
|
|
29
|
+
for (const entry of entries) callbacks.get(entry.target)?.(entry)
|
|
30
|
+
})
|
|
31
|
+
watcher = { observer, callbacks }
|
|
32
|
+
watchers.set(Constructor, watcher)
|
|
33
|
+
}
|
|
34
|
+
const { observer, callbacks } = watcher
|
|
21
35
|
callbacks.set(element, callback)
|
|
22
|
-
|
|
36
|
+
observer.observe(element)
|
|
23
37
|
return () => {
|
|
24
38
|
if (callbacks.get(element) !== callback) return
|
|
25
39
|
callbacks.delete(element)
|
|
26
|
-
|
|
40
|
+
observer.unobserve(element)
|
|
27
41
|
}
|
|
28
42
|
}
|
package/package.json
CHANGED
|
@@ -304,7 +304,15 @@ export function parseInline(
|
|
|
304
304
|
continue
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
-
if (depth < MAX_INLINE_DEPTH && char === '[') {
|
|
307
|
+
if (depth < MAX_INLINE_DEPTH && char === '[' && paren !== -1) {
|
|
308
|
+
// Don't allocate bracket pairs for text that cannot contain a complete
|
|
309
|
+
// link. Remember the last miss so later literal openers are O(1).
|
|
310
|
+
if (paren < index) paren = text.indexOf(')', index + 1)
|
|
311
|
+
if (paren === -1) {
|
|
312
|
+
buffer += char
|
|
313
|
+
index += 1
|
|
314
|
+
continue
|
|
315
|
+
}
|
|
308
316
|
brackets ??= bracketMatches(text)
|
|
309
317
|
const labelEnd = brackets.get(index) ?? -1
|
|
310
318
|
if (labelEnd > index && text[labelEnd + 1] === '(') {
|
|
@@ -10,19 +10,33 @@
|
|
|
10
10
|
*/
|
|
11
11
|
type ResizeCallback = (entry: ResizeObserverEntry) => void
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
type Watcher = {
|
|
14
|
+
observer: ResizeObserver
|
|
15
|
+
callbacks: WeakMap<Element, ResizeCallback>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Reuse a single observer for a platform constructor. Replacing the constructor
|
|
19
|
+
// (for example, installing a polyfill) must not leave new callers using the old observer.
|
|
20
|
+
const watchers = new WeakMap<typeof ResizeObserver, Watcher>()
|
|
15
21
|
|
|
16
22
|
export function observeResize(element: Element, callback: ResizeCallback): () => void {
|
|
17
23
|
if (typeof ResizeObserver === 'undefined') return () => {}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
const Constructor = ResizeObserver
|
|
25
|
+
let watcher = watchers.get(Constructor)
|
|
26
|
+
if (!watcher) {
|
|
27
|
+
const callbacks = new WeakMap<Element, ResizeCallback>()
|
|
28
|
+
const observer = new Constructor((entries) => {
|
|
29
|
+
for (const entry of entries) callbacks.get(entry.target)?.(entry)
|
|
30
|
+
})
|
|
31
|
+
watcher = { observer, callbacks }
|
|
32
|
+
watchers.set(Constructor, watcher)
|
|
33
|
+
}
|
|
34
|
+
const { observer, callbacks } = watcher
|
|
21
35
|
callbacks.set(element, callback)
|
|
22
|
-
|
|
36
|
+
observer.observe(element)
|
|
23
37
|
return () => {
|
|
24
38
|
if (callbacks.get(element) !== callback) return
|
|
25
39
|
callbacks.delete(element)
|
|
26
|
-
|
|
40
|
+
observer.unobserve(element)
|
|
27
41
|
}
|
|
28
42
|
}
|