From 4217eabe34859bb7ff1fea23d4ebe21e75913436 Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Tue, 17 Nov 2026 17:24:21 +0000 Subject: [PATCH 1/7] fix(parse): ignore closing fences indented 4+ spaces inside components Amp-Thread-ID: https://ampcode.com/threads/T-01a1ea1f-799f-74fe-a965-dcf9fceb5e92 --- AGENTS.md | 9 ++ ...omponent-codeblock-closing-fence-indent.md | 138 ++++++++++++++++++ .../component-codeblock-indented-fence.md | 77 ++++++++++ .../component-codeblock-indented-opener.md | 74 ++++++++++ ...mponent-nested-codeblock-indented-fence.md | 99 +++++++++++++ ...component-slot-codeblock-indented-fence.md | 87 +++++++++++ packages/comark/SPEC/auto-close.md | 12 ++ packages/comark/src/auto-close.ts | 5 +- packages/comark/src/plugins/components.ts | 29 ++-- packages/comark/test/auto-close.test.ts | 12 ++ test/bundle.test.ts | 2 +- 11 files changed, 532 insertions(+), 12 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/component-codeblock-closing-fence-indent.md create mode 100644 packages/comark/SPEC/COMARK/component-codeblock-indented-fence.md create mode 100644 packages/comark/SPEC/COMARK/component-codeblock-indented-opener.md create mode 100644 packages/comark/SPEC/COMARK/component-nested-codeblock-indented-fence.md create mode 100644 packages/comark/SPEC/COMARK/component-slot-codeblock-indented-fence.md diff --git a/AGENTS.md b/AGENTS.md index b94ffe9a..814533cf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -801,6 +801,15 @@ SPEC coverage: `SPEC/COMARK/component-nested-*-outdented.md`, `SPEC/COMARK/component-nested-codeblock-indented.md`, `SPEC/COMARK/codeblock-indented-content.md`. +**Closing fences.** `comark_block` scans ahead for its closing `::` and skips +code fences on the way. As in CommonMark, a closing fence may sit at most 3 +spaces right of its container: the innermost component still open at the +opening fence, but never right of that fence. A deeper fence line is code +content, so a `::` after it does not close the component. `autoCloseMarkdown` +follows the same rule. SPEC coverage: `SPEC/COMARK/component-*codeblock-indented-fence.md`, +`SPEC/COMARK/component-codeblock-closing-fence-indent.md`, +`SPEC/COMARK/component-codeblock-indented-opener.md`. + ## Embedded HTML Contract: `packages/comark/SPEC/HTML/README.md`. HTML nodes carry diff --git a/packages/comark/SPEC/COMARK/component-codeblock-closing-fence-indent.md b/packages/comark/SPEC/COMARK/component-codeblock-closing-fence-indent.md new file mode 100644 index 00000000..f52f0bd3 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-codeblock-closing-fence-indent.md @@ -0,0 +1,138 @@ +## Input + +```md +::box +```js +a + ``` +```js +b + ``` +```js +c + ``` + :::inner + ```js + d + ``` + ::: +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "box", + {}, + [ + "pre", + { + "language": "js" + }, + [ + "code", + { + "class": "language-js" + }, + "a" + ] + ], + [ + "pre", + { + "language": "js" + }, + [ + "code", + { + "class": "language-js" + }, + "b" + ] + ], + [ + "pre", + { + "language": "js" + }, + [ + "code", + { + "class": "language-js" + }, + "c" + ] + ], + [ + "inner", + {}, + [ + "pre", + { + "language": "js" + }, + [ + "code", + { + "class": "language-js" + }, + "d" + ] + ] + ] + ], + [ + "p", + {}, + "After" + ] + ] +} +``` + +## HTML + +```html + +
a
+
b
+
c
+ +
d
+
+
+

After

+``` + +## Markdown + +```md +::box +```js +a +``` + +```js +b +``` + +```js +c +``` + + :::inner + ```js + d + ``` + ::: +:: + +After +``` diff --git a/packages/comark/SPEC/COMARK/component-codeblock-indented-fence.md b/packages/comark/SPEC/COMARK/component-codeblock-indented-fence.md new file mode 100644 index 00000000..7b4f4f80 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-codeblock-indented-fence.md @@ -0,0 +1,77 @@ +## Input + +```md +::box +```mdc +::a + ```js + x + ``` +:: +``` +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "box", + {}, + [ + "pre", + { + "language": "mdc" + }, + [ + "code", + { + "class": "language-mdc" + }, + "::a\n ```js\n x\n ```\n::" + ] + ] + ], + [ + "p", + {}, + "After" + ] + ] +} +``` + +## HTML + +```html + +
::a
+      ```js
+      x
+      ```
+  ::
+
+

After

+``` + +## Markdown + +```md +::box +```mdc +::a + ```js + x + ``` +:: +``` +:: + +After +``` diff --git a/packages/comark/SPEC/COMARK/component-codeblock-indented-opener.md b/packages/comark/SPEC/COMARK/component-codeblock-indented-opener.md new file mode 100644 index 00000000..10d8bb85 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-codeblock-indented-opener.md @@ -0,0 +1,74 @@ +## Input + +```md +::box + ```md + - item + ```js + x + ``` + ``` +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "box", + {}, + [ + "pre", + { + "language": "md" + }, + [ + "code", + { + "class": "language-md" + }, + "- item\n ```js\n x\n ```" + ] + ] + ], + [ + "p", + {}, + "After" + ] + ] +} +``` + +## HTML + +```html + +
- item
+    ```js
+    x
+    ```
+
+

After

+``` + +## Markdown + +```md +::box +~~~md +- item + ```js + x + ``` +~~~ +:: + +After +``` diff --git a/packages/comark/SPEC/COMARK/component-nested-codeblock-indented-fence.md b/packages/comark/SPEC/COMARK/component-nested-codeblock-indented-fence.md new file mode 100644 index 00000000..4735aeb4 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-nested-codeblock-indented-fence.md @@ -0,0 +1,99 @@ +## Input + +```md +::code-preview + :::tabs + ::::tabs-item{label="Code"} + ```mdc + ::a + ```js + x + ``` + :: + ``` + :::: + ::: +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "code-preview", + {}, + [ + "tabs", + {}, + [ + "tabs-item", + { + "label": "Code" + }, + [ + "pre", + { + "language": "mdc" + }, + [ + "code", + { + "class": "language-mdc" + }, + "::a\n ```js\n x\n ```\n::" + ] + ] + ] + ] + ], + [ + "p", + {}, + "After" + ] + ] +} +``` + +## HTML + +```html + + + +
::a
+          ```js
+          x
+          ```
+      ::
+
+
+
+

After

+``` + +## Markdown + +```md +::code-preview + :::tabs + ::::tabs-item{label="Code"} + ```mdc + ::a + ```js + x + ``` + :: + ``` + :::: + ::: +:: + +After +``` diff --git a/packages/comark/SPEC/COMARK/component-slot-codeblock-indented-fence.md b/packages/comark/SPEC/COMARK/component-slot-codeblock-indented-fence.md new file mode 100644 index 00000000..2cc70494 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-slot-codeblock-indented-fence.md @@ -0,0 +1,87 @@ +## Input + +```md +::component +#code +```mdc +::a + ```js + x + ``` +:: +``` +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "component", + {}, + [ + "template", + { + "name": "code" + }, + [ + "pre", + { + "language": "mdc" + }, + [ + "code", + { + "class": "language-mdc" + }, + "::a\n ```js\n x\n ```\n::" + ] + ] + ] + ], + [ + "p", + {}, + "After" + ] + ] +} +``` + +## HTML + +```html + + + +

After

+``` + +## Markdown + +```md +::component +#code +```mdc +::a + ```js + x + ``` +:: +``` +:: + +After +``` diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md index 156e0254..3a149797 100644 --- a/packages/comark/SPEC/auto-close.md +++ b/packages/comark/SPEC/auto-close.md @@ -2337,6 +2337,18 @@ A shorter or different marker is not a closer either: + ```\ncode\n~~~\n**bold ``` +A closer indented 4 or more spaces is code content; up to 3 spaces still closes: + +```diff +- ```\ncode\n ```\n**bold ++ ```\ncode\n ```\n**bold +``` + +```diff +- ```\ncode\n ```\n**bold ++ ```\ncode\n ```\n**bold** +``` + --- ## Leave horizontal rules alone diff --git a/packages/comark/src/auto-close.ts b/packages/comark/src/auto-close.ts index c4cd74f5..8791fd57 100644 --- a/packages/comark/src/auto-close.ts +++ b/packages/comark/src/auto-close.ts @@ -204,6 +204,7 @@ function scanBlocks(src: string, o: Opts): DocState { const comps: Component[] = [] let fenceLen = 0 let fenceCh = 0 + let fenceIndent = 0 // a closer must stay within 3 spaces of this column (its container's) let raw: RawTag | null = null let fmOpen = false let fmContent = false @@ -242,10 +243,12 @@ function scanBlocks(src: string, o: Opts): DocState { if (!(c0 === 96 && isIncompleteInlineFence(src, st, en))) { fenceLen = run fenceCh = c0 + // The container is the innermost open component, never right of the fence itself. + fenceIndent = Math.min(comps.length > 0 ? comps[comps.length - 1].indent.length : 0, st - ls) shielded = i continue } - } else if (c0 === fenceCh && run >= fenceLen && st + run === en) { + } else if (c0 === fenceCh && run >= fenceLen && st + run === en && st - ls - fenceIndent < 4) { // A closer is the same character, at least as long, and followed only by // whitespace. `` ```js `` inside an open block is code content, not a closer. fenceLen = 0 diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index f57df3db..589b73e6 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -105,9 +105,11 @@ const markdownItComarkBlock: PluginSimple = (md) => { let inCodeFence = false let codeFenceCharCode = 0 let codeFenceCount = 0 + // A closing fence must sit within 3 spaces of this column (CommonMark container indent). + let codeFenceIndent = 0 - // Track nesting depth for blocks with the same marker count - let nestingDepth = 0 + // Nested components still open, as `[markerCount, indent]`. + const openComponents: Array<[number, number]> = [] if (state.src[start] !== ':') return false @@ -155,7 +157,7 @@ const markdownItComarkBlock: PluginSimple = (md) => { // Detect closing code fence (``` or ~~~) if (inCodeFence) { - if (lineCharCode === codeFenceCharCode) { + if (lineCharCode === codeFenceCharCode && state.sCount[nextLine] - codeFenceIndent < 4) { let fencePos = start + 1 while (fencePos < max && state.src.charCodeAt(fencePos) === codeFenceCharCode) fencePos++ if (fencePos - start >= codeFenceCount) { @@ -175,6 +177,9 @@ const markdownItComarkBlock: PluginSimple = (md) => { codeFenceCharCode = lineCharCode codeFenceCount = fencePos - start codeFenceShift = childShifts[childShifts.length - 1] + // The fence's container is the innermost open component, never right of the fence itself. + const containerIndent = openComponents.length ? openComponents[openComponents.length - 1][1] : indent + codeFenceIndent = Math.min(containerIndent, state.sCount[nextLine]) continue } } @@ -185,22 +190,26 @@ const markdownItComarkBlock: PluginSimple = (md) => { if (marker_str !== state.src[pos]) break } - // Closing fence must match the opening fence length - if (pos - start !== marker_count) continue - + const count = pos - start pos = state.skipSpaces(pos) if (pos < max) { - // A new nested block opens with same marker count - nestingDepth++ + // A nested block opens + if (count >= min_markers) openComponents.push([count, state.sCount[nextLine]]) continue } - if (nestingDepth > 0) { - nestingDepth-- + // A bare marker closes the innermost open nested block with the same length, and any left open inside it + let open = openComponents.length - 1 + while (open >= 0 && openComponents[open][0] !== count) open-- + if (open >= 0) { + openComponents.length = open continue } + // Closing fence must match the opening fence length + if (count !== marker_count) continue + auto_closed = true break } diff --git a/packages/comark/test/auto-close.test.ts b/packages/comark/test/auto-close.test.ts index af46be61..8da58d4a 100644 --- a/packages/comark/test/auto-close.test.ts +++ b/packages/comark/test/auto-close.test.ts @@ -220,6 +220,18 @@ describe('autoCloseMarkdown - Comark Components', () => { expect(autoCloseMarkdown(input)).toBe(expected) }) + it('measures a closing code fence against its innermost component', () => { + // 4+ spaces past the component is code content: `**bold` stays code and only `::box` gets closed. + expect(autoCloseMarkdown('::box\n```\n::a\n ```\n::\n**bold')).toBe('::box\n```\n::a\n ```\n::\n**bold\n::') + // The children of `::box` sit at column 0, even when the fence itself is indented. + const indentedFence = '::box\n ```md\n ```js\n ```\n ```\n::' + expect(autoCloseMarkdown(indentedFence)).toBe(indentedFence) + // Within 3 spaces of `:::inner` still closes. + expect(autoCloseMarkdown('::box\n :::inner\n ```\n x\n ```\n :::\n::\n**bold')).toBe( + '::box\n :::inner\n ```\n x\n ```\n :::\n::\n**bold**' + ) + }) + it('should not add closing markers to empty content', () => { const input = '' expect(autoCloseMarkdown(input)).toBe(input) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index aac8fcb8..5dfaa94e 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "42.9k (83 files)", "@comark/svelte": "50.3k (91 files)", "@comark/vue": "62.6k (89 files)", - "comark": "440k (171 files)", + "comark": "441k (171 files)", } `) }) From fc0c26fc1bef2c19797b6e5d41cb3e1d95ebcb9e Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Wed, 18 Nov 2026 14:41:23 +0000 Subject: [PATCH 2/7] fix(parse): count list items and indented code when scanning fences Amp-Thread-ID: https://ampcode.com/threads/T-01a1ea1f-799f-74fe-a965-dcf9fceb5e92 --- AGENTS.md | 16 ++- ...component-codeblock-invalid-nested-name.md | 64 ++++++++++ .../COMARK/component-indented-code-fence.md | 49 ++++++++ ...component-list-codeblock-indented-fence.md | 119 ++++++++++++++++++ packages/comark/SPEC/auto-close.md | 26 ++++ packages/comark/src/auto-close.ts | 36 +++++- packages/comark/src/internal/list-item.ts | 29 +++++ packages/comark/src/plugins/components.ts | 27 +++- test/bundle.test.ts | 2 +- 9 files changed, 352 insertions(+), 16 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/component-codeblock-invalid-nested-name.md create mode 100644 packages/comark/SPEC/COMARK/component-indented-code-fence.md create mode 100644 packages/comark/SPEC/COMARK/component-list-codeblock-indented-fence.md create mode 100644 packages/comark/src/internal/list-item.ts diff --git a/AGENTS.md b/AGENTS.md index 814533cf..9d167bad 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -100,6 +100,7 @@ packages/comark/ │ ├── shiki.ts # Shared Shiki runtime used by both entry points │ ├── frontmatter.ts # parseFrontmatter() / renderFrontmatter() │ ├── heading-id.ts # Auto heading id generation (shared by parser + stringifier) +│ ├── list-item.ts # List item content column (component fence scanner + auto-close) │ ├── yaml.ts # YAML helpers │ ├── sanitize.ts # Component props validation and frontmatter sanitization │ ├── parse/ # Parsing pipeline @@ -803,12 +804,17 @@ SPEC coverage: `SPEC/COMARK/component-nested-*-outdented.md`, **Closing fences.** `comark_block` scans ahead for its closing `::` and skips code fences on the way. As in CommonMark, a closing fence may sit at most 3 -spaces right of its container: the innermost component still open at the -opening fence, but never right of that fence. A deeper fence line is code -content, so a `::` after it does not close the component. `autoCloseMarkdown` -follows the same rule. SPEC coverage: `SPEC/COMARK/component-*codeblock-indented-fence.md`, +spaces right of its container: the innermost component or list item still +open at the opening fence, but never right of that fence. A deeper fence line +is code content, so a `::` after it does not close the component. An opening +fence 4+ columns right of its container is indented code and opens nothing. +Tabs count to the next multiple of 4 columns. `autoCloseMarkdown` follows the +same rules; both share `listItemContentColumn()` (`src/internal/list-item.ts`). +SPEC coverage: `SPEC/COMARK/component-*codeblock-indented-fence.md`, `SPEC/COMARK/component-codeblock-closing-fence-indent.md`, -`SPEC/COMARK/component-codeblock-indented-opener.md`. +`SPEC/COMARK/component-codeblock-indented-opener.md`, +`SPEC/COMARK/component-indented-code-fence.md`, +`SPEC/COMARK/component-codeblock-invalid-nested-name.md`. ## Embedded HTML diff --git a/packages/comark/SPEC/COMARK/component-codeblock-invalid-nested-name.md b/packages/comark/SPEC/COMARK/component-codeblock-invalid-nested-name.md new file mode 100644 index 00000000..9736706c --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-codeblock-invalid-nested-name.md @@ -0,0 +1,64 @@ +## Input + +```md +::box + :::inner +::123 + ~~~js + x + ~~~ + ::: +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "box", + {}, + [ + "inner", + {}, + ["p", {}, "::123"], + ["pre", { "language": "js" }, ["code", { "class": "language-js" }, "x"]] + ] + ], + ["p", {}, "After"] + ] +} +``` + +## HTML + +```html + + +

::123

+
x
+
+
+

After

+``` + +## Markdown + +```md +::box + :::inner + \::123 + + ```js + x + ``` + ::: +:: + +After +``` diff --git a/packages/comark/SPEC/COMARK/component-indented-code-fence.md b/packages/comark/SPEC/COMARK/component-indented-code-fence.md new file mode 100644 index 00000000..8e91cefa --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-indented-code-fence.md @@ -0,0 +1,49 @@ +## Input + +```md +::box + ``` + x + ``` +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + ["box", {}, ["pre", {}, ["code", {}, "```\nx\n```"]]], + ["p", {}, "After"] + ] +} +``` + +## HTML + +```html + +
```
+  x
+  ```
+
+

After

+``` + +## Markdown + +```md +::box +~~~ +``` +x +``` +~~~ +:: + +After +``` diff --git a/packages/comark/SPEC/COMARK/component-list-codeblock-indented-fence.md b/packages/comark/SPEC/COMARK/component-list-codeblock-indented-fence.md new file mode 100644 index 00000000..1bb41d46 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-list-codeblock-indented-fence.md @@ -0,0 +1,119 @@ +## Input + +```md +::box +- item + + ~~~js + x + ~~~ +:: + +::box +- a + - b + + ```js + x + ``` +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "box", + {}, + [ + "ul", + {}, + [ + "li", + {}, + ["p", {}, "item"], + ["pre", { "language": "js" }, ["code", { "class": "language-js" }, "x"]] + ] + ] + ], + [ + "box", + {}, + [ + "ul", + {}, + [ + "li", + {}, + ["p", {}, "a"], + [ + "ul", + {}, + [ + "li", + {}, + ["p", {}, "b"], + ["pre", { "language": "js" }, ["code", { "class": "language-js" }, "x"]] + ] + ] + ] + ] + ], + ["p", {}, "After"] + ] +} +``` + +## HTML + +```html + +
    +
  • +

    item

    +
    x
    +
  • +
+
+ +
    +
  • +

    a

    +
      +
    • +

      b

      +
      x
      +
    • +
    +
  • +
+
+

After

+``` + +## Markdown + +```md +::box +- item + ```js + x + ``` +:: + +::box +- a + - b + ```js + x + ``` +:: + +After +``` diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md index 3a149797..b42f0a1f 100644 --- a/packages/comark/SPEC/auto-close.md +++ b/packages/comark/SPEC/auto-close.md @@ -2349,6 +2349,32 @@ A closer indented 4 or more spaces is code content; up to 3 spaces still closes: + ```\ncode\n ```\n**bold** ``` +A tab counts as 4 columns: + +```diff +- ```\ncode\n\t```\n**bold ++ ```\ncode\n\t```\n**bold +``` + +Inside a list item, indentation counts from the item's content column: + +```diff +- - item\n\n ```\n x\n ```\n\n**bold ++ - item\n\n ```\n x\n ```\n\n**bold** +``` + +```diff +- - a\n - b\n\n ```\n x\n ```\n\n**bold ++ - a\n - b\n\n ```\n x\n ```\n\n**bold** +``` + +A fence indented 4 or more spaces past its container is indented code, not an opener: + +```diff +- intro\n\n ```\n x\n\n**bold ++ intro\n\n ```\n x\n\n**bold** +``` + --- ## Leave horizontal rules alone diff --git a/packages/comark/src/auto-close.ts b/packages/comark/src/auto-close.ts index 8791fd57..e95445b6 100644 --- a/packages/comark/src/auto-close.ts +++ b/packages/comark/src/auto-close.ts @@ -16,6 +16,7 @@ */ import { findBracketClose, isBracketMath } from './internal/bracket-math.ts' +import { listItemContentColumn } from './internal/list-item.ts' export const INCOMPLETE_LINK_PLACEHOLDER = 'comark:incomplete-link' export const INCOMPLETE_IMAGE_PLACEHOLDER = 'comark:incomplete-image' @@ -204,7 +205,8 @@ function scanBlocks(src: string, o: Opts): DocState { const comps: Component[] = [] let fenceLen = 0 let fenceCh = 0 - let fenceIndent = 0 // a closer must stay within 3 spaces of this column (its container's) + let fenceIndent = 0 // a closer must stay within 3 columns of this one (its container's) + const listColumns: number[] = [] // content columns of the list items still open, innermost last let raw: RawTag | null = null let fmOpen = false let fmContent = false @@ -234,21 +236,36 @@ function scanBlocks(src: string, o: Opts): DocState { } } + const column = c0 === -1 ? 0 : columnsOf(src, ls, st) + + // A line left of a list item's content ends it + if (fenceLen === 0 && !fmOpen && c0 !== -1) { + while (listColumns.length > 0 && column < listColumns[listColumns.length - 1]) listColumns.pop() + const contentColumn = listItemContentColumn(src, st, en, column) + if (contentColumn !== -1) listColumns.push(contentColumn) + } + if (c0 === 96 /* ` */ || c0 === 126 /* ~ */) { let run = 1 while (st + run < en && src.charCodeAt(st + run) === c0) run++ if (run >= 3) { if (fenceLen === 0) { - // `` ```code`` `` is an incomplete inline code span, not a fence opener. - if (!(c0 === 96 && isIncompleteInlineFence(src, st, en))) { + // The container is the innermost open component or list item. + const top = comps.length > 0 ? comps[comps.length - 1].indent : '' + const container = Math.max( + columnsOf(top, 0, top.length), + listColumns.length > 0 ? listColumns[listColumns.length - 1] : 0 + ) + // `` ```code`` `` is an incomplete inline code span, and 4+ columns past the container is indented code. + if (!(c0 === 96 && isIncompleteInlineFence(src, st, en)) && column - container < 4) { fenceLen = run fenceCh = c0 - // The container is the innermost open component, never right of the fence itself. - fenceIndent = Math.min(comps.length > 0 ? comps[comps.length - 1].indent.length : 0, st - ls) + // An outdented fence measures its closer from itself. + fenceIndent = Math.min(container, column) shielded = i continue } - } else if (c0 === fenceCh && run >= fenceLen && st + run === en && st - ls - fenceIndent < 4) { + } else if (c0 === fenceCh && run >= fenceLen && st + run === en && column - fenceIndent < 4) { // A closer is the same character, at least as long, and followed only by // whitespace. `` ```js `` inside an open block is code content, not a closer. fenceLen = 0 @@ -363,6 +380,13 @@ const ATX_HEADING_RE = /^#{1,6}(\s|$)/ const THEMATIC_BREAK_RE = /^(\*{3,}|_{3,}|-{3,})\s*$/ const ORDERED_LIST_RE = /^\d{1,9}[.)](\s|$)/ +/** Column reached by the indentation in `src[from, to)`, with tabs stopping every 4 columns. */ +function columnsOf(src: string, from: number, to: number): number { + let column = 0 + for (let i = from; i < to; i++) column = src.charCodeAt(i) === 9 ? column + 4 - (column % 4) : column + 1 + return column +} + function isIndentCode(c: number): boolean { return c === 32 || c === 9 } diff --git a/packages/comark/src/internal/list-item.ts b/packages/comark/src/internal/list-item.ts new file mode 100644 index 00000000..41d8ca42 --- /dev/null +++ b/packages/comark/src/internal/list-item.ts @@ -0,0 +1,29 @@ +/** + * Content column of the list item whose marker starts at `start`, or -1 when the line opens none. + * `column` is the column of `start` and `end` the end of the line. + */ +export function listItemContentColumn(src: string, start: number, end: number, column: number): number { + let pos = start + const c = src.charCodeAt(pos) + if (c === 0x2d /* - */ || c === 0x2b /* + */ || c === 0x2a /* * */) { + pos++ + } else { + while (pos < end && pos - start < 9 && src.charCodeAt(pos) >= 0x30 && src.charCodeAt(pos) <= 0x39) pos++ + const d = pos < end ? src.charCodeAt(pos) : -1 + if (pos === start || (d !== 0x2e /* . */ && d !== 0x29) /* ) */) return -1 + pos++ + } + + const markerEnd = column + pos - start + if (pos >= end) return markerEnd + 1 + + let spaces = 0 + while (pos < end && (src.charCodeAt(pos) === 0x20 || src.charCodeAt(pos) === 0x09)) { + spaces++ + pos++ + } + if (spaces === 0) return -1 + + // An empty item, or content 5+ columns out (indented code), starts one column after the marker. + return pos >= end || spaces > 4 ? markerEnd + 1 : markerEnd + spaces +} diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index 589b73e6..2ec3bfd9 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -15,6 +15,7 @@ import { findClosingBracket, parseBracketContent } from '../internal/parse/synta import { parseBlockParams } from '../internal/parse/syntax/block-params.ts' import { parseYaml } from '../internal/yaml.ts' import { tokenizeDedented } from '../internal/parse/indent.ts' +import { listItemContentColumn } from '../internal/list-item.ts' /** * A component name must start with a letter or `$`, followed by word chars, @@ -110,6 +111,8 @@ const markdownItComarkBlock: PluginSimple = (md) => { // Nested components still open, as `[markerCount, indent]`. const openComponents: Array<[number, number]> = [] + // Content columns of the list items still open, innermost last. + const listColumns: number[] = [] if (state.src[start] !== ':') return false @@ -168,17 +171,31 @@ const markdownItComarkBlock: PluginSimple = (md) => { continue } + // A line left of a list item's content ends it + if (start < max) { + while (listColumns.length > 0 && state.sCount[nextLine] < listColumns[listColumns.length - 1]) + listColumns.pop() + const contentColumn = listItemContentColumn(state.src, start, max, state.sCount[nextLine]) + if (contentColumn !== -1) listColumns.push(contentColumn) + } + // Detect opening code fence (``` or ~~~) if (lineCharCode === 0x60 /* ` */ || lineCharCode === 0x7e /* ~ */) { let fencePos = start + 1 while (fencePos < max && state.src.charCodeAt(fencePos) === lineCharCode) fencePos++ - if (fencePos - start >= 3) { + // The container is the innermost open component or list item. + const componentIndent = openComponents.length ? openComponents[openComponents.length - 1][1] : indent + const containerIndent = Math.max( + componentIndent, + listColumns.length ? listColumns[listColumns.length - 1] : 0 + ) + // 4+ columns past the container is indented code, not a fence. + if (fencePos - start >= 3 && state.sCount[nextLine] - containerIndent < 4) { inCodeFence = true codeFenceCharCode = lineCharCode codeFenceCount = fencePos - start codeFenceShift = childShifts[childShifts.length - 1] - // The fence's container is the innermost open component, never right of the fence itself. - const containerIndent = openComponents.length ? openComponents[openComponents.length - 1][1] : indent + // An outdented fence measures its closer from itself. codeFenceIndent = Math.min(containerIndent, state.sCount[nextLine]) continue } @@ -195,7 +212,9 @@ const markdownItComarkBlock: PluginSimple = (md) => { if (pos < max) { // A nested block opens - if (count >= min_markers) openComponents.push([count, state.sCount[nextLine]]) + if (count >= min_markers && isValidComponentName(state.src.slice(pos, max))) { + openComponents.push([count, state.sCount[nextLine]]) + } continue } diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 5dfaa94e..78132e41 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "42.9k (83 files)", "@comark/svelte": "50.3k (91 files)", "@comark/vue": "62.6k (89 files)", - "comark": "441k (171 files)", + "comark": "444k (173 files)", } `) }) From c2ebe44d22bf159ed9382e6fe893161047d2ea6f Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Wed, 18 Nov 2026 15:06:47 +0000 Subject: [PATCH 3/7] fix(parse): ignore list markers in indented code and keep auto-close fast Amp-Thread-ID: https://ampcode.com/threads/T-01a1ea1f-799f-74fe-a965-dcf9fceb5e92 --- AGENTS.md | 13 ++-- .../COMARK/component-list-marker-indent.md | 78 +++++++++++++++++++ packages/comark/SPEC/auto-close.md | 12 +++ packages/comark/src/auto-close.ts | 70 +++++++++++++---- packages/comark/src/internal/list-item.ts | 6 +- packages/comark/src/plugins/components.ts | 18 +++-- test/bundle.test.ts | 2 +- 7 files changed, 165 insertions(+), 34 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/component-list-marker-indent.md diff --git a/AGENTS.md b/AGENTS.md index 6a14477c..15a0b50a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -805,17 +805,18 @@ SPEC coverage: `SPEC/COMARK/component-nested-*-outdented.md`, **Closing fences.** `comark_block` scans ahead for its closing `::` and skips code fences on the way. As in CommonMark, a closing fence may sit at most 3 -spaces right of its container: the innermost component or list item still -open at the opening fence, but never right of that fence. A deeper fence line -is code content, so a `::` after it does not close the component. An opening -fence 4+ columns right of its container is indented code and opens nothing. -Tabs count to the next multiple of 4 columns. `autoCloseMarkdown` follows the +columns right of its container: the innermost component or list item still +open at the opening fence, or the opening fence itself when that is further +left. A deeper fence line is code content, so a `::` after it does not close +the component. A line 4+ columns right of its container is indented code: it +opens no fence and no list item. Tabs count to the next multiple of 4 columns. `autoCloseMarkdown` follows the same rules; both share `listItemContentColumn()` (`src/internal/list-item.ts`). SPEC coverage: `SPEC/COMARK/component-*codeblock-indented-fence.md`, `SPEC/COMARK/component-codeblock-closing-fence-indent.md`, `SPEC/COMARK/component-codeblock-indented-opener.md`, `SPEC/COMARK/component-indented-code-fence.md`, -`SPEC/COMARK/component-codeblock-invalid-nested-name.md`. +`SPEC/COMARK/component-codeblock-invalid-nested-name.md`, +`SPEC/COMARK/component-list-marker-indent.md`. ## Embedded HTML diff --git a/packages/comark/SPEC/COMARK/component-list-marker-indent.md b/packages/comark/SPEC/COMARK/component-list-marker-indent.md new file mode 100644 index 00000000..156f36b3 --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-list-marker-indent.md @@ -0,0 +1,78 @@ +## Input + +```md +::box + - item + ~~~ +:: + +::box +- item + + ```js + x + ``` +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + ["box", {}, ["pre", {}, ["code", {}, "- item\n ~~~"]]], + [ + "box", + {}, + [ + "ul", + {}, + ["li", {}, ["p", {}, "item"], ["pre", { "language": "js" }, ["code", { "class": "language-js" }, "x"]]] + ] + ], + ["p", {}, "After"] + ] +} +``` + +## HTML + +```html + +
- item
+    ~~~
+
+ +
    +
  • +

    item

    +
    x
    +
  • +
+
+

After

+``` + +## Markdown + +```md +::box +``` +- item + ~~~ +``` +:: + +::box +- item + ```js + x + ``` +:: + +After +``` diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md index b42f0a1f..6b4474c8 100644 --- a/packages/comark/SPEC/auto-close.md +++ b/packages/comark/SPEC/auto-close.md @@ -2368,6 +2368,18 @@ Inside a list item, indentation counts from the item's content column: + - a\n - b\n\n ```\n x\n ```\n\n**bold** ``` +A tab after a list marker pads to the next multiple of 4 columns, and a marker 4+ columns past its container is indented code, not a list item: + +```diff +- -\titem\n\n ```\n x\n ```\n\n**bold ++ -\titem\n\n ```\n x\n ```\n\n**bold** +``` + +```diff +- - item\n ~~~\n\n**bold ++ - item\n ~~~\n\n**bold** +``` + A fence indented 4 or more spaces past its container is indented code, not an opener: ```diff diff --git a/packages/comark/src/auto-close.ts b/packages/comark/src/auto-close.ts index e95445b6..a4fe45ac 100644 --- a/packages/comark/src/auto-close.ts +++ b/packages/comark/src/auto-close.ts @@ -161,6 +161,8 @@ export function autoCloseMarkdown(markdown: string, options: AutoCloseOptions = interface Component { depth: number indent: string + /** Column of `indent`, tabs expanded. */ + column: number yaml: boolean } @@ -206,7 +208,7 @@ function scanBlocks(src: string, o: Opts): DocState { let fenceLen = 0 let fenceCh = 0 let fenceIndent = 0 // a closer must stay within 3 columns of this one (its container's) - const listColumns: number[] = [] // content columns of the list items still open, innermost last + const lists: ListItems = { columns: [], depth: 0, from: 0 } let raw: RawTag | null = null let fmOpen = false let fmContent = false @@ -236,26 +238,17 @@ function scanBlocks(src: string, o: Opts): DocState { } } - const column = c0 === -1 ? 0 : columnsOf(src, ls, st) - - // A line left of a list item's content ends it - if (fenceLen === 0 && !fmOpen && c0 !== -1) { - while (listColumns.length > 0 && column < listColumns[listColumns.length - 1]) listColumns.pop() - const contentColumn = listItemContentColumn(src, st, en, column) - if (contentColumn !== -1) listColumns.push(contentColumn) - } - if (c0 === 96 /* ` */ || c0 === 126 /* ~ */) { let run = 1 while (st + run < en && src.charCodeAt(st + run) === c0) run++ if (run >= 3) { + const column = columnsOf(src, ls, st) if (fenceLen === 0) { - // The container is the innermost open component or list item. - const top = comps.length > 0 ? comps[comps.length - 1].indent : '' - const container = Math.max( - columnsOf(top, 0, top.length), - listColumns.length > 0 ? listColumns[listColumns.length - 1] : 0 - ) + // An unindented line ends every list item, so only an indented fence reads the list items before it. + if (column === 0) lists.depth = 0 + else catchUpListItems(src, starts, i + 1, comps, lists) + lists.from = i + 1 + const container = containerColumn(comps, lists) // `` ```code`` `` is an incomplete inline code span, and 4+ columns past the container is indented code. if (!(c0 === 96 && isIncompleteInlineFence(src, st, en)) && column - container < 4) { fenceLen = run @@ -269,6 +262,7 @@ function scanBlocks(src: string, o: Opts): DocState { // A closer is the same character, at least as long, and followed only by // whitespace. `` ```js `` inside an open block is code content, not a closer. fenceLen = 0 + lists.from = i + 1 shielded = i continue } else { @@ -317,7 +311,7 @@ function scanBlocks(src: string, o: Opts): DocState { } if (colons >= 2) { if (!bare && isNameStart(src.charCodeAt(st + colons))) { - comps.push({ depth: colons, indent: src.slice(ls, st), yaml: false }) + comps.push({ depth: colons, indent: src.slice(ls, st), column: columnsOf(src, ls, st), yaml: false }) } else if (bare && comps.length > 0 && comps[comps.length - 1].depth === colons) { comps.pop() } @@ -380,6 +374,48 @@ const ATX_HEADING_RE = /^#{1,6}(\s|$)/ const THEMATIC_BREAK_RE = /^(\*{3,}|_{3,}|-{3,})\s*$/ const ORDERED_LIST_RE = /^\d{1,9}[.)](\s|$)/ +function isBulletOrDigit(c: number): boolean { + return c === 45 /* - */ || c === 43 /* + */ || c === 42 /* * */ || (c >= 48 && c <= 57) +} + +/** Content columns of the list items still open, innermost at `depth - 1`; slots past `depth` are stale. */ +interface ListItems { + columns: number[] + depth: number + /** First line not read yet: list items are read only when an indented fence needs them. */ + from: number +} + +/** Reads the list items on lines `lists.from` up to `to`. Lines inside fences are never read. */ +function catchUpListItems(src: string, starts: number[], to: number, comps: Component[], lists: ListItems): void { + for (let j = lists.from; j < to; j++) { + const ls = starts[j] + const le = j + 1 < starts.length ? starts[j + 1] - 1 : src.length + let st = ls + while (st < le && isIndentCode(src.charCodeAt(st))) st++ + if (st < le) trackListItems(src, ls, st, le, comps, lists) + } +} + +/** + * Ends the list items the line at `ls` is left of, then opens the one it starts. + * A line 4+ columns past its container is indented code and opens none. + */ +function trackListItems(src: string, ls: number, st: number, en: number, comps: Component[], lists: ListItems) { + const column = columnsOf(src, ls, st) + while (lists.depth > 0 && column < lists.columns[lists.depth - 1]) lists.depth-- + if (!isBulletOrDigit(src.charCodeAt(st)) || column - containerColumn(comps, lists) >= 4) return + const contentColumn = listItemContentColumn(src, st, en, column) + if (contentColumn !== -1) lists.columns[lists.depth++] = contentColumn +} + +/** Column of the innermost open component or list item. */ +function containerColumn(comps: Component[], lists: ListItems): number { + const component = comps.length > 0 ? comps[comps.length - 1].column : 0 + const list = lists.depth > 0 ? lists.columns[lists.depth - 1] : 0 + return component > list ? component : list +} + /** Column reached by the indentation in `src[from, to)`, with tabs stopping every 4 columns. */ function columnsOf(src: string, from: number, to: number): number { let column = 0 diff --git a/packages/comark/src/internal/list-item.ts b/packages/comark/src/internal/list-item.ts index 41d8ca42..5c366df5 100644 --- a/packages/comark/src/internal/list-item.ts +++ b/packages/comark/src/internal/list-item.ts @@ -17,11 +17,13 @@ export function listItemContentColumn(src: string, start: number, end: number, c const markerEnd = column + pos - start if (pos >= end) return markerEnd + 1 - let spaces = 0 + // Padding is measured in columns: a tab moves to the next multiple of 4. + let padded = markerEnd while (pos < end && (src.charCodeAt(pos) === 0x20 || src.charCodeAt(pos) === 0x09)) { - spaces++ + padded = src.charCodeAt(pos) === 0x09 ? padded + 4 - (padded % 4) : padded + 1 pos++ } + const spaces = padded - markerEnd if (spaces === 0) return -1 // An empty item, or content 5+ columns out (indented code), starts one column after the marker. diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index 2ec3bfd9..87fc6f41 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -175,6 +175,15 @@ const markdownItComarkBlock: PluginSimple = (md) => { if (start < max) { while (listColumns.length > 0 && state.sCount[nextLine] < listColumns[listColumns.length - 1]) listColumns.pop() + } + + // The container is the innermost open component or list item. + // A line 4+ columns past it is indented code: it opens no list item and no fence. + const componentIndent = openComponents.length ? openComponents[openComponents.length - 1][1] : indent + const containerIndent = Math.max(componentIndent, listColumns.length ? listColumns[listColumns.length - 1] : 0) + const isIndentedCode = state.sCount[nextLine] - containerIndent >= 4 + + if (start < max && !isIndentedCode) { const contentColumn = listItemContentColumn(state.src, start, max, state.sCount[nextLine]) if (contentColumn !== -1) listColumns.push(contentColumn) } @@ -183,14 +192,7 @@ const markdownItComarkBlock: PluginSimple = (md) => { if (lineCharCode === 0x60 /* ` */ || lineCharCode === 0x7e /* ~ */) { let fencePos = start + 1 while (fencePos < max && state.src.charCodeAt(fencePos) === lineCharCode) fencePos++ - // The container is the innermost open component or list item. - const componentIndent = openComponents.length ? openComponents[openComponents.length - 1][1] : indent - const containerIndent = Math.max( - componentIndent, - listColumns.length ? listColumns[listColumns.length - 1] : 0 - ) - // 4+ columns past the container is indented code, not a fence. - if (fencePos - start >= 3 && state.sCount[nextLine] - containerIndent < 4) { + if (fencePos - start >= 3 && !isIndentedCode) { inCodeFence = true codeFenceCharCode = lineCharCode codeFenceCount = fencePos - start diff --git a/test/bundle.test.ts b/test/bundle.test.ts index c739ce8a..76832ac5 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "43.1k (85 files)", "@comark/svelte": "51.2k (93 files)", "@comark/vue": "62.8k (91 files)", - "comark": "447k (175 files)", + "comark": "449k (175 files)", } `) }) From 64ba3a90ed9b18048abf26df5da8cf2fecb27342 Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Wed, 18 Nov 2026 15:33:10 +0000 Subject: [PATCH 4/7] fix(parse): read indented fences in slot bodies Amp-Thread-ID: https://ampcode.com/threads/T-01a1ea1f-799f-74fe-a965-dcf9fceb5e92 --- AGENTS.md | 4 +- .../COMARK/component-slot-indented-fence.md | 62 +++++++++++++++++++ packages/comark/SPEC/auto-close.md | 2 +- packages/comark/src/plugins/components.ts | 45 ++++++++------ 4 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/component-slot-indented-fence.md diff --git a/AGENTS.md b/AGENTS.md index 57e2283d..eb3517f4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -260,8 +260,8 @@ Block component children may be indented under `::` or outdented; both round-tri - **Parse (`src/internal/parse/indent.ts`)**: Dedent children rather than raising the floor. Each child line shifts left by `min(markerIndent, lineIndent)` and tokenizes against a zero floor. Lines inside a fenced code block all take the opening fence's shift so fence and body move together. If a tab would have to be split across columns, the shift aborts and keeps the marker floor. - **Stringify (`indent()` in `src/utils/index.ts`)**: Nested components indent their rendered block by 2 spaces per level across every line, including fenced code bodies, reaching a fixed point on re-parse. -- **Closing fences (`comark_block` in `src/plugins/components.ts`)**: The scan for the closing `::` skips code fences. A closing fence may sit at most 3 columns right of its container: the innermost component or list item open at the opening fence, or the opening fence itself when that is further left. Deeper fence lines are code content, so a `::` after one does not close the component. A line 4+ columns right of its container is indented code and opens no fence or list item. Tabs count to the next multiple of 4 columns. `autoCloseMarkdown` follows the same rules; both use `listItemContentColumn()` (`src/internal/list-item.ts`). -- Spec fixtures: `SPEC/COMARK/component-nested-*-outdented.md`, `SPEC/COMARK/component-nested-codeblock-indented.md`, `SPEC/COMARK/codeblock-indented-content.md`, and for closing fences `SPEC/COMARK/component-*codeblock-indented-fence.md`, `SPEC/COMARK/component-codeblock-closing-fence-indent.md`, `SPEC/COMARK/component-codeblock-indented-opener.md`, `SPEC/COMARK/component-indented-code-fence.md`, `SPEC/COMARK/component-codeblock-invalid-nested-name.md`, `SPEC/COMARK/component-list-marker-indent.md`. +- **Closing fences (`comark_block` in `src/plugins/components.ts`)**: The scan for the closing `::` skips code fences. A closing fence may sit at most 3 columns right of its container: the innermost component or list item open at the opening fence, or the opening fence itself when that is further left. Deeper fence lines are code content, so a `::` after one does not close the component. A line 4+ columns right of its container is indented code and opens no fence or list item. Tabs count to the next multiple of 4 columns. The `#slot` scan reads fences by the same rules. `autoCloseMarkdown` follows them too; it and `comark_block` use `listItemContentColumn()` (`src/internal/list-item.ts`). +- Spec fixtures: `SPEC/COMARK/component-nested-*-outdented.md`, `SPEC/COMARK/component-nested-codeblock-indented.md`, `SPEC/COMARK/codeblock-indented-content.md`, and for closing fences `SPEC/COMARK/component-*codeblock-indented-fence.md`, `SPEC/COMARK/component-codeblock-closing-fence-indent.md`, `SPEC/COMARK/component-codeblock-indented-opener.md`, `SPEC/COMARK/component-indented-code-fence.md`, `SPEC/COMARK/component-codeblock-invalid-nested-name.md`, `SPEC/COMARK/component-list-marker-indent.md`, `SPEC/COMARK/component-slot-indented-fence.md`. ### Embedded HTML diff --git a/packages/comark/SPEC/COMARK/component-slot-indented-fence.md b/packages/comark/SPEC/COMARK/component-slot-indented-fence.md new file mode 100644 index 00000000..e4aa734d --- /dev/null +++ b/packages/comark/SPEC/COMARK/component-slot-indented-fence.md @@ -0,0 +1,62 @@ +## Input + +```md +::box +#code + ~~~ +#notASlot + ~~~ +#other +text +:: + +After +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "box", + {}, + ["template", { "name": "code" }, ["pre", {}, ["code", {}, "#notASlot"]]], + ["template", { "name": "other" }, "text"] + ], + ["p", {}, "After"] + ] +} +``` + +## HTML + +```html + + + + +

After

+``` + +## Markdown + +```md +::box +#code +``` +#notASlot +``` + +#other +text +:: + +After +``` diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md index 6b4474c8..a602d758 100644 --- a/packages/comark/SPEC/auto-close.md +++ b/packages/comark/SPEC/auto-close.md @@ -2349,7 +2349,7 @@ A closer indented 4 or more spaces is code content; up to 3 spaces still closes: + ```\ncode\n ```\n**bold** ``` -A tab counts as 4 columns: +A tab advances to the next multiple of 4 columns: ```diff - ```\ncode\n\t```\n**bold diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index afa2a14c..02fc4e69 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -373,37 +373,44 @@ const markdownItComarkBlock: PluginSimple = (md) => { let lineEnd = startLine + 1 let inCodeFence = false - let codeFenceChar = '' + let codeFenceCharCode = 0 let codeFenceCount = 0 + let codeFenceIndent = 0 while (lineEnd < endLine) { const inner = state.src.slice(state.bMarks[lineEnd] + state.tShift[startLine], state.eMarks[lineEnd]) - if (inCodeFence) { - // Matching closing fence: same char, >= opening count, nothing but spaces after. - if (inner[0] === codeFenceChar) { - let fencePos = 1 - while (fencePos < inner.length && inner[fencePos] === codeFenceChar) fencePos++ - if (fencePos >= codeFenceCount && inner.slice(fencePos).trim() === '') { + // Fences are read at the line's own indentation, with the same rules as `comark_block`. + const start = state.bMarks[lineEnd] + state.tShift[lineEnd] + const max = state.eMarks[lineEnd] + const charCode = state.src.charCodeAt(start) + if (charCode === 0x60 /* ` */ || charCode === 0x7e /* ~ */) { + let fencePos = start + 1 + while (fencePos < max && state.src.charCodeAt(fencePos) === charCode) fencePos++ + const column = state.sCount[lineEnd] + if (inCodeFence) { + if ( + charCode === codeFenceCharCode && + fencePos - start >= codeFenceCount && + state.skipSpaces(fencePos) >= max && + column - codeFenceIndent < 4 + ) { inCodeFence = false } - } - lineEnd += 1 - continue - } - - if (inner[0] === '`' || inner[0] === '~') { - const ch = inner[0] - let fencePos = 1 - while (fencePos < inner.length && inner[fencePos] === ch) fencePos++ - if (fencePos >= 3) { + } else if (fencePos - start >= 3 && column - state.blkIndent < 4) { inCodeFence = true - codeFenceChar = ch - codeFenceCount = fencePos + codeFenceCharCode = charCode + codeFenceCount = fencePos - start + codeFenceIndent = Math.min(state.blkIndent, column) lineEnd += 1 continue } } + if (inCodeFence) { + lineEnd += 1 + continue + } + if (/^#\w+/.test(inner) || inner.startsWith('::')) break lineEnd += 1 } From c800f9078ec116e7164f4e048810f969c5aa1416 Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Thu, 19 Nov 2026 11:01:45 +0000 Subject: [PATCH 5/7] fix(parse): handle thematic breaks, nested list markers and indented components in fence scans Amp-Thread-ID: https://ampcode.com/threads/T-01a1ea1f-799f-74fe-a965-dcf9fceb5e92 --- AGENTS.md | 4 +- .../COMARK/component-list-marker-indent.md | 68 ++++++++++++++++ packages/comark/SPEC/auto-close.md | 12 +++ packages/comark/src/auto-close.ts | 16 +++- packages/comark/src/internal/list-item.ts | 78 +++++++++++++------ packages/comark/src/plugins/components.ts | 8 +- packages/comark/test/auto-close.test.ts | 4 + test/bundle.test.ts | 2 +- 8 files changed, 159 insertions(+), 33 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 9f702b9f..9bc31b51 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -98,7 +98,7 @@ packages/comark/ │ ├── frontmatter.ts # parseFrontmatter and renderFrontmatter │ ├── heading-id.ts # Slug generation shared by parser and stringifier │ ├── inline-code-lang.ts # Inline code `{lang="..."}` fragment highlighting -│ ├── list-item.ts # List item content column, shared by the component fence scan and auto-close +│ ├── list-item.ts # List item content columns, shared by the component fence scan and auto-close │ ├── sanitize.ts # Prop, URL, and frontmatter validation │ ├── shiki.ts # Shared Shiki runtime │ ├── yaml.ts # YAML parsing and serialization helpers @@ -262,7 +262,7 @@ Block component children may be indented under `::` or outdented; both round-tri - **Parse (`src/internal/parse/indent.ts`)**: Dedent children rather than raising the floor. Each child line shifts left by `min(markerIndent, lineIndent)` and tokenizes against a zero floor. Lines inside a fenced code block all take the opening fence's shift so fence and body move together. If a tab would have to be split across columns, the shift aborts and keeps the marker floor. - **Stringify (`indent()` in `src/utils/index.ts`)**: Nested components indent their rendered block by 2 spaces per level across every line, including fenced code bodies, reaching a fixed point on re-parse. -- **Closing fences (`comark_block` in `src/plugins/components.ts`)**: The scan for the closing `::` skips code fences. A closing fence may sit at most 3 columns right of its container: the innermost component or list item open at the opening fence, or the opening fence itself when that is further left. Deeper fence lines are code content, so a `::` after one does not close the component. A line 4+ columns right of its container is indented code and opens no fence or list item. Tabs count to the next multiple of 4 columns. The `#slot` scan reads fences by the same rules. `autoCloseMarkdown` follows them too; it and `comark_block` use `listItemContentColumn()` (`src/internal/list-item.ts`). +- **Closing fences (`comark_block` in `src/plugins/components.ts`)**: The scan for the closing `::` skips code fences. A closing fence may sit at most 3 columns right of its container: the innermost component or list item open at the opening fence, or the opening fence itself when that is further left. Deeper fence lines are code content, so a `::` after one does not close the component. A line 4+ columns right of its container is indented code and opens no fence, list item, or nested component. `- - item` opens two list items, and a thematic break such as `- - -` opens none. Tabs count to the next multiple of 4 columns. The `#slot` scan reads fences by the same rules. `autoCloseMarkdown` follows them too; it and `comark_block` use `pushListItems()` (`src/internal/list-item.ts`). - Spec fixtures: `SPEC/COMARK/component-nested-*-outdented.md`, `SPEC/COMARK/component-nested-codeblock-indented.md`, `SPEC/COMARK/codeblock-indented-content.md`, and for closing fences `SPEC/COMARK/component-*codeblock-indented-fence.md`, `SPEC/COMARK/component-codeblock-closing-fence-indent.md`, `SPEC/COMARK/component-codeblock-indented-opener.md`, `SPEC/COMARK/component-indented-code-fence.md`, `SPEC/COMARK/component-codeblock-invalid-nested-name.md`, `SPEC/COMARK/component-list-marker-indent.md`, `SPEC/COMARK/component-slot-indented-fence.md`. ### Embedded HTML diff --git a/packages/comark/SPEC/COMARK/component-list-marker-indent.md b/packages/comark/SPEC/COMARK/component-list-marker-indent.md index 156f36b3..f89f6f4e 100644 --- a/packages/comark/SPEC/COMARK/component-list-marker-indent.md +++ b/packages/comark/SPEC/COMARK/component-list-marker-indent.md @@ -14,6 +14,25 @@ ``` :: +::box +- - - + ~~~ + ~~~ +:: + +::box +- - item + ~~~ + x + ~~~ +:: + +::box + :::inner + ~~~ + ~~~ +:: + After ``` @@ -34,6 +53,9 @@ After ["li", {}, ["p", {}, "item"], ["pre", { "language": "js" }, ["code", { "class": "language-js" }, "x"]]] ] ], + ["box", {}, ["hr", {}], ["pre", {}, ["code", {}, "~~~\n ~~~"]]], + ["box", {}, ["ul", {}, ["li", {}, ["ul", {}, ["li", {}, ["p", {}, "item"], ["pre", {}, ["code", {}, "x"]]]]]]], + ["box", {}, ["pre", {}, ["code", {}, ":::inner\n ~~~\n ~~~"]]], ["p", {}, "After"] ] } @@ -54,6 +76,28 @@ After + +
+
~~~
+    ~~~
+
+ +
    +
  • +
      +
    • +

      item

      +
      x
      +
    • +
    +
  • +
+
+ +
:::inner
+    ~~~
+        ~~~
+

After

``` @@ -74,5 +118,29 @@ After ``` :: +::box +--- + +``` +~~~ + ~~~ +``` +:: + +::box +- - item + ``` + x + ``` +:: + +::box +``` +:::inner + ~~~ + ~~~ +``` +:: + After ``` diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md index a602d758..ae40efc7 100644 --- a/packages/comark/SPEC/auto-close.md +++ b/packages/comark/SPEC/auto-close.md @@ -2380,6 +2380,18 @@ A tab after a list marker pads to the next multiple of 4 columns, and a marker 4 + - item\n ~~~\n\n**bold** ``` +A thematic break such as `- - -` opens no list item, and each marker of `- - item` opens its own: + +```diff +- - - -\n ~~~\n ~~~\n\n**bold ++ - - -\n ~~~\n ~~~\n\n**bold** +``` + +```diff +- - - item\n ~~~\n x\n ~~~\n\n**bold ++ - - item\n ~~~\n x\n ~~~\n\n**bold** +``` + A fence indented 4 or more spaces past its container is indented code, not an opener: ```diff diff --git a/packages/comark/src/auto-close.ts b/packages/comark/src/auto-close.ts index 38d53bd9..524b4e57 100644 --- a/packages/comark/src/auto-close.ts +++ b/packages/comark/src/auto-close.ts @@ -16,7 +16,7 @@ */ import { findBracketClose, isBracketMath } from './internal/bracket-math.ts' -import { listItemContentColumn } from './internal/list-item.ts' +import { pushListItems } from './internal/list-item.ts' export const INCOMPLETE_LINK_PLACEHOLDER = 'comark:incomplete-link' export const INCOMPLETE_IMAGE_PLACEHOLDER = 'comark:incomplete-image' @@ -307,7 +307,16 @@ function scanBlocks(src: string, o: Opts): DocState { } if (colons >= 2) { if (!bare && isNameStart(src.charCodeAt(st + colons))) { - comps.push({ depth: colons, indent: src.slice(ls, st), column: columnsOf(src, ls, st), yaml: false }) + // Inside a component, an indented-code line opens no nested one, as in the parser. + // A top-level indented component is still closed. + const column = st === ls ? 0 : columnsOf(src, ls, st) + let indentedCode = false + if (column !== 0 && comps.length > 0) { + catchUpListItems(src, starts, i + 1, comps, lists) + lists.from = i + 1 + indentedCode = column - containerColumn(comps, lists) >= 4 + } + if (!indentedCode) comps.push({ depth: colons, indent: src.slice(ls, st), column, yaml: false }) } else if (bare && comps.length > 0 && comps[comps.length - 1].depth === colons) { comps.pop() } @@ -401,8 +410,7 @@ function trackListItems(src: string, ls: number, st: number, en: number, comps: const column = columnsOf(src, ls, st) while (lists.depth > 0 && column < lists.columns[lists.depth - 1]) lists.depth-- if (!isBulletOrDigit(src.charCodeAt(st)) || column - containerColumn(comps, lists) >= 4) return - const contentColumn = listItemContentColumn(src, st, en, column) - if (contentColumn !== -1) lists.columns[lists.depth++] = contentColumn + lists.depth = pushListItems(src, st, en, column, lists.columns, lists.depth) } /** Column of the innermost open component or list item. */ diff --git a/packages/comark/src/internal/list-item.ts b/packages/comark/src/internal/list-item.ts index 5c366df5..5defc2ce 100644 --- a/packages/comark/src/internal/list-item.ts +++ b/packages/comark/src/internal/list-item.ts @@ -1,31 +1,65 @@ /** - * Content column of the list item whose marker starts at `start`, or -1 when the line opens none. + * Writes the content column of each list item whose marker starts at `start` into `columns`, from index `depth`. + * Returns the new depth. `- - item` opens two items; a thematic break such as `- - -` opens none. * `column` is the column of `start` and `end` the end of the line. */ -export function listItemContentColumn(src: string, start: number, end: number, column: number): number { +export function pushListItems( + src: string, + start: number, + end: number, + column: number, + columns: number[], + depth: number +): number { let pos = start - const c = src.charCodeAt(pos) - if (c === 0x2d /* - */ || c === 0x2b /* + */ || c === 0x2a /* * */) { - pos++ - } else { - while (pos < end && pos - start < 9 && src.charCodeAt(pos) >= 0x30 && src.charCodeAt(pos) <= 0x39) pos++ - const d = pos < end ? src.charCodeAt(pos) : -1 - if (pos === start || (d !== 0x2e /* . */ && d !== 0x29) /* ) */) return -1 - pos++ - } + let markerColumn = column + while (pos < end && !isThematicBreak(src, pos, end)) { + const markerStart = pos + const c = src.charCodeAt(pos) + if (c === 0x2d /* - */ || c === 0x2b /* + */ || c === 0x2a /* * */) { + pos++ + } else { + while (pos < end && pos - markerStart < 9 && src.charCodeAt(pos) >= 0x30 && src.charCodeAt(pos) <= 0x39) pos++ + const d = pos < end ? src.charCodeAt(pos) : -1 + if (pos === markerStart || (d !== 0x2e /* . */ && d !== 0x29) /* ) */) break + pos++ + } + + const markerEnd = markerColumn + pos - markerStart + if (pos >= end) { + columns[depth++] = markerEnd + 1 + break + } - const markerEnd = column + pos - start - if (pos >= end) return markerEnd + 1 + // Padding is measured in columns: a tab moves to the next multiple of 4. + let padded = markerEnd + while (pos < end && (src.charCodeAt(pos) === 0x20 || src.charCodeAt(pos) === 0x09)) { + padded = src.charCodeAt(pos) === 0x09 ? padded + 4 - (padded % 4) : padded + 1 + pos++ + } + const spaces = padded - markerEnd + if (spaces === 0) break - // Padding is measured in columns: a tab moves to the next multiple of 4. - let padded = markerEnd - while (pos < end && (src.charCodeAt(pos) === 0x20 || src.charCodeAt(pos) === 0x09)) { - padded = src.charCodeAt(pos) === 0x09 ? padded + 4 - (padded % 4) : padded + 1 - pos++ + // An empty item, or content 5+ columns out (indented code), starts one column after the marker and nests nothing. + if (pos >= end || spaces > 4) { + columns[depth++] = markerEnd + 1 + break + } + columns[depth++] = padded + markerColumn = padded } - const spaces = padded - markerEnd - if (spaces === 0) return -1 + return depth +} - // An empty item, or content 5+ columns out (indented code), starts one column after the marker. - return pos >= end || spaces > 4 ? markerEnd + 1 : markerEnd + spaces +/** Whether `src[start, end)` is a thematic break: 3+ of one of `-`, `*`, `_`, with only spaces or tabs between. */ +function isThematicBreak(src: string, start: number, end: number): boolean { + const c = src.charCodeAt(start) + if (c !== 0x2d /* - */ && c !== 0x2a /* * */ && c !== 0x5f /* _ */) return false + let count = 0 + for (let pos = start; pos < end; pos++) { + const code = src.charCodeAt(pos) + if (code === c) count++ + else if (code !== 0x20 && code !== 0x09) return false + } + return count >= 3 } diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index 02fc4e69..f338aae8 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -15,7 +15,7 @@ import { findClosingBracket, parseBracketContent } from '../internal/parse/synta import { parseBlockParams } from '../internal/parse/syntax/block-params.ts' import { parseYaml } from '../internal/yaml.ts' import { tokenizeDedented } from '../internal/parse/indent.ts' -import { listItemContentColumn } from '../internal/list-item.ts' +import { pushListItems } from '../internal/list-item.ts' /** * A component name must start with a letter or `$`, followed by word chars, @@ -183,8 +183,7 @@ const markdownItComarkBlock: PluginSimple = (md) => { const isIndentedCode = state.sCount[nextLine] - containerIndent >= 4 if (start < max && !isIndentedCode) { - const contentColumn = listItemContentColumn(state.src, start, max, state.sCount[nextLine]) - if (contentColumn !== -1) listColumns.push(contentColumn) + pushListItems(state.src, start, max, state.sCount[nextLine], listColumns, listColumns.length) } if (lineCharCode === 0x60 /* ` */ || lineCharCode === 0x7e /* ~ */) { @@ -212,7 +211,8 @@ const markdownItComarkBlock: PluginSimple = (md) => { if (pos < max) { // A nested block opens - if (count >= min_markers && isValidComponentName(state.src.slice(pos, max))) { + // An indented-code line opens no component, as in the child parse. + if (count >= min_markers && !isIndentedCode && isValidComponentName(state.src.slice(pos, max))) { openComponents.push([count, state.sCount[nextLine]]) } continue diff --git a/packages/comark/test/auto-close.test.ts b/packages/comark/test/auto-close.test.ts index 588604d0..f3473cd4 100644 --- a/packages/comark/test/auto-close.test.ts +++ b/packages/comark/test/auto-close.test.ts @@ -226,6 +226,10 @@ describe('autoCloseMarkdown - Comark Components', () => { // The children of `::box` sit at column 0, even when the fence itself is indented. const indentedFence = '::box\n ```md\n ```js\n ```\n ```\n::' expect(autoCloseMarkdown(indentedFence)).toBe(indentedFence) + // ` :::inner` is indented code, so it opens no component. + expect(autoCloseMarkdown('::box\n :::inner\n ~~~\n ~~~\n::\n\n**bold')).toBe( + '::box\n :::inner\n ~~~\n ~~~\n::\n\n**bold**' + ) // Within 3 spaces of `:::inner` still closes. expect(autoCloseMarkdown('::box\n :::inner\n ```\n x\n ```\n :::\n::\n**bold')).toBe( '::box\n :::inner\n ```\n x\n ```\n :::\n::\n**bold**' diff --git a/test/bundle.test.ts b/test/bundle.test.ts index a9c35c7d..6cfe2a71 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "43.3k (87 files)", "@comark/svelte": "51.0k (95 files)", "@comark/vue": "62.7k (93 files)", - "comark": "454k (177 files)", + "comark": "455k (177 files)", } `) }) From a4f3c9f61728cab8bc50460dea372beedba7b89e Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Thu, 19 Nov 2026 14:08:02 +0000 Subject: [PATCH 6/7] fix(parse): end list fences with their list item and scan list markers in linear time Amp-Thread-ID: https://ampcode.com/threads/T-01a1ea1f-799f-74fe-a965-dcf9fceb5e92 --- AGENTS.md | 2 +- .../COMARK/component-list-marker-indent.md | 35 +++++++++++++++++++ packages/comark/SPEC/auto-close.md | 7 ++++ packages/comark/src/auto-close.ts | 9 +++++ packages/comark/src/internal/list-item.ts | 32 +++++++++-------- packages/comark/src/plugins/components.ts | 7 ++++ packages/comark/test/dos.test.ts | 7 ++++ test/bundle.test.ts | 2 +- 8 files changed, 85 insertions(+), 16 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 599ee9ee..7cd9c97a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -262,7 +262,7 @@ Block component children may be indented under `::` or outdented; both round-tri - **Parse (`src/internal/parse/indent.ts`)**: Dedent children rather than raising the floor. Each child line shifts left by `min(markerIndent, lineIndent)` and tokenizes against a zero floor. Lines inside a fenced code block all take the opening fence's shift so fence and body move together. If a tab would have to be split across columns, the shift aborts and keeps the marker floor. - **Stringify (`indent()` in `src/utils/index.ts`)**: Nested components indent their rendered block by 2 spaces per level across every line, including fenced code bodies, reaching a fixed point on re-parse. -- **Closing fences (`comark_block` in `src/plugins/components.ts`)**: The scan for the closing `::` skips code fences. A closing fence may sit at most 3 columns right of its container: the innermost component or list item open at the opening fence, or the opening fence itself when that is further left. Deeper fence lines are code content, so a `::` after one does not close the component. A line 4+ columns right of its container is indented code and opens no fence, list item, or nested component. `- - item` opens two list items, and a thematic break such as `- - -` opens none. Tabs count to the next multiple of 4 columns. The `#slot` scan reads fences by the same rules. `autoCloseMarkdown` follows them too; it and `comark_block` use `pushListItems()` (`src/internal/list-item.ts`). +- **Closing fences (`comark_block` in `src/plugins/components.ts`)**: The scan for the closing `::` skips code fences. A closing fence may sit at most 3 columns right of its container: the innermost component or list item open at the opening fence, or the opening fence itself when that is further left. Deeper fence lines are code content, so a `::` after one does not close the component. A line 4+ columns right of its container is indented code and opens no fence, list item, or nested component. `- - item` opens two list items, and a thematic break such as `- - -` opens none. A fence inside a list item ends when the list item does, even without a closer. Tabs count to the next multiple of 4 columns. The `#slot` scan reads fences by the same rules. `autoCloseMarkdown` follows them too; it and `comark_block` use `pushListItems()` (`src/internal/list-item.ts`). - Spec fixtures: `SPEC/COMARK/component-nested-*-outdented.md`, `SPEC/COMARK/component-nested-codeblock-indented.md`, `SPEC/COMARK/codeblock-indented-content.md`, and for closing fences `SPEC/COMARK/component-*codeblock-indented-fence.md`, `SPEC/COMARK/component-codeblock-closing-fence-indent.md`, `SPEC/COMARK/component-codeblock-indented-opener.md`, `SPEC/COMARK/component-indented-code-fence.md`, `SPEC/COMARK/component-codeblock-invalid-nested-name.md`, `SPEC/COMARK/component-list-marker-indent.md`, `SPEC/COMARK/component-slot-indented-fence.md`. ### Embedded HTML diff --git a/packages/comark/SPEC/COMARK/component-list-marker-indent.md b/packages/comark/SPEC/COMARK/component-list-marker-indent.md index 721bef82..3c18c3ff 100644 --- a/packages/comark/SPEC/COMARK/component-list-marker-indent.md +++ b/packages/comark/SPEC/COMARK/component-list-marker-indent.md @@ -33,6 +33,12 @@ ~~~ :: +::box +- - item + ~~~ + x +:: + After ``` @@ -56,6 +62,7 @@ After ["box", {}, ["hr", {}], ["pre", {}, ["code", {}, "~~~\n ~~~"]]], ["box", {}, ["ul", {}, ["li", {}, ["ul", {}, ["li", {}, ["p", {}, "item"], ["pre", {}, ["code", {}, "x"]]]]]]], ["box", {}, ["pre", {}, ["code", {}, ":::inner\n ~~~\n ~~~"]]], + ["box", {}, ["ul", {}, ["li", {}, ["ul", {}, ["li", {}, ["p", {}, "item"], ["pre", {}, ["code", {}, "x"]]]]]]], ["p", {}, "After"] ] } @@ -98,6 +105,18 @@ After ~~~ ~~~ + +
    +
  • +
      +
    • +

      item

      +
      x
      +
    • +
    +
  • +
+

After

``` @@ -142,6 +161,13 @@ After ``` :: +::box +- - item + ``` + x + ``` +:: + After ``` @@ -196,5 +222,14 @@ After + + +- - item + ``` + x + ``` + + + After ``` diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md index ae40efc7..853bf70f 100644 --- a/packages/comark/SPEC/auto-close.md +++ b/packages/comark/SPEC/auto-close.md @@ -2392,6 +2392,13 @@ A thematic break such as `- - -` opens no list item, and each marker of `- - ite + - - item\n ~~~\n x\n ~~~\n\n**bold** ``` +A fence inside a list item ends with the list item, even without a closer: + +```diff +- - - item\n\n ~~~\n x\n\n**bold ++ - - item\n\n ~~~\n x\n\n**bold** +``` + A fence indented 4 or more spaces past its container is indented code, not an opener: ```diff diff --git a/packages/comark/src/auto-close.ts b/packages/comark/src/auto-close.ts index 524b4e57..575b094b 100644 --- a/packages/comark/src/auto-close.ts +++ b/packages/comark/src/auto-close.ts @@ -204,6 +204,7 @@ function scanBlocks(src: string, o: Opts): DocState { let fenceLen = 0 let fenceCh = 0 let fenceIndent = 0 // a closer must stay within 3 columns of this one (its container's) + let fenceListColumn = -1 // content column of the list item holding the open fence, or -1 const lists: ListItems = { columns: [], depth: 0, from: 0 } let raw: RawTag | null = null let fmOpen = false @@ -234,6 +235,12 @@ function scanBlocks(src: string, o: Opts): DocState { } } + // A line left of the list item holding the open fence ends both, even without a closer. + if (fenceLen !== 0 && fenceListColumn !== -1 && c0 !== -1 && columnsOf(src, ls, st) < fenceListColumn) { + fenceLen = 0 + lists.from = i + } + if (c0 === 96 /* ` */ || c0 === 126 /* ~ */) { let run = 1 while (st + run < en && src.charCodeAt(st + run) === c0) run++ @@ -251,6 +258,8 @@ function scanBlocks(src: string, o: Opts): DocState { fenceCh = c0 // An outdented fence measures its closer from itself. fenceIndent = Math.min(container, column) + const listColumn = lists.depth > 0 ? lists.columns[lists.depth - 1] : -1 + fenceListColumn = listColumn > (comps.length > 0 ? comps[comps.length - 1].column : 0) ? listColumn : -1 shielded = i continue } diff --git a/packages/comark/src/internal/list-item.ts b/packages/comark/src/internal/list-item.ts index 5defc2ce..aad863de 100644 --- a/packages/comark/src/internal/list-item.ts +++ b/packages/comark/src/internal/list-item.ts @@ -11,12 +11,29 @@ export function pushListItems( columns: number[], depth: number ): number { + // One backward pass finds the tail that could be a thematic break: one of `-`, `*`, `_`, with spaces or tabs. + // A marker opens no item once the rest of the line is that tail and holds 3+ of its character. + let tailStart = end + let breakChar = -1 + let breakCount = 0 + for (let p = end - 1; p >= start; p--) { + const code = src.charCodeAt(p) + if (code !== 0x20 && code !== 0x09) { + // `-`, `*` or `_` + if (breakChar === -1 && (code === 0x2d || code === 0x2a || code === 0x5f)) breakChar = code + if (code !== breakChar) break + breakCount++ + } + tailStart = p + } + let pos = start let markerColumn = column - while (pos < end && !isThematicBreak(src, pos, end)) { + while (pos < end && !(pos >= tailStart && breakCount >= 3)) { const markerStart = pos const c = src.charCodeAt(pos) if (c === 0x2d /* - */ || c === 0x2b /* + */ || c === 0x2a /* * */) { + if (c === breakChar && pos >= tailStart) breakCount-- pos++ } else { while (pos < end && pos - markerStart < 9 && src.charCodeAt(pos) >= 0x30 && src.charCodeAt(pos) <= 0x39) pos++ @@ -50,16 +67,3 @@ export function pushListItems( } return depth } - -/** Whether `src[start, end)` is a thematic break: 3+ of one of `-`, `*`, `_`, with only spaces or tabs between. */ -function isThematicBreak(src: string, start: number, end: number): boolean { - const c = src.charCodeAt(start) - if (c !== 0x2d /* - */ && c !== 0x2a /* * */ && c !== 0x5f /* _ */) return false - let count = 0 - for (let pos = start; pos < end; pos++) { - const code = src.charCodeAt(pos) - if (code === c) count++ - else if (code !== 0x20 && code !== 0x09) return false - } - return count >= 3 -} diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index f338aae8..b56215c7 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -108,6 +108,8 @@ const markdownItComarkBlock: PluginSimple = (md) => { let codeFenceCount = 0 // A closing fence must sit within 3 spaces of this column (CommonMark container indent). let codeFenceIndent = 0 + // Content column of the list item holding the open fence, or -1 when a component holds it. + let codeFenceListColumn = -1 // Nested components still open, as `[markerCount, indent]`. const openComponents: Array<[number, number]> = [] @@ -152,6 +154,9 @@ const markdownItComarkBlock: PluginSimple = (md) => { if (start < max && state.sCount[nextLine] < state.blkIndent) break + // A line left of the list item holding the fence ends both, even without a closer. + if (inCodeFence && start < max && state.sCount[nextLine] < codeFenceListColumn) inCodeFence = false + const lineIndent = start >= max ? indent : state.sCount[nextLine] if (lineIndent < indent) hasOutdentedChild = true childShifts.push(start >= max ? 0 : inCodeFence ? codeFenceShift : Math.min(indent, lineIndent)) @@ -196,6 +201,8 @@ const markdownItComarkBlock: PluginSimple = (md) => { codeFenceShift = childShifts[childShifts.length - 1] // An outdented fence measures its closer from itself. codeFenceIndent = Math.min(containerIndent, state.sCount[nextLine]) + const listColumn = listColumns.length ? listColumns[listColumns.length - 1] : -1 + codeFenceListColumn = listColumn > componentIndent ? listColumn : -1 continue } } diff --git a/packages/comark/test/dos.test.ts b/packages/comark/test/dos.test.ts index 1aaa5e9b..118fb209 100644 --- a/packages/comark/test/dos.test.ts +++ b/packages/comark/test/dos.test.ts @@ -25,6 +25,13 @@ describe('denial-of-service hardening', () => { expect(performance.now() - start).toBeLessThan(500) }) + it('scans a line of thousands of nested list markers inside a component in linear time', async () => { + const start = performance.now() + const doc = await parseMarkdown('::box\n' + '- '.repeat(20_000) + 'item\n::\n\nAfter') + expect(performance.now() - start).toBeLessThan(500) + expect(doc.nodes.at(-1)).toEqual(['p', {}, 'After']) + }) + it('parses thousands of unclosed nested inline HTML tags without stack overflow', async () => { const md = ''.repeat(10_000) const doc = await parseMarkdown(md) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 6ea4f01a..09f871d9 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "43.3k (87 files)", "@comark/svelte": "51.0k (95 files)", "@comark/vue": "62.7k (93 files)", - "comark": "461k (179 files)", + "comark": "462k (179 files)", } `) }) From 280aa1209b6c66511682e0049ee3a25905624715 Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Thu, 19 Nov 2026 15:22:27 +0000 Subject: [PATCH 7/7] fix(parse): keep nested component scan linear on unmatched bare markers Amp-Thread-ID: https://ampcode.com/threads/T-01a1f427-26f0-73d7-aed8-0f580eef49f4 --- packages/comark/src/plugins/components.ts | 13 +++++++++---- packages/comark/test/dos.test.ts | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts index b56215c7..134d34a1 100644 --- a/packages/comark/src/plugins/components.ts +++ b/packages/comark/src/plugins/components.ts @@ -113,6 +113,8 @@ const markdownItComarkBlock: PluginSimple = (md) => { // Nested components still open, as `[markerCount, indent]`. const openComponents: Array<[number, number]> = [] + // Open nested components per marker count: a bare marker matching none must not walk the stack (DoS bound). + const openCounts = new Map() // Content columns of the list items still open, innermost last. const listColumns: number[] = [] @@ -221,15 +223,18 @@ const markdownItComarkBlock: PluginSimple = (md) => { // An indented-code line opens no component, as in the child parse. if (count >= min_markers && !isIndentedCode && isValidComponentName(state.src.slice(pos, max))) { openComponents.push([count, state.sCount[nextLine]]) + openCounts.set(count, (openCounts.get(count) || 0) + 1) } continue } // A bare marker closes the innermost open nested block with the same length, and any left open inside it - let open = openComponents.length - 1 - while (open >= 0 && openComponents[open][0] !== count) open-- - if (open >= 0) { - openComponents.length = open + if (openCounts.get(count)) { + let closed: number + do { + closed = openComponents.pop()![0] + openCounts.set(closed, openCounts.get(closed)! - 1) + } while (closed !== count) continue } diff --git a/packages/comark/test/dos.test.ts b/packages/comark/test/dos.test.ts index 118fb209..b8b205f1 100644 --- a/packages/comark/test/dos.test.ts +++ b/packages/comark/test/dos.test.ts @@ -32,6 +32,14 @@ describe('denial-of-service hardening', () => { expect(doc.nodes.at(-1)).toEqual(['p', {}, 'After']) }) + it('scans thousands of unmatched bare markers under open nested components in linear time', async () => { + const start = performance.now() + const nested = ':::x\n'.repeat(1_000) + '::::\n'.repeat(8_000) + ':::\n'.repeat(1_000) + const doc = await parseMarkdown('::box\n' + nested + '::\n\nAfter') + expect(performance.now() - start).toBeLessThan(500) + expect(doc.nodes.at(-1)).toEqual(['p', {}, 'After']) + }) + it('parses thousands of unclosed nested inline HTML tags without stack overflow', async () => { const md = ''.repeat(10_000) const doc = await parseMarkdown(md)