eleventy-plugin-asciidoc 4.0.2 → 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 = {}) {
@@ -77,15 +90,7 @@ function eleventyAsciidoctor(convertOptions = {}) {
77
90
  ...convertOptions,
78
91
  };
79
92
 
80
- const compile = (str, inputPath) => (data) => {
81
- if (str) {
82
- // So if str has a value, it's a permalink (which can be a string or a function)
83
- debug(`Permalink: ${str}`);
84
- return typeof str === "function"
85
- ? str(data)
86
- : nunjucks.renderString(str, data);
87
- }
88
-
93
+ const compile = (_contents, inputPath) => (data) => {
89
94
  debug(`Reading ${inputPath}`);
90
95
  const { content } = readFileSync(inputPath);
91
96
 
@@ -134,12 +139,28 @@ function eleventyAsciidoctor(convertOptions = {}) {
134
139
  }
135
140
  };
136
141
 
142
+ const permalink = function (contents, _inputPath) {
143
+ if (contents && typeof contents === "string") {
144
+ return async (data) => {
145
+ const permalink = nunjucks.renderString(contents, data);
146
+ debug("permalink: ", permalink);
147
+ return permalink;
148
+ };
149
+ }
150
+
151
+ // Fallbacks to Eleventy default behaviour
152
+ return contents;
153
+ };
154
+
137
155
  const getData = generateGetData(options);
138
156
 
139
157
  return {
140
158
  read: false,
141
159
  getData,
142
160
  compile,
161
+ compileOptions: {
162
+ permalink,
163
+ },
143
164
  };
144
165
  }
145
166
 
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.2",
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/",
@@ -27,22 +27,22 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@asciidoctor/core": "^3.0.4",
30
- "debug": "^4.3.4",
30
+ "debug": "^4.4.0",
31
31
  "gray-matter": "^4.0.3",
32
32
  "nunjucks": "^3.2.4"
33
33
  },
34
34
  "devDependencies": {
35
- "@11ty/eleventy": "^2.0.1",
36
- "ava": "^6.1.1",
35
+ "@11ty/eleventy": "^3.0.0",
36
+ "ava": "^6.2.0",
37
37
  "eslint": "^8.56.0",
38
- "eslint-config-prettier": "^9.1.0",
38
+ "eslint-config-prettier": "^10.0.1",
39
39
  "eslint-config-xo-space": "^0.35.0",
40
- "eslint-plugin-prettier": "^5.1.3",
41
- "husky": "^9.0.11",
42
- "lint-staged": "^15.2.2",
43
- "nyc": "^15.1.0",
44
- "prettier": "^3.2.5",
45
- "rimraf": "^5.0.5"
40
+ "eslint-plugin-prettier": "^5.2.3",
41
+ "husky": "^9.1.7",
42
+ "lint-staged": "^15.4.3",
43
+ "nyc": "^17.1.0",
44
+ "prettier": "^3.5.2",
45
+ "rimraf": "^6.0.1"
46
46
  },
47
47
  "lint-staged": {
48
48
  "*.js": "eslint --cache --fix --ignore-pattern \"!.eleventy.js\"",