eleventy-plugin-asciidoc 5.0.0-alpha.1 → 5.0.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.
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,40 @@ 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
+ > [!NOTE]
45
+ > 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.
46
+
47
+ ```adoc
48
+ :eleventy-permalink: /hello-world/
49
+ :eleventy-layout: base.njk
50
+
51
+ = Hello World
52
+
53
+ Hello everyone!
54
+ ```
55
+
56
+ The above AsciiDoc attribute front matter is the same as YAML based front matter below:
57
+
58
+ ```adoc
59
+ ---
60
+ permalink: /hello-world/
61
+ layout: base.njk
62
+ ---
63
+
64
+ = Hello World
65
+
66
+ Hello everyone!
67
+ ```
68
+
69
+ > [!WARNING]
70
+ > 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).
71
+
38
72
  ### Customize with Options
39
73
 
40
74
  You can pass options to `convert()` of Asciidoctor.js as second argument in `addPlugin()`.
@@ -92,6 +126,12 @@ module.exports = function (eleventyConfig) {
92
126
 
93
127
  Refer to [Asciidoctor.js documentation](https://docs.asciidoctor.org/asciidoctor.js/latest/extend/extensions/) to know more about extensions.
94
128
 
129
+ #### `eleventyAttributesPrefix`
130
+
131
+ Default value is `eleventy-`.
132
+
133
+ This config can be used to change the prefix string for [AsciiDoc style front matter](#front-matter).
134
+
95
135
  ### CSS Styles
96
136
 
97
137
  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,10 +53,14 @@ 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)}`);
@@ -67,11 +72,9 @@ const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
67
72
  ...eleventyAttributes,
68
73
  };
69
74
 
70
- // Removes undefined or null values
75
+ // Removes undefined values
71
76
  // 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 }), {});
77
+ data = pick(data, (k, v) => v !== undefined);
75
78
 
76
79
  return {
77
80
  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.0",
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": {