eleventy-plugin-asciidoc 4.0.3 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/eleventy-asciidoc.js +38 -13
- package/lib/utils.js +43 -0
- package/package.json +1 -1
package/lib/eleventy-asciidoc.js
CHANGED
|
@@ -7,14 +7,19 @@ const fs = require("fs");
|
|
|
7
7
|
const matter = require("gray-matter");
|
|
8
8
|
const path = require("path");
|
|
9
9
|
const nunjucks = require("nunjucks");
|
|
10
|
-
const { parseDocumentAttributes } = require("./utils.js");
|
|
10
|
+
const { parseDocumentAttributes, pickByKeyPrefix } = require("./utils.js");
|
|
11
11
|
|
|
12
12
|
// @ts-ignore
|
|
13
13
|
const processor = asciidoctor();
|
|
14
14
|
|
|
15
|
-
/** @typedef {import('@asciidoctor/core').ProcessorOptions} ProcessorOptions */
|
|
16
15
|
/** @typedef {import('gray-matter').GrayMatterFile} GrayMatterFile */
|
|
17
16
|
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {Object} ExtraProcessorOptions
|
|
19
|
+
* @property {string=} eleventyAttributesPrefix
|
|
20
|
+
* @typedef {import('@asciidoctor/core').ProcessorOptions & ExtraProcessorOptions} EleventyAsciidocOptions
|
|
21
|
+
*/
|
|
22
|
+
|
|
18
23
|
/**
|
|
19
24
|
* Reads a file synchronously.
|
|
20
25
|
*
|
|
@@ -30,26 +35,46 @@ const readFileSync = (inputPath) => {
|
|
|
30
35
|
*/
|
|
31
36
|
|
|
32
37
|
/**
|
|
33
|
-
* Creates
|
|
38
|
+
* Creates generateGetDataFromInputPath function with options initialised.
|
|
34
39
|
*
|
|
35
|
-
* @param {
|
|
40
|
+
* @param {EleventyAsciidocOptions} converterOptions The converter options
|
|
36
41
|
* @return {GetDataFn}
|
|
37
42
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
|
|
44
|
+
const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
|
|
45
|
+
const { content } = readFileSync(inputPath);
|
|
46
|
+
const { eleventyAttributesPrefix = "eleventy-" } = converterOptions;
|
|
47
|
+
|
|
40
48
|
const doc = processor.load(content, {
|
|
41
49
|
...converterOptions,
|
|
42
50
|
base_dir: getBaseDir(inputPath, converterOptions.base_dir),
|
|
43
51
|
});
|
|
44
|
-
|
|
52
|
+
|
|
45
53
|
const title = doc.getDocumentTitle();
|
|
54
|
+
const attributes = doc.getAttributes();
|
|
55
|
+
const eleventyAttributes = pickByKeyPrefix(
|
|
56
|
+
attributes,
|
|
57
|
+
eleventyAttributesPrefix,
|
|
58
|
+
);
|
|
59
|
+
|
|
46
60
|
debug(`Document title ${title}`);
|
|
47
61
|
debug(`Document attributes: ${JSON.stringify(attributes)}`);
|
|
62
|
+
debug(`Document Eleventy attributes: ${JSON.stringify(eleventyAttributes)}`);
|
|
48
63
|
|
|
49
|
-
|
|
64
|
+
let data = {
|
|
50
65
|
title,
|
|
51
66
|
asciidocAttributes: attributes,
|
|
52
|
-
...
|
|
67
|
+
...eleventyAttributes,
|
|
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,
|
|
53
78
|
};
|
|
54
79
|
};
|
|
55
80
|
|
|
@@ -67,7 +92,7 @@ function getBaseDir(inputPath, base_dir) {
|
|
|
67
92
|
/**
|
|
68
93
|
* Generates Eleventy template renderer for AsciiDoctor
|
|
69
94
|
*
|
|
70
|
-
* @param {
|
|
95
|
+
* @param {EleventyAsciidocOptions=} convertOptions Options for Asciidoctor.converter()
|
|
71
96
|
* @return {Object} Eleventy data processor object
|
|
72
97
|
*/
|
|
73
98
|
function eleventyAsciidoctor(convertOptions = {}) {
|
|
@@ -139,12 +164,12 @@ function eleventyAsciidoctor(convertOptions = {}) {
|
|
|
139
164
|
return contents;
|
|
140
165
|
};
|
|
141
166
|
|
|
142
|
-
const
|
|
167
|
+
const getInstanceFromInputPath = generateGetDataFromInputPath(options);
|
|
143
168
|
|
|
144
169
|
return {
|
|
145
|
-
read: false,
|
|
146
|
-
getData,
|
|
147
170
|
compile,
|
|
171
|
+
getData: ["data"],
|
|
172
|
+
getInstanceFromInputPath,
|
|
148
173
|
compileOptions: {
|
|
149
174
|
permalink,
|
|
150
175
|
},
|
package/lib/utils.js
CHANGED
|
@@ -20,6 +20,49 @@ function parseDocumentAttributes(attrs) {
|
|
|
20
20
|
return attrs;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Return a copy of the object, filtered to only have values for the allowed keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.
|
|
25
|
+
*
|
|
26
|
+
* @param {object} obj The object
|
|
27
|
+
* @param {(key: string, value: any, obj: object ) => boolean | Array<string>} keys The keys
|
|
28
|
+
* @return {object} Copy of the object with filtered values
|
|
29
|
+
*/
|
|
30
|
+
function pick(obj, keys = (k) => Boolean(k)) {
|
|
31
|
+
if (Array.isArray(keys)) {
|
|
32
|
+
return pick(obj, (key) => keys.includes(key));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return Object.keys(obj).reduce((acc, k) => {
|
|
36
|
+
if (keys(k, obj[k], obj)) {
|
|
37
|
+
return {
|
|
38
|
+
[k]: obj[k],
|
|
39
|
+
...acc,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return acc;
|
|
44
|
+
}, {});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Picks keys with matching prefix and removes prefix from keys
|
|
49
|
+
*
|
|
50
|
+
* @param {object} obj The objec
|
|
51
|
+
* @param {string} prefix The key prefix
|
|
52
|
+
* @return {object} Copy of the object with filtered values
|
|
53
|
+
*/
|
|
54
|
+
function pickByKeyPrefix(obj, prefix) {
|
|
55
|
+
const filteredObj = pick(obj, (k) => k.startsWith(prefix));
|
|
56
|
+
return Object.entries(filteredObj).reduce((acc, [k, v]) => {
|
|
57
|
+
return {
|
|
58
|
+
[k.slice(prefix.length)]: v,
|
|
59
|
+
...acc,
|
|
60
|
+
};
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
|
63
|
+
|
|
23
64
|
module.exports = {
|
|
24
65
|
parseDocumentAttributes,
|
|
66
|
+
pickByKeyPrefix,
|
|
67
|
+
pick,
|
|
25
68
|
};
|