@d10f/asciidoc-astro-loader 0.0.4 → 0.0.6

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
@@ -19,7 +19,7 @@ This package will allow you to load Asciidoc files (with either an `.asciidoc` o
19
19
 
20
20
  Install the package from `npm`:
21
21
 
22
- ```console
22
+ ```
23
23
  npm install @d10f/asciidoc-astro-loader
24
24
  ```
25
25
 
@@ -151,7 +151,7 @@ const blog = defineCollection({
151
151
  base: ".src/content/blog",
152
152
  syntaxHighlighting: {
153
153
  transformers: [
154
- transformerAsciidocSomethign({
154
+ transformerAsciidocSomething({
155
155
  cssClasses: 'text-red-500'
156
156
  }),
157
157
  ],
@@ -179,7 +179,7 @@ Just like regular Asciidoctor.js, any files located here will be used to replace
179
179
 
180
180
  ### Registering A Templating Engine
181
181
 
182
- By default, only Handlebars and Nunjucks are available for processing templates, but you can create your own as well to leverage whatever other template engine you like. A template engine in the context of this package a class that extends the `AbstractEngine` class.
182
+ Templates can be written as plain functions that return an HTML string, or as templates written in a format supported by one of the registered templating engines. By default, only Handlebars and Nunjucks are supported, but you can create your own. A templating engine in the context of this package is a class that extends the `AbstractEngine` class:
183
183
 
184
184
  ```ts
185
185
  import { Php, Request } from '@platformatic/php-node';
@@ -125,8 +125,29 @@ var NunjucksEngine = class extends AbstractEngine {
125
125
  }
126
126
  };
127
127
 
128
+ // src/lib/asciidoc/templates/engines/TemplateStr.ts
129
+ var TemplateStr = class extends AbstractEngine {
130
+ renderMap;
131
+ constructor(name = "templateStr", extensions = ["ts", "js"]) {
132
+ super(name, extensions);
133
+ this.renderMap = /* @__PURE__ */ new Map();
134
+ }
135
+ async load() {
136
+ for (const [context, path] of this.templateList.entries()) {
137
+ const templateFn = await import(path);
138
+ this.renderMap.set(context, templateFn.default);
139
+ }
140
+ }
141
+ renderNode(node, options) {
142
+ const context = node.getNodeName();
143
+ const renderFn = this.renderMap.get(context);
144
+ return renderFn ? renderFn(node, options) : "";
145
+ }
146
+ };
147
+
128
148
  export {
129
149
  AbstractEngine,
130
150
  HandlebarsEngine,
131
- NunjucksEngine
151
+ NunjucksEngine,
152
+ TemplateStr
132
153
  };
package/dist/index.cjs CHANGED
@@ -260,6 +260,15 @@ var AsciidocDocument = class {
260
260
  const [preambleBlock] = documentBlock?.getBlocks();
261
261
  if (preambleBlock) return preambleBlock.getSourceLines()[0];
262
262
  }
263
+ get description() {
264
+ return this.document.getAttribute("description", "");
265
+ }
266
+ get author() {
267
+ return this.document.getAuthor();
268
+ }
269
+ get authors() {
270
+ return this.document.getAuthors();
271
+ }
263
272
  get version() {
264
273
  return this.document.getRevisionNumber();
265
274
  }
@@ -416,6 +425,26 @@ var NunjucksEngine = class extends AbstractEngine {
416
425
  }
417
426
  };
418
427
 
428
+ // src/lib/asciidoc/templates/engines/TemplateStr.ts
429
+ var TemplateStr = class extends AbstractEngine {
430
+ renderMap;
431
+ constructor(name = "templateStr", extensions = ["ts", "js"]) {
432
+ super(name, extensions);
433
+ this.renderMap = /* @__PURE__ */ new Map();
434
+ }
435
+ async load() {
436
+ for (const [context, path] of this.templateList.entries()) {
437
+ const templateFn = await import(path);
438
+ this.renderMap.set(context, templateFn.default);
439
+ }
440
+ }
441
+ renderNode(node, options) {
442
+ const context = node.getNodeName();
443
+ const renderFn = this.renderMap.get(context);
444
+ return renderFn ? renderFn(node, options) : "";
445
+ }
446
+ };
447
+
419
448
  // src/schemas/index.ts
420
449
  var import_zod3 = require("astro/zod");
421
450
 
@@ -624,7 +653,8 @@ function asciidocLoader(_options) {
624
653
  if (options.document.templateEngines === void 0) {
625
654
  options.document.templateEngines = [
626
655
  new HandlebarsEngine(),
627
- new NunjucksEngine()
656
+ new NunjucksEngine(),
657
+ new TemplateStr()
628
658
  ];
629
659
  }
630
660
  let highlighter;
@@ -715,8 +745,16 @@ async function setDocument(doc, converters, templateEngine, { parseData, store }
715
745
  id: doc.slug,
716
746
  data: {
717
747
  title: doc.title,
748
+ subtitle: doc.subtitle,
749
+ author: doc.author,
750
+ authors: doc.authors,
751
+ version: doc.version,
718
752
  createdAt: new Date(doc.createdAt),
719
- description: doc.preamble
753
+ email: doc.authors.at(0)?.getEmail(),
754
+ description: doc.description,
755
+ slug: doc.slug,
756
+ preamble: doc.preamble,
757
+ keywords: doc.keywords
720
758
  }
721
759
  });
722
760
  store.set({
package/dist/index.js CHANGED
@@ -10,8 +10,9 @@ import {
10
10
  } from "./chunk-2UGTFP4R.js";
11
11
  import {
12
12
  HandlebarsEngine,
13
- NunjucksEngine
14
- } from "./chunk-2F52PMNV.js";
13
+ NunjucksEngine,
14
+ TemplateStr
15
+ } from "./chunk-HVA4AVUD.js";
15
16
 
16
17
  // src/loader.ts
17
18
  import { readdirSync as readdirSync2, realpathSync } from "fs";
@@ -222,6 +223,15 @@ var AsciidocDocument = class {
222
223
  const [preambleBlock] = documentBlock?.getBlocks();
223
224
  if (preambleBlock) return preambleBlock.getSourceLines()[0];
224
225
  }
226
+ get description() {
227
+ return this.document.getAttribute("description", "");
228
+ }
229
+ get author() {
230
+ return this.document.getAuthor();
231
+ }
232
+ get authors() {
233
+ return this.document.getAuthors();
234
+ }
225
235
  get version() {
226
236
  return this.document.getRevisionNumber();
227
237
  }
@@ -359,7 +369,8 @@ function asciidocLoader(_options) {
359
369
  if (options.document.templateEngines === void 0) {
360
370
  options.document.templateEngines = [
361
371
  new HandlebarsEngine(),
362
- new NunjucksEngine()
372
+ new NunjucksEngine(),
373
+ new TemplateStr()
363
374
  ];
364
375
  }
365
376
  let highlighter;
@@ -450,8 +461,16 @@ async function setDocument(doc, converters, templateEngine, { parseData, store }
450
461
  id: doc.slug,
451
462
  data: {
452
463
  title: doc.title,
464
+ subtitle: doc.subtitle,
465
+ author: doc.author,
466
+ authors: doc.authors,
467
+ version: doc.version,
453
468
  createdAt: new Date(doc.createdAt),
454
- description: doc.preamble
469
+ email: doc.authors.at(0)?.getEmail(),
470
+ description: doc.description,
471
+ slug: doc.slug,
472
+ preamble: doc.preamble,
473
+ keywords: doc.keywords
455
474
  }
456
475
  });
457
476
  store.set({
@@ -32,7 +32,8 @@ var engines_exports = {};
32
32
  __export(engines_exports, {
33
33
  AbstractEngine: () => AbstractEngine,
34
34
  HandlebarsEngine: () => HandlebarsEngine,
35
- NunjucksEngine: () => NunjucksEngine
35
+ NunjucksEngine: () => NunjucksEngine,
36
+ TemplateStr: () => TemplateStr
36
37
  });
37
38
  module.exports = __toCommonJS(engines_exports);
38
39
 
@@ -162,9 +163,30 @@ var NunjucksEngine = class extends AbstractEngine {
162
163
  return this.render(str, options);
163
164
  }
164
165
  };
166
+
167
+ // src/lib/asciidoc/templates/engines/TemplateStr.ts
168
+ var TemplateStr = class extends AbstractEngine {
169
+ renderMap;
170
+ constructor(name = "templateStr", extensions = ["ts", "js"]) {
171
+ super(name, extensions);
172
+ this.renderMap = /* @__PURE__ */ new Map();
173
+ }
174
+ async load() {
175
+ for (const [context, path] of this.templateList.entries()) {
176
+ const templateFn = await import(path);
177
+ this.renderMap.set(context, templateFn.default);
178
+ }
179
+ }
180
+ renderNode(node, options) {
181
+ const context = node.getNodeName();
182
+ const renderFn = this.renderMap.get(context);
183
+ return renderFn ? renderFn(node, options) : "";
184
+ }
185
+ };
165
186
  // Annotate the CommonJS export names for ESM import in node:
166
187
  0 && (module.exports = {
167
188
  AbstractEngine,
168
189
  HandlebarsEngine,
169
- NunjucksEngine
190
+ NunjucksEngine,
191
+ TemplateStr
170
192
  });
@@ -21,4 +21,11 @@ declare class NunjucksEngine extends AbstractEngine implements AsciidocTemplate,
21
21
  renderString(str: string, options?: Record<string, unknown>): string;
22
22
  }
23
23
 
24
- export { AbstractEngine, HandlebarsEngine, NunjucksEngine };
24
+ declare class TemplateStr extends AbstractEngine implements AsciidocTemplate, TemplateModule {
25
+ private renderMap;
26
+ constructor(name?: string, extensions?: string[]);
27
+ load(): Promise<void>;
28
+ renderNode(node: AbstractBlock, options: Record<string, unknown>): string;
29
+ }
30
+
31
+ export { AbstractEngine, HandlebarsEngine, NunjucksEngine, TemplateStr };
@@ -21,4 +21,11 @@ declare class NunjucksEngine extends AbstractEngine implements AsciidocTemplate,
21
21
  renderString(str: string, options?: Record<string, unknown>): string;
22
22
  }
23
23
 
24
- export { AbstractEngine, HandlebarsEngine, NunjucksEngine };
24
+ declare class TemplateStr extends AbstractEngine implements AsciidocTemplate, TemplateModule {
25
+ private renderMap;
26
+ constructor(name?: string, extensions?: string[]);
27
+ load(): Promise<void>;
28
+ renderNode(node: AbstractBlock, options: Record<string, unknown>): string;
29
+ }
30
+
31
+ export { AbstractEngine, HandlebarsEngine, NunjucksEngine, TemplateStr };
@@ -1,10 +1,12 @@
1
1
  import {
2
2
  AbstractEngine,
3
3
  HandlebarsEngine,
4
- NunjucksEngine
5
- } from "../../../../chunk-2F52PMNV.js";
4
+ NunjucksEngine,
5
+ TemplateStr
6
+ } from "../../../../chunk-HVA4AVUD.js";
6
7
  export {
7
8
  AbstractEngine,
8
9
  HandlebarsEngine,
9
- NunjucksEngine
10
+ NunjucksEngine,
11
+ TemplateStr
10
12
  };
@@ -21,7 +21,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var transformers_exports = {};
22
22
  __export(transformers_exports, {
23
23
  transformAsciidocCallout: () => transformAsciidocCallout,
24
- transformConsolePrompt: () => transformConsolePrompt,
25
24
  transformerPrompt: () => transformerPrompt
26
25
  });
27
26
  module.exports = __toCommonJS(transformers_exports);
@@ -90,47 +89,6 @@ var transformAsciidocCallout = (options) => {
90
89
  };
91
90
  };
92
91
 
93
- // src/lib/shiki/transformers/transformConsolePrompt.ts
94
- var transformConsolePrompt = (options) => {
95
- return (node) => {
96
- const language = node.getAttribute("language");
97
- const cssClasses = options?.cssClasses;
98
- const promptChar = options?.promptChar ?? "$";
99
- const unselectablePrompt = `<span $1 class="${cssClasses}">${promptChar}</span>`;
100
- const linePrefix = '<span class="line[^>]+?>';
101
- const splitPrompt = new RegExp(
102
- `(?<=${linePrefix})(?:<span (style="[^"]*?")>\\s*?\\${promptChar}\\s+?([^<]))`
103
- );
104
- const trimWhitespace = new RegExp(
105
- `(?<=${linePrefix})(?:<span (style="[^"]*?")>\\s*?\\${promptChar}\\s*?<\\/span>(?:<span>\\s+<\\/span>)?)`
106
- );
107
- const trimWhitespaceAhead = new RegExp(
108
- `(?<=${linePrefix}<span [^>]+?>\\${promptChar}<\\/span>)(<span style="[^"]+?">)\\s+?`
109
- );
110
- return {
111
- line(hast) {
112
- if (language !== "console") return;
113
- const textNode = hast.children.at(0)?.children.at(0);
114
- if (textNode && textNode.type === "text") {
115
- const leadingChar = textNode.value;
116
- if (!leadingChar.startsWith(promptChar)) {
117
- textNode.value = promptChar + " " + textNode.value;
118
- }
119
- }
120
- },
121
- postprocess: (html) => {
122
- if (language !== "console") return;
123
- return html.split("\n").map((line) => {
124
- return line.replace(
125
- splitPrompt,
126
- unselectablePrompt + "<span $1>$2"
127
- ).replace(trimWhitespace, unselectablePrompt).replace(trimWhitespaceAhead, "$1");
128
- }).join("\n");
129
- }
130
- };
131
- };
132
- };
133
-
134
92
  // src/lib/shiki/transformers/transformerPrompt.ts
135
93
  var transformerPrompt = (options) => {
136
94
  return (node) => {
@@ -138,7 +96,7 @@ var transformerPrompt = (options) => {
138
96
  const customPrompt = node.getAttribute(
139
97
  options?.promptAttribute ?? "custom-prompt"
140
98
  );
141
- const promptToAdd = customPrompt ?? (options?.prompts && options.prompts[language]);
99
+ const promptToAdd = customPrompt ?? (options?.langs && options.langs[language]);
142
100
  const cssClasses = typeof promptToAdd === "object" ? promptToAdd.cssClasses : options?.cssClasses;
143
101
  return {
144
102
  line(hast) {
@@ -163,6 +121,5 @@ var transformerPrompt = (options) => {
163
121
  // Annotate the CommonJS export names for ESM import in node:
164
122
  0 && (module.exports = {
165
123
  transformAsciidocCallout,
166
- transformConsolePrompt,
167
124
  transformerPrompt
168
125
  });
@@ -9,45 +9,6 @@ type TransformerOptions = Partial<{
9
9
  }>;
10
10
  declare const transformAsciidocCallout: ShikiTransformerFactory<TransformerOptions>;
11
11
 
12
- type TransformOptions$1 = {
13
- /**
14
- * String of CSS classes to apply to leading prompt character(s) on
15
- * source code blocks of type console.
16
- */
17
- cssClasses: string;
18
- /**
19
- * The prompt character representing the actual console prompt.
20
- *
21
- * @default $
22
- */
23
- promptChar?: string;
24
- };
25
- /**
26
- * For code blocks of language "console", prepends each line with a
27
- * prompt character(s), where applicable. By default, a "$" is used but
28
- * this can be controlled via the *promptChar* option.
29
- * Use the *cssClasses* option to style this prompt for things like
30
- * making it unselectable, or dimmed..
31
- *
32
- * In addition, the leading spaces before the actual command are deleted
33
- * from the span elements generated by Shiki to avoid accidentally
34
- * running commands that would not appear in the shell history.
35
- *
36
- * @example
37
- * before: <span>$ npm run dev</span>
38
- * after: <span>$</span><span>npm run dev</span>
39
- *
40
- * @example
41
- * before: <span> $ </span><span> </span>
42
- * before: <span> $ </span>
43
- * after: <span>$</span>
44
- *
45
- * @example
46
- * before: <span>$</span><span> npm run dev</span>
47
- * after: <span>$</span><span>npm run dev</span>
48
- */
49
- declare const transformConsolePrompt: ShikiTransformerFactory<TransformOptions$1>;
50
-
51
12
  type PromptOptions = {
52
13
  prompt: string;
53
14
  cssClasses: string;
@@ -65,7 +26,7 @@ type TransformOptions = {
65
26
  * want to style them differently from the
66
27
  * If not provided, it will default to look at the block attribute.
67
28
  */
68
- prompts?: Partial<Record<BundledLanguage, PromptOptions | string>>;
29
+ langs?: Partial<Record<BundledLanguage, PromptOptions | string>>;
69
30
  /**
70
31
  * The name of the block attribute on the original document to look
71
32
  * for and use as the prompt for this block.
@@ -76,4 +37,4 @@ type TransformOptions = {
76
37
  };
77
38
  declare const transformerPrompt: ShikiTransformerFactory<TransformOptions>;
78
39
 
79
- export { transformAsciidocCallout, transformConsolePrompt, transformerPrompt };
40
+ export { transformAsciidocCallout, transformerPrompt };
@@ -9,45 +9,6 @@ type TransformerOptions = Partial<{
9
9
  }>;
10
10
  declare const transformAsciidocCallout: ShikiTransformerFactory<TransformerOptions>;
11
11
 
12
- type TransformOptions$1 = {
13
- /**
14
- * String of CSS classes to apply to leading prompt character(s) on
15
- * source code blocks of type console.
16
- */
17
- cssClasses: string;
18
- /**
19
- * The prompt character representing the actual console prompt.
20
- *
21
- * @default $
22
- */
23
- promptChar?: string;
24
- };
25
- /**
26
- * For code blocks of language "console", prepends each line with a
27
- * prompt character(s), where applicable. By default, a "$" is used but
28
- * this can be controlled via the *promptChar* option.
29
- * Use the *cssClasses* option to style this prompt for things like
30
- * making it unselectable, or dimmed..
31
- *
32
- * In addition, the leading spaces before the actual command are deleted
33
- * from the span elements generated by Shiki to avoid accidentally
34
- * running commands that would not appear in the shell history.
35
- *
36
- * @example
37
- * before: <span>$ npm run dev</span>
38
- * after: <span>$</span><span>npm run dev</span>
39
- *
40
- * @example
41
- * before: <span> $ </span><span> </span>
42
- * before: <span> $ </span>
43
- * after: <span>$</span>
44
- *
45
- * @example
46
- * before: <span>$</span><span> npm run dev</span>
47
- * after: <span>$</span><span>npm run dev</span>
48
- */
49
- declare const transformConsolePrompt: ShikiTransformerFactory<TransformOptions$1>;
50
-
51
12
  type PromptOptions = {
52
13
  prompt: string;
53
14
  cssClasses: string;
@@ -65,7 +26,7 @@ type TransformOptions = {
65
26
  * want to style them differently from the
66
27
  * If not provided, it will default to look at the block attribute.
67
28
  */
68
- prompts?: Partial<Record<BundledLanguage, PromptOptions | string>>;
29
+ langs?: Partial<Record<BundledLanguage, PromptOptions | string>>;
69
30
  /**
70
31
  * The name of the block attribute on the original document to look
71
32
  * for and use as the prompt for this block.
@@ -76,4 +37,4 @@ type TransformOptions = {
76
37
  };
77
38
  declare const transformerPrompt: ShikiTransformerFactory<TransformOptions>;
78
39
 
79
- export { transformAsciidocCallout, transformConsolePrompt, transformerPrompt };
40
+ export { transformAsciidocCallout, transformerPrompt };
@@ -3,47 +3,6 @@ import {
3
3
  } from "../../../chunk-5P6LDJGO.js";
4
4
  import "../../../chunk-2UGTFP4R.js";
5
5
 
6
- // src/lib/shiki/transformers/transformConsolePrompt.ts
7
- var transformConsolePrompt = (options) => {
8
- return (node) => {
9
- const language = node.getAttribute("language");
10
- const cssClasses = options?.cssClasses;
11
- const promptChar = options?.promptChar ?? "$";
12
- const unselectablePrompt = `<span $1 class="${cssClasses}">${promptChar}</span>`;
13
- const linePrefix = '<span class="line[^>]+?>';
14
- const splitPrompt = new RegExp(
15
- `(?<=${linePrefix})(?:<span (style="[^"]*?")>\\s*?\\${promptChar}\\s+?([^<]))`
16
- );
17
- const trimWhitespace = new RegExp(
18
- `(?<=${linePrefix})(?:<span (style="[^"]*?")>\\s*?\\${promptChar}\\s*?<\\/span>(?:<span>\\s+<\\/span>)?)`
19
- );
20
- const trimWhitespaceAhead = new RegExp(
21
- `(?<=${linePrefix}<span [^>]+?>\\${promptChar}<\\/span>)(<span style="[^"]+?">)\\s+?`
22
- );
23
- return {
24
- line(hast) {
25
- if (language !== "console") return;
26
- const textNode = hast.children.at(0)?.children.at(0);
27
- if (textNode && textNode.type === "text") {
28
- const leadingChar = textNode.value;
29
- if (!leadingChar.startsWith(promptChar)) {
30
- textNode.value = promptChar + " " + textNode.value;
31
- }
32
- }
33
- },
34
- postprocess: (html) => {
35
- if (language !== "console") return;
36
- return html.split("\n").map((line) => {
37
- return line.replace(
38
- splitPrompt,
39
- unselectablePrompt + "<span $1>$2"
40
- ).replace(trimWhitespace, unselectablePrompt).replace(trimWhitespaceAhead, "$1");
41
- }).join("\n");
42
- }
43
- };
44
- };
45
- };
46
-
47
6
  // src/lib/shiki/transformers/transformerPrompt.ts
48
7
  var transformerPrompt = (options) => {
49
8
  return (node) => {
@@ -51,7 +10,7 @@ var transformerPrompt = (options) => {
51
10
  const customPrompt = node.getAttribute(
52
11
  options?.promptAttribute ?? "custom-prompt"
53
12
  );
54
- const promptToAdd = customPrompt ?? (options?.prompts && options.prompts[language]);
13
+ const promptToAdd = customPrompt ?? (options?.langs && options.langs[language]);
55
14
  const cssClasses = typeof promptToAdd === "object" ? promptToAdd.cssClasses : options?.cssClasses;
56
15
  return {
57
16
  line(hast) {
@@ -75,6 +34,5 @@ var transformerPrompt = (options) => {
75
34
  };
76
35
  export {
77
36
  transformAsciidocCallout,
78
- transformConsolePrompt,
79
37
  transformerPrompt
80
38
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d10f/asciidoc-astro-loader",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "An Astro collections loader for Asciidoc files",
5
5
  "author": "D10f",
6
6
  "license": "MIT",