Skip to content

Commit

Permalink
Front matter: Handle dashes in content
Browse files Browse the repository at this point in the history
If the front matter contains a sequence of three or more dashes,
the front matter fences must be adjusted.

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
  • Loading branch information
susnux committed Jul 28, 2022
1 parent ad1b431 commit 55992c9
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/nodes/FrontMatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ const FrontMatter = TiptapCodeBlock.extend({
mergeAttributes(HTMLAttributes, { 'data-title': t('text', 'Front matter'), class: 'frontmatter' }),
})
},
parseHTML: () => {
return [
{
tag: 'pre[id="frontmatter"]',
preserveWhitespace: 'full',
priority: 9001,
attrs: {
language: 'yaml',
},
parseHTML() {
return [{
tag: 'pre#frontmatter',
preserveWhitespace: 'full',
priority: 9001,
attrs: {
language: 'yaml',
},
]
}]
},
toMarkdown: (state, node) => {
if (!state.out.match(/^\s*/)) throw Error('FrontMatter must be the first node of the document!')
const text = node.textContent
// Make sure the front matter fences are longer than any dash sequence within it
const dashes = text.match(/-{3,}/gm)
const separator = '-'.repeat(dashes ? dashes.sort().slice(-1)[0].length + 1 : 3)

state.write('')
state.out = ''
state.write('---\n')
state.text(node.textContent, false)
state.write(`${separator}\n`)
state.text(text, false)
state.ensureNewLine()
state.write('---')
state.write(separator)
state.closeBlock(node)
},

Expand Down

0 comments on commit 55992c9

Please sign in to comment.