Skip to content

Commit c1a07a5

Browse files
committed
feat: pass eleventy front matter from asciidoc attributes
Fixes #24
1 parent 62f912a commit c1a07a5

File tree

9 files changed

+97
-4
lines changed

9 files changed

+97
-4
lines changed

lib/eleventy-asciidoc.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const fs = require("fs");
77
const matter = require("gray-matter");
88
const path = require("path");
99
const nunjucks = require("nunjucks");
10-
const { parseDocumentAttributes } = require("./utils.js");
10+
const { parseDocumentAttributes, pickByKeyPrefix } = require("./utils.js");
1111

1212
// @ts-ignore
1313
const processor = asciidoctor();
1414

15-
/** @typedef {import('@asciidoctor/core').ProcessorOptions} ProcessorOptions */
1615
/** @typedef {import('gray-matter').GrayMatterFile} GrayMatterFile */
1716

17+
/**
18+
* @typedef {Object} ExtraProcessorOptions
19+
* @property {string=} eleventyAttributesPrefix
20+
* @typedef {import('@asciidoctor/core').ProcessorOptions & ExtraProcessorOptions} EleventyAsciidocOptions
21+
*/
22+
1823
/**
1924
* Reads a file synchronously.
2025
*
@@ -32,23 +37,31 @@ const readFileSync = (inputPath) => {
3237
/**
3338
* Creates getData function with options initialised.
3439
*
35-
* @param {ProcessorOptions} converterOptions The converter options
40+
* @param {EleventyAsciidocOptions} converterOptions The converter options
3641
* @return {GetDataFn}
3742
*/
3843
const generateGetData = (converterOptions) => (inputPath) => {
3944
const { data, content } = readFileSync(inputPath);
45+
const { eleventyAttributesPrefix = "eleventy-" } = converterOptions;
46+
4047
const doc = processor.load(content, {
4148
...converterOptions,
4249
base_dir: getBaseDir(inputPath, converterOptions.base_dir),
4350
});
4451
const attributes = doc.getAttributes();
4552
const title = doc.getDocumentTitle();
53+
const eleventyAttributes = pickByKeyPrefix(
54+
attributes,
55+
eleventyAttributesPrefix,
56+
);
57+
4658
debug(`Document title ${title}`);
4759
debug(`Document attributes: ${JSON.stringify(attributes)}`);
4860

4961
return {
5062
title,
5163
asciidocAttributes: attributes,
64+
...eleventyAttributes,
5265
...data,
5366
};
5467
};
@@ -67,7 +80,7 @@ function getBaseDir(inputPath, base_dir) {
6780
/**
6881
* Generates Eleventy template renderer for AsciiDoctor
6982
*
70-
* @param {ProcessorOptions=} convertOptions Options for Asciidoctor.converter()
83+
* @param {EleventyAsciidocOptions=} convertOptions Options for Asciidoctor.converter()
7184
* @return {Object} Eleventy data processor object
7285
*/
7386
function eleventyAsciidoctor(convertOptions = {}) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint camelcase: ["error", {allow: ["base_dir"]}] */
2+
3+
import Eleventy from "@11ty/eleventy";
4+
import test from "ava";
5+
import { removeNewlines } from "./utils.js";
6+
7+
test("Permalinks are mapped correctly", async (t) => {
8+
const elev = new Eleventy(
9+
"./tests/fixtures/attributes-to-front-matter/",
10+
"./tests/fixtures/_site",
11+
{
12+
configPath: "./tests/fixtures/attributes-to-front-matter/.eleventy.js",
13+
},
14+
);
15+
const json = await elev.toJSON();
16+
17+
t.is(json.length, 2);
18+
19+
const home = json.find((d) => d.inputPath.endsWith("home.adoc"));
20+
t.is(home.url, "/");
21+
t.snapshot(removeNewlines(home.content));
22+
23+
const permalinkTemplate = json.find((d) =>
24+
d.inputPath.endsWith("permalink-template.adoc"),
25+
);
26+
t.is(permalinkTemplate.url, "/another-index.html");
27+
28+
t.snapshot(removeNewlines(permalinkTemplate.content));
29+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const eleventyAsciidoc = require("../../../");
2+
3+
module.exports = function (eleventyConfig) {
4+
eleventyConfig.addPlugin(eleventyAsciidoc);
5+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>{{ title }}</title>
7+
</head>
8+
<body>
9+
{% block content %}{{ content | safe }}{% endblock %}
10+
</body>
11+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
:eleventy-layout: base.njk
2+
:eleventy-permalink: index.html
3+
4+
= Home
5+
6+
This text is written in AsciiDoc format.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:eleventy-layout: base.njk
2+
:eleventy-urlslug: another-index
3+
:eleventy-permalink: {{ urlslug }}.html
4+
5+
= Another Page
6+
7+
This text is written in AsciiDoc format.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Snapshot report for `tests/attributes-to-front-matter.mjs`
2+
3+
The actual snapshot is saved in `attributes-to-front-matter.mjs.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## Permalinks are mapped correctly
8+
9+
> Snapshot 1
10+
11+
'<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Home</title></head><body> <div class="paragraph"><p>This text is written in AsciiDoc format.</p></div></body></html>'
12+
13+
> Snapshot 2
14+
15+
'<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Another Page</title></head><body> <div class="paragraph"><p>This text is written in AsciiDoc format.</p></div></body></html>'
355 Bytes
Binary file not shown.

tests/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function removeNewlines(str) {
2+
return str.replace(/[^\x20-\x7E]/gim, "");
3+
}
4+
5+
module.exports = {
6+
removeNewlines,
7+
};

0 commit comments

Comments
 (0)