Skip to content

Instantly share code, notes, and snippets.

@blenderous
Last active November 29, 2016 14:08
Show Gist options
  • Save blenderous/d69fb9430a63998f5c0a47590ea953ad to your computer and use it in GitHub Desktop.
Save blenderous/d69fb9430a63998f5c0a47590ea953ad to your computer and use it in GitHub Desktop.
Node module that takes markdown file as input and returns html into the log after inserting links on headings
// includes
var marked = require('marked');
var fs = require('fs');
// show me the code!
var s,
markdownShowdown = {
settings : {
//
},
init : function() {
s = this.settings;
s.generateHtml = this.generateHtml;
this.readFile('raw-content/example-page.md')
.then(function(contents){
console.log(s.generateHtml(contents));
}, function(reject){
console.error(reject);
});
},
readFile : function (fileName, encoding) {
var setEncoding = encoding ? encoding : 'utf8';
return new Promise(function(resolve, reject) {
fs.readFile(fileName, setEncoding,
function(error, contents){
if (error) {
reject(error);
}
resolve(contents);
}
);
});
},
generateHtml : function (inputContent) {
var renderer = new marked.Renderer();
renderer.heading = function (text, level) {;
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
return '<h' + level + '> ' +
'<a name="' + escapedText + '" class="anchor" href="#' + escapedText + '">' +
text +
'</a>' +
'</h' + level + '>';
}
return marked(inputContent);
}
};
// get set, go!
(function(){
markdownShowdown.init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment