Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/comark-ansi/src/handlers/li.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { NodeHandler } from 'comark/render'
import type { ComarkElement, ComarkNode } from 'comark'
import { indent } from 'comark/utils'

// Block elements that need explicit indentation in list items.
// Note: ol/ul are handled by their own handlers which manage indentation via listIndent context.
const blockElements = new Set(['pre', 'blockquote', 'table'])

export const li: NodeHandler = async (node, state) => {
const children = node.slice(2) as ComarkNode[]
Expand All @@ -14,15 +19,31 @@ export const li: NodeHandler = async (node, state) => {
prefix += input[1].checked || input[1][':checked'] ? '[x] ' : '[ ] '
}

let content = ''
const prefixWidth = prefix.length
let result = ''
for (const child of children) {
content += await state.one(child, state, node)
const rendered = await state.one(child, state, node)
if (result && Array.isArray(child)) {
if (blockElements.has(child[0] as string)) {
// Block-level child: put on its own line and indent to align with list prefix
const indented = indent(rendered, { width: prefixWidth })
result = result.trimEnd() + '\n' + indented.trimEnd() + '\n'
continue
}

if (child[0] === 'p') {
const indented = indent(rendered, { width: prefixWidth })
result = result.trimEnd() + '\n\n' + indented.trimEnd() + '\n'
continue
}
}
result += rendered
}
content = content.trim()
result = result.trim()

if (typeof order === 'number') {
state.applyContext({ order: order + 1 })
}

return `${prefix}${content}\n`
return `${prefix}${result}\n`
}
7 changes: 6 additions & 1 deletion packages/comark-ansi/src/handlers/p.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { NodeHandler } from 'comark/render'
import type { ComarkNode } from 'comark'

export const p: NodeHandler = async (node, state) => {
export const p: NodeHandler = async (node, state, parent) => {
const children = node.slice(2) as ComarkNode[]
let result = ''
for (const child of children) {
result += await state.one(child, state, node)
}

if (parent?.[0] === 'li') {
return result
}

return result + '\n\n'
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ options:
[
"li",
{},
"Setup:",
[
"p",
{},
"Setup:"
],
[
"pre",
{
Expand Down Expand Up @@ -100,7 +104,8 @@ options:
```html
<ol>
<li>
Setup:<pre language="rust" class="shiki shiki-themes github-dark dark:github-dark" tabindex="0"><code class="language-rust"><span class="line" style="display: inline"><span style="color:#F97583">let</span><span style="color:#E1E4E8"> x </span><span style="color:#F97583">=</span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8">;</span></span></code></pre>
<p>Setup:</p>
<pre language="rust" class="shiki shiki-themes github-dark dark:github-dark" tabindex="0"><code class="language-rust"><span class="line" style="display: inline"><span style="color:#F97583">let</span><span style="color:#E1E4E8"> x </span><span style="color:#F97583">=</span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8">;</span></span></code></pre>
</li>
</ol>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
[
"li",
{},
"Complex item:",
[
"p",
{},
"Complex item:"
],
[
"pre",
{
Expand All @@ -54,7 +58,11 @@
[
"li",
{},
"Nested with table:",
[
"p",
{},
"Nested with table:"
],
[
"table",
{},
Expand Down Expand Up @@ -108,13 +116,14 @@
```html
<ol>
<li>
Complex item:<pre language="js"><code class="language-js">code()</code></pre>
<p>Complex item:</p>
<pre language="js"><code class="language-js">code()</code></pre>
<blockquote>
A note
</blockquote>
<ul>
<li>
Nested with table:
<p>Nested with table:</p>
<table>
<thead>
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
[
"li",
{},
"First point:",
[
"p",
{},
"First point:"
],
[
"blockquote",
{},
Expand Down Expand Up @@ -60,7 +64,7 @@
```html
<ol>
<li>
First point:
<p>First point:</p>
<blockquote>
A quote here
</blockquote>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@
[
"li",
{},
"First level",
[
"p",
{},
"First level"
],
[
"ol",
{},
[
"li",
{},
"Second level with code:",
[
"p",
{},
"Second level with code:"
],
[
"pre",
{
Expand All @@ -56,10 +64,11 @@
```html
<ol>
<li>
First level
<p>First level</p>
<ol>
<li>
Second level with code:<pre language="rust"><code class="language-rust">let x = 42;</code></pre>
<p>Second level with code:</p>
<pre language="rust"><code class="language-rust">let x = 42;</code></pre>
</li>
</ol>
</li>
Expand Down
8 changes: 6 additions & 2 deletions packages/comark/SPEC/common-mark/ordered-list-nested.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
[
"li",
{},
"Third item",
[
"p",
{},
"Third item"
],
[
"ol",
{},
Expand Down Expand Up @@ -65,7 +69,7 @@
<li>First item</li>
<li>Second item</li>
<li>
Third item
<p>Third item</p>
<ol>
<li>Indented item</li>
<li>Indented item</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
[
"li",
{},
"Item with quote:",
[
"p",
{},
"Item with quote:"
],
[
"blockquote",
{},
Expand All @@ -36,7 +40,7 @@
```html
<ul>
<li>
Item with quote:
<p>Item with quote:</p>
<blockquote>
This is a blockquote
</blockquote>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Input

```md
- para1

para2
```

## AST

```json
{
"frontmatter": {},
"meta": {},
"nodes": [
[
"ul",
{},
[
"li",
{},
[
"p",
{},
"para1"
],
[
"p",
{},
"para2"
]
]
]
]
}
```

## HTML

```html
<ul>
<li>
<p>para1</p>
<p>para2</p>
</li>
</ul>
```

## Markdown

```md
- para1

para2
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
[
"li",
{},
"Item with quote:",
[
"p",
{},
"Item with quote:"
],
[
"blockquote",
{},
Expand All @@ -35,7 +39,11 @@
[
"li",
{},
"Nested item with quote:",
[
"p",
{},
"Nested item with quote:"
],
[
"blockquote",
{},
Expand All @@ -54,13 +62,13 @@
```html
<ul>
<li>
Item with quote:
<p>Item with quote:</p>
<blockquote>
Quote level 1
</blockquote>
<ul>
<li>
Nested item with quote:
<p>Nested item with quote:</p>
<blockquote>
Quote level 2
</blockquote>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,33 @@
[
"li",
{},
"Level 1",
[
"p",
{},
"Level 1"
],
[
"ul",
{},
[
"li",
{},
"Level 2",
[
"p",
{},
"Level 2"
],
[
"ul",
{},
[
"li",
{},
"Level 3 with code:",
[
"p",
{},
"Level 3 with code:"
],
[
"pre",
{
Expand Down Expand Up @@ -66,13 +78,14 @@
```html
<ul>
<li>
Level 1
<p>Level 1</p>
<ul>
<li>
Level 2
<p>Level 2</p>
<ul>
<li>
Level 3 with code:<pre language="py"><code class="language-py">print("deep")</code></pre>
<p>Level 3 with code:</p>
<pre language="py"><code class="language-py">print("deep")</code></pre>
</li>
</ul>
</li>
Expand Down
Loading
Loading