eleventy-plugin-asciidoc 4.0.3 → 5.0.0-alpha.0

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.
@@ -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
  *
@@ -32,23 +37,31 @@ const readFileSync = (inputPath) => {
32
37
  /**
33
38
  * Creates getData function with options initialised.
34
39
  *
35
- * @param {ProcessorOptions} converterOptions The converter options
40
+ * @param {EleventyAsciidocOptions} converterOptions The converter options
36
41
  * @return {GetDataFn}
37
42
  */
38
43
  const generateGetData = (converterOptions) => (inputPath) => {
39
44
  const { data, content } = readFileSync(inputPath);
45
+ const { eleventyAttributesPrefix = "eleventy-" } = converterOptions;
46
+
40
47
  const doc = processor.load(content, {
41
48
  ...converterOptions,
42
49
  base_dir: getBaseDir(inputPath, converterOptions.base_dir),
43
50
  });
44
51
  const attributes = doc.getAttributes();
45
52
  const title = doc.getDocumentTitle();
53
+ const eleventyAttributes = pickByKeyPrefix(
54
+ attributes,
55
+ eleventyAttributesPrefix,
56
+ );
57
+
46
58
  debug(`Document title ${title}`);
47
59
  debug(`Document attributes: ${JSON.stringify(attributes)}`);
48
60
 
49
61
  return {
50
62
  title,
51
63
  asciidocAttributes: attributes,
64
+ ...eleventyAttributes,
52
65
  ...data,
53
66
  };
54
67
  };
@@ -67,7 +80,7 @@ function getBaseDir(inputPath, base_dir) {
67
80
  /**
68
81
  * Generates Eleventy template renderer for AsciiDoctor
69
82
  *
70
- * @param {ProcessorOptions=} convertOptions Options for Asciidoctor.converter()
83
+ * @param {EleventyAsciidocOptions=} convertOptions Options for Asciidoctor.converter()
71
84
  * @return {Object} Eleventy data processor object
72
85
  */
73
86
  function eleventyAsciidoctor(convertOptions = {}) {
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eleventy-plugin-asciidoc",
3
- "version": "4.0.3",
3
+ "version": "5.0.0-alpha.0",
4
4
  "description": "Adds support for AsciiDoc to Eleventy",
5
5
  "main": ".eleventy.js",
6
6
  "funding": "https://github.com/sponsors/saneef/",