Skip to content

Instantly share code, notes, and snippets.

@haayhappen
Created July 2, 2021 08:45
Show Gist options
  • Save haayhappen/57b7c77151165584e810f036d536c3aa to your computer and use it in GitHub Desktop.
Save haayhappen/57b7c77151165584e810f036d536c3aa to your computer and use it in GitHub Desktop.
Marked custom extension not working
import marked from 'marked'
const variable = {
name: 'variable',
level: 'block',
start (src) { return src.match(/\{{(.*?)}}/g)?.index },
tokenizer (src, tokens) {
const rule = /\{{(.*?)}}/g
const match = rule.exec(src)
if (match) {
return {
type: 'variable', // Should match "name" above
raw: match[0], // Text to consume from the source
variable: this.inlineTokens(match[1].trim())
}
}
},
renderer (token) {
return `\n<span class="variable">${this.parseInline(token.variable)}</span>`
}
}
marked.use({ extensions: [variable] })
export function markdownToHTML (str) {
if (!str) {
return ''
}
return marked(str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment