eleventy-plugin-asciidoc 5.0.0 → 5.1.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
@@ -35,14 +35,13 @@ module.exports = function (eleventyConfig) {
35
35
  };
36
36
  ```
37
37
 
38
- ### Front Matter
38
+ ## Front Matter
39
39
 
40
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
41
 
42
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
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.
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.
46
45
 
47
46
  ```adoc
48
47
  :eleventy-permalink: /hello-world/
@@ -69,9 +68,55 @@ Hello everyone!
69
68
  > [!WARNING]
70
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).
71
70
 
72
- ### Customize with Options
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
+
90
+ ## Options
91
+
92
+ You can pass options as the second argument in `addPlugin()`.
93
+
94
+ ### Options for this plugin
95
+
96
+ #### `eleventyAttributesPrefix`
97
+
98
+ Default value is `eleventy-`.
99
+
100
+ This config can be used to change the prefix string for [AsciiDoc style front matter](#front-matter).
101
+
102
+ #### `resolveDocumentTitle`
103
+
104
+ Default value is `false`.
105
+
106
+ If enabled, the title will be resolved from the heading of the first section in the document. Otherwise, the title will be the Level 0 heading.
107
+
108
+ ```adoc
109
+ == A second level heading
110
+
111
+ This text is written in AsciiDoc format.
112
+ ```
113
+
114
+ With `resolveDocumentTitle: true`, the above document will have a title (in page data). `A second level heading`.
115
+
116
+ ### Options for Asciidoctor.js
117
+
118
+ All properties other than [the ones specific to the plugin](#options-for-this-plugin) will be passed to Asciidoctor.js.
73
119
 
74
- You can pass options to `convert()` of Asciidoctor.js as second argument in `addPlugin()`.
75
120
  These are the [available options](https://docs.asciidoctor.org/asciidoctor.js/latest/processor/convert-options/).
76
121
 
77
122
  ```js
@@ -126,12 +171,6 @@ module.exports = function (eleventyConfig) {
126
171
 
127
172
  Refer to [Asciidoctor.js documentation](https://docs.asciidoctor.org/asciidoctor.js/latest/extend/extensions/) to know more about extensions.
128
173
 
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
-
135
174
  ### CSS Styles
136
175
 
137
176
  The plugin does not include any CSS styles. It is up to you to style the content.
@@ -18,6 +18,7 @@ const processor = asciidoctor();
18
18
  /**
19
19
  * @typedef {Object} ExtraProcessorOptions
20
20
  * @property {string=} eleventyAttributesPrefix
21
+ * @property {Boolean} [resolveDocumentTitle=false]
21
22
  * @typedef {import('@asciidoctor/core').ProcessorOptions & ExtraProcessorOptions} EleventyAsciidocOptions
22
23
  */
23
24
 
@@ -31,6 +32,21 @@ const readFileSync = (inputPath) => {
31
32
  return matter(fs.readFileSync(inputPath, "utf8"));
32
33
  };
33
34
 
35
+ /**
36
+ * Gets the title from ascii document document.
37
+ *
38
+ * @param {object} document The document
39
+ * @param {boolean} [resolve=false] The resolve
40
+ * @return {string | undefined} The title of the document.
41
+ */
42
+ function getTitleFromAsciiDocDocument(document, resolve = false) {
43
+ if (resolve) {
44
+ return document.getDocumentTitle();
45
+ }
46
+
47
+ return document.getHeader()?.title;
48
+ }
49
+
34
50
  /** @typedef {(inputPath: string) => Object.<string, any>} GetDataFn
35
51
  * Gets front-matter data from the file synchronously
36
52
  */
@@ -44,14 +60,19 @@ const readFileSync = (inputPath) => {
44
60
 
45
61
  const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
46
62
  const { content } = readFileSync(inputPath);
47
- const { eleventyAttributesPrefix = "eleventy-" } = converterOptions;
63
+ const {
64
+ eleventyAttributesPrefix = "eleventy-",
65
+ resolveDocumentTitle = false,
66
+ ...restOfConverterOptions
67
+ } = converterOptions;
48
68
 
49
69
  const doc = processor.load(content, {
50
- ...converterOptions,
51
- base_dir: getBaseDir(inputPath, converterOptions.base_dir),
70
+ ...restOfConverterOptions,
71
+ base_dir: getBaseDir(inputPath, restOfConverterOptions.base_dir),
52
72
  });
53
73
 
54
- const title = doc.getDocumentTitle();
74
+ const title = getTitleFromAsciiDocDocument(doc, resolveDocumentTitle);
75
+
55
76
  const attributes = doc.getAttributes();
56
77
 
57
78
  let eleventyAttributes = pickByKeyPrefix(
@@ -67,9 +88,11 @@ const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
67
88
  debug(`Document Eleventy attributes: ${JSON.stringify(eleventyAttributes)}`);
68
89
 
69
90
  let data = {
70
- title,
71
91
  asciidocAttributes: attributes,
72
92
  ...eleventyAttributes,
93
+ // Pass title only if defined. Otherwise, `undefined`
94
+ // will override the title in `eleventyAttributes`
95
+ ...(title === undefined ? {} : { title }),
73
96
  };
74
97
 
75
98
  // Removes undefined values
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eleventy-plugin-asciidoc",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Adds support for AsciiDoc to Eleventy",
5
5
  "main": "index.js",
6
6
  "funding": "https://github.com/sponsors/saneef/",