diff --git a/AGENTS.md b/AGENTS.md
index 1e9ef374..7cd9c97a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -98,6 +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 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
@@ -261,7 +262,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.
-- Spec fixtures: `SPEC/COMARK/component-nested-*-outdented.md`, `SPEC/COMARK/component-nested-codeblock-indented.md`, `SPEC/COMARK/codeblock-indented-content.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, 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-codeblock-closing-fence-indent.md b/packages/comark/SPEC/COMARK/component-codeblock-closing-fence-indent.md
new file mode 100644
index 00000000..6568c1ac
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-codeblock-closing-fence-indent.md
@@ -0,0 +1,168 @@
+## 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
+```
+
+## Markdown HTML
+
+```md
+
+
+```js
+a
+```
+
+```js
+b
+```
+
+```js
+c
+```
+
+
+
+```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..a54bbd95
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-codeblock-indented-fence.md
@@ -0,0 +1,95 @@
+## 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
+```
+
+## Markdown HTML
+
+```md
+
+
+```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..fd0226bb
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-codeblock-indented-opener.md
@@ -0,0 +1,91 @@
+## 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
+```
+
+## Markdown HTML
+
+```md
+
+
+~~~md
+- item
+ ```js
+ x
+ ```
+~~~
+
+
+
+After
+```
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..edb285bb
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-codeblock-invalid-nested-name.md
@@ -0,0 +1,84 @@
+## 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
+```
+
+## Markdown HTML
+
+```md
+
+
+
+
+\::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..3542fe2d
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-indented-code-fence.md
@@ -0,0 +1,65 @@
+## 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
+```
+
+## Markdown HTML
+
+```md
+
+
+~~~
+```
+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..1348910a
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-list-codeblock-indented-fence.md
@@ -0,0 +1,144 @@
+## 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
+
+
+
+
+
+
+After
+```
+
+## Markdown
+
+```md
+::box
+- item
+ ```js
+ x
+ ```
+::
+
+::box
+- a
+ - b
+ ```js
+ x
+ ```
+::
+
+After
+```
+
+## Markdown HTML
+
+```md
+
+
+- item
+ ```js
+ x
+ ```
+
+
+
+
+
+- a
+ - b
+ ```js
+ x
+ ```
+
+
+
+After
+```
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..3c18c3ff
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-list-marker-indent.md
@@ -0,0 +1,235 @@
+## Input
+
+```md
+::box
+ - item
+ ~~~
+::
+
+::box
+- item
+
+ ```js
+ x
+ ```
+::
+
+::box
+- - -
+ ~~~
+ ~~~
+::
+
+::box
+- - item
+ ~~~
+ x
+ ~~~
+::
+
+::box
+ :::inner
+ ~~~
+ ~~~
+::
+
+::box
+- - item
+ ~~~
+ 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"]]]
+ ]
+ ],
+ ["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"]
+ ]
+}
+```
+
+## HTML
+
+```html
+
+ - item
+ ~~~
+
+
+
+
+
+
+ ~~~
+ ~~~
+
+
+
+
+
+ :::inner
+ ~~~
+ ~~~
+
+
+
+
+After
+```
+
+## Markdown
+
+```md
+::box
+```
+- item
+ ~~~
+```
+::
+
+::box
+- item
+ ```js
+ x
+ ```
+::
+
+::box
+---
+
+```
+~~~
+ ~~~
+```
+::
+
+::box
+- - item
+ ```
+ x
+ ```
+::
+
+::box
+```
+:::inner
+ ~~~
+ ~~~
+```
+::
+
+::box
+- - item
+ ```
+ x
+ ```
+::
+
+After
+```
+
+## Markdown HTML
+
+```md
+
+
+```
+- item
+ ~~~
+```
+
+
+
+
+
+- item
+ ```js
+ x
+ ```
+
+
+
+
+
+---
+
+```
+~~~
+ ~~~
+```
+
+
+
+
+
+- - item
+ ```
+ x
+ ```
+
+
+
+
+
+```
+:::inner
+ ~~~
+ ~~~
+```
+
+
+
+
+
+- - item
+ ```
+ 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..da05e261
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-nested-codeblock-indented-fence.md
@@ -0,0 +1,125 @@
+## 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
+```
+
+## Markdown HTML
+
+```md
+
+
+
+
+
+
+```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..c7fbfc7a
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-slot-codeblock-indented-fence.md
@@ -0,0 +1,109 @@
+## 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
+
+
+ ::a
+ ```js
+ x
+ ```
+ ::
+
+
+After
+```
+
+## Markdown
+
+```md
+::component
+#code
+```mdc
+::a
+ ```js
+ x
+ ```
+::
+```
+::
+
+After
+```
+
+## Markdown HTML
+
+```md
+
+
+
+
+```mdc
+::a
+ ```js
+ x
+ ```
+::
+```
+
+
+
+
+
+After
+```
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..f59fcf45
--- /dev/null
+++ b/packages/comark/SPEC/COMARK/component-slot-indented-fence.md
@@ -0,0 +1,86 @@
+## 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
+
+
+ #notASlot
+
+
+ text
+
+
+After
+```
+
+## Markdown
+
+```md
+::box
+#code
+```
+#notASlot
+```
+
+#other
+text
+::
+
+After
+```
+
+## Markdown HTML
+
+```md
+
+
+
+
+```
+#notASlot
+```
+
+
+
+
+
+text
+
+
+
+
+
+After
+```
diff --git a/packages/comark/SPEC/auto-close.md b/packages/comark/SPEC/auto-close.md
index 156e0254..853bf70f 100644
--- a/packages/comark/SPEC/auto-close.md
+++ b/packages/comark/SPEC/auto-close.md
@@ -2337,6 +2337,75 @@ 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**
+```
+
+A tab advances to the next multiple of 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 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 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 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
+- 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 725772ad..79d67365 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 { pushListItems } from './internal/list-item.ts'
export const INCOMPLETE_LINK_PLACEHOLDER = 'comark:incomplete-link'
export const INCOMPLETE_IMAGE_PLACEHOLDER = 'comark:incomplete-image'
@@ -156,6 +157,8 @@ export function autoCloseMarkdown(markdown: string, options: AutoCloseOptions =
interface Component {
depth: number
indent: string
+ /** Column of `indent`, tabs expanded. */
+ column: number
yaml: boolean
}
@@ -200,6 +203,9 @@ 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 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
let fmContent = false
@@ -229,22 +235,39 @@ 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++
if (run >= 3) {
+ const column = columnsOf(src, ls, st)
if (fenceLen === 0) {
- // `` ```code`` `` is an incomplete inline code span, not a fence opener.
- if (!(c0 === 96 && isIncompleteInlineFence(src, st, en))) {
+ // 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
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
}
- } else if (c0 === fenceCh && run >= fenceLen && st + run === en) {
+ } 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
+ lists.from = i + 1
shielded = i
continue
} else {
@@ -303,7 +326,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), 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()
}
@@ -366,6 +398,54 @@ 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
+ lists.depth = pushListItems(src, st, en, column, lists.columns, lists.depth)
+}
+
+/** 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
+ 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..aad863de
--- /dev/null
+++ b/packages/comark/src/internal/list-item.ts
@@ -0,0 +1,69 @@
+/**
+ * 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 pushListItems(
+ src: string,
+ start: number,
+ end: number,
+ column: number,
+ 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 && !(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++
+ 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
+ }
+
+ // 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
+
+ // 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
+ }
+ return depth
+}
diff --git a/packages/comark/src/plugins/components.ts b/packages/comark/src/plugins/components.ts
index 2c67cc7d..134d34a1 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 { pushListItems } from '../internal/list-item.ts'
/**
* A component name must start with a letter or `$`, followed by word chars,
@@ -105,9 +106,17 @@ const markdownItComarkBlock: PluginSimple = (md) => {
let inCodeFence = false
let codeFenceCharCode = 0
let codeFenceCount = 0
-
- // Track nesting depth for blocks with the same marker count
- let nestingDepth = 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]> = []
+ // 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[] = []
if (state.src[start] !== ':') return false
@@ -147,6 +156,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))
@@ -154,7 +166,7 @@ const markdownItComarkBlock: PluginSimple = (md) => {
const lineCharCode = state.src.charCodeAt(start)
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) {
@@ -165,14 +177,34 @@ 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()
+ }
+
+ // 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) {
+ pushListItems(state.src, start, max, state.sCount[nextLine], listColumns, listColumns.length)
+ }
+
if (lineCharCode === 0x60 /* ` */ || lineCharCode === 0x7e /* ~ */) {
let fencePos = start + 1
while (fencePos < max && state.src.charCodeAt(fencePos) === lineCharCode) fencePos++
- if (fencePos - start >= 3) {
+ if (fencePos - start >= 3 && !isIndentedCode) {
inCodeFence = true
codeFenceCharCode = lineCharCode
codeFenceCount = fencePos - start
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
}
}
@@ -183,22 +215,32 @@ 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
+ // 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
}
- if (nestingDepth > 0) {
- nestingDepth--
+ // A bare marker closes the innermost open nested block with the same length, and any left open inside it
+ if (openCounts.get(count)) {
+ let closed: number
+ do {
+ closed = openComponents.pop()![0]
+ openCounts.set(closed, openCounts.get(closed)! - 1)
+ } while (closed !== count)
continue
}
+ // Closing fence must match the opening fence length
+ if (count !== marker_count) continue
+
auto_closed = true
break
}
@@ -343,37 +385,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
}
diff --git a/packages/comark/test/auto-close.test.ts b/packages/comark/test/auto-close.test.ts
index ad34f042..536df3b9 100644
--- a/packages/comark/test/auto-close.test.ts
+++ b/packages/comark/test/auto-close.test.ts
@@ -220,6 +220,22 @@ 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)
+ // ` :::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**'
+ )
+ })
+
it('should not add closing markers to empty content', () => {
const input = ''
expect(autoCloseMarkdown(input)).toBe(input)
diff --git a/packages/comark/test/dos.test.ts b/packages/comark/test/dos.test.ts
index 1aaa5e9b..b8b205f1 100644
--- a/packages/comark/test/dos.test.ts
+++ b/packages/comark/test/dos.test.ts
@@ -25,6 +25,21 @@ 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('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)
diff --git a/test/bundle.test.ts b/test/bundle.test.ts
index 6cf3cb97..193a46c8 100644
--- a/test/bundle.test.ts
+++ b/test/bundle.test.ts
@@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => {
"@comark/react": "44.4k (87 files)",
"@comark/svelte": "51.7k (95 files)",
"@comark/vue": "63.3k (93 files)",
- "comark": "458k (177 files)",
+ "comark": "465k (179 files)",
}
`)
})