From fe05db6bdc387d47c1f850a20631017426d0a7ca Mon Sep 17 00:00:00 2001 From: Helen Chong <119173961+helenclx@users.noreply.github.com> Date: Tue, 7 May 2024 01:01:15 +0800 Subject: [PATCH] Make markdown configurations into a separate config file --- eleventy.config.js | 77 +++---------------------------------------- eleventy.config.md.js | 75 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 73 deletions(-) create mode 100644 eleventy.config.md.js diff --git a/eleventy.config.js b/eleventy.config.js index 5f3e1c0b..a9b11ca8 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -1,91 +1,22 @@ // Installed Plugins -const { EleventyRenderPlugin } = require("@11ty/eleventy"); const pluginRss = require("@11ty/eleventy-plugin-rss"); const eleventyNavigationPlugin = require("@11ty/eleventy-navigation"); const metagen = require('eleventy-plugin-metagen'); -const pluginTOC = require('eleventy-plugin-nesting-toc'); const emojiReadTime = require("@11tyrocks/eleventy-plugin-emoji-readtime"); -const embedEverything = require("eleventy-plugin-embed-everything"); - -// Configure slug filter -const slugify = require("slugify"); - -// Configure markdown-it plugins -const markdownIt = require("markdown-it"); -const markdownItAnchor = require("markdown-it-anchor"); module.exports = function (eleventyConfig) { // Copy files eleventyConfig.addPassthroughCopy("./src/assets/"); eleventyConfig.addWatchTarget("./src/assets/"); - // Installed Plug-ins - eleventyConfig.addPlugin(EleventyRenderPlugin); + // Custom Plugins + eleventyConfig.addPlugin(require('./eleventy.config.md.js')); + + // Installed Plugins eleventyConfig.addPlugin(pluginRss); eleventyConfig.addPlugin(eleventyNavigationPlugin); eleventyConfig.addPlugin(metagen); - eleventyConfig.addPlugin(pluginTOC, { tags: ['h2', 'h3', 'h4', 'h5'] }); eleventyConfig.addPlugin(emojiReadTime); - eleventyConfig.addPlugin(embedEverything, { add: ['soundcloud'] }); - - // Configure slug filter - eleventyConfig.addFilter("slug", (str) => { - if (!str) { - return; - } - - return slugify(str, { - lower: true, - remove: /["]/g, - }); - }); - - // Configure markdown-it plugins - eleventyConfig.setLibrary('md', markdownIt().use(markdownItAnchor)) - const linkAfterHeader = markdownItAnchor.permalink.linkAfterHeader({ - class: "heading-anchor", - symbol: "", - style: "aria-labelledby", - }); - const markdownItAnchorOptions = { - level: [1, 2, 3, 4, 5], - slugify: (str) => - slugify(str, { - lower: true, - strict: true, - remove: /["]/g, - }), - tabIndex: false, - permalink(slug, opts, state, idx) { - state.tokens.splice(idx, 0, - Object.assign(new state.Token("div_open", "div", 1), { - // Add class "header-wrapper [h1 or h2 or h3]" - attrs: [["class", `heading-wrapper ${state.tokens[idx].tag}`]], - block: true, - }) - ); - - state.tokens.splice(idx + 4, 0, - Object.assign(new state.Token("div_close", "div", -1), { - block: true, - }) - ); - - linkAfterHeader(slug, opts, state, idx + 1); - }, - }; - - /* Markdown Overrides */ - let markdownLibrary = markdownIt({ - html: true, - }) - .use(markdownItAnchor, markdownItAnchorOptions) - .use(require("markdown-it-attrs")) - .use(require("markdown-it-bracketed-spans")) - .use(require("markdown-it-deflist")); - - /* This is the part that tells 11ty to swap to our custom config */ - eleventyConfig.setLibrary("md", markdownLibrary); // Add content categories to a collection eleventyConfig.addCollection("categories", function(collectionApi) { diff --git a/eleventy.config.md.js b/eleventy.config.md.js new file mode 100644 index 00000000..faba95d0 --- /dev/null +++ b/eleventy.config.md.js @@ -0,0 +1,75 @@ +// Installed Plugins +const pluginTOC = require('eleventy-plugin-nesting-toc'); +const embedEverything = require("eleventy-plugin-embed-everything"); + +// Configure slug filter +const slugify = require("slugify"); + +// Configure markdown-it plugins +const markdownIt = require("markdown-it"); +const markdownItAnchor = require("markdown-it-anchor"); + +module.exports = function (eleventyConfig) { + // Installed Plugins + eleventyConfig.addPlugin(pluginTOC, { tags: ['h2', 'h3', 'h4', 'h5'] }); + eleventyConfig.addPlugin(embedEverything, { add: ['soundcloud'] }); + + // Configure slug filter + eleventyConfig.addFilter("slug", (str) => { + if (!str) { + return; + } + + return slugify(str, { + lower: true, + remove: /["]/g, + }); + }); + + // Configure markdown-it plugins + eleventyConfig.setLibrary('md', markdownIt().use(markdownItAnchor)) + const linkAfterHeader = markdownItAnchor.permalink.linkAfterHeader({ + class: "heading-anchor", + symbol: "", + style: "aria-labelledby", + }); + const markdownItAnchorOptions = { + level: [1, 2, 3, 4, 5], + slugify: (str) => + slugify(str, { + lower: true, + strict: true, + remove: /["]/g, + }), + tabIndex: false, + permalink(slug, opts, state, idx) { + state.tokens.splice(idx, 0, + Object.assign(new state.Token("div_open", "div", 1), { + // Add class "header-wrapper [h1 or h2 or h3]" + attrs: [["class", `heading-wrapper ${state.tokens[idx].tag}`]], + block: true, + }) + ); + + state.tokens.splice(idx + 4, 0, + Object.assign(new state.Token("div_close", "div", -1), { + block: true, + }) + ); + + linkAfterHeader(slug, opts, state, idx + 1); + }, + }; + + /* Markdown Overrides */ + let markdownLibrary = markdownIt({ + html: true, + }) + .use(markdownItAnchor, markdownItAnchorOptions) + .use(require("markdown-it-attrs")) + .use(require("markdown-it-bracketed-spans")) + .use(require("markdown-it-deflist")); + + /* This is the part that tells 11ty to swap to our custom config */ + eleventyConfig.setLibrary("md", markdownLibrary); +}