Skip to content

Instantly share code, notes, and snippets.

@robozevel
Last active September 12, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robozevel/b08152523aa616b5087d57367a1b5375 to your computer and use it in GitHub Desktop.
Save robozevel/b08152523aa616b5087d57367a1b5375 to your computer and use it in GitHub Desktop.
const components = {
'*': 'strong',
'_': 'em',
'~': 's',
'```': 'code'
}
export default {
functional: true,
props: {
tag: {
default: 'span'
},
recursive: {
type: Boolean,
default: false
}
},
render (h, context) {
const { recursive } = context.props
function parse (markdown) {
const re = new RegExp('(\\*|_|~|```)([^\\1]*?)\\1', 'g')
const vnodes = []
let lastIndex = 0
let match
while ((match = re.exec(markdown)) !== null) {
const [, char, text] = match
const tag = components[char]
const child = tag ? h(tag, recursive ? parse(text) : text) : text
vnodes.push(markdown.slice(lastIndex, match.index), child)
lastIndex = re.lastIndex
}
return vnodes.concat(markdown.slice(lastIndex))
}
const [vnode] = context.slots().default
return h(context.props.tag, parse(vnode.text))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment