Skip to content

Instantly share code, notes, and snippets.

@embarq
Last active June 18, 2016 23:17
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 embarq/347d04e86ebc539be48586c7de414304 to your computer and use it in GitHub Desktop.
Save embarq/347d04e86ebc539be48586c7de414304 to your computer and use it in GitHub Desktop.
HTML to MD by Node
"use strict";
var fs = require('fs');
var md = require('to-markdown');
var ic = require('iconv-lite');
var src = 'docs';
var dist = 'dist';
fs.readdir(src, (err, contents) => {
contents.forEach((file, i) => {
if (file.match('.html')) {
fs.readFile(`./${src}/${file}`, (err, data) => {
let dir = `./${dist}/part-${/[0-9]+/.exec((/№[0-9]+/.exec(file))[0])}/`;
// extract lab num from file<string>
let filename = file.replace('.html', '.md').replace(file.substring(0, file.indexOf('.html')), 'index');
fs.mkdirSync(dir);
fs.appendFile(`${dir}/${filename}`, md(ic.decode(new Buffer(data), "windows1251"), {gfm: true}));
})
} // if
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment