Created
June 29, 2019 18:56
-
-
Save jambonrose/a896a108916b04cdccd5770947418f37 to your computer and use it in GitHub Desktop.
Script to use Turndown to convert HTML to Python-Markdown-compatible Markdown.
This file contains 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
// npm install turndown turndown-plugin-gfm | |
var fs = require("fs"); | |
var TurndownService = require("turndown"); | |
var turndownService = new TurndownService({ | |
headingStyle: "atx", | |
bulletListMarker: "-", | |
codeBlockStyle: "indent" | |
// linkStyle: "referenced", | |
// linkReferenceStyle: "shortcut" | |
}); | |
turndownService.addRule("python-markdown-fence-code", { | |
filter: function(node, options) { | |
return options.codeBlockStyle === "fenced" && node.nodeName === "PRE"; | |
}, | |
replacement: function(content, node, options) { | |
var className = node.firstChild.className || ""; | |
var language = (className.match(/language-(\S+)/) || [null, ""])[1]; | |
return ( | |
"\n\n" + | |
options.fence + | |
language + | |
"\n" + | |
node.textContent + | |
// "\n" + | |
options.fence + | |
"\n\n" | |
); | |
} | |
}); | |
turndownService.addRule("python-markdown-indent-code", { | |
filter: function(node, options) { | |
return options.codeBlockStyle === "indent" && node.nodeName === "PRE"; | |
}, | |
replacement: function(content, node, options) { | |
return "\n\n " + node.textContent.replace(/\n/g, "\n "); | |
} | |
}); | |
var contents = fs.readFileSync("index.html", "utf8"); | |
var markdown = turndownService.turndown(contents); | |
fs.writeFileSync("article.md", markdown); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment