extension helper for commonjs projects to require markdown files
Created
August 10, 2019 11:49
-
-
Save NonPolynomial/453c4bd942da1c7dcd4be318cdfeed67 to your computer and use it in GitHub Desktop.
CommonJS Markdown Extension Helper
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 fs = require('fs'); | |
| const util = require('util'); | |
| const fm = require('front-matter'); | |
| const sd = require('showdown'); | |
| require.extensions['.md'] = (module, filename) => { | |
| const fileContents = fs.readFileSync(filename, 'utf8'); | |
| const result = fm(fileContents); | |
| const sdConverter = new sd.Converter(); | |
| result.html = sdConverter.makeHtml(result.body); | |
| Reflect.deleteProperty(result, 'frontmatter'); | |
| module.exports = result; | |
| }; |
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
| require('./extensions-helper'); | |
| const example = require('./example.md'); | |
| console.dir(example); |
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": "cjs-md-extension", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "start": "node ." | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "front-matter": "^3.0.2", | |
| "showdown": "^1.9.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment