Last active
January 31, 2026 23:15
-
-
Save blewert/a9bd4f382c5d96ca0bc22ddc2dfc2c72 to your computer and use it in GitHub Desktop.
A minimal example of using marked with gulp.js without the need for a separate package to do it for you.
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
| /* | |
| This is free and unencumbered software released into the public domain. | |
| Anyone is free to copy, modify, publish, use, compile, sell, or | |
| distribute this software, either in source code form or as a compiled | |
| binary, for any purpose, commercial or non-commercial, and by any | |
| means. | |
| In jurisdictions that recognize copyright laws, the author or authors | |
| of this software dedicate any and all copyright interest in the | |
| software to the public domain. We make this dedication for the benefit | |
| of the public at large and to the detriment of our heirs and | |
| successors. We intend this dedication to be an overt act of | |
| relinquishment in perpetuity of all present and future rights to this | |
| software under copyright law. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
| OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
| ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| OTHER DEALINGS IN THE SOFTWARE. | |
| For more information, please refer to <https://unlicense.org> | |
| */ | |
| /** | |
| * This is a minimal example which doesn't use an external package | |
| * to convert markdown code to HTML using marked, with gulp. | |
| * | |
| * I wrote this as: | |
| * a) there is very little documentation on how to do so. | |
| * b) downloading a package to do this is ridiculous given that it just calls one function. | |
| * c) pure frustration. | |
| * | |
| * The meat & potatoes are in markedToHTML. Hope it helps. | |
| */ | |
| /** | |
| * This is a minimal example which doesn't use an external package | |
| * to convert markdown code to HTML using marked, with gulp. | |
| * | |
| * I wrote this as: | |
| * a) there is very little documentation on how to do so. | |
| * b) downloading a package to do this is ridiculous given that it just calls one function. | |
| * c) pure frustration. | |
| * | |
| * The meat & potatoes are in markedToHTML. Hope it helps. | |
| */ | |
| const { watch, src, dest } = require("gulp"); | |
| const { marked } = require("marked"); | |
| const Path = require("path"); | |
| var Transform = require("stream").Transform; | |
| function markedToHTML(options) | |
| { | |
| //See <https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/README.md> for | |
| //more guidance on what is going on here. | |
| var transformStream = new Transform({ objectMode: true }); | |
| transformStream._transform = function(file, encoding, callback) | |
| { | |
| var error = null; | |
| var output = file; | |
| //Get the contents decoded to a string we can use | |
| const content = file.contents.toString(encoding); | |
| //Parse it from MD to HTML | |
| const html = marked.parse(content, options); | |
| //Rename from .md to .html.. without gulp-rename | |
| let filename = Path.basename(file.path, ".md"); | |
| file.path = Path.join(Path.dirname(file.path), `${filename}.html`); | |
| //Set contents: output must be a File, not a string | |
| output.contents = Buffer.from(html, encoding); | |
| callback(error, output); | |
| } | |
| return transformStream; | |
| } | |
| function compileMD(cb) | |
| { | |
| src("*.md") | |
| .pipe(markedToHTML()) | |
| .pipe(dest("dist/")); | |
| } | |
| exports.default = function() | |
| { | |
| watch("*.md", compileMD); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment