Last active
September 12, 2019 10:02
-
-
Save robozevel/b08152523aa616b5087d57367a1b5375 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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