Skip to content

Instantly share code, notes, and snippets.

@hanford
Created March 27, 2020 15:57
Show Gist options
  • Save hanford/58d8cbbe5f8e29527cbd07a9da7f559a to your computer and use it in GitHub Desktop.
Save hanford/58d8cbbe5f8e29527cbd07a9da7f559a to your computer and use it in GitHub Desktop.
export default function plugin(settings = {}) {
this.Compiler = compiler;
function compiler(node) {
return node.children.map(markdownToSlate);
}
}
function markdownToSlate(node) {
let children = [];
if (Array.isArray(node.children)) {
children = node.children.map(c => markdownToSlate({ ...c, ordered: node.ordered || false }));
}
switch (node.type) {
case 'heading':
return { type: depthToHeading[node.depth], children };
case 'list':
return {
type: node.ordered ? 'numbered-list' : 'bulleted-list',
children,
};
case 'listItem':
return { type: 'list-item', children };
case 'emphasis':
return { ...forceLeafNode(children), italic: true };
case 'strong':
return { ...forceLeafNode(children), bold: true };
case 'delete':
return { ...forceLeafNode(children), strikeThrough: true };
case 'paragraph':
return { type: node.type, children };
case 'link':
return { type: node.type, link: node.url, children };
case 'text':
default:
return { text: node.value };
}
}
const depthToHeading = {
1: 'heading-one',
2: 'heading-two',
3: 'heading-three',
4: 'heading-four',
5: 'heading-five',
6: 'heading-six',
};
const forceLeafNode = children => {
return { text: children[0].text };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment