Skip to content

Commit a63389f

Browse files
committed
fix(stringify): detect inline components
1 parent 840b059 commit a63389f

File tree

6 files changed

+80
-7
lines changed

6 files changed

+80
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"rehype-sort-attributes": "^5.0.1",
100100
"remark-emoji": "^5.0.2",
101101
"remark-gfm": "^4.0.1",
102-
"remark-mdc": "^3.8.1",
102+
"remark-mdc": "https://pkg.pr.new/remark-mdc@0c5d4fa",
103103
"remark-parse": "^11.0.0",
104104
"remark-rehype": "^11.1.2",
105105
"remark-stringify": "^11.0.0",

pnpm-lock.yaml

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/stringify/mdc-remark.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ const mdcRemarkNodeHandlers = {
163163
}
164164
}
165165

166+
// if node is not a list item or paragraph and has no children, wrap it in a paragraph and render it as a text component
167+
if (!['li', 'p'].includes(parent?.tagName || '') && !node.children?.length) {
168+
return {
169+
type: 'paragraph',
170+
children: [{
171+
type: mdastTextComponentType,
172+
name: node.tagName,
173+
attributes: node.properties,
174+
children: state.all(node),
175+
}]
176+
}
177+
}
178+
166179
let children = state.all(node)
167180
if (children.every(child => [mdastTextComponentType, 'text'].includes(child.type))) {
168181
children = [{ type: 'paragraph', children: children }] as unknown as typeof children

test/stringify/format.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, it, describe } from 'vitest'
2+
import { parseMarkdown, stringifyMarkdown } from '../utils/parser'
3+
4+
describe('stringify format', () => {
5+
it('should stringify format correctly', async () => {
6+
const md = [
7+
'::container{background-color="#eee" padding="20px"}',
8+
'# This is a header',
9+
'',
10+
':icon{color="#000" name="mdi:github" size="36px"}',
11+
'',
12+
' :::content2',
13+
' Well',
14+
' :::',
15+
'::',
16+
''
17+
].join('\n')
18+
19+
20+
const { body } = await parseMarkdown(md)
21+
const result = await stringifyMarkdown(body)
22+
expect(result).toBe(md)
23+
})
24+
})

test/stringify/nesting.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect, it, describe } from 'vitest'
2+
import { parseMarkdown, stringifyMarkdown } from '../utils/parser'
3+
4+
describe('stringify ordered list (ol)', () => {
5+
it('should stringify complex AST with steps component', async () => {
6+
const md = [
7+
'::container{padding="0px"}',
8+
' :::container',
9+
' ---',
10+
' styles: |',
11+
' pre {',
12+
' border: 1px solid red !important;',
13+
'',
14+
' span {',
15+
' line-height: 1;',
16+
' }',
17+
' }',
18+
' ---',
19+
' This container has a code block.',
20+
'',
21+
' ```js',
22+
' function test() {',
23+
' console.log("test");',
24+
' }',
25+
' ```',
26+
' :::',
27+
'::',
28+
''
29+
].join('\n')
30+
31+
32+
const { body } = await parseMarkdown(md)
33+
const result = await stringifyMarkdown(body)
34+
expect(result).toBe(md)
35+
})
36+
})

test/stringify/ul.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('stringify unordered list (ul)', () => {
1010
'',
1111
].join('\n')
1212
const { body } = await parseMarkdown(md)
13-
// console.log(JSON.stringify(body, null, 2))
1413
const result = await stringifyMarkdown(body)
1514
expect(result).toBe(md)
1615
})

0 commit comments

Comments
 (0)