eleventy-plugin-asciidoc 5.0.0-alpha.0 → 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 +41 -1
- package/{.eleventy.js → index.js} +2 -2
- package/lib/eleventy-asciidoc.js +26 -11
- package/lib/utils/asciidoc.js +25 -0
- package/lib/{utils.js → utils/object.js} +16 -25
- package/package.json +4 -4
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').
|
|
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 {
|
|
12
|
+
* @param {EleventyAsciidocOptions} converterOptions Options for Asciidoctor.converter()
|
|
13
13
|
*/
|
|
14
14
|
configFunction(eleventyConfig, converterOptions) {
|
|
15
15
|
try {
|
package/lib/eleventy-asciidoc.js
CHANGED
|
@@ -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
|
|
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();
|
|
@@ -35,34 +36,48 @@ const readFileSync = (inputPath) => {
|
|
|
35
36
|
*/
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
|
-
* Creates
|
|
39
|
+
* Creates generateGetDataFromInputPath function with options initialised.
|
|
39
40
|
*
|
|
40
41
|
* @param {EleventyAsciidocOptions} converterOptions The converter options
|
|
41
42
|
* @return {GetDataFn}
|
|
42
43
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
|
|
45
|
+
const generateGetDataFromInputPath = (converterOptions) => (inputPath) => {
|
|
46
|
+
const { content } = readFileSync(inputPath);
|
|
45
47
|
const { eleventyAttributesPrefix = "eleventy-" } = converterOptions;
|
|
46
48
|
|
|
47
49
|
const doc = processor.load(content, {
|
|
48
50
|
...converterOptions,
|
|
49
51
|
base_dir: getBaseDir(inputPath, converterOptions.base_dir),
|
|
50
52
|
});
|
|
51
|
-
|
|
53
|
+
|
|
52
54
|
const title = doc.getDocumentTitle();
|
|
53
|
-
const
|
|
55
|
+
const attributes = doc.getAttributes();
|
|
56
|
+
|
|
57
|
+
let eleventyAttributes = pickByKeyPrefix(
|
|
54
58
|
attributes,
|
|
55
59
|
eleventyAttributesPrefix,
|
|
56
60
|
);
|
|
61
|
+
eleventyAttributes = mapKeys(eleventyAttributes, (k) =>
|
|
62
|
+
k.slice(eleventyAttributesPrefix.length),
|
|
63
|
+
);
|
|
57
64
|
|
|
58
65
|
debug(`Document title ${title}`);
|
|
59
66
|
debug(`Document attributes: ${JSON.stringify(attributes)}`);
|
|
67
|
+
debug(`Document Eleventy attributes: ${JSON.stringify(eleventyAttributes)}`);
|
|
60
68
|
|
|
61
|
-
|
|
69
|
+
let data = {
|
|
62
70
|
title,
|
|
63
71
|
asciidocAttributes: attributes,
|
|
64
72
|
...eleventyAttributes,
|
|
65
|
-
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// Removes undefined values
|
|
76
|
+
// that may override Eleventy page data.
|
|
77
|
+
data = pick(data, (k, v) => v !== undefined);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
data,
|
|
66
81
|
};
|
|
67
82
|
};
|
|
68
83
|
|
|
@@ -152,12 +167,12 @@ function eleventyAsciidoctor(convertOptions = {}) {
|
|
|
152
167
|
return contents;
|
|
153
168
|
};
|
|
154
169
|
|
|
155
|
-
const
|
|
170
|
+
const getInstanceFromInputPath = generateGetDataFromInputPath(options);
|
|
156
171
|
|
|
157
172
|
return {
|
|
158
|
-
read: false,
|
|
159
|
-
getData,
|
|
160
173
|
compile,
|
|
174
|
+
getData: ["data"],
|
|
175
|
+
getInstanceFromInputPath,
|
|
161
176
|
compileOptions: {
|
|
162
177
|
permalink,
|
|
163
178
|
},
|
|
@@ -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 = (
|
|
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
|
-
|
|
56
|
-
|
|
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
|
-
[
|
|
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
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Adds support for AsciiDoc to Eleventy",
|
|
5
|
-
"main": ".
|
|
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": ">=
|
|
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
|
|
48
|
+
"*.{js,mjs,cjs}": "eslint --cache --fix",
|
|
49
49
|
"*.{js,md,json}": "prettier --write"
|
|
50
50
|
},
|
|
51
51
|
"ava": {
|