Skip to content

Commit

Permalink
feat: Adds new option, configure_extension_registry
Browse files Browse the repository at this point in the history
This can be use instead of Asciidoctor's 'extension_registry' option.
'configure_extension_registry' will create new registry for each file
conversion.

Fixes #10
  • Loading branch information
saneef committed Jul 28, 2023
1 parent e3e099a commit a4e13a2
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module.exports = {
);
}

if (converterOptions?.extension_registry !== undefined) {
console.log(
`WARN: 'extension_registry' doesn't work well with Asciidoctor.js v3+. Use 'configure_extension_registry'.`,
);
}

eleventyConfig.addTemplateFormats("adoc");
eleventyConfig.addExtension("adoc", eleventyAsciidoc(converterOptions));
},
Expand Down
18 changes: 15 additions & 3 deletions lib/eleventy-asciidoc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
/* eslint camelcase: ["error", {allow: ["base_dir"]}] */
/* eslint camelcase: ["error", {allow: ["base_dir", "extension_registry", "configure_extension_registry"]}] */

const asciidoctor = require("@asciidoctor/core");
const debug = require("debug")("eleventy-plugin-asciidoc");
Expand Down Expand Up @@ -70,13 +70,25 @@ function eleventyAsciidoctor(convertOptions = {}) {
debug(`Reading ${inputPath}`);
const { content } = readFileSync(inputPath);

let { base_dir } = options;
let { base_dir, configure_extension_registry } = options;
base_dir = base_dir === undefined ? path.dirname(inputPath) : base_dir;

if (content) {
debug(`Converting:\n ${content}`);
debug(`base_dir: ${base_dir}`);
return processor.convert(content, { ...options, base_dir });

let registry;
if (typeof configure_extension_registry === "function") {
debug(`Creating an extension registry`);
registry = processor.Extensions.create();
configure_extension_registry(registry);
}

return processor.convert(content, {
...options,
base_dir,
extension_registry: registry,
});
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"rimraf": "^5.0.1"
},
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.js": "eslint --cache --fix --ignore-pattern \"!.eleventy.js\"",
"*.{js,md,json}": "prettier --write"
},
"ava": {
Expand Down
36 changes: 36 additions & 0 deletions tests/extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const test = require("ava").default;
const Eleventy = require("@11ty/eleventy");

test("Render with shout extension", async (t) => {
const elev = new Eleventy(
"./tests/fixtures/extensions/src",
"./tests/fixtures/extensions/_site",
{
configPath: "./tests/fixtures/extensions/.eleventy.js",
},
);
const json = await elev.toJSON();

// Here two pages are checked because, from asciidoctor.js v3
// onwards registry can only be used on one convert. Unless,
// a fresh registry is passed, in the subsequent convert call
// the content won't be processed with the extensions.
// See: https://github.com/asciidoctor/asciidoctor.js/issues/1709
t.is(json.length, 2);

const page1 = json.find((d) => d.inputPath.endsWith("index.adoc"));
t.is(
page1.content.trim(),
`<div class="paragraph">
<p>IF IT ALL WORKS, THIS TEXT SHOULD BE IN UPPER CASE.</p>
</div>`,
);

const page2 = json.find((d) => d.inputPath.endsWith("another-page.adoc"));
t.is(
page2.content.trim(),
`<div class="paragraph">
<p>IF IT ALL WORKS, THIS TEXT SHOULD ALSO BE IN UPPER CASE.</p>
</div>`,
);
});
12 changes: 12 additions & 0 deletions tests/fixtures/extensions/.eleventy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint camelcase: ["error", {allow: ["configure_extension_registry"]}] */

const eleventyAsciidoc = require("../../../");
const shout = require("./shout.js");

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyAsciidoc, {
configure_extension_registry(registry) {
shout.register(registry);
},
});
};
23 changes: 23 additions & 0 deletions tests/fixtures/extensions/shout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function shout() {
const self = this;
self.named("shout");
self.onContext("paragraph");
self.process(function (parent, reader) {
const lines = reader.getLines().map((l) => l.toUpperCase());
return self.createBlock(parent, "paragraph", lines);
});
}

function register(registry) {
if (typeof registry.register === "function") {
registry.register(function () {
this.block(shout);
});
} else if (typeof registry.block === "function") {
registry.block(shout);
}

return registry;
}

module.exports = { register };
4 changes: 4 additions & 0 deletions tests/fixtures/extensions/src/another-page.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= A Second Test Document for Asciidoc Extensions

[shout]
If it all works, this text should also be in upper case.
4 changes: 4 additions & 0 deletions tests/fixtures/extensions/src/index.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Test Document for Asciidoc Extensions

[shout]
If it all works, this text should be in upper case.

0 comments on commit a4e13a2

Please sign in to comment.