Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Markdown): pair emphasis and strong markers by CommonMark's rules (#7051)

Cindy Zhang9051ea50b0a4
diff --git a/.changeset/markdown-nested-emphasis.md b/.changeset/markdown-nested-emphasis.md
new file mode 100644
index 0000000000..787992ad4e
--- /dev/null
+++ b/.changeset/markdown-nested-emphasis.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Markdown: pair emphasis and strong markers by CommonMark's rules, so strong inside emphasis keeps its strong
+@cixzhang
+
+Runs of `*` and `_` now pair as CommonMark specifies: by which side of a word they touch, by the nearest compatible opener, and by the rule of three. `*see **bold** more*` and `*see **bold***` render the bold inside the emphasis, `**bold *both***` renders the emphasis inside the strong, `__foo, __bar__, baz__` nests, and an escaped `\*` inside emphasis stays literal. Before, the first matching marker closed emphasis early and left stray `*` or `_` in the text. `***text***` still renders as strong around emphasis. Streaming closes an unfinished `**bold` before trailing spaces, so the partial text keeps its formatting.
diff --git a/packages/core/src/Markdown/Markdown.doc.mjs b/packages/core/src/Markdown/Markdown.doc.mjs
index a0bdf568af..ff172865c4 100644
--- a/packages/core/src/Markdown/Markdown.doc.mjs
+++ b/packages/core/src/Markdown/Markdown.doc.mjs
@@ -247,6 +247,11 @@ export const docs = {
description:
'Use contentWidth to keep prose at a readable line length in wide layouts.',
},
+ {
+ guidance: true,
+ description:
+ 'Emphasis and strong pair as CommonMark specifies, so *see **bold** more* puts the bold inside the emphasis. ***text*** renders as strong around emphasis, the order RichText draws, and is announced as both. Emphasis nested more than 100 levels deep stays as written text.',
+ },
{
guidance: true,
description:
@@ -762,6 +767,11 @@ export const docsZh = {
description:
'Use contentWidth to keep prose at a readable line length in wide layouts.',
},
+ {
+ guidance: true,
+ description:
+ 'Emphasis and strong pair as CommonMark specifies, so *see **bold** more* puts the bold inside the emphasis. ***text*** renders as strong around emphasis, the order RichText draws, and is announced as both. Emphasis nested more than 100 levels deep stays as written text.',
+ },
{
guidance: true,
description:
@@ -859,6 +869,11 @@ export const docsDense = {
description:
'Use contentWidth to keep prose at a readable line length in wide layouts.',
},
+ {
+ guidance: true,
+ description:
+ 'Emphasis and strong pair as CommonMark specifies, so *see **bold** more* puts the bold inside the emphasis. ***text*** renders as strong around emphasis, the order RichText draws, and is announced as both. Emphasis nested more than 100 levels deep stays as written text.',
+ },
{
guidance: true,
description:
diff --git a/packages/core/src/Markdown/emphasis.test.tsx b/packages/core/src/Markdown/emphasis.test.tsx
new file mode 100644
index 0000000000..febe8e6316
--- /dev/null
+++ b/packages/core/src/Markdown/emphasis.test.tsx
@@ -0,0 +1,214 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {describe, expect, it} from 'vitest';
+import {render} from '@testing-library/react';
+import {Markdown} from './Markdown';
+import {parseInlineAst, parseMarkdownAst} from './parser';
+
+type Json = {
+ readonly type: string;
+ readonly value?: string;
+ readonly url?: string;
+ readonly children?: ReadonlyArray<Json>;
+};
+
+const escapeHtml = (text: string): string =>
+ text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+
+/** Inline content as the HTML CommonMark's examples show. */
+function html(markdown: string): string {
+ const write = (node: Json): string => {
+ const inner = () => (node.children ?? []).map(write).join('');
+ switch (node.type) {
+ case 'text':
+ return escapeHtml(node.value ?? '');
+ case 'inlineCode':
+ return `<code>${escapeHtml(node.value ?? '')}</code>`;
+ case 'emphasis':
+ return `<em>${inner()}</em>`;
+ case 'strong':
+ return `<strong>${inner()}</strong>`;
+ case 'link':
+ return `<a href="${node.url ?? ''}">${inner()}</a>`;
+ case 'break':
+ return '<br />\n';
+ default:
+ return inner();
+ }
+ };
+ return (parseInlineAst(markdown) as unknown as ReadonlyArray<Json>)
+ .map(write)
+ .join('');
+}
+
+/**
+ * CommonMark 0.31.2 §6.2 examples, by example number. Each pairs a run of
+ * `*` or `_` by its flanking, its character, and the rule of three.
+ */
+const COMMONMARK: ReadonlyArray<readonly [number, string, string]> = [
+ [350, '*foo bar*', '<em>foo bar</em>'],
+ [351, 'a * foo bar*', 'a * foo bar*'],
+ [352, 'a*"foo"*', 'a*&quot;foo&quot;*'.replace(/&quot;/g, '"')],
+ [354, 'foo*bar*', 'foo<em>bar</em>'],
+ [355, '5*6*78', '5<em>6</em>78'],
+ [356, '_foo bar_', '<em>foo bar</em>'],
+ [357, '_ foo bar_', '_ foo bar_'],
+ [359, 'foo_bar_', 'foo_bar_'],
+ [360, '5_6_78', '5_6_78'],
+ [361, 'пристаням_стремятся_', 'пристаням_стремятся_'],
+ [363, 'foo-_(bar)_', 'foo-<em>(bar)</em>'],
+ [364, '_foo*', '_foo*'],
+ [365, '*foo bar *', '*foo bar *'],
+ [368, '*foo*bar', '<em>foo</em>bar'],
+ [374, '_foo_bar_baz_', '<em>foo_bar_baz</em>'],
+ [378, '**foo bar**', '<strong>foo bar</strong>'],
+ [386, 'foo**bar**', 'foo<strong>bar</strong>'],
+ [
+ 390,
+ '__foo, __bar__, baz__',
+ '<strong>foo, <strong>bar</strong>, baz</strong>',
+ ],
+ [413, '*foo **bar** baz*', '<em>foo <strong>bar</strong> baz</em>'],
+ [414, '*foo**bar**baz*', '<em>foo<strong>bar</strong>baz</em>'],
+ [415, '*foo**bar*', '<em>foo**bar</em>'],
+ [416, '***foo** bar*', '<em><strong>foo</strong> bar</em>'],
+ [417, '*foo **bar***', '<em>foo <strong>bar</strong></em>'],
+ [418, '*foo**bar***', '<em>foo<strong>bar</strong></em>'],
+ [
+ 420,
+ 'foo******bar*********baz',
+ 'foo<strong><strong><strong>bar</strong></strong></strong>***baz',
+ ],
+ [
+ 421,
+ '*foo **bar *baz* bim** bop*',
+ '<em>foo <strong>bar <em>baz</em> bim</strong> bop</em>',
+ ],
+ [422, '*foo [*bar*](/url)*', '<em>foo <a href="/url"><em>bar</em></a></em>'],
+ [423, '** is not an empty emphasis', '** is not an empty emphasis'],
+ [
+ 424,
+ '**** is not an empty strong emphasis',
+ '**** is not an empty strong emphasis',
+ ],
+ [442, '**foo*', '*<em>foo</em>'],
+ [443, '*foo**', '<em>foo</em>*'],
+ [449, '*foo _bar* baz_', '<em>foo _bar</em> baz_'],
+ [
+ 450,
+ '*foo __bar *baz bim__ bam*',
+ '<em>foo <strong>bar *baz bim</strong> bam</em>',
+ ],
+ [451, '**foo **bar baz**', '**foo <strong>bar baz</strong>'],
+ [452, '*foo *bar baz*', '*foo <em>bar baz</em>'],
+ [453, '*[bar*](/url)', '*<a href="/url">bar*</a>'],
+ [459, '*a `*`*', '<em>a <code>*</code></em>'],
+ [460, '_a `_`_', '<em>a <code>_</code></em>'],
+];
+
+describe('emphasis and strong (CommonMark 0.31.2 §6.2)', () => {
+ it.each(COMMONMARK)('example %i: %j', (_, markdown, expected) => {
+ expect(html(markdown)).toBe(expected);
+ });
+
+ it('keeps an escaped delimiter literal inside emphasis', () => {
+ expect(html('*a\\*b*')).toBe('<em>a*b</em>');
+ expect(html('**a\\*\\*b**')).toBe('<strong>a**b</strong>');
+ });
+
+ it('draws `***x***` as strong outside emphasis, as Markdown always has', () => {
+ expect(html('***both***')).toBe('<strong><em>both</em></strong>');
+ expect(html('foo***bar***baz')).toBe('foo<strong><em>bar</em></strong>baz');
+ });
+});
+
+describe('strong inside emphasis, as RichText writes it (spec:AST-061 FR7)', () => {
+ it.each([
+ ['*see **bold***', '<em>see <strong>bold</strong></em>'],
+ ['*see **bold** more*', '<em>see <strong>bold</strong> more</em>'],
+ ['**bold *both***', '<strong>bold <em>both</em></strong>'],
+ ['_see __bold__ more_', '<em>see <strong>bold</strong> more</em>'],
+ [
+ '*see **bold** and **more***',
+ '<em>see <strong>bold</strong> and <strong>more</strong></em>',
+ ],
+ ])('reads %j', (markdown, expected) => {
+ expect(html(markdown)).toBe(expected);
+ });
+
+ it('renders the strong inside the emphasis', () => {
+ const {container} = render(
+ <Markdown>{'Keep *see **bold** more* here.'}</Markdown>,
+ );
+ const emphasis = container.querySelector('em');
+ expect(emphasis?.textContent).toBe('see bold more');
+ expect(emphasis?.querySelector('strong')?.textContent).toBe('bold');
+ expect(container.textContent).not.toContain('*');
+ });
+});
+
+/**
+ * The fastest of three parses of `markdown`, in milliseconds, after one
+ * warm-up parse: the minimum is the least disturbed by other work running on
+ * the same machine.
+ */
+function parseTime(markdown: string): number {
+ parseMarkdownAst(markdown);
+ let fastest = Number.POSITIVE_INFINITY;
+ for (let round = 0; round < 3; round++) {
+ const started = performance.now();
+ parseMarkdownAst(markdown);
+ fastest = Math.min(fastest, performance.now() - started);
+ }
+ return fastest;
+}
+
+describe('pathological emphasis', () => {
+ const nestedStrongEmph = (levels: number): string =>
+ `${'*a **a '.repeat(levels)}b${' a** a*'.repeat(levels)}`;
+
+ it('nests up to 100 levels exactly', () => {
+ expect(html(nestedStrongEmph(50))).toBe(
+ `${'<em>a <strong>a '.repeat(50)}b${' a</strong> a</em>'.repeat(50)}`,
+ );
+ });
+
+ it('keeps runs past 100 levels as text, around the nesting it allows', () => {
+ expect(html(nestedStrongEmph(51))).toBe(
+ `*a **a ${'<em>a <strong>a '.repeat(50)}b${' a</strong> a</em>'.repeat(50)} a** a*`,
+ );
+ });
+
+ it.each([
+ ['nested strong emph', nestedStrongEmph, 5_000],
+ ['openers with no closers', (n: number) => '*a **a '.repeat(n), 7_000],
+ [
+ 'openers, then as many closers',
+ (n: number) => `${'*a '.repeat(n)}${'b* '.repeat(n)}`,
+ 4_000,
+ ],
+ ['closers with no openers', (n: number) => ' a*'.repeat(n), 15_000],
+ [
+ 'runs whose lengths sum to a multiple of three',
+ (n: number) => `${'a**b'.repeat(n)}${'c* '.repeat(n)}`,
+ 7_000,
+ ],
+ ] as const)(
+ 'parses %s without throwing, within a time budget',
+ (_, input, size) => {
+ const large = input(size * 2);
+ expect(() => parseMarkdownAst(large)).not.toThrow();
+ // Alone, the largest of these parses in about 0.1 s. The budget leaves
+ // room for a loaded test machine running other suites in parallel; the
+ // quadratic pass took 20 to 200 seconds on the nested inputs.
+ expect(parseTime(large)).toBeLessThan(5000);
+ },
+ 60_000,
+ );
+
+ it('renders 10,000 levels of nesting without exhausting the stack', () => {
+ const {container} = render(<Markdown>{nestedStrongEmph(10_000)}</Markdown>);
+ expect(container.querySelectorAll('em').length).toBe(50);
+ expect(container.querySelectorAll('strong').length).toBe(50);
+ });
+});
diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.ts
index b4846b1641..6cb0be9c7e 100644
--- a/packages/core/src/Markdown/parser.ts
+++ b/packages/core/src/Markdown/parser.ts
@@ -1151,11 +1151,350 @@ function findClosingParen(text: string, start: number): number {
return -1;
}
-function isWordChar(ch: string | undefined): boolean {
- if (ch == null) {
- return false;
+// ---------------------------------------------------------------------------
+// Emphasis and strong (CommonMark 0.31 §6.2)
+// ---------------------------------------------------------------------------
+
+/**
+ * Adds `value` to the last text node of `nodes`, unless that node is a
+ * delimiter run, which stays its own node until emphasis is resolved.
+ */
+function appendInlineText(
+ nodes: MarkdownAstPhrasingContent<RuntimeExtensionNode>[],
+ delimiterNodes: ReadonlySet<object> | null,
+ value: string,
+): void {
+ const last = nodes[nodes.length - 1];
+ if (last?.type === 'text' && delimiterNodes?.has(last) !== true) {
+ nodes[nodes.length - 1] = {...last, value: last.value + value};
+ } else {
+ nodes.push({type: 'text', value});
+ }
+}
+
+type PhrasingNode = MarkdownAstPhrasingContent<RuntimeExtensionNode>;
+
+/** A run of `*` or `_`: what it can do, and how many delimiters are left. */
+interface DelimiterRun {
+ /** The run's text node; its text shrinks as delimiters are used. */
+ readonly node: {type: 'text'; value: string};
+ readonly character: '*' | '_';
+ readonly originalLength: number;
+ length: number;
+ readonly canOpen: boolean;
+ readonly canClose: boolean;
+}
+
+const UNICODE_WHITESPACE = /^[\t\n\f\r\p{Zs}]$/u;
+const UNICODE_PUNCTUATION = /^[\p{P}\p{S}]$/u;
+
+/** The character before `index` (a whole code point), if any. */
+function characterBefore(text: string, index: number): string | undefined {
+ if (index <= 0) {
+ return undefined;
+ }
+ const low = text.charCodeAt(index - 1);
+ if (low >= 0xdc00 && low <= 0xdfff && index >= 2) {
+ const high = text.charCodeAt(index - 2);
+ if (high >= 0xd800 && high <= 0xdbff) {
+ return text.slice(index - 2, index);
+ }
+ }
+ return text[index - 1];
+}
+
+/** The character at `index` (a whole code point), if any. */
+function characterAt(text: string, index: number): string | undefined {
+ if (index >= text.length) {
+ return undefined;
}
- return /\w/.test(ch);
+ const codePoint = text.codePointAt(index);
+ return codePoint == null ? undefined : String.fromCodePoint(codePoint);
+}
+
+/**
+ * The delimiter run `text.slice(start, end)`: left- or right-flanking by the
+ * characters around it, and whether it can open or close emphasis — an
+ * underscore run only where it is not inside a word.
+ */
+function delimiterRun(
+ character: '*' | '_',
+ text: string,
+ start: number,
+ end: number,
+): DelimiterRun {
+ const before = characterBefore(text, start);
+ const after = characterAt(text, end);
+ // The start and end of the text count as whitespace.
+ const isSpaceBefore = before == null || UNICODE_WHITESPACE.test(before);
+ const isSpaceAfter = after == null || UNICODE_WHITESPACE.test(after);
+ const isPunctuationBefore =
+ before != null && UNICODE_PUNCTUATION.test(before);
+ const isPunctuationAfter = after != null && UNICODE_PUNCTUATION.test(after);
+ const isLeftFlanking =
+ !isSpaceAfter &&
+ (!isPunctuationAfter || isSpaceBefore || isPunctuationBefore);
+ const isRightFlanking =
+ !isSpaceBefore &&
+ (!isPunctuationBefore || isSpaceAfter || isPunctuationAfter);
+ return {
+ node: {type: 'text', value: text.slice(start, end)},
+ character,
+ originalLength: end - start,
+ length: end - start,
+ canOpen:
+ character === '*'
+ ? isLeftFlanking
+ : isLeftFlanking && (!isRightFlanking || isPunctuationBefore),
+ canClose:
+ character === '*'
+ ? isRightFlanking
+ : isRightFlanking && (!isLeftFlanking || isPunctuationAfter),
+ };
+}
+
+/**
+ * The deepest emphasis and strong may nest. Pairs that would nest deeper stay
+ * text, as other CommonMark parsers cap nesting, so a hostile input cannot
+ * build a tree that overflows the stack of a recursive walk or render.
+ */
+const MAX_EMPHASIS_DEPTH = 100;
+
+interface Entry {
+ node: PhrasingNode;
+ previous: Entry | null;
+ next: Entry | null;
+}
+
+/**
+ * Pairs the delimiter runs of one inline text into emphasis and strong, as
+ * CommonMark's process-emphasis procedure does: each closer, in order, takes
+ * the nearest opener of its character that the rule of three allows, two
+ * delimiters for strong when both have two, else one for emphasis, and the
+ * nodes between them become its children. A run left unpaired stays text,
+ * joined to the text beside it. `***x***` keeps strong outside emphasis, as
+ * Markdown has always drawn it. Runs are taken off a linked stack as they
+ * are used or passed, so the pass is linear in the number of runs, and
+ * nesting stops at MAX_EMPHASIS_DEPTH.
+ */
+function resolveEmphasis(
+ nodes: ReadonlyArray<PhrasingNode>,
+ delimiters: ReadonlyArray<DelimiterRun>,
+): PhrasingNode[] {
+ const head: Entry = {
+ node: {type: 'text', value: ''},
+ previous: null,
+ next: null,
+ };
+ let tail = head;
+ const entryOf = new Map<object, Entry>();
+ for (const node of nodes) {
+ const entry: Entry = {node, previous: tail, next: null};
+ tail.next = entry;
+ tail = entry;
+ entryOf.set(node, entry);
+ }
+ const unlink = (entry: Entry): void => {
+ if (entry.previous != null) {
+ entry.previous.next = entry.next;
+ }
+ if (entry.next != null) {
+ entry.next.previous = entry.previous;
+ }
+ };
+ // The delimiter stack, as a doubly linked list in source order: a run
+ // leaves it once used up, paired across, or unable to pair, so every
+ // search below visits only runs still on it, and the whole pass is linear.
+ interface StackRun {
+ readonly run: DelimiterRun;
+ readonly index: number;
+ previous: StackRun | null;
+ next: StackRun | null;
+ /** The deepest emphasis among the nodes after this run, up to the next. */
+ depthAfter: number;
+ canClose: boolean;
+ }
+ const stack: StackRun[] = delimiters.map((run, index) => ({
+ run,
+ index,
+ previous: null,
+ next: null,
+ depthAfter: 0,
+ canClose: run.canClose,
+ }));
+ stack.forEach((item, index) => {
+ item.previous = stack[index - 1] ?? null;
+ item.next = stack[index + 1] ?? null;
+ });
+ // Takes a run off the stack; what followed it now follows the one before.
+ const leave = (item: StackRun): void => {
+ if (item.previous != null) {
+ item.previous.next = item.next;
+ item.previous.depthAfter = Math.max(
+ item.previous.depthAfter,
+ item.depthAfter,
+ );
+ }
+ if (item.next != null) {
+ item.next.previous = item.previous;
+ }
+ };
+ // Where the search for an opener stops, by source index, for closers of
+ // each kind: no opener at or before it can match them.
+ const openersBottom = new Map<string, number>();
+ // The strong a pair of runs made last, to keep `***x***` strong outside.
+ let lastStrong: {
+ readonly node: PhrasingNode;
+ readonly opener: DelimiterRun;
+ readonly closer: DelimiterRun;
+ } | null = null;
+ let closerItem: StackRun | null = stack[0] ?? null;
+ while (closerItem != null) {
+ const closer = closerItem.run;
+ if (!closerItem.canClose || closer.length === 0) {
+ closerItem = closerItem.next;
+ continue;
+ }
+ const key = `${closer.character}${closer.canOpen ? 1 : 0}${closer.originalLength % 3}`;
+ const bottom = openersBottom.get(key) ?? -1;
+ let openerItem: StackRun | null = closerItem.previous;
+ for (; openerItem != null && openerItem.index > bottom;) {
+ const candidate = openerItem.run;
+ // The rule of three: a run that can both open and close does not pair
+ // with one whose lengths sum to a multiple of three, unless both are.
+ const isOddMatch =
+ (closer.canOpen || candidate.canClose) &&
+ (candidate.originalLength + closer.originalLength) % 3 === 0 &&
+ !(
+ candidate.originalLength % 3 === 0 && closer.originalLength % 3 === 0
+ );
+ if (
+ candidate.character === closer.character &&
+ candidate.canOpen &&
+ candidate.length > 0 &&
+ !isOddMatch
+ ) {
+ break;
+ }
+ openerItem = openerItem.previous;
+ }
+ if (openerItem == null || openerItem.index <= bottom) {
+ openersBottom.set(key, closerItem.index - 1);
+ const next: StackRun | null = closerItem.next;
+ if (!closer.canOpen) {
+ leave(closerItem);
+ }
+ closerItem = next;
+ continue;
+ }
+ const opener = openerItem.run;
+ // Runs between the pair can no longer pair: take them off the stack, and
+ // note the deepest emphasis among the nodes between.
+ let innerDepth = openerItem.depthAfter;
+ for (
+ let between = openerItem.next;
+ between != null && between !== closerItem;
+ between = between.next
+ ) {
+ innerDepth = Math.max(innerDepth, between.depthAfter);
+ }
+ openerItem.next = closerItem;
+ closerItem.previous = openerItem;
+ openerItem.depthAfter = innerDepth;
+ if (innerDepth >= MAX_EMPHASIS_DEPTH) {
+ // Nesting deeper than the cap: both runs stay text, so no input can
+ // build a tree deep enough to exhaust the stack of whatever walks it.
+ const next: StackRun | null = closerItem.next;
+ leave(openerItem);
+ closerItem.canClose = false;
+ if (!closer.canOpen) {
+ leave(closerItem);
+ }
+ closerItem = next;
+ continue;
+ }
+ const openerEntry = entryOf.get(opener.node);
+ const closerEntry = entryOf.get(closer.node);
+ if (openerEntry == null || closerEntry == null) {
+ break;
+ }
+ const use = opener.length >= 2 && closer.length >= 2 ? 2 : 1;
+ const children: PhrasingNode[] = [];
+ for (
+ let entry = openerEntry.next;
+ entry != null && entry !== closerEntry;
+ entry = entry.next
+ ) {
+ children.push(entry.node);
+ }
+ let wrapper: PhrasingNode;
+ if (use === 2) {
+ wrapper = {type: 'strong', children};
+ lastStrong = {node: wrapper, opener, closer};
+ } else {
+ const [only] = children;
+ wrapper =
+ children.length === 1 &&
+ only === lastStrong?.node &&
+ lastStrong.opener === opener &&
+ lastStrong.closer === closer &&
+ only.type === 'strong'
+ ? {
+ type: 'strong',
+ children: [{type: 'emphasis', children: only.children}],
+ }
+ : {type: 'emphasis', children};
+ }
+ const wrapperEntry: Entry = {
+ node: wrapper,
+ previous: openerEntry,
+ next: closerEntry,
+ };
+ openerEntry.next = wrapperEntry;
+ closerEntry.previous = wrapperEntry;
+ openerItem.depthAfter = innerDepth + 1;
+ opener.length -= use;
+ closer.length -= use;
+ opener.node.value = opener.character.repeat(opener.length);
+ closer.node.value = closer.character.repeat(closer.length);
+ if (opener.length === 0) {
+ unlink(openerEntry);
+ leave(openerItem);
+ }
+ if (closer.length === 0) {
+ unlink(closerEntry);
+ const next: StackRun | null = closerItem.next;
+ leave(closerItem);
+ closerItem = next;
+ }
+ }
+ // An unpaired run is text, joined to the text beside it.
+ const leftover = new Set<object>(
+ delimiters.filter(run => run.length > 0).map(run => run.node),
+ );
+ const result: PhrasingNode[] = [];
+ let previousIsLeftover = false;
+ for (let entry = head.next; entry != null; entry = entry.next) {
+ const {node} = entry;
+ const isLeftover = leftover.has(node);
+ const last = result[result.length - 1];
+ if (
+ node.type === 'text' &&
+ last?.type === 'text' &&
+ (isLeftover || previousIsLeftover)
+ ) {
+ result[result.length - 1] = {...last, value: last.value + node.value};
+ previousIsLeftover = true;
+ continue;
+ }
+ result.push(
+ isLeftover
+ ? {type: 'text', value: (node as {value: string}).value}
+ : node,
+ );
+ previousIsLeftover = isLeftover;
+ }
+ return result;
}
/** True when the character at `index` is preceded by an odd backslash run. */
@@ -1547,6 +1886,10 @@ function parseInlineImpl(
context: 'default' | 'tableCell' = 'default',
): MarkdownAstPhrasingContent<RuntimeExtensionNode>[] {
const nodes: MarkdownAstPhrasingContent<RuntimeExtensionNode>[] = [];
+ // Runs of `*` and `_`, in order; each is also a text node in `nodes` until
+ // resolveEmphasis pairs it. Created only when a run appears.
+ let delimiters: DelimiterRun[] | null = null;
+ let delimiterNodes: Set<object> | null = null;
// Only a plugin that actually contributes INLINE syntax may cost anything
// per source position. A transform-only list contributes none, so it takes
// the same path as an omitted or empty one: no candidate probe per
@@ -1700,66 +2043,29 @@ function parseInlineImpl(
}
}
- // --- Bold-italic: *** or ___ ---
- if (
- (text[i] === '*' && text[i + 1] === '*' && text[i + 2] === '*') ||
- (text[i] === '_' && text[i + 1] === '_' && text[i + 2] === '_')
- ) {
- const marker = text.slice(i, i + 3);
- const isUnderscore = text[i] === '_';
- if (isUnderscore && isWordChar(text[i - 1])) {
- // mid-word underscore — fall through
- } else {
- const closeIndex = text.indexOf(marker, i + 3);
- if (
- closeIndex !== -1 &&
- (!isUnderscore || !isWordChar(text[closeIndex + 3]))
- ) {
- nodes.push({
- type: 'strong',
- children: [
- {
- type: 'emphasis',
- children: parseInlineImpl(
- text.slice(i + 3, closeIndex),
- opts,
- context,
- ),
- },
- ],
- });
- i = closeIndex + 3;
- continue;
- }
+ // --- Emphasis and strong: a run of `*` or `_` is a delimiter run,
+ // paired with the others once the whole text is read (CommonMark 0.31
+ // §6.2; see resolveEmphasis). ---
+ if (text[i] === '*' || text[i] === '_') {
+ const character = text[i] as '*' | '_';
+ let runEnd = i + 1;
+ while (text[runEnd] === character) {
+ runEnd++;
}
- }
-
- // --- Bold: ** or __ ---
- if (
- (text[i] === '*' && text[i + 1] === '*') ||
- (text[i] === '_' && text[i + 1] === '_')
- ) {
- const marker = text.slice(i, i + 2);
- const isUnderscore = text[i] === '_';
- if (isUnderscore && isWordChar(text[i - 1])) {
- // mid-word underscore — fall through
- } else {
- const closeIndex = text.indexOf(marker, i + 2);
- if (
- closeIndex !== -1 &&
- (!isUnderscore || !isWordChar(text[closeIndex + 2]))
- ) {
- nodes.push({
- type: 'strong',
- children: parseInlineImpl(
- text.slice(i + 2, closeIndex),
- opts,
- context,
- ),
- });
- i = closeIndex + 2;
- continue;
- }
+ const run = delimiterRun(character, text, i, runEnd);
+ if (run.canOpen || run.canClose) {
+ nodes.push(run.node);
+ (delimiters ??= []).push(run);
+ (delimiterNodes ??= new Set()).add(run.node);
+ i = runEnd;
+ continue;
+ }
+ // A run that can neither open nor close is text; a plugin that starts
+ // with it may still read it below.
+ if (inlineExtensionStarts?.has(character) !== true) {
+ appendInlineText(nodes, delimiterNodes, text.slice(i, runEnd));
+ i = runEnd;
+ continue;
}
}
@@ -1780,32 +2086,6 @@ function parseInlineImpl(
}
}
- // --- Italic: * or _ ---
- if (text[i] === '*' || text[i] === '_') {
- const isUnderscore = text[i] === '_';
- if (isUnderscore && isWordChar(text[i - 1])) {
- // mid-word underscore — fall through
- } else {
- const closeIndex = text.indexOf(text[i], i + 1);
- if (
- closeIndex !== -1 &&
- closeIndex > i + 1 &&
- (!isUnderscore || !isWordChar(text[closeIndex + 1]))
- ) {
- nodes.push({
- type: 'emphasis',
- children: parseInlineImpl(
- text.slice(i + 1, closeIndex),
- opts,
- context,
- ),
- });
- i = closeIndex + 1;
- continue;
- }
- }
- }
-
// --- Extension syntax (built-ins and protected contexts win) ---
if (inlineExtensionStarts !== undefined) {
const extension = matchExtensionSyntax(text, i, 'inline', opts);
@@ -1825,15 +2105,7 @@ function parseInlineImpl(
if (text[i] === '&') {
const reference = matchCharacterReference(text, i);
if (reference != null) {
- const last = nodes[nodes.length - 1];
- if (last?.type === 'text') {
- nodes[nodes.length - 1] = {
- ...last,
- value: last.value + reference.value,
- };
- } else {
- nodes.push({type: 'text', value: reference.value});
- }
+ appendInlineText(nodes, delimiterNodes, reference.value);
i = reference.end;
continue;
}
@@ -1861,15 +2133,7 @@ function parseInlineImpl(
const trimmed = line.replace(/ +$/, '');
if (line.length - trimmed.length >= 2) {
if (trimmed.length > 0) {
- const last = nodes[nodes.length - 1];
- if (last?.type === 'text') {
- nodes[nodes.length - 1] = {
- ...last,
- value: last.value + trimmed,
- };
- } else {
- nodes.push({type: 'text', value: trimmed});
- }
+ appendInlineText(nodes, delimiterNodes, trimmed);
}
nodes.push({type: 'break'});
i = end + 1;
@@ -1877,15 +2141,10 @@ function parseInlineImpl(
}
}
- const last = nodes[nodes.length - 1];
- if (last?.type === 'text') {
- nodes[nodes.length - 1] = {...last, value: last.value + content};
- } else {
- nodes.push({type: 'text', value: content});
- }
+ appendInlineText(nodes, delimiterNodes, content);
i = end;
}
- return nodes;
+ return delimiters == null ? nodes : resolveEmphasis(nodes, delimiters);
}
// ---------------------------------------------------------------------------
@@ -3477,15 +3736,22 @@ export function trimStreamingArtifacts(
}
}
}
- // Append closing markers for each unpaired opener (in reverse order)
+ // Append closing markers for each unpaired opener (in reverse order),
+ // before any trailing whitespace: a closer must follow content to close
+ // (CommonMark's flanking rules), so `**bold ` closes as `**bold** `.
for (let i = markers.length - 1; i >= 0; i--) {
if (!paired.has(i)) {
const marker = markers[i];
+ const content = tail.slice(marker.pos + marker.len);
+ const trailing = content.length - content.trimEnd().length;
// Only close if there's actual content after the opener
- if (marker.pos + marker.len < tail.length) {
- tail = tail + '*'.repeat(marker.len);
+ if (trailing < content.length) {
+ tail =
+ tail.slice(0, tail.length - trailing) +
+ '*'.repeat(marker.len) +
+ tail.slice(tail.length - trailing);
} else {
- // Trailing marker with no content — trim it
+ // A marker with nothing but whitespace after it — trim it
tail = tail.slice(0, marker.pos);
}
}