Skip to content

Instantly share code, notes, and snippets.

@hanford
Created March 27, 2020 15:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hanford/3fb30423ed866e7718db79d39c77e1a5 to your computer and use it in GitHub Desktop.
Save hanford/3fb30423ed866e7718db79d39c77e1a5 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;
}
}
@siawyoung
Copy link

This was a lifesaver. Thank you for sharing it.

@siawyoung
Copy link

Also send my regards to Stephen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment