|
1 | 1 | /**
|
2 |
| - * This is a modified version of the |
3 |
| - * [markdown-to-txt](https://www.npmjs.com/package/markdown-to-txt) library. |
| 2 | + * This is a function to convert markdown to txt based on markedjs v13+. |
| 3 | + * Copies the escape/unescape functions from [lodash](https://www.npmjs.com/package/lodash) instead import to reduce the size. |
4 | 4 | */
|
5 | 5 | import { marked } from 'marked';
|
6 |
| -import { escape, unescape } from 'lodash'; |
7 |
| -const block = text => text + '\n\n'; |
8 |
| -const escapeBlock = text => escape(text) + '\n\n'; |
9 |
| -const line = text => text + '\n'; |
10 |
| -const inline = text => text; |
11 |
| -const newline = () => '\n'; |
12 |
| -const empty = () => ''; |
13 |
| - |
14 |
| -const TxtRenderer = { |
15 |
| - // Block elements |
16 |
| - code: escapeBlock, |
17 |
| - blockquote: block, |
18 |
| - html: empty, |
19 |
| - heading: block, |
20 |
| - hr: newline, |
21 |
| - list: text => block(text.trim()), |
22 |
| - listitem: line, |
23 |
| - checkbox: empty, |
24 |
| - paragraph: block, |
25 |
| - table: (header, body) => line(header + body), |
26 |
| - tablerow: text => line(text.trim()), |
27 |
| - tablecell: text => text + ' ', |
28 |
| - // Inline elements |
29 |
| - strong: inline, |
30 |
| - em: inline, |
31 |
| - codespan: inline, |
32 |
| - br: newline, |
33 |
| - del: inline, |
34 |
| - link: (_0, _1, text) => text, |
35 |
| - image: (_0, _1, text) => text, |
36 |
| - text: inline, |
37 |
| - // etc. |
38 |
| - options: {}, |
| 6 | + |
| 7 | +const reEscapedHtml = /&(?:amp|lt|gt|quot|#(0+)?39);/g; |
| 8 | +const reHasEscapedHtml = RegExp(reEscapedHtml.source); |
| 9 | +const htmlUnescapes = { |
| 10 | + '&': '&', |
| 11 | + '<': '<', |
| 12 | + '>': '>', |
| 13 | + '"': '"', |
| 14 | + ''': "'", |
39 | 15 | };
|
40 | 16 |
|
41 |
| -/** |
42 |
| - * Converts markdown to plaintext using the marked Markdown library. |
43 |
| - * Accepts [MarkedOptions](https://marked.js.org/using_advanced#options) as |
44 |
| - * the second argument. |
45 |
| - * |
46 |
| - * NOTE: The output of markdownToTxt is NOT sanitized. The output may contain |
47 |
| - * valid HTML, JavaScript, etc. Be sure to sanitize if the output is intended |
48 |
| - * for web use. |
49 |
| - * |
50 |
| - * @param markdown the markdown text to txtify |
51 |
| - * @param options the marked options |
52 |
| - * @returns the unmarked text |
53 |
| - */ |
54 |
| -export function markdownToTxt(markdown, options) { |
55 |
| - const unmarked = marked(markdown, { ...options, renderer: TxtRenderer }); |
| 17 | +function unescape(string) { |
| 18 | + return string && reHasEscapedHtml.test(string) |
| 19 | + ? string.replace(reEscapedHtml, entity => htmlUnescapes[entity] || "'") |
| 20 | + : string || ''; |
| 21 | +} |
| 22 | + |
| 23 | +const reUnescapedHtml = /[&<>"']/g; |
| 24 | +const reHasUnescapedHtml = RegExp(reUnescapedHtml.source); |
| 25 | +const htmlEscapes = { |
| 26 | + '&': '&', |
| 27 | + '<': '<', |
| 28 | + '>': '>', |
| 29 | + '"': '"', |
| 30 | + "'": ''', |
| 31 | +}; |
| 32 | + |
| 33 | +function escape(string) { |
| 34 | + return string && reHasUnescapedHtml.test(string) |
| 35 | + ? string.replace(reUnescapedHtml, chr => htmlEscapes[chr]) |
| 36 | + : string || ''; |
| 37 | +} |
| 38 | + |
| 39 | +function helpersCleanup(string) { |
| 40 | + return string && string.replace('!>', '').replace('?>', ''); |
| 41 | +} |
| 42 | + |
| 43 | +const markdownToTxtRenderer = { |
| 44 | + space() { |
| 45 | + return ''; |
| 46 | + }, |
| 47 | + |
| 48 | + code({ text }) { |
| 49 | + const code = text.replace(/\n$/, ''); |
| 50 | + return escape(code); |
| 51 | + }, |
| 52 | + |
| 53 | + blockquote({ tokens }) { |
| 54 | + return this.parser?.parse(tokens) || ''; |
| 55 | + }, |
| 56 | + |
| 57 | + html() { |
| 58 | + return ''; |
| 59 | + }, |
| 60 | + |
| 61 | + heading({ tokens }) { |
| 62 | + return this.parser?.parse(tokens) || ''; |
| 63 | + }, |
| 64 | + |
| 65 | + hr() { |
| 66 | + return ''; |
| 67 | + }, |
| 68 | + |
| 69 | + list(token) { |
| 70 | + let body = ''; |
| 71 | + for (let j = 0; j < token.items.length; j++) { |
| 72 | + const item = token.items[j]; |
| 73 | + body += this.listitem?.(item); |
| 74 | + } |
| 75 | + |
| 76 | + return body; |
| 77 | + }, |
| 78 | + |
| 79 | + listitem(item) { |
| 80 | + let itemBody = ''; |
| 81 | + if (item.task) { |
| 82 | + const checkbox = this.checkbox?.({ checked: !!item.checked }); |
| 83 | + if (item.loose) { |
| 84 | + if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') { |
| 85 | + item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; |
| 86 | + if ( |
| 87 | + item.tokens[0].tokens && |
| 88 | + item.tokens[0].tokens.length > 0 && |
| 89 | + item.tokens[0].tokens[0].type === 'text' |
| 90 | + ) { |
| 91 | + item.tokens[0].tokens[0].text = |
| 92 | + checkbox + ' ' + item.tokens[0].tokens[0].text; |
| 93 | + } |
| 94 | + } else { |
| 95 | + item.tokens.unshift({ |
| 96 | + type: 'text', |
| 97 | + raw: checkbox + ' ', |
| 98 | + text: checkbox + ' ', |
| 99 | + }); |
| 100 | + } |
| 101 | + } else { |
| 102 | + itemBody += checkbox + ' '; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + itemBody += this.parser?.parse(item.tokens, !!item.loose); |
| 107 | + |
| 108 | + return `${itemBody || ''}`; |
| 109 | + }, |
| 110 | + |
| 111 | + checkbox() { |
| 112 | + return ''; |
| 113 | + }, |
| 114 | + |
| 115 | + paragraph({ tokens }) { |
| 116 | + return this.parser?.parseInline(tokens) || ''; |
| 117 | + }, |
| 118 | + |
| 119 | + table(token) { |
| 120 | + let header = ''; |
| 121 | + |
| 122 | + let cell = ''; |
| 123 | + for (let j = 0; j < token.header.length; j++) { |
| 124 | + cell += this.tablecell?.(token.header[j]); |
| 125 | + } |
| 126 | + header += this.tablerow?.({ text: cell }); |
| 127 | + |
| 128 | + let body = ''; |
| 129 | + for (let j = 0; j < token.rows.length; j++) { |
| 130 | + const row = token.rows[j]; |
| 131 | + |
| 132 | + cell = ''; |
| 133 | + for (let k = 0; k < row.length; k++) { |
| 134 | + cell += this.tablecell?.(row[k]); |
| 135 | + } |
| 136 | + |
| 137 | + body += this.tablerow?.({ text: cell }); |
| 138 | + } |
| 139 | + |
| 140 | + return header + ' ' + body; |
| 141 | + }, |
| 142 | + |
| 143 | + tablerow({ text }) { |
| 144 | + return text; |
| 145 | + }, |
| 146 | + |
| 147 | + tablecell(token) { |
| 148 | + return this.parser?.parseInline(token.tokens) || ''; |
| 149 | + }, |
| 150 | + |
| 151 | + strong({ text }) { |
| 152 | + return text; |
| 153 | + }, |
| 154 | + |
| 155 | + em({ tokens }) { |
| 156 | + return this.parser?.parseInline(tokens) || ''; |
| 157 | + }, |
| 158 | + |
| 159 | + codespan({ text }) { |
| 160 | + return text; |
| 161 | + }, |
| 162 | + |
| 163 | + br() { |
| 164 | + return ' '; |
| 165 | + }, |
| 166 | + |
| 167 | + del({ tokens }) { |
| 168 | + return this.parser?.parseInline(tokens); |
| 169 | + }, |
| 170 | + |
| 171 | + link({ tokens, href, title }) { |
| 172 | + // Remain the href and title attributes for searching, so is the image |
| 173 | + // e.g. [filename](_media/example.js ':include :type=code :fragment=demo') |
| 174 | + // Result: filename _media/example.js :include :type=code :fragment=demo |
| 175 | + return `${this.parser?.parseInline(tokens) || ''} ${href || ''} ${title || ''}`; |
| 176 | + }, |
| 177 | + |
| 178 | + image({ title, text, href }) { |
| 179 | + return `${text || ''} ${href || ''} ${title || ''}`; |
| 180 | + }, |
| 181 | + |
| 182 | + text(token) { |
| 183 | + return token.tokens |
| 184 | + ? this.parser?.parseInline(token.tokens) || '' |
| 185 | + : token.text || ''; |
| 186 | + }, |
| 187 | +}; |
| 188 | +const _marked = marked.setOptions({ renderer: markdownToTxtRenderer }); |
| 189 | + |
| 190 | +export function markdownToTxt(markdown) { |
| 191 | + const unmarked = _marked.parse(markdown); |
56 | 192 | const unescaped = unescape(unmarked);
|
57 |
| - const trimmed = unescaped.trim(); |
58 |
| - return trimmed; |
| 193 | + const helpersCleaned = helpersCleanup(unescaped); |
| 194 | + return helpersCleaned.trim(); |
59 | 195 | }
|
60 | 196 |
|
61 | 197 | export default markdownToTxt;
|
0 commit comments