Skip to content

Instantly share code, notes, and snippets.

@ntotten
Created July 9, 2016 20:26
Show Gist options
  • Save ntotten/918800a35e88b16ce3401f29ae33d8de to your computer and use it in GitHub Desktop.
Save ntotten/918800a35e88b16ce3401f29ae33d8de to your computer and use it in GitHub Desktop.
var lsr = require('lsr');
var path = require('path');
var fs = require('fs');
var toMarkdown = require('to-markdown');
var striptags = require('striptags');
var mkdirp = require('mkdirp');
var matter = require('gray-matter');
var toTitleCase = require('titlecase');
var beautifyHtml = require('js-beautify').html;
var files = [];
const outputFolder = 'output'
function tryGetTitle(content) {
var regex = /\#{1}[^\n\#]+/g;
var match = content.match(regex);
if (match && match.length) match = match[0].slice(1).trim();
return match;
}
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
deleteFolderRecursive(path.join(__dirname, outputFolder));
function loadPath(loadPath) {
var resolvedPath = path.resolve(__dirname, loadPath);
lsr
.sync(resolvedPath)
.forEach(function(fileStat) {
var filename = fileStat.path;
var filepath = path.resolve(resolvedPath, filename);
if (/\.c\.html$/.test(filename)) {
return;
}
if (!/\.html$/.test(filename)) return;
var text = fs.readFileSync(filepath, 'utf8');
files.push({
filename: path.join('/', loadPath, filename),
text: text
});
});
}
[
'./www.digitechbranding.com/pages',
'./www.digitechbranding.com/instructions',
'./www.dynasend.com/pages'
].map(loadPath);
files.forEach(function(file) {
var x = file.text.indexOf('<!--START PAGE CONTENT-->');
var y = file.text.indexOf('<!--END PAGE CONTENT-->');
var z = 25;
if (x < 0 || y < 0) {
x = file.text.indexOf('<body>');
y = file.text.indexOf('</body>');
z = 6;
}
var text;
if (x > -1 && y > -1) {
text = file.text.substring(x + z, y).trim();
} else {
text = file.text;
}
if (!text) return;
var md = striptags(toMarkdown(text));
md = md.replace(/\n\s*\n\s*\n/g, '\n\n'); // strip extra line breaks
md = md.replace(/(\d)\\\./g, '$1.'); // fix lists
var title = tryGetTitle(md);
if (!title) {
title = path.basename(file.filename, '.html').replace(/\-/g, ' ');
}
title = toTitleCase(title);
var parts = file.filename.split('/');
parts.shift();
parts[0] = '';
var redirectPath = parts.join('/');
var options = {
layout: 'page',
title: title,
redirect_from: [
redirectPath
]
}
var outputPath = path.join(__dirname, outputFolder, file.filename.toLowerCase());
mkdirp.sync(path.dirname(outputPath));
var html = beautifyHtml(text);
html = html.replace(/\<p\>\&nbsp\;\<\/p\>/g, ''); // strip empty paragraphs
html = html.replace(/\n\s*\n\s*\n/g, '\n\n'); // strip extra line breaks
fs.writeFileSync(outputPath, matter.stringify(html, options));
if (md) {
fs.writeFileSync(outputPath.replace('.html', '.md'), matter.stringify(md, options));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment