Skip to content

Commit 2deec35

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

File tree

8 files changed

+113
-4
lines changed

8 files changed

+113
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint camelcase: ["error", {allow: ["base_dir"]}] */
2+
3+
import Eleventy from "@11ty/eleventy";
4+
import test from "ava";
5+
6+
test("Permalinks are mapped correctly", async (t) => {
7+
const elev = new Eleventy(
8+
"./tests/fixtures/attributes-to-front-matter/",
9+
"./tests/fixtures/_site",
10+
{
11+
configPath: "./tests/fixtures/attributes-to-front-matter/.eleventy.js",
12+
},
13+
);
14+
const json = await elev.toJSON();
15+
16+
t.is(json.length, 2);
17+
18+
const home = json.find((d) => d.inputPath.endsWith("home.adoc"));
19+
t.is(home.url, "/");
20+
t.snapshot(home.content);
21+
22+
const permalinkTemplate = json.find((d) =>
23+
d.inputPath.endsWith("permalink-template.adoc"),
24+
);
25+
t.is(permalinkTemplate.url, "/another-index.html");
26+
27+
t.snapshot(permalinkTemplate.content);
28+
});
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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>␊
12+
<html lang="en">␊
13+
<head>␊
14+
<meta charset="UTF-8">␊
15+
<meta name="viewport" content="width=device-width, initial-scale=1.0">␊
16+
<title>Home</title>␊
17+
</head>␊
18+
<body>␊
19+
<div class="paragraph">␊
20+
<p>This text is written in AsciiDoc format.</p>␊
21+
</div>␊
22+
</body>␊
23+
</html>`
24+
25+
> Snapshot 2
26+
27+
`<!DOCTYPE html>␊
28+
<html lang="en">␊
29+
<head>␊
30+
<meta charset="UTF-8">␊
31+
<meta name="viewport" content="width=device-width, initial-scale=1.0">␊
32+
<title>Another Page</title>␊
33+
</head>␊
34+
<body>␊
35+
<div class="paragraph">␊
36+
<p>This text is written in AsciiDoc format.</p>␊
37+
</div>␊
38+
</body>␊
39+
</html>`
357 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)