Skip to content

Commit f0acbb4

Browse files
committed
fix: pass thru front matter
- Let Eleventy handle front matter. - Permalink is render by the plugin using Nunjucks.
1 parent 6913546 commit f0acbb4

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

lib/eleventy-asciidoc.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,46 @@ const readFileSync = (inputPath) => {
3535
*/
3636

3737
/**
38-
* Creates getData function with options initialised.
38+
* Creates generateGetDataFromInputPath function with options initialised.
3939
*
4040
* @param {EleventyAsciidocOptions} converterOptions The converter options
4141
* @return {GetDataFn}
4242
*/
43-
const generateGetData = (converterOptions) => (inputPath) => {
44-
const { data, content } = readFileSync(inputPath);
43+
44+
const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
45+
const { content } = readFileSync(inputPath);
4546
const { eleventyAttributesPrefix = "eleventy-" } = converterOptions;
4647

4748
const doc = processor.load(content, {
4849
...converterOptions,
4950
base_dir: getBaseDir(inputPath, converterOptions.base_dir),
5051
});
51-
const attributes = doc.getAttributes();
52+
5253
const title = doc.getDocumentTitle();
54+
const attributes = doc.getAttributes();
5355
const eleventyAttributes = pickByKeyPrefix(
5456
attributes,
5557
eleventyAttributesPrefix,
5658
);
5759

5860
debug(`Document title ${title}`);
5961
debug(`Document attributes: ${JSON.stringify(attributes)}`);
62+
debug(`Document Eleventy attributes: ${JSON.stringify(eleventyAttributes)}`);
6063

61-
return {
64+
let data = {
6265
title,
6366
asciidocAttributes: attributes,
6467
...eleventyAttributes,
65-
...data,
68+
};
69+
70+
// Removes undefined or null values
71+
// that may override Eleventy page data.
72+
data = Object.entries(data)
73+
.filter((v) => Boolean(v[1]))
74+
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
75+
76+
return {
77+
data,
6678
};
6779
};
6880

@@ -152,12 +164,12 @@ function eleventyAsciidoctor(convertOptions = {}) {
152164
return contents;
153165
};
154166

155-
const getData = generateGetData(options);
167+
const getInstanceFromInputPath = generateGetDataFromInputPath(options);
156168

157169
return {
158-
read: false,
159-
getData,
160170
compile,
171+
getData: ["data"],
172+
getInstanceFromInputPath,
161173
compileOptions: {
162174
permalink,
163175
},

tests/compile.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,22 @@ test("Render AsciiDoc with converter options", async (t) => {
4040
t.is(result, output);
4141
});
4242

43-
test("Get data from front matter", async (t) => {
44-
const processor = eleventyAsciidoc();
45-
const result = processor.getData(path.join(sourcePath, "hello.adoc"));
46-
47-
t.is(result.title, "Hello world");
48-
t.is(result.mySlug, "hello-world");
49-
});
50-
5143
test("Get title from AsciiDoc document title", async (t) => {
5244
const processor = eleventyAsciidoc();
53-
const result = processor.getData(
45+
const result = processor.getInstanceFromInputPath(
5446
path.join(sourcePath, "with-asciidoc-attributes.adoc"),
5547
);
5648

57-
t.is(result.title, "Hello world");
49+
t.is(result.data.title, "Hello world");
5850
});
5951

6052
test("Populate data.asciidocAttributes with AsciiDoc attributes", async (t) => {
6153
const processor = eleventyAsciidoc();
62-
const result = processor.getData(
54+
const result = processor.getInstanceFromInputPath(
6355
path.join(sourcePath, "with-asciidoc-attributes.adoc"),
6456
);
6557

66-
t.is(result.asciidocAttributes.author, "Jane Doe");
58+
t.is(result.data.asciidocAttributes.author, "Jane Doe");
6759
});
6860

6961
test("Render AsciiDoc in 'unsafe' mode with 'include'", async (t) => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---json
2+
{
3+
"title": "JSON Front Matter",
4+
"layout": "base.njk",
5+
"permalink": "front-matter-json/index.html"
6+
}
7+
---
8+
9+
This text is written in AsciiDoc format.

tests/front-matter.mjs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ test("Permalinks are mapped correctly", async (t) => {
3838
);
3939
const json = await elev.toJSON();
4040

41-
t.is(json.length, 4);
42-
4341
const home = json.find((d) => d.inputPath.endsWith("home.adoc"));
4442
t.is(home.url, "/");
4543

@@ -55,4 +53,22 @@ test("Permalinks are mapped correctly", async (t) => {
5553
d.inputPath.endsWith("permalink-template.adoc"),
5654
);
5755
t.is(permalinkTemplate.url, "/from-page-data.html");
56+
});
57+
58+
test("JSON front matter parsed", async (t) => {
59+
const elev = new Eleventy(
60+
"./tests/fixtures/front-matter/",
61+
"./tests/fixtures/_site",
62+
{
63+
configPath: "./tests/fixtures/front-matter/.eleventy.js",
64+
},
65+
);
66+
const json = await elev.toJSON();
67+
68+
const page = json.find((d) => d.inputPath.endsWith("front-matter-json.adoc"));
69+
70+
const pageTitle = getHtmlTitle(page.content);
71+
t.is(pageTitle, `JSON Front Matter`);
72+
73+
t.is(page.url, "/front-matter-json/");
5874
});

0 commit comments

Comments
 (0)