Skip to content

Commit 840b059

Browse files
committed
fix(stringify): nested lists
1 parent c54cd9e commit 840b059

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/runtime/stringify/mdc-remark.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,30 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents, parent: Pa
198198
children: state.toFlow(state.all(node)),
199199
}
200200
},
201+
'li': (state: State, node: Parents) => {
202+
const result = defaultHandlers.li(state, node as Element)
203+
204+
if (result.children[0]?.type === 'paragraph') {
205+
const paragraph = result.children[0]!
206+
const lastChild = paragraph.children[paragraph.children.length - 1] as Text
207+
if (lastChild?.type === 'text' && lastChild.value?.endsWith('\n')) {
208+
lastChild.value = lastChild.value.trim()
209+
}
210+
}
211+
212+
return result
213+
},
201214
'ul': (state: State, node: Parents, parent: Parents | undefined) => {
202215
const result = defaultHandlers.ul(state, node as Element)
203216

204-
return parent?.tagName === 'p'
217+
return ['p', 'li'].includes(parent?.tagName || '')
205218
? result
206219
: { type: 'paragraph', children: [result] }
207220
},
208221
'ol': (state: State, node: Parents, parent: Parents | undefined) => {
209222
const result = defaultHandlers.ol(state, node as Element)
210223

211-
return parent?.tagName === 'p'
224+
return ['p', 'li'].includes(parent?.tagName || '')
212225
? result
213226
: { type: 'paragraph', children: [result] }
214227
},

test/stringify/ul.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test, describe } from 'vitest'
2+
import { parseMarkdown, stringifyMarkdown } from '../utils/parser'
3+
4+
describe('stringify unordered list (ul)', () => {
5+
test('should stringify ul correctly', async () => {
6+
const md = [
7+
'- This is a list item.',
8+
' - This is a CHILD list item.',
9+
' - This is a GRANDCHILD list item.',
10+
'',
11+
].join('\n')
12+
const { body } = await parseMarkdown(md)
13+
// console.log(JSON.stringify(body, null, 2))
14+
const result = await stringifyMarkdown(body)
15+
expect(result).toBe(md)
16+
})
17+
})

0 commit comments

Comments
 (0)