|
| 1 | +import { |
| 2 | + ELEMENT_BLOCKQUOTE, |
| 3 | + ELEMENT_CODE_BLOCK, |
| 4 | + ELEMENT_H1, |
| 5 | + ELEMENT_H2, |
| 6 | + ELEMENT_H3, |
| 7 | + ELEMENT_H4, |
| 8 | + ELEMENT_H5, |
| 9 | + ELEMENT_H6, |
| 10 | + ELEMENT_HR, |
| 11 | + // ELEMENT_INDENT, |
| 12 | + ELEMENT_LI, |
| 13 | + ELEMENT_LINK, |
| 14 | + ELEMENT_MEDIA_EMBED, |
| 15 | + ELEMENT_OL, |
| 16 | + ELEMENT_PARAGRAPH, |
| 17 | + ELEMENT_SECTION_SEPARATOR, |
| 18 | + ELEMENT_TAG, |
| 19 | + // ELEMENT_TABLE_CELL, |
| 20 | + // ELEMENT_TABLE_PRE, |
| 21 | + // ELEMENT_TABLE_ROW, |
| 22 | + // ELEMENT_TABLE_SUF, |
| 23 | + // ELEMENT_TABLE_WRAP, |
| 24 | + // ELEMENT_TASK_LIST, |
| 25 | + ELEMENT_TODO_LI, |
| 26 | + ELEMENT_UL, |
| 27 | + SECTION_SEPARATOR, |
| 28 | + SLIDE_SEPARATOR |
| 29 | +} from '@mexit/core' |
| 30 | + |
| 31 | +const LIST_TYPES = [ |
| 32 | + ELEMENT_H1, |
| 33 | + ELEMENT_H2, |
| 34 | + ELEMENT_H3, |
| 35 | + ELEMENT_H4, |
| 36 | + ELEMENT_H5, |
| 37 | + ELEMENT_H6, |
| 38 | + ELEMENT_HR, |
| 39 | + // ELEMENT_INDENT, |
| 40 | + ELEMENT_LI, |
| 41 | + ELEMENT_SECTION_SEPARATOR, |
| 42 | + ELEMENT_LINK, |
| 43 | + ELEMENT_OL, |
| 44 | + ELEMENT_PARAGRAPH, |
| 45 | + // ELEMENT_TABLE_CELL, |
| 46 | + // ELEMENT_TABLE_PRE, |
| 47 | + // ELEMENT_TABLE_ROW, |
| 48 | + // ELEMENT_TABLE_SUF, |
| 49 | + // ELEMENT_TABLE_WRAP, |
| 50 | + // ELEMENT_TASK_LIST, |
| 51 | + ELEMENT_UL |
| 52 | +] |
| 53 | + |
| 54 | +type LeafType = { |
| 55 | + text: string |
| 56 | + strikeThrough?: boolean |
| 57 | + bold?: boolean |
| 58 | + italic?: boolean |
| 59 | +} |
| 60 | + |
| 61 | +type BlockType = { |
| 62 | + type: string |
| 63 | + parentType?: string |
| 64 | + link?: string |
| 65 | + children: Array<any> |
| 66 | +} |
| 67 | + |
| 68 | +export default function parseToMarkdown(chunk: any, ignoreParagraphNewline = false, listDepth = 0) { |
| 69 | + const text = chunk.text || '' |
| 70 | + let type = chunk.type || '' |
| 71 | + |
| 72 | + let children = |
| 73 | + type && chunk.children |
| 74 | + ? // if we have a type, we're a BlockType element which _always_ has a children array. |
| 75 | + // $FlowFixMe |
| 76 | + chunk.children |
| 77 | + ?.map((c) => { |
| 78 | + const isTag = type === ELEMENT_TAG |
| 79 | + if (isTag) return chunk.value |
| 80 | + const isList = LIST_TYPES.includes(c.type || '') |
| 81 | + const selfIsList = LIST_TYPES.includes(chunk.type || '') |
| 82 | + const childrenHasLink = |
| 83 | + Array.isArray(chunk.children) && chunk.children.some((f) => f.type && f.type === 'link') |
| 84 | + |
| 85 | + return parseToMarkdown( |
| 86 | + { ...c, parentType: type }, |
| 87 | + // WOAH. |
| 88 | + // what we're doing here is pretty tricky, it relates to the block below where |
| 89 | + // we check for ignoreParagraphNewline and set type to paragraph. |
| 90 | + // We want to strip out empty paragraphs sometimes, but other times we don't. |
| 91 | + // If we're the descendant of a list, we know we don't want a bunch |
| 92 | + // of whitespace. If we're parallel to a link we also don't want |
| 93 | + // to respect neighboring paraphs |
| 94 | + ignoreParagraphNewline || isList || selfIsList || childrenHasLink, |
| 95 | + // track depth of nested lists so we can add proper spacing |
| 96 | + LIST_TYPES.includes(c.type || '') ? listDepth + 1 : listDepth |
| 97 | + ) |
| 98 | + }) |
| 99 | + .join('') |
| 100 | + : text |
| 101 | + |
| 102 | + // This is pretty fragile code, check the long comment where we iterate over children |
| 103 | + if (!ignoreParagraphNewline && chunk.text === '' && chunk.parentType === ELEMENT_PARAGRAPH) { |
| 104 | + type = ELEMENT_PARAGRAPH |
| 105 | + children = '<br>' |
| 106 | + } |
| 107 | + |
| 108 | + if (children === '' && type !== ELEMENT_HR && type !== ELEMENT_SECTION_SEPARATOR) return |
| 109 | + |
| 110 | + if (chunk.bold) { |
| 111 | + children = retainWhitespaceAndFormat(children, '**') |
| 112 | + } |
| 113 | + |
| 114 | + if (chunk.italic) { |
| 115 | + children = retainWhitespaceAndFormat(children, '_') |
| 116 | + } |
| 117 | + |
| 118 | + if (chunk.strikeThrough) { |
| 119 | + children = `~~${children}~~` |
| 120 | + } |
| 121 | + |
| 122 | + switch (type) { |
| 123 | + case ELEMENT_H1: |
| 124 | + return `# ${children}\n` |
| 125 | + case ELEMENT_H2: |
| 126 | + return `## ${children}\n` |
| 127 | + case ELEMENT_H3: |
| 128 | + return `### ${children}\n` |
| 129 | + case ELEMENT_H4: |
| 130 | + return `#### ${children}\n` |
| 131 | + case ELEMENT_H5: |
| 132 | + return `##### ${children}\n` |
| 133 | + case ELEMENT_H6: |
| 134 | + return `###### ${children}\n` |
| 135 | + case ELEMENT_HR: |
| 136 | + return `\n${SLIDE_SEPARATOR}\n` |
| 137 | + case ELEMENT_SECTION_SEPARATOR: |
| 138 | + return `\n${SECTION_SEPARATOR}\n` |
| 139 | + case ELEMENT_MEDIA_EMBED: |
| 140 | + return `[${children}](${chunk.link || ''})` |
| 141 | + case ELEMENT_BLOCKQUOTE: |
| 142 | + // For some reason, marked is parsing blockquotes w/ one new line |
| 143 | + // as contiued blockquotes, so adding two new lines ensures that doesn't |
| 144 | + // happen |
| 145 | + return `> ${children}\n\n` |
| 146 | + case ELEMENT_CODE_BLOCK: |
| 147 | + return ` |
| 148 | +<div> |
| 149 | +\`\`\` |
| 150 | +${children}\`\`\` |
| 151 | +</div> |
| 152 | + ` |
| 153 | + case 'link': |
| 154 | + case 'a': |
| 155 | + return `[${children}](${chunk.link || ''})` |
| 156 | + case ELEMENT_TAG: |
| 157 | + return `\`#${children}\`` |
| 158 | + case ELEMENT_UL: |
| 159 | + case ELEMENT_OL: |
| 160 | + return `${children}` |
| 161 | + case ELEMENT_TODO_LI: |
| 162 | + return `\n[ ] ${children}` |
| 163 | + case ELEMENT_LI: { |
| 164 | + // $FlowFixMe // We're not a LeafType here and flow doesn't get that |
| 165 | + const isOL = chunk && chunk.parentType === ELEMENT_OL |
| 166 | + |
| 167 | + let spacer = '' |
| 168 | + for (let k = 1; listDepth > k; k++) { |
| 169 | + if (isOL) { |
| 170 | + // https://github.com/remarkjs/remark-react/issues/65 |
| 171 | + spacer += ' ' |
| 172 | + } else { |
| 173 | + spacer += ' ' |
| 174 | + } |
| 175 | + } |
| 176 | + return `${spacer}${isOL ? '1.' : '-'} ${children}` |
| 177 | + } |
| 178 | + case ELEMENT_PARAGRAPH: |
| 179 | + return `${children}\n` |
| 180 | + default: |
| 181 | + return `${children}\n` |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// This function handles the case of a string like this: " foo " |
| 186 | +// Where it would be invalid markdown to generate this: "** foo **" |
| 187 | +// We instead, want to trim the whitespace out, apply formatting, and then |
| 188 | +// bring the whitespace back. So our returned string looks like this: " **foo** " |
| 189 | +function retainWhitespaceAndFormat(string: string, format: string) { |
| 190 | + const left = string.trimLeft() |
| 191 | + const right = string.trimRight() |
| 192 | + let children = string.trim() |
| 193 | + |
| 194 | + const fullFormat = `${format}${children}${format}` |
| 195 | + |
| 196 | + if (children.length === string.length) { |
| 197 | + return fullFormat |
| 198 | + } |
| 199 | + |
| 200 | + const leftFormat = `${format}${children}` |
| 201 | + |
| 202 | + if (left.length !== string.length) { |
| 203 | + const diff = string.length - left.length |
| 204 | + children = `${new Array(diff + 1).join(' ')}${leftFormat}` |
| 205 | + } else { |
| 206 | + children = leftFormat |
| 207 | + } |
| 208 | + |
| 209 | + const rightFormat = `${children}${format}` |
| 210 | + |
| 211 | + if (right.length !== string.length) { |
| 212 | + const diff = string.length - right.length |
| 213 | + children = `${rightFormat}${new Array(diff + 1).join(' ')}` |
| 214 | + } else { |
| 215 | + children = rightFormat |
| 216 | + } |
| 217 | + |
| 218 | + return children |
| 219 | +} |
0 commit comments