Created
August 15, 2021 14:53
-
-
Save jpalala/36164b3864423095d06c73f87412c5e9 to your computer and use it in GitHub Desktop.
node - create html from markdown file (using showdowns)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const showdown = require('showdown'), | |
converter = new showdown.Converter(); | |
const fs = require('fs'); | |
const cwd= __dirname; | |
try { | |
// 1. Update the path to the markdown file | |
var text = fs.readFileSync('path/to/MARKDOWN_FILE.md', 'utf8'); | |
var html = converter.makeHtml(text.toString()); | |
// 2. change this title | |
const title = 'Example Title'; | |
let head = '<!doctype html><html><head><title>' + title + '</title>'; | |
head += '<meta charset="utf-8">'; | |
head += '<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="site.css">'; | |
head += '</head>'; | |
const htmlString = head + '<body>' + html + '</body>'; | |
// 3. Currently outputs to outpuit.html. You can change the console log or the output file. | |
fs.writeFile('output.html', htmlString, function (err) { | |
if (err) return console.log(err); | |
console.log("README > output.html \r\n . Use `cp output.html public/index.html` to copy to your public folder"); | |
}); | |
} catch(e) { | |
console.log('Error:', e.stack); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "your-project", | |
"version": "1.0.0", | |
"description": "builds the markdown into html", | |
"main": "build.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Jo Doe", | |
"license": "MIT", | |
"devDependencies": { | |
"showdown": "^1.9.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment