Skip to content

Commit

Permalink
feat: handle markdown-it plugins with options (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
greatislander committed Aug 17, 2023
1 parent 8665948 commit 931711a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 127 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ module.exports = function (config) {

## Usage

For any options passed to `eleventy-plugin-fluid` in the configurations described below, you can override the default
rather than merging with it by passing the option with `override:` as a prefix to the key. For example, to override the
default options for the `css` configuration block, you could do the following:

```diff
const fluidPlugin = require("eleventy-plugin-fluid");

module.exports = function (config) {
- config.addPlugin(fluidPlugin);
+ config.addPlugin(fluidPlugin, {
+ "override:css": {
+ // Your options here.
+ }
+ });
};
```

Note that if you don't override required defaults when using this method, your configuration will not be valid, so
proceed with caution if you are using this technique.

### Asset Handling

`eleventy-plugin-fluid` includes configuration for processing and bundling Sass and CSS files using [Sass](https://sass-lang.com)
Expand Down Expand Up @@ -413,14 +433,16 @@ registering `eleventy-plugin-fluid` as follows:

```diff
const fluidPlugin = require("eleventy-plugin-fluid");
+ const markdownItEmoji = require("markdown-it-emoji");

module.exports = function (config) {
- config.addPlugin(fluidPlugin);
+ config.addPlugin(fluidPlugin, {
+ markdown: {
+ plugins: [
+ markdownItEmoji
+ // The npm package name of the plugin, which must be installed in your project.
+ "markdown-it-emoji",
+ // The npm package name of the plugin, which must be installed in your project, and an options object for the plugin.
+ ["markdown-it-anchor", {}]
+ ]
+ }
+ });
Expand Down
6 changes: 6 additions & 0 deletions eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ https://github.com/fluid-project/eleventy-plugin-fluid/raw/main/LICENSE.md.
"use strict";

const fluidPlugin = require("./index.js");
const slugify = require("@sindresorhus/slugify");

const inputPath = "fixtures";

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(fluidPlugin, {
markdown: {
plugins: [
["markdown-it-anchor", { slugify: s => slugify(s) }]
]
},
sass: {
enabled: true
},
Expand Down
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ const languages = require("./src/config/languages.json");
const compileCss = require("./src/compilers/compile-css.js");
const compileSass = require("./src/compilers/compile-sass.js");
const compileJs = require("./src/compilers/compile-js.js");
const deepMerge = require("./src/utils/deep-merge.js");
const merge = require("@11ty/eleventy/src/Util/Merge.js");

module.exports = {
initArguments: {},
configFunction: function (eleventyConfig, options = {}) {
options = deepMerge({
options = merge({
uio: true,
markdown: {
options: {
Expand Down Expand Up @@ -118,8 +118,11 @@ module.exports = {

const md = new MarkdownIt(options.markdown.options);
options.markdown.plugins.forEach(plugin => {
if (plugin) {
if (typeof plugin === "string") {
md.use(require(plugin));
} else {
const [pluginPackage, options = {}] = plugin;
md.use(require(pluginPackage), options);
}
});

Expand Down Expand Up @@ -156,9 +159,12 @@ module.exports = {
/** Template Formats */
eleventyConfig.amendLibrary("md", md => {
md.set(options.markdown.options);
Object.values(options.markdown.plugins).forEach(plugin => {
if (plugin) {
options.markdown.plugins.forEach(plugin => {
if (typeof plugin === "string") {
md.use(require(plugin));
} else {
const [pluginPackage, options = {}] = plugin;
md.use(require(pluginPackage), options);
}
});
});
Expand Down
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"fluid-lint-all": "1.2.9",
"husky": "8.0.3",
"infusion": "4.6.0",
"markdown-it-anchor": "8.6.7",
"rimraf": "5.0.1"
},
"dependencies": {
Expand Down
65 changes: 0 additions & 65 deletions src/utils/deep-merge.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test("Builds minified JavaScript", async function (t) {

test("Renders Markdown via shortcode", async function (t) {
let testHtml = fs.readFileSync("_site/test.html", "utf8");
t.true(testHtml.includes("<h1>eleventy-plugin-fluid</h1>"));
t.true(testHtml.includes("<h1 id=\"eleventy-plugin-fluid\" tabindex=\"-1\">eleventy-plugin-fluid</h1>"));
});

test("Doesn't render unsupported template language via shortcode", async function (t) {
Expand Down
27 changes: 0 additions & 27 deletions test/deep-merge.test.js

This file was deleted.

39 changes: 39 additions & 0 deletions test/merge.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright the eleventy-plugin-fluid copyright holders.
See the AUTHORS.md file at the top-level directory of this distribution and at
https://github.com/fluid-project/eleventy-plugin-fluid/raw/main/AUTHORS.md.
Licensed under the New BSD license. You may not use this file except in compliance with this License.
You may obtain a copy of the New BSD License at
https://github.com/fluid-project/eleventy-plugin-fluid/raw/main/LICENSE.md.
*/
"use strict";

const test = require("ava");
const merge = require("@11ty/eleventy/src/Util/Merge.js");

test("Passing false replaces an original value", function (t) {
t.deepEqual(merge({browserslist: "> 2%"}, {browserslist: false}), {browserslist: false});
});

test("Passing key with override prefix overrides an original value", function (t) {
t.deepEqual(merge({browserslist: "> 2%"}, {"override:browserslist": "> 1%"}), {browserslist: "> 1%"});
});

test("Nested objects can be merged", function (t) {
t.deepEqual(merge({drafts: {nesting: true}}, {drafts: {customMedia: true}}), {drafts: {nesting: true, customMedia: true}});
});

test("Nested objects can be overriden", function (t) {
t.deepEqual(merge({drafts: {nesting: true}}, {"override:drafts": {customMedia: true}}), {drafts: {customMedia: true}});
});

test("Nested arrays can be merged", function (t) {
t.deepEqual(merge({browserslist: ["ie 8", "ie 9"]}, {browserslist: ["ie 7"]}), {browserslist: ["ie 8", "ie 9", "ie 7"]});
});

test("Nested arrays can be overridden", function (t) {
t.deepEqual(merge({browserslist: ["ie 8", "ie 9"]}, {"override:browserslist": ["ie 7"]}), {browserslist: ["ie 7"]});
});
27 changes: 0 additions & 27 deletions test/utils.test.js

This file was deleted.

0 comments on commit 931711a

Please sign in to comment.