eleventy-plugin-asciidoc 5.0.0-alpha.1 → 5.0.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/README.md CHANGED
@@ -25,7 +25,7 @@ npm install eleventy-plugin-asciidoc
25
25
 
26
26
  ### Add to Configuration File
27
27
 
28
- Usually `.eleventy.js`:
28
+ Usually `eleventy.config.js` or `.eleventy.js`:
29
29
 
30
30
  ```js
31
31
  const eleventyAsciidoc = require("eleventy-plugin-asciidoc");
@@ -35,6 +35,58 @@ module.exports = function (eleventyConfig) {
35
35
  };
36
36
  ```
37
37
 
38
+ ### Front Matter
39
+
40
+ You can use either [Eleventy style front matter](https://www.11ty.dev/docs/data-frontmatter/#front-matter-formats) or AsciiDoc document attributes to write front matter.
41
+
42
+ Any AsciiDoc document attributes that are prefixed with `eleventy-` ([configurable](#eleventyAttributesPrefix)) can be used as front matter. _The prefix, `eleventy-`, will be removed from variable names available in the templates._
43
+
44
+ Only document-scoped attributes or variables can be used for front matter. Attributes that are written after the document title (`= Document Title`) will not be considered for front matter.
45
+
46
+ ```adoc
47
+ :eleventy-permalink: /hello-world/
48
+ :eleventy-layout: base.njk
49
+
50
+ = Hello World
51
+
52
+ Hello everyone!
53
+ ```
54
+
55
+ The above AsciiDoc attribute front matter is the same as YAML based front matter below:
56
+
57
+ ```adoc
58
+ ---
59
+ permalink: /hello-world/
60
+ layout: base.njk
61
+ ---
62
+
63
+ = Hello World
64
+
65
+ Hello everyone!
66
+ ```
67
+
68
+ > [!WARNING]
69
+ > Asciidoctor.js converts all attribute names to lower case letters. Example `:eleventy-aTitle:` will be made available as `atitle` in front matter data (also as `eleventy-atitle` in document attributes).
70
+
71
+ #### Data Cascade
72
+
73
+ Data specified using AsciiDoc style front matter override YAML (or front matter in other Eleventy supported formats).
74
+
75
+ ```adoc
76
+ ---
77
+ layout: layout-a.njk
78
+ ---
79
+ :eleventy-layout: layout-b.njk
80
+
81
+ = Hello World
82
+
83
+ Hello everyone!
84
+ ```
85
+
86
+ In the above case, front matter data will have `{ layout: layout-b.njk }`.
87
+
88
+ In the case of `title`, [the AsciiDoc document title](https://docs.asciidoctor.org/asciidoc/latest/document/title/) (including `title` and `doctitle` attributes) takes precedence over front matter.
89
+
38
90
  ### Customize with Options
39
91
 
40
92
  You can pass options to `convert()` of Asciidoctor.js as second argument in `addPlugin()`.
@@ -92,6 +144,12 @@ module.exports = function (eleventyConfig) {
92
144
 
93
145
  Refer to [Asciidoctor.js documentation](https://docs.asciidoctor.org/asciidoctor.js/latest/extend/extensions/) to know more about extensions.
94
146
 
147
+ #### `eleventyAttributesPrefix`
148
+
149
+ Default value is `eleventy-`.
150
+
151
+ This config can be used to change the prefix string for [AsciiDoc style front matter](#front-matter).
152
+
95
153
  ### CSS Styles
96
154
 
97
155
  The plugin does not include any CSS styles. It is up to you to style the content.
@@ -2,14 +2,14 @@
2
2
  const eleventyAsciidoc = require("./lib/eleventy-asciidoc.js");
3
3
  const pkg = require("./package.json");
4
4
 
5
- /** @typedef {import('./lib/eleventy-asciidoc.js').ProcessorOptions} ProcessorOptions} */
5
+ /** @typedef {import('./lib/eleventy-asciidoc.js').EleventyAsciidocOptions} EleventyAsciidocOptions} */
6
6
 
7
7
  module.exports = {
8
8
  /**
9
9
  * Plugin config function
10
10
  *
11
11
  * @param {object} eleventyConfig The eleventy configuration object
12
- * @param {ProcessorOptions} converterOptions Options for Asciidoctor.converter()
12
+ * @param {EleventyAsciidocOptions} converterOptions Options for Asciidoctor.converter()
13
13
  */
14
14
  configFunction(eleventyConfig, converterOptions) {
15
15
  try {
@@ -7,7 +7,8 @@ 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, pickByKeyPrefix } = require("./utils.js");
10
+ const { parseDocumentAttributes } = require("./utils/asciidoc.js");
11
+ const { pickByKeyPrefix, mapKeys, pick } = require("./utils/object.js");
11
12
 
12
13
  // @ts-ignore
13
14
  const processor = asciidoctor();
@@ -52,26 +53,30 @@ const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
52
53
 
53
54
  const title = doc.getDocumentTitle();
54
55
  const attributes = doc.getAttributes();
55
- const eleventyAttributes = pickByKeyPrefix(
56
+
57
+ let eleventyAttributes = pickByKeyPrefix(
56
58
  attributes,
57
59
  eleventyAttributesPrefix,
58
60
  );
61
+ eleventyAttributes = mapKeys(eleventyAttributes, (k) =>
62
+ k.slice(eleventyAttributesPrefix.length),
63
+ );
59
64
 
60
65
  debug(`Document title ${title}`);
61
66
  debug(`Document attributes: ${JSON.stringify(attributes)}`);
62
67
  debug(`Document Eleventy attributes: ${JSON.stringify(eleventyAttributes)}`);
63
68
 
64
69
  let data = {
65
- title,
66
70
  asciidocAttributes: attributes,
67
71
  ...eleventyAttributes,
72
+ // Pass title only if defined. Otherwise, `undefined`
73
+ // will override the title in `eleventyAttributes`
74
+ ...(title === undefined ? {} : { title }),
68
75
  };
69
76
 
70
- // Removes undefined or null values
77
+ // Removes undefined values
71
78
  // 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 }), {});
79
+ data = pick(data, (k, v) => v !== undefined);
75
80
 
76
81
  return {
77
82
  data,
@@ -0,0 +1,25 @@
1
+ // @ts-check
2
+
3
+ /** @typedef {import('@asciidoctor/core').Attributes} Attributes */
4
+
5
+ /**
6
+ * Parses AsciiDoc Document attributes into object
7
+ *
8
+ * @param {Attributes | string[] | string} attrs AsciiDoc Document Attributes
9
+ * @return {Attributes} Attributes as Object
10
+ */
11
+ function parseDocumentAttributes(attrs) {
12
+ if (Array.isArray(attrs)) {
13
+ return Object.fromEntries(attrs.map((a) => a.split("=")));
14
+ }
15
+
16
+ if (typeof attrs === "string") {
17
+ return Object.fromEntries([attrs.split("=")]);
18
+ }
19
+
20
+ return attrs;
21
+ }
22
+
23
+ module.exports = {
24
+ parseDocumentAttributes,
25
+ };
@@ -1,25 +1,5 @@
1
1
  // @ts-check
2
2
 
3
- /** @typedef {import('@asciidoctor/core').Attributes} Attributes */
4
-
5
- /**
6
- * Parses AsciiDoc Document attributes into object
7
- *
8
- * @param {Attributes | string[] | string} attrs AsciiDoc Document Attributes
9
- * @return {Attributes} Attributes as Object
10
- */
11
- function parseDocumentAttributes(attrs) {
12
- if (Array.isArray(attrs)) {
13
- return Object.fromEntries(attrs.map((a) => a.split("=")));
14
- }
15
-
16
- if (typeof attrs === "string") {
17
- return Object.fromEntries([attrs.split("=")]);
18
- }
19
-
20
- return attrs;
21
- }
22
-
23
3
  /**
24
4
  * 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
5
  *
@@ -27,7 +7,7 @@ function parseDocumentAttributes(attrs) {
27
7
  * @param {(key: string, value: any, obj: object ) => boolean | Array<string>} keys The keys
28
8
  * @return {object} Copy of the object with filtered values
29
9
  */
30
- function pick(obj, keys = (k) => Boolean(k)) {
10
+ function pick(obj, keys = (_k, v) => Boolean(v)) {
31
11
  if (Array.isArray(keys)) {
32
12
  return pick(obj, (key) => keys.includes(key));
33
13
  }
@@ -52,17 +32,28 @@ function pick(obj, keys = (k) => Boolean(k)) {
52
32
  * @return {object} Copy of the object with filtered values
53
33
  */
54
34
  function pickByKeyPrefix(obj, prefix) {
55
- const filteredObj = pick(obj, (k) => k.startsWith(prefix));
56
- return Object.entries(filteredObj).reduce((acc, [k, v]) => {
35
+ return pick(obj, (k) => k.startsWith(prefix));
36
+ }
37
+
38
+ /**
39
+ * Creates a copy of of the object keys generated by running mapKey
40
+ *
41
+ * @param {object} obj The object
42
+ * @param {(k: string, v: any)=> string} [mapKey=(k)=>k] The function to map the keys
43
+ * @return {object} { Copy of the object with mapped keys }
44
+ */
45
+ function mapKeys(obj, mapKey = (k) => k) {
46
+ return Object.entries(obj).reduce((acc, [k, v]) => {
47
+ const newKey = mapKey(k, v);
57
48
  return {
58
- [k.slice(prefix.length)]: v,
49
+ [newKey]: v,
59
50
  ...acc,
60
51
  };
61
52
  }, {});
62
53
  }
63
54
 
64
55
  module.exports = {
65
- parseDocumentAttributes,
66
56
  pickByKeyPrefix,
67
57
  pick,
58
+ mapKeys,
68
59
  };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "eleventy-plugin-asciidoc",
3
- "version": "5.0.0-alpha.1",
3
+ "version": "5.0.1",
4
4
  "description": "Adds support for AsciiDoc to Eleventy",
5
- "main": ".eleventy.js",
5
+ "main": "index.js",
6
6
  "funding": "https://github.com/sponsors/saneef/",
7
7
  "repository": {
8
8
  "type": "git",
@@ -23,7 +23,7 @@
23
23
  "author": "Saneef Ansari <hello@saneef.com> (https://saneef.com/)",
24
24
  "license": "MIT",
25
25
  "11ty": {
26
- "compatibility": ">=1.0.0"
26
+ "compatibility": ">=2.0.0-canary.19"
27
27
  },
28
28
  "dependencies": {
29
29
  "@asciidoctor/core": "^3.0.4",
@@ -45,7 +45,7 @@
45
45
  "rimraf": "^6.0.1"
46
46
  },
47
47
  "lint-staged": {
48
- "*.js": "eslint --cache --fix --ignore-pattern \"!.eleventy.js\"",
48
+ "*.{js,mjs,cjs}": "eslint --cache --fix",
49
49
  "*.{js,md,json}": "prettier --write"
50
50
  },
51
51
  "ava": {