Skip to content

Commit 6ea4163

Browse files
committed
fix: do not add space between components and punctuation
1 parent c3efdd5 commit 6ea4163

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

src/runtime/stringify/mdc-remark.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function mdcRemark(options?: Options | undefined | null) {
7777
}
7878
if (index && index < parent.children.length - 1 && parent.children[index + 1].type === 'text') {
7979
const text = parent.children[index + 1] as Text
80-
if (!['\n', ' ', '\t'].includes(text.value.slice(0, 1))) {
80+
if (!['\n', ' ', '\t', ',', '.'].includes(text.value.slice(0, 1))) {
8181
text.value = ' ' + text.value
8282
}
8383
}
@@ -153,7 +153,8 @@ const mdcRemarkNodeHandlers = {
153153
return result
154154
}
155155

156-
const isInlineElement = (parent?.children || []).some(child => child.type === 'text') || ['p', 'li'].includes(parent?.tagName)
156+
const isInlineElement = (parent?.children || [])
157+
.some(child => child.type === 'text') || ['p', 'li', 'strong', 'em', 'span'].includes(parent?.tagName)
157158
if (isInlineElement) {
158159
return {
159160
type: mdastTextComponentType,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { expect, it } from 'vitest'
2+
import { parseMarkdown, stringifyMarkdown } from '../utils/parser'
3+
4+
it('Add space before and after :br', async () => {
5+
const input = `Hello\\\nWorld`
6+
7+
const { body } = await parseMarkdown(input)
8+
const output = await stringifyMarkdown(body)
9+
10+
expect(output).toBe(`Hello :br\nWorld\n`)
11+
})
12+
13+
it('Add space before :br', async () => {
14+
const input = `\\\nWorld`
15+
16+
const { body } = await parseMarkdown(input)
17+
const output = await stringifyMarkdown(body)
18+
19+
expect(output).toBe(`:br\nWorld\n`)
20+
})
21+
22+
it('Do not change spacings of binding elements', async () => {
23+
const inputs = [
24+
`Hello{{ name }}`,
25+
`Hello {{ name }}`,
26+
`Hello{{ name }},`,
27+
`Hello {{ name }},`,
28+
`Hello {{ name }} ,`
29+
]
30+
31+
for (const input of inputs) {
32+
const { body } = await parseMarkdown(input)
33+
const output = await stringifyMarkdown(body)
34+
35+
expect(output).toBe(input + '\n')
36+
}
37+
})
38+
39+
it('Ensure spacing between inline components', async () => {
40+
const input = `This is a :test :for components :spacing`
41+
42+
const { body } = await parseMarkdown(input)
43+
const output = await stringifyMarkdown(body)
44+
45+
expect(output).toBe(`This is a :test :for components :spacing\n`)
46+
})
47+
48+
it('Allow punctuation after inline components', async () => {
49+
const input = `This is a :test :for components :spacing,`
50+
51+
const { body } = await parseMarkdown(input)
52+
const output = await stringifyMarkdown(body)
53+
54+
expect(output).toBe(`This is a :test :for components :spacing,\n`)
55+
})
56+
57+
it('No spacing inside inline components', async () => {
58+
const inputs = [
59+
`**:component[text]{.class}**`,
60+
`*:component[text]{.class}*`,
61+
`[:component[text]{.class}]`
62+
]
63+
64+
for (const input of inputs) {
65+
const { body } = await parseMarkdown(input)
66+
const output = await stringifyMarkdown(body)
67+
68+
expect(output).toBe(input + '\n')
69+
}
70+
})

test/utils/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createShikiHighlighter } from '../../src/runtime/highlighter/shiki'
44
import { rehypeHighlight } from '../../src/runtime/highlighter/rehype-nuxt'
55
import type { MDCParseOptions } from '../../src/types'
66
import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'
7+
import { stringifyMarkdown as _stringifyMarkDown } from '../../src/runtime/stringify'
78

89
vi.mock('#mdc-imports', () => {
910
return {
@@ -47,3 +48,7 @@ export const parseMarkdown = (md: string, options: MDCParseOptions = {}) => {
4748

4849
return _parseMarkDown(md, options)
4950
}
51+
52+
export const stringifyMarkdown = (node, data = {}) => {
53+
return _stringifyMarkDown(node, data)
54+
}

0 commit comments

Comments
 (0)