Skip to content

Instantly share code, notes, and snippets.

@jambonrose
Created June 29, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jambonrose/a896a108916b04cdccd5770947418f37 to your computer and use it in GitHub Desktop.
Save jambonrose/a896a108916b04cdccd5770947418f37 to your computer and use it in GitHub Desktop.
Script to use Turndown to convert HTML to Python-Markdown-compatible Markdown.
// 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