-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
renderer.* to vue3 component #2915
Comments
You will have to create your own Parser. The marked parser concats the output of the renderers as strings. |
Can you tell me how to do it? Are there any examples? renderer.code = (code: string, lang: string) => {
const language = hljs.getLanguage(lang) ? lang : 'plaintext'
const result = hljs.highlight(code, { language }).value
return h('CodeBlock', { code, lang, result })
} From your answer I understood that we still get HTML code on the output, but most likely functions and events inside the component will not work in this case. |
You can copy the marked Parser and run it on the lexer tokens. marked.use({renderer});
const tokens = marked.lexer(markdown);
const vueComponent = myParser(tokens); |
Yeah, I gather it's not exactly an easy task, too bad. |
Following hints from #2915 (comment), I managed to implement a markdown-to-vue-component parser. Here are some gists: Take the source code of Marked and put them into your project - It's well structured and has no external dependency. Start with /**
* Parse Inline Tokens
*/
parseInline(tokens: Token[], renderer?: _Renderer): (VNode | string)[] {
renderer = renderer || this.renderer
const out: (VNode | string)[] = [] where Now follow the type error to amend the list(body: (VNode | string)[], ordered: boolean, start: number | ''): VNode {
const type = ordered ? 'ol' : 'ul'
const startatt = ordered && start !== 1 ? { start: start } : {}
return h(type, startatt, body)
}
listitem(text: (VNode | string)[]): VNode {
return h('li', {}, text)
}
checkbox(checked: boolean): VNode {
interface Props {
type: string
disabled: boolean
checked?: ''
}
const props: Props = {
type: 'checkbox',
disabled: true,
}
if (checked) {
props['checked'] = ''
}
return h('input', props)
} You may import and use your own tailored Vue components in the Repeat the process for /**
* Parse Loop
*/
parse(tokens: Token[], top = true): (VNode | string)[] {
const out: (VNode | string)[] = [] Follow the type errors to modify Also usages of Finally, a functional component is going to consume Good luck. Many thanks to the authors of Marked, the project is compact and well-structured. I really enjoy the code reading and hacking process. Cheers! 🍻 |
I'm working on this, might be of help, still very much a WIP and I honestly am not incredibly sure I have a great handle on everything yet, but it mostly works: https://github.com/L422Y/nuxt-content-lite/tree/main/src/runtime/src/vueMarked |
Describe the feature
Make it possible to render tokens using vue 3 components.
App.vue
CustomLink.vue
Why is this feature necessary?
In vue 3 you need to render code,link etc from a component so that you can set @click events etc for elements inside the component.
Describe alternatives you've considered
I used this code, it works, but the registered events inside the component will not work as we are only returning the HTML code of the component, not the component itself.
The text was updated successfully, but these errors were encountered: