-
Notifications
You must be signed in to change notification settings - Fork 23
/
url_to_markdown_formatters.js
executable file
·38 lines (37 loc) · 1.41 KB
/
url_to_markdown_formatters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const table_to_markdown = require('./html_table_to_markdown.js');
const htmlEntities = require('html-entities');
module.exports = {
format_tables: function (html, replacements) {
const start = replacements.length;
const tables = html.match(/(<table[^>]*>(?:.|\n)*?<\/table>)/gi);
if (tables) {
for (let t=0;t<tables.length;t++) {
const table = tables[t];
let markdown = table_to_markdown.convert(table);
let placeholder = "urltomarkdowntableplaceholder"+t+Math.random();
replacements[start+t] = { placeholder: placeholder, replacement: markdown};
html = html.replace(table, "<p>"+placeholder+"</p>");
}
}
return html;
},
format_codeblocks: function (html, replacements) {
const start = replacements.length;
const codeblocks = html.match(/(<pre[^>]*>(?:.|\n)*?<\/pre>)/gi);
if (codeblocks) {
for (let c=0;c<codeblocks.length;c++) {
const codeblock = codeblocks[c];
let filtered = codeblock;
filtered = filtered.replace(/<br[^>]*>/g, "\n");
filtered = filtered.replace(/<p>/g, "\n");
filtered = filtered.replace(/<\/?[^>]+(>|$)/g, "");
filtered = htmlEntities.decode(filtered);
let markdown = "```\n"+filtered+"\n```\n";
let placeholder = "urltomarkdowncodeblockplaceholder"+c+Math.random();
replacements[start+c] = { placeholder: placeholder, replacement: markdown};
html = html.replace(codeblock, "<p>"+placeholder+"</p>");
}
}
return html;
}
}