Skip to content

Commit

Permalink
fix: Check for transforms before applying them
Browse files Browse the repository at this point in the history
Also include JSDoc definitions
  • Loading branch information
NickColley committed Oct 28, 2022
1 parent f176080 commit c991d71
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 53 deletions.
4 changes: 4 additions & 0 deletions .eleventy.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const plugin = require("./src/index.cjs");

/**
* @param {Object} eleventyConfig
* @param {Object|Array.<String>} pluginOptions
*/
module.exports = function EleventyPluginUnified(eleventyConfig, pluginOptions) {
if (pluginOptions instanceof Array) {
pluginOptions = { markdownTransforms: pluginOptions };
Expand Down
5 changes: 5 additions & 0 deletions src/import-plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { join, parse } from "node:path";

/**
* @param {Array.<String>} plugins
* @param {String} [transformsDirectory]
* @returns {Array} [plugin, options]
*/
export default async function importPlugins(
plugins = [],
transformsDirectory = "."
Expand Down
71 changes: 41 additions & 30 deletions src/index.cjs
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
/**
* @param {Object} eleventyConfig
* @param {Object} pluginOptions
* @param {Array.<String>} pluginOptions.markdownTransforms
* @param {Array.<String>} [pluginOptions.htmlTransforms]
* @param {String} [pluginOptions.transformsDirectory]
*/
function EleventyUnifiedPlugin(
eleventyConfig,
{ markdownTransforms, htmlTransforms, transformsDirectory } = {}
) {
eleventyConfig.setLibrary("md", {
disable: () => {}, // Broken in 2.0.0 canary https://github.com/11ty/eleventy/issues/2613
render: async (content, pageContext) => {
const { default: render } = await import("./remark.js");
return render(content, {
transformsDirectory,
markdownTransforms,
pageContext,
eleventyConfig,
});
},
});
eleventyConfig.addTransform(
"eleventy-plugin-unified",
async function (content) {
const pageContext = this;
if (
!pageContext.outputPath ||
!pageContext.outputPath.endsWith(".html")
) {
return content;
if (markdownTransforms instanceof Array && markdownTransforms.length) {
eleventyConfig.setLibrary("md", {
disable: () => {}, // Broken in 2.0.0 canary https://github.com/11ty/eleventy/issues/2613
render: async (content, pageContext) => {
const { default: render } = await import("./remark.js");
return render(content, {
transformsDirectory,
markdownTransforms,
pageContext,
eleventyConfig,
});
},
});
}
if (htmlTransforms instanceof Array && htmlTransforms.length) {
eleventyConfig.addTransform(
"eleventy-plugin-unified",
async function (content) {
const pageContext = this;
if (
!pageContext.outputPath ||
!pageContext.outputPath.endsWith(".html")
) {
return content;
}
const { default: render } = await import("./rehype.js");
return render(content, {
htmlTransforms,
transformsDirectory,
pageContext,
eleventyConfig,
});
}
const { default: render } = await import("./rehype.js");
return render(content, {
htmlTransforms,
transformsDirectory,
pageContext,
eleventyConfig,
});
}
);
);
}
}

module.exports = EleventyUnifiedPlugin;
11 changes: 10 additions & 1 deletion src/rehype.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import parse from "rehype-parse";
import stringify from "rehype-stringify";
import importPlugins from "./import-plugins.js";

export default async function (
/**
* @param {String} html html
* @param {Object} pluginOptions
* @param {Array.<String>} [pluginOptions.htmlTransforms]
* @param {String} [pluginOptions.transformsDirectory]
* @param {Object} [pluginOptions.pageContext]
* @param {Object} [pluginOptions.eleventyConfig]
* @returns {String} html
*/
export default async function rehype(
html,
{ htmlTransforms, transformsDirectory, pageContext, eleventyConfig } = {}
) {
Expand Down
11 changes: 10 additions & 1 deletion src/remark.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import stringify from "rehype-stringify";

import importPlugins from "./import-plugins.js";

export default async function (
/**
* @param {String} markdown markdown
* @param {Object} pluginOptions
* @param {Array.<String>} [pluginOptions.markdownTransforms]
* @param {String} [pluginOptions.transformsDirectory]
* @param {Object} [pluginOptions.pageContext]
* @param {Object} [pluginOptions.eleventyConfig]
* @returns {String} html
*/
export default async function remark(
markdown,
{ markdownTransforms, transformsDirectory, pageContext, eleventyConfig } = {}
) {
Expand Down
25 changes: 4 additions & 21 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,15 @@ test("is a function", (assert) => {
assert.is(typeof index, "function");
});

test("calls eleventy functions", (assert) => {
assert.plan(2);
const eleventyConfig = {
setLibrary: (...args) => {
assert.is(args[0], "md");
},
addTransform: (...args) => {
assert.is(args[0], "eleventy-plugin-unified");
},
};
index(eleventyConfig);
});

test("setLibrary › renders markdown", async (assert) => {
await new Promise((resolve) => {
const eleventyConfig = {
setLibrary: async (type, { render }) => {
assert.is(await render("# Hello, world"), "<h1>Hello, world</h1>");
resolve();
},
addTransform: () => {},
};
index(eleventyConfig);
index(eleventyConfig, { markdownTransforms: ["remark-parse"] });
});
});

Expand All @@ -38,28 +24,25 @@ test("setLibrary › disables broken method", (assert) => {
setLibrary: (type, { disable }) => {
assert.is(disable(), undefined);
},
addTransform: () => {},
};
index(eleventyConfig);
index(eleventyConfig, { markdownTransforms: ["remark-parse"] });
});

test("addTransform › does nothing to regular content", async (assert) => {
await new Promise((resolve) => {
const eleventyConfig = {
setLibrary: () => {},
addTransform: async (type, render) => {
assert.is(await render("# Hello, world"), "# Hello, world");
resolve();
},
};
index(eleventyConfig);
index(eleventyConfig, { htmlTransforms: ["rehype-parse"] });
});
});

test("addTransform › parses and renders html", async (assert) => {
await new Promise((resolve) => {
const eleventyConfig = {
setLibrary: () => {},
addTransform: async (type, render) => {
const context = { outputPath: "index.html" };
assert.is(
Expand All @@ -69,6 +52,6 @@ test("addTransform › parses and renders html", async (assert) => {
resolve();
},
};
index(eleventyConfig);
index(eleventyConfig, { htmlTransforms: ["rehype-parse"] });
});
});

0 comments on commit c991d71

Please sign in to comment.