Created
March 27, 2020 15:56
-
-
Save hanford/3fb30423ed866e7718db79d39c77e1a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This was a lifesaver. Thank you for sharing it.