Skip to content

Commit

Permalink
fix(markdown): attributes of span inside headings (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Jun 29, 2022
1 parent a77bbd9 commit 965a56b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { markdownSpace } from 'micromark-util-character'
import type { Effects, State, Code, TokenizeContext } from 'micromark-util-types'
import { Codes } from './constants'
import createLabel from './factory-label'
import createAttributes from './factory-attributes'

const label: any = { tokenize: tokenizeLabel, partial: true }
const gfmCheck: any = { tokenize: checkGfmTaskCheckbox, partial: true }
const attributes: any = { tokenize: tokenizeAttributes, partial: true }

function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: State) {
const self = this
Expand Down Expand Up @@ -33,10 +35,19 @@ function tokenize (this: TokenizeContext, effects: Effects, ok: State, nok: Stat
}

function exit (code: Code): void | State {
// prevent conflict with link syntax
// Prevent conflict with link syntax
if (code === Codes.openingParentheses || code === Codes.openingSquareBracket) {
return nok(code)
}
// Attemp parsing attributes
if (code === Codes.openingCurlyBracket) {
return effects.attempt(attributes, exitOK, exitOK)(code)
}

return exitOK(code)
}

function exitOK (code: Code): void | State {
effects.exit('textSpan')
return ok(code)
}
Expand Down Expand Up @@ -80,3 +91,23 @@ function checkGfmTaskCheckbox (effects: Effects, ok: State, nok: State) {
return nok(code)
}
}

function tokenizeAttributes (effects: Effects, ok: State, nok: State) {
// Always a `{`
return createAttributes(
effects,
ok,
nok,
'componentTextAttributes',
'componentTextAttributesMarker',
'componentTextAttribute',
'componentTextAttributeId',
'componentTextAttributeClass',
'componentTextAttributeName',
'componentTextAttributeInitializerMarker',
'componentTextAttributeValueLiteral',
'componentTextAttributeValue',
'componentTextAttributeValueMarker',
'componentTextAttributeValueData'
)
}
21 changes: 21 additions & 0 deletions test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,26 @@ export const testMarkdownParser = () => {
expect(parsed.body.children.length).toEqual(1)
expect(parsed.body.children[0].tag).toEqual('h1')
})

test('span attributes', async () => {
const parsed = await $fetch('/api/parse', {
method: 'POST',
body: {
id: 'content:index.md',
content: [
'# Hello [World]{.text-green}',
'The answer to life the universe and everything: [42]{.font-bold .text-green}'
].join('\n')
}
})

expect(parsed.body).toHaveProperty('children')
expect(parsed.body.children.length).toEqual(2)
expect(parsed.body.children[0].tag).toEqual('h1')
expect(parsed.body.children[0].children[1].props.class).toEqual('text-green')

expect(parsed.body.children[1].tag).toEqual('p')
expect(parsed.body.children[1].children[1].props.class).toEqual('font-bold text-green')
})
})
}

0 comments on commit 965a56b

Please sign in to comment.