Skip to content

Commit 34f75a5

Browse files
authored
fix: stricter boolean prop guessing (#212)
!BREAKING: As of now props with empty value will be treated as string. `:component{props=""}` will be parsed to `["component", { "props": ""}]`.
1 parent 6f8f4b3 commit 34f75a5

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

packages/comark/src/internal/parse/token-processor.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,7 @@ function processAttributes(
118118
}
119119

120120
// Handle boolean attributes: {bool} -> {":bool": "true"}
121-
if (
122-
handleBoolean &&
123-
!key.startsWith(':') &&
124-
!key.startsWith('#') &&
125-
!key.startsWith('.') &&
126-
(!value || value === 'true' || value === '')
127-
) {
121+
if (handleBoolean && !key.startsWith(':') && !key.startsWith('#') && !key.startsWith('.') && value === 'true') {
128122
attrs[`:${key}`] = 'true'
129123
continue
130124
}

packages/comark/test/streaming.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ describe('streaming mode', () => {
204204
expect(alert[0]).toBe('alert')
205205
})
206206

207+
it('keeps auto-closed empty string attributes as strings', async () => {
208+
const parse = createParse()
209+
const result = await parse('::callout{color="info" icon', { streaming: true })
210+
const callout = result.nodes[0] as ComarkElement
211+
212+
expect(callout[0]).toBe('callout')
213+
expect(callout[1]).toMatchObject({ color: 'info', ':icon': 'true' })
214+
})
215+
216+
it('keeps auto-closed empty string attributes as strings', async () => {
217+
const parse = createParse()
218+
const result = await parse('::callout{color="info" icon="', { streaming: true })
219+
const callout = result.nodes[0] as ComarkElement
220+
221+
expect(callout[0]).toBe('callout')
222+
expect(callout[1]).toMatchObject({ color: 'info', icon: '' })
223+
expect(callout[1]).not.toHaveProperty(':icon')
224+
})
225+
207226
it('caches nodes before MDC components', async () => {
208227
const parse = createParse()
209228

0 commit comments

Comments
 (0)