Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: populate attributes.outdir with permalink #15

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# eleventy-plugin-asciidoc

Eleventy plugin to add support for [AsciiDoc](https://asciidoc.org/). You don't need to use to shortcodes. You can directly use AsciiDoc files (`.adoc`), just like Markdown (`.md`).
Eleventy plugin to add support for [AsciiDoc](https://asciidoc.org/).
You don't need to use to shortcodes.
You can directly use AsciiDoc files (`.adoc`), just like Markdown (`.md`).

The plugin uses [Asciidoctor.js](https://docs.asciidoctor.org/asciidoctor.js) under the hood.

Expand Down Expand Up @@ -35,7 +37,8 @@ module.exports = function (eleventyConfig) {

### Customize with Options

You can pass options to `convert()` of Asciidoctor.js as second argument in `addPlugin()`. These are the [available options](https://docs.asciidoctor.org/asciidoctor.js/latest/processor/convert-options/).
You can pass options to `convert()` of Asciidoctor.js as second argument in `addPlugin()`.
These are the [available options](https://docs.asciidoctor.org/asciidoctor.js/latest/processor/convert-options/).

```js
const eleventyAsciidoc = require("eleventy-plugin-asciidoc");
Expand All @@ -52,7 +55,13 @@ module.exports = function (eleventyConfig) {

#### `base_dir`

The `base_dir` of [convert options](https://docs.asciidoctor.org/asciidoctor.js/latest/processor/convert-options/) is relative to the document. This can be changed using above [options](#customize-with-options).
The `base_dir` of [convert options](https://docs.asciidoctor.org/asciidoctor.js/latest/processor/convert-options/) is relative to the document.
This can be changed using above [options](#customize-with-options).

#### `attributes.outdir`

By default, [`attributes.outdir`](https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes-ref/#intrinsic-attributes) will output directory (`permalink`) of the document.
This can be changed using above [options](#customize-with-options).

#### `extension_registry` (⚠️ deprecated)

Expand Down Expand Up @@ -87,7 +96,8 @@ Refer to [Asciidoctor.js documentation](https://docs.asciidoctor.org/asciidoctor

The plugin does not include any CSS styles. It is up to you to style the content.

The quick way to style the content is to use the CSS file from Asciidoctor.js. The CSS file is [available on cdnjs](https://cdnjs.com/libraries/asciidoctor.js).
The quickest way to style the content is to use the CSS file from Asciidoctor.js.
The CSS file is [available on cdnjs](https://cdnjs.com/libraries/asciidoctor.js).

## Enhancements

Expand Down
17 changes: 17 additions & 0 deletions lib/eleventy-asciidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require("fs");
const matter = require("gray-matter");
const path = require("path");
const nunjucks = require("nunjucks");
const { parseDocumentAttributes } = require("./utils.js");

// @ts-ignore
const processor = asciidoctor();
Expand Down Expand Up @@ -71,11 +72,23 @@ function eleventyAsciidoctor(convertOptions = {}) {
const { content } = readFileSync(inputPath);

let { base_dir, configure_extension_registry } = options;
const attributes = parseDocumentAttributes(options.attributes ?? {});
let { outdir } = attributes;

base_dir = base_dir === undefined ? path.dirname(inputPath) : base_dir;

if (outdir === undefined) {
const { page } = data ?? {};
const { outputPath } = page ?? {};
if (outputPath !== undefined) {
outdir = path.dirname(outputPath);
}
}

if (content) {
debug(`Converting:\n ${content}`);
debug(`base_dir: ${base_dir}`);
debug(`attributes:\n ${JSON.stringify(attributes)}`);

let registry;
if (typeof configure_extension_registry === "function") {
Expand All @@ -86,6 +99,10 @@ function eleventyAsciidoctor(convertOptions = {}) {

return processor.convert(content, {
...options,
attributes: {
...attributes,
outdir,
},
base_dir,
extension_registry: registry,
});
Expand Down
25 changes: 25 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @ts-check

/** @typedef {import('@asciidoctor/core').Attributes} Attributes */

/**
* Parses AsciiDoc Document attributes into object
*
* @param {Attributes | string[] | string} attrs AsciiDoc Document Attributes
* @return {Attributes} Attributes as Object
*/
function parseDocumentAttributes(attrs) {
if (Array.isArray(attrs)) {
return Object.fromEntries(attrs.map((a) => a.split("=")));
}

if (typeof attrs === "string") {
return Object.fromEntries([attrs.split("=")]);
}

return attrs;
}

module.exports = {
parseDocumentAttributes,
};
17 changes: 17 additions & 0 deletions tests/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const test = require("ava").default;
const { parseDocumentAttributes } = require("../lib/utils.js");

test("parseDocumentAttributes: should return object as it is", async (t) => {
t.deepEqual(parseDocumentAttributes({ some: "value" }), { some: "value" });
});

test("parseDocumentAttributes: should parse string to object", async (t) => {
t.deepEqual(parseDocumentAttributes("some=value"), { some: "value" });
});

test("parseDocumentAttributes: should parse array of strings to object", async (t) => {
t.deepEqual(parseDocumentAttributes(["some=value", "foo=bar"]), {
some: "value",
foo: "bar",
});
});
Loading