Created
July 2, 2021 08:45
-
-
Save haayhappen/57b7c77151165584e810f036d536c3aa to your computer and use it in GitHub Desktop.
Marked custom extension not working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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