Skip to content

Commit 1aeaa52

Browse files
committed
fix: better inline detection for buttons
1 parent 96d3555 commit 1aeaa52

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

src/runtime/stringify/mdc-remark.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ const mdcRemarkNodeHandlers = {
152152
return result
153153
}
154154

155-
const isInlineElement = (parent?.children || [])
156-
.some(child => child.type === 'text') || ['p', 'li', 'strong', 'em', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(parent?.tagName)
155+
const isInlineElement = isForcedToBeInlineByItsParent(node, parent)
157156
if (isInlineElement) {
158157
return {
159158
type: mdastTextComponentType,
@@ -287,20 +286,17 @@ const mdcRemarkHandlers: Record<string, (state: State, node: Parents, parent: Pa
287286
meta,
288287
}
289288
},
290-
'button': (state: State, node: Parents) => {
291-
if (
292-
// @ts-expect-error: custom type
293-
node.children?.find(child => child.type === mdcRemarkElementType)
294-
|| node.children?.find(child => child.type === 'text' && child.value.includes('\n'))
295-
) {
296-
return {
297-
type: 'containerComponent',
298-
name: 'button',
299-
children: state.all(node),
300-
attributes: node.properties,
301-
}
289+
'button': (state: State, node: Parents, parent: Parents | undefined) => {
290+
if (isInlineNode(node, parent)) {
291+
return createTextComponent('button')(state, node)
292+
}
293+
294+
return {
295+
type: 'containerComponent',
296+
name: 'button',
297+
children: state.all(node),
298+
attributes: node.properties,
302299
}
303-
return createTextComponent('button')(state, node)
304300
},
305301
'span': createTextComponent('span'),
306302
'kbd': createTextComponent('kbd'),
@@ -390,3 +386,31 @@ function createTextComponent(name: string) {
390386
return result
391387
}
392388
}
389+
390+
function isForcedToBeInlineByItsParent(node: Parents, parent: Parents | undefined) {
391+
if (['p', 'li', 'strong', 'em', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(parent?.tagName || '')) {
392+
return true
393+
}
394+
395+
if (parent?.children?.some(child => child.type === 'text')) {
396+
return true
397+
}
398+
399+
return false
400+
}
401+
402+
function isInlineNode(node: Parents, parent: Parents | undefined) {
403+
if (
404+
// @ts-expect-error: custom type
405+
node.children?.find(child => child.type === mdcRemarkElementType)
406+
|| node.children?.find(child => child.type === 'text' && child.value.includes('\n'))
407+
) {
408+
return false
409+
}
410+
411+
if (!isForcedToBeInlineByItsParent(node, parent)) {
412+
return false
413+
}
414+
415+
return true
416+
}

test/stringify/format.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,43 @@ describe('stringify format', () => {
8686
const result = await stringifyMarkdown(body)
8787
expect(result).toBe(md)
8888
})
89+
90+
it('should stringify format correctly with yaml props and nested objects', async () => {
91+
const md = [
92+
'::page-section',
93+
' :::page-section',
94+
' #tagline',
95+
' {{ $doc.snippet.tagline }}',
96+
'',
97+
' #title',
98+
' {{ $doc.snippet.title }}',
99+
'',
100+
' #description',
101+
' {{ $doc.snippet.description }}',
102+
'',
103+
' ::::button{:to="$doc.snippet.link" appearance="primary"}',
104+
' Button Text',
105+
' ::::',
106+
'',
107+
' ::::button{:to="$doc.snippet.link" appearance="primary"}',
108+
' Button Text',
109+
' ::::',
110+
'',
111+
' ::::button',
112+
' ---',
113+
' :data-testid: $doc.snippet.description',
114+
' external: true',
115+
' :to: $doc.snippet.link',
116+
' appearance: primary',
117+
' ---',
118+
' Button Text',
119+
' ::::',
120+
' :::',
121+
'::',
122+
'',
123+
].join('\n')
124+
const { body } = await parseMarkdown(md)
125+
const result = await stringifyMarkdown(body)
126+
expect(result).toBe(md)
127+
})
89128
})

0 commit comments

Comments
 (0)