Skip to content

Instantly share code, notes, and snippets.

@Merovex
Forked from hanford/slate-to-markdown.js
Created March 28, 2020 16:00
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 Merovex/5753f1f1a75bdbac2b4c362cc1ff4158 to your computer and use it in GitHub Desktop.
Save Merovex/5753f1f1a75bdbac2b4c362cc1ff4158 to your computer and use it in GitHub Desktop.
export default function parseToMarkdown(chunk) {
let children = !chunk.type
? chunk.text
: chunk.children.map(c => parseToMarkdown({ ...c, parentType: chunk.type })).join('');
if (children === '') return;
// formatting
if (chunk.bold) {
children = `**${children.trim()}** `;
}
if (chunk.italic) {
children = `_${children.trim()}_ `;
}
if (chunk.strikeThrough) {
children = `~~${children.trim()}~~ `;
}
// node type
switch (chunk.type) {
case 'heading-one':
return `# ${children} \n`;
case 'heading-two':
return `## ${children} \n`;
case 'link':
return `[${children}](${chunk.url})`;
case 'numbered-list':
case 'bulleted-list':
return `\n${children}\n`;
case 'list-item':
return `${chunk.parentType === 'numbered-list' ? '1.' : '-'} ${children} \n`;
case 'paragraph':
return `${children}\n`;
default:
return children;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment