Slatejs to Vue 3 custom renderer #689
MinSomai
started this conversation in
Show and tell
Replies: 2 comments 1 reply
-
@MinSomai Thank you for sharing. It would be great to create a Markdown renderer to integrate with Hugo SSG. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Made an improved version for Vue 2. import { CreateElement, defineComponent, PropType } from 'vue';
const recursivlyCreateText = (createElement: CreateElement, tags: string[], text: string): any => {
if (tags.length === 1) {
return createElement(tags[0], text);
}
const nextTags = tags.filter((t, idx) => idx !== 0);
return createElement(tags[0], [recursivlyCreateText(createElement, nextTags, text)]);
};
const serialize = (createElement: CreateElement, children: any) => {
const recursive = (children: Parameters<typeof serialize>[1]) => serialize(createElement, children);
return children?.map((node: any): any => {
if (!node) {
return null;
}
if (typeof node.text === 'string') {
const textTags: string[] = [];
if (node.bold) {
textTags.push('strong');
}
if (node.italic) {
textTags.push('i');
}
if (node.underline) {
textTags.push('u');
}
if (node.strikethrough) {
textTags.push('s');
}
if (textTags.length > 0) {
return recursivlyCreateText(createElement, textTags, node.text);
}
return node.text;
}
switch (node.type) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
return createElement(node.type, [recursive(node.children)]);
case 'quote':
return createElement('blockquote', [recursive(node.children)]);
case 'ul':
return createElement('ul', [recursive(node.children)]);
case 'ol':
return createElement('ol', [recursive(node.children)]);
case 'li':
return createElement('li', [recursive(node.children)]);
case 'link':
let url = '';
switch (node.linkType) {
case 'internal':
// TODO: Find url of internal link
break;
case 'custom':
url = node.url;
break;
}
return createElement('a', { attrs: { href: url, target: node.newTab ? '_blank' : null } }, [
recursive(node.children),
]);
default:
if (node.children?.length > 0) {
if (node.children.length === 1 && node.children[0].text === '') {
return createElement('br');
}
return createElement('p', [recursive(node.children)]);
}
return null;
}
});
};
export default defineComponent({
props: {
node: {
type: Array as PropType<any[]>,
required: true,
},
},
render: function (createElement) {
return createElement('div', serialize(createElement, this.node));
},
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I couldn't find anything on the internet to render the Rich Text values in NuxtJS.
So, created a simple SlateJS renderer for vue 3.
The implementation is from payload's documentation. You can extend this further.
https://gist.github.com/MinSomai/4f6cde69db926398b1cfaf4f8d7a083a
Beta Was this translation helpful? Give feedback.
All reactions