Skip to content

Instantly share code, notes, and snippets.

@wizardforcel
Last active February 5, 2019 14:40
Show Gist options
  • Save wizardforcel/27e835679b6c3bec20ba308bf14df50f to your computer and use it in GitHub Desktop.
Save wizardforcel/27e835679b6c3bec20ba308bf14df50f to your computer and use it in GitHub Desktop.
split a md file & convert html to md
var fs = require('fs');
try {fs.mkdirSync('out');}
catch(ex){}
var co = fs.readFileSync('a.md', 'utf-8');
var files = co.split(/<split>123<\/split>/g);
var summary = '';
//var pre = "{% raw %}\n",
// sub = "\n{% endraw %}";
for(var i = 0; i < files.length; i++)
{
fs.writeFileSync('out/' + i + '.md', files[i], {encoding: 'utf-8'});
var rms = files[i].match(/^#+\x20(.+?)(?:\x20#+)?$/m);
var name = rms? rms[1]: "";
summary += '+ [' + name + '](' + i + '.md)\n';
}
fs.writeFileSync('out/SUMMARY.md', summary, {encoding: 'utf-8'});
var toMD = require('to-markdown');
var fs = require('fs');
var myConventors = require('./my-conventors');
var co = fs.readFileSync('a.html', 'utf-8')
.replace(/(<h1[^>]*>)/g, "<split>123</split>$1");
var tmpConv = [{
filter: function(n) {
var classes = n.className.split(' ');
return n.nodeName === 'SPAN' &&
classes.indexOf('math') != -1;
},
replacement: function (content) {
return '`' + content + '`';
}
},
{
filter: function(n) {
var classes = n.className.split(' ');
return n.nodeName === 'DIV' &&
classes.indexOf('math') != -1;
},
replacement: function (content) {
return '\n\n```\n' + content + '\n```\n\n';
}
}];
var md = toMD(co, {
gfm: true,
converters: tmpConv.concat(myConventors)
});
fs.writeFileSync('a.md', md, {encoding: 'utf-8'});
console.log('done...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment