diff options
| author | Shipwreckt <me@shipwreckt.co.uk> | 2025-10-31 20:02:14 +0000 |
|---|---|---|
| committer | Shipwreckt <me@shipwreckt.co.uk> | 2025-10-31 20:02:14 +0000 |
| commit | 7a52ddeba2a68388b544f529d2d92104420f77b0 (patch) | |
| tree | 15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/@11ty/eleventy/src/Engines/Markdown.js | |
| parent | 53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff) | |
Changed from static to 11ty!
Diffstat (limited to 'node_modules/@11ty/eleventy/src/Engines/Markdown.js')
| -rw-r--r-- | node_modules/@11ty/eleventy/src/Engines/Markdown.js | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/Engines/Markdown.js b/node_modules/@11ty/eleventy/src/Engines/Markdown.js new file mode 100644 index 0000000..ec1e1f6 --- /dev/null +++ b/node_modules/@11ty/eleventy/src/Engines/Markdown.js @@ -0,0 +1,100 @@ +import markdownIt from "markdown-it"; + +import TemplateEngine from "./TemplateEngine.js"; + +export default class Markdown extends TemplateEngine { + constructor(name, eleventyConfig) { + super(name, eleventyConfig); + + this.markdownOptions = {}; + + this.setLibrary(this.config.libraryOverrides.md); + } + + get cacheable() { + return true; + } + + setLibrary(mdLib) { + this.mdLib = mdLib || markdownIt(this.getMarkdownOptions()); + + // Overrides a highlighter set in `markdownOptions` + // This is separate so devs can pass in a new mdLib and still use the official eleventy plugin for markdown highlighting + if (this.config.markdownHighlighter && typeof this.mdLib.set === "function") { + this.mdLib.set({ + highlight: this.config.markdownHighlighter, + }); + } + + if (typeof this.mdLib.disable === "function") { + // Disable indented code blocks by default (Issue #2438) + this.mdLib.disable("code"); + } + + this.setEngineLib(this.mdLib, Boolean(this.config.libraryOverrides.md)); + } + + setMarkdownOptions(options) { + this.markdownOptions = options; + } + + getMarkdownOptions() { + // work with "mode" presets https://github.com/markdown-it/markdown-it#init-with-presets-and-options + if (typeof this.markdownOptions === "string") { + return this.markdownOptions; + } + + return Object.assign( + { + html: true, + }, + this.markdownOptions || {}, + ); + } + + // TODO use preTemplateEngine to help inform this + // needsCompilation() { + // return super.needsCompilation(); + // } + + async #getPreEngine(preTemplateEngine) { + if (typeof preTemplateEngine === "string") { + return this.engineManager.getEngine(preTemplateEngine, this.extensionMap); + } + + return preTemplateEngine; + } + + async compile(str, inputPath, preTemplateEngine, bypassMarkdown) { + let mdlib = this.mdLib; + + if (preTemplateEngine) { + let engine = await this.#getPreEngine(preTemplateEngine); + let fnReady = engine.compile(str, inputPath); + + if (bypassMarkdown) { + return async function (data) { + let fn = await fnReady; + return fn(data); + }; + } else { + return async function (data) { + let fn = await fnReady; + let preTemplateEngineRender = await fn(data); + let finishedRender = mdlib.render(preTemplateEngineRender, data); + return finishedRender; + }; + } + } else { + if (bypassMarkdown) { + return function () { + return str; + }; + } else { + return function (data) { + return mdlib.render(str, data); + }; + } + } + } +} |
