@d10f/asciidoc-astro-loader 0.2.1 → 0.3.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.
@@ -1,4 +1,6 @@
1
- // src/lib/utils.ts
1
+ // src/utils.ts
2
+ import { resolve } from "path";
3
+ import { glob } from "tinyglobby";
2
4
  function slugify(text) {
3
5
  return text.trim().normalize().toLowerCase().replace(/\s+/g, "-").replace(/[^\w-]+/g, "").replace(/--+/g, "-").replace(/^-+/, "").replace(/-+$/, "");
4
6
  }
@@ -14,9 +16,19 @@ function splitFilenameComponents(filename) {
14
16
  extension: match?.groups?.ext ?? null
15
17
  };
16
18
  }
19
+ function walk(root, pattern) {
20
+ const dirPath = resolve(root);
21
+ return glob(pattern, {
22
+ cwd: dirPath,
23
+ absolute: true,
24
+ expandDirectories: false,
25
+ followSymbolicLinks: true
26
+ });
27
+ }
17
28
 
18
29
  export {
19
30
  slugify,
20
31
  escapeRegexCharacters,
21
- splitFilenameComponents
32
+ splitFilenameComponents,
33
+ walk
22
34
  };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  escapeRegexCharacters
3
- } from "./chunk-2UGTFP4R.js";
3
+ } from "./chunk-37PTJDHV.js";
4
4
 
5
5
  // src/lib/shiki/transformers/transformAsciidocCallout.ts
6
6
  var transformAsciidocCallout = (options) => {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  splitFilenameComponents
3
- } from "./chunk-2UGTFP4R.js";
3
+ } from "./chunk-37PTJDHV.js";
4
4
 
5
5
  // src/lib/asciidoc/converters/sourceCodeConverter.ts
6
6
  import { resolve } from "path";
@@ -7,6 +7,7 @@ type DocumentOptions = RequiredOptions['document'];
7
7
  type PreambleOptions = RequiredOptions['preamble'];
8
8
  declare const loaderOptionsSchema: z.ZodObject<{
9
9
  base: z.ZodString;
10
+ pattern: z.ZodDefault<z.ZodString>;
10
11
  images: z.ZodOptional<z.ZodObject<{
11
12
  imageDir: z.ZodOptional<z.ZodString>;
12
13
  imageUrl: z.ZodOptional<z.ZodString>;
@@ -239,9 +240,18 @@ type CalloutOptions = Partial<{
239
240
  }>;
240
241
  type AsciidocLoader = {
241
242
  /**
242
- * Path to the Asciidoc documents to parse.
243
+ * Path to the directory containing the AsciiDoc files to parse.
244
+ * It resolves relative to the root of the project.
245
+ *
246
+ * @example ./src/content/blog
247
+ * @example src/content/blog
243
248
  */
244
249
  base: string;
250
+ /**
251
+ * A glob pattern to match files against. Can be used to recursively
252
+ * descent.
253
+ */
254
+ pattern?: string;
245
255
  /**
246
256
  * Options for the Asciidoctor parser.
247
257
  */
@@ -7,6 +7,7 @@ type DocumentOptions = RequiredOptions['document'];
7
7
  type PreambleOptions = RequiredOptions['preamble'];
8
8
  declare const loaderOptionsSchema: z.ZodObject<{
9
9
  base: z.ZodString;
10
+ pattern: z.ZodDefault<z.ZodString>;
10
11
  images: z.ZodOptional<z.ZodObject<{
11
12
  imageDir: z.ZodOptional<z.ZodString>;
12
13
  imageUrl: z.ZodOptional<z.ZodString>;
@@ -239,9 +240,18 @@ type CalloutOptions = Partial<{
239
240
  }>;
240
241
  type AsciidocLoader = {
241
242
  /**
242
- * Path to the Asciidoc documents to parse.
243
+ * Path to the directory containing the AsciiDoc files to parse.
244
+ * It resolves relative to the root of the project.
245
+ *
246
+ * @example ./src/content/blog
247
+ * @example src/content/blog
243
248
  */
244
249
  base: string;
250
+ /**
251
+ * A glob pattern to match files against. Can be used to recursively
252
+ * descent.
253
+ */
254
+ pattern?: string;
245
255
  /**
246
256
  * Options for the Asciidoctor parser.
247
257
  */
package/dist/index.cjs CHANGED
@@ -37,13 +37,15 @@ __export(index_exports, {
37
37
  module.exports = __toCommonJS(index_exports);
38
38
 
39
39
  // src/loader.ts
40
- var import_node_fs4 = require("fs");
40
+ var import_node_path4 = require("path");
41
41
  var import_node_process = require("process");
42
42
 
43
43
  // src/lib/asciidoc/AsciidocDocument.ts
44
44
  var import_asciidoctor = __toESM(require("asciidoctor"), 1);
45
45
 
46
- // src/lib/utils.ts
46
+ // src/utils.ts
47
+ var import_node_path = require("path");
48
+ var import_tinyglobby = require("tinyglobby");
47
49
  function slugify(text) {
48
50
  return text.trim().normalize().toLowerCase().replace(/\s+/g, "-").replace(/[^\w-]+/g, "").replace(/--+/g, "-").replace(/^-+/, "").replace(/-+$/, "");
49
51
  }
@@ -59,6 +61,15 @@ function splitFilenameComponents(filename) {
59
61
  extension: match?.groups?.ext ?? null
60
62
  };
61
63
  }
64
+ function walk(root, pattern) {
65
+ const dirPath = (0, import_node_path.resolve)(root);
66
+ return (0, import_tinyglobby.glob)(pattern, {
67
+ cwd: dirPath,
68
+ absolute: true,
69
+ expandDirectories: false,
70
+ followSymbolicLinks: true
71
+ });
72
+ }
62
73
 
63
74
  // src/lib/asciidoc/BaseConverter.ts
64
75
  var BaseConverter = class {
@@ -471,11 +482,11 @@ var TemplateStr = class extends AbstractEngine {
471
482
  var import_zod3 = require("astro/zod");
472
483
 
473
484
  // src/schemas/document.ts
474
- var import_node_path = require("path");
485
+ var import_node_path2 = require("path");
475
486
  var import_zod = require("astro/zod");
476
487
  var documentOptionsSchema = import_zod.z.object({
477
488
  mode: import_zod.z.union([import_zod.z.literal("safe"), import_zod.z.literal("unsafe")]).default("safe"),
478
- template: import_zod.z.string().default("").transform((val) => val ? (0, import_node_path.resolve)(process.cwd(), val) : ""),
489
+ template: import_zod.z.string().default("").transform((val) => val ? (0, import_node_path2.resolve)(process.cwd(), val) : ""),
479
490
  recursive: import_zod.z.boolean().default(false)
480
491
  }).passthrough().default({
481
492
  mode: "safe",
@@ -546,6 +557,7 @@ var imagesOptionsSchema = import_zod3.z.object({
546
557
  }).optional();
547
558
  var loaderOptionsSchema = import_zod3.z.object({
548
559
  base: import_zod3.z.string(),
560
+ pattern: import_zod3.z.string().default("**/*.{adoc,asciidoc}"),
549
561
  images: imagesOptionsSchema,
550
562
  document: documentOptionsSchema,
551
563
  syntaxHighlighting: shikiOptionsSchema,
@@ -563,10 +575,16 @@ async function createHighlighter(documents, themes) {
563
575
  }, /* @__PURE__ */ new Set());
564
576
  const highlighter = await (0, import_shiki2.createHighlighterCore)({
565
577
  themes: Object.values(themes).map((theme) => {
566
- return import(`@shikijs/themes/${theme}`);
578
+ return import(
579
+ /* @vite-ignore */
580
+ `@shikijs/themes/${theme}`
581
+ );
567
582
  }),
568
583
  langs: Array.from(langs).map((lang) => {
569
- return import(`@shikijs/langs/${lang}`);
584
+ return import(
585
+ /* @vite-ignore */
586
+ `@shikijs/langs/${lang}`
587
+ );
570
588
  }),
571
589
  engine: (0, import_shiki2.createJavaScriptRegexEngine)()
572
590
  });
@@ -574,7 +592,7 @@ async function createHighlighter(documents, themes) {
574
592
  }
575
593
 
576
594
  // src/lib/asciidoc/converters/sourceCodeConverter.ts
577
- var import_node_path2 = require("path");
595
+ var import_node_path3 = require("path");
578
596
  var sourceCodeConverter = (converterOptions) => {
579
597
  const transformers = converterOptions?.transformers;
580
598
  const template = converterOptions?.template;
@@ -598,7 +616,7 @@ var sourceCodeConverter = (converterOptions) => {
598
616
  );
599
617
  const engine = extension ? templateEngine.getEngineByExtension(extension ?? "") : templateEngine.getEngineByContext(this.nodeContext);
600
618
  if (template) {
601
- const templateFile = (0, import_node_path2.resolve)(process.cwd(), template);
619
+ const templateFile = (0, import_node_path3.resolve)(process.cwd(), template);
602
620
  engine?.addContext(this.nodeContext, templateFile);
603
621
  }
604
622
  if (engine) {
@@ -691,10 +709,19 @@ function asciidocLoader(_options) {
691
709
  name: "asciidoc-loader",
692
710
  async load(loaderContext) {
693
711
  const { config, logger, watcher } = loaderContext;
694
- const root = config.root.pathname;
695
- const base = options.base.startsWith(".") ? (0, import_node_fs4.realpathSync)(options.base) : root + options.base;
696
- const docs = (0, import_node_fs4.readdirSync)(base, "utf8").filter((file) => file.match(/(?:\.a(?:scii)?doc)$/)).map((filename) => {
697
- const doc = new AsciidocDocument(`${base}/${filename}`);
712
+ if (options.base.includes("../")) {
713
+ throw new Error(
714
+ "Base path cannot traverse outwards of project directory."
715
+ );
716
+ }
717
+ if (options.base.startsWith("/") && !options.base.includes(config.root.pathname)) {
718
+ throw new Error(
719
+ "Base path must be contained within project directory."
720
+ );
721
+ }
722
+ const root = (0, import_node_path4.resolve)(config.root.pathname, options.base);
723
+ const docs = (await walk(root, options.pattern)).map((file) => {
724
+ const doc = new AsciidocDocument(file);
698
725
  if (options.images?.imageUrl) {
699
726
  doc.setAttribute("imagesdir", options.images.imageUrl);
700
727
  }
@@ -751,7 +778,7 @@ function asciidocLoader(_options) {
751
778
  };
752
779
  var handle = handle2;
753
780
  const watchedFileRegExp = new RegExp(
754
- `^${base}.*.(a(?:scii)?doc)$`
781
+ `^${root}.*.(a(?:scii)?doc)$`
755
782
  );
756
783
  watcher.on("change", async (changedPath) => {
757
784
  if (changedPath.match(watchedFileRegExp)) {
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { LoaderContext } from 'astro/loaders';
2
- import { a as AsciidocLoader } from './index-Bbw2Lfb5.cjs';
3
- export { b as AsciidocTemplate, C as CustomConverterFactoryFn, F as FilesystemTemplate, N as NodeContext, R as RawTemplate } from './index-Bbw2Lfb5.cjs';
2
+ import { a as AsciidocLoader } from './index-CA5P6VBa.cjs';
3
+ export { b as AsciidocTemplate, C as CustomConverterFactoryFn, F as FilesystemTemplate, N as NodeContext, R as RawTemplate } from './index-CA5P6VBa.cjs';
4
4
  export { transformAsciidocCallout, transformerPrompt } from './lib/shiki/transformers/index.cjs';
5
5
  import 'shiki';
6
6
  import 'asciidoctor';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { LoaderContext } from 'astro/loaders';
2
- import { a as AsciidocLoader } from './index-Bbw2Lfb5.js';
3
- export { b as AsciidocTemplate, C as CustomConverterFactoryFn, F as FilesystemTemplate, N as NodeContext, R as RawTemplate } from './index-Bbw2Lfb5.js';
2
+ import { a as AsciidocLoader } from './index-CA5P6VBa.js';
3
+ export { b as AsciidocTemplate, C as CustomConverterFactoryFn, F as FilesystemTemplate, N as NodeContext, R as RawTemplate } from './index-CA5P6VBa.js';
4
4
  export { transformAsciidocCallout, transformerPrompt } from './lib/shiki/transformers/index.js';
5
5
  import 'shiki';
6
6
  import 'asciidoctor';
package/dist/index.js CHANGED
@@ -1,22 +1,23 @@
1
- import {
2
- transformAsciidocCallout,
3
- transformerPrompt
4
- } from "./chunk-KPBQHX55.js";
5
1
  import {
6
2
  sourceCodeConverter
7
- } from "./chunk-SUQERYPQ.js";
8
- import {
9
- slugify,
10
- splitFilenameComponents
11
- } from "./chunk-2UGTFP4R.js";
3
+ } from "./chunk-YJTKOSBY.js";
12
4
  import {
13
5
  HandlebarsEngine,
14
6
  NunjucksEngine,
15
7
  TemplateStr
16
8
  } from "./chunk-S7W7VGV4.js";
9
+ import {
10
+ transformAsciidocCallout,
11
+ transformerPrompt
12
+ } from "./chunk-FVYBQ3D5.js";
13
+ import {
14
+ slugify,
15
+ splitFilenameComponents,
16
+ walk
17
+ } from "./chunk-37PTJDHV.js";
17
18
 
18
19
  // src/loader.ts
19
- import { readdirSync as readdirSync2, realpathSync } from "fs";
20
+ import { resolve as resolve2 } from "path";
20
21
  import { nextTick } from "process";
21
22
 
22
23
  // src/lib/asciidoc/AsciidocDocument.ts
@@ -353,6 +354,7 @@ var imagesOptionsSchema = z3.object({
353
354
  }).optional();
354
355
  var loaderOptionsSchema = z3.object({
355
356
  base: z3.string(),
357
+ pattern: z3.string().default("**/*.{adoc,asciidoc}"),
356
358
  images: imagesOptionsSchema,
357
359
  document: documentOptionsSchema,
358
360
  syntaxHighlighting: shikiOptionsSchema,
@@ -370,10 +372,16 @@ async function createHighlighter(documents, themes) {
370
372
  }, /* @__PURE__ */ new Set());
371
373
  const highlighter = await createHighlighterCore({
372
374
  themes: Object.values(themes).map((theme) => {
373
- return import(`@shikijs/themes/${theme}`);
375
+ return import(
376
+ /* @vite-ignore */
377
+ `@shikijs/themes/${theme}`
378
+ );
374
379
  }),
375
380
  langs: Array.from(langs).map((lang) => {
376
- return import(`@shikijs/langs/${lang}`);
381
+ return import(
382
+ /* @vite-ignore */
383
+ `@shikijs/langs/${lang}`
384
+ );
377
385
  }),
378
386
  engine: createJavaScriptRegexEngine()
379
387
  });
@@ -399,10 +407,19 @@ function asciidocLoader(_options) {
399
407
  name: "asciidoc-loader",
400
408
  async load(loaderContext) {
401
409
  const { config, logger, watcher } = loaderContext;
402
- const root = config.root.pathname;
403
- const base = options.base.startsWith(".") ? realpathSync(options.base) : root + options.base;
404
- const docs = readdirSync2(base, "utf8").filter((file) => file.match(/(?:\.a(?:scii)?doc)$/)).map((filename) => {
405
- const doc = new AsciidocDocument(`${base}/${filename}`);
410
+ if (options.base.includes("../")) {
411
+ throw new Error(
412
+ "Base path cannot traverse outwards of project directory."
413
+ );
414
+ }
415
+ if (options.base.startsWith("/") && !options.base.includes(config.root.pathname)) {
416
+ throw new Error(
417
+ "Base path must be contained within project directory."
418
+ );
419
+ }
420
+ const root = resolve2(config.root.pathname, options.base);
421
+ const docs = (await walk(root, options.pattern)).map((file) => {
422
+ const doc = new AsciidocDocument(file);
406
423
  if (options.images?.imageUrl) {
407
424
  doc.setAttribute("imagesdir", options.images.imageUrl);
408
425
  }
@@ -459,7 +476,7 @@ function asciidocLoader(_options) {
459
476
  };
460
477
  var handle = handle2;
461
478
  const watchedFileRegExp = new RegExp(
462
- `^${base}.*.(a(?:scii)?doc)$`
479
+ `^${root}.*.(a(?:scii)?doc)$`
463
480
  );
464
481
  watcher.on("change", async (changedPath) => {
465
482
  if (changedPath.match(watchedFileRegExp)) {
@@ -25,9 +25,11 @@ __export(converters_exports, {
25
25
  module.exports = __toCommonJS(converters_exports);
26
26
 
27
27
  // src/lib/asciidoc/converters/sourceCodeConverter.ts
28
- var import_node_path = require("path");
28
+ var import_node_path2 = require("path");
29
29
 
30
- // src/lib/utils.ts
30
+ // src/utils.ts
31
+ var import_node_path = require("path");
32
+ var import_tinyglobby = require("tinyglobby");
31
33
  function splitFilenameComponents(filename) {
32
34
  const match = filename.match(/^(?<path>.*\/)*(?<name>[^\.]+)\.(?<ext>.*)$/);
33
35
  return {
@@ -61,7 +63,7 @@ var sourceCodeConverter = (converterOptions) => {
61
63
  );
62
64
  const engine = extension ? templateEngine.getEngineByExtension(extension ?? "") : templateEngine.getEngineByContext(this.nodeContext);
63
65
  if (template) {
64
- const templateFile = (0, import_node_path.resolve)(process.cwd(), template);
66
+ const templateFile = (0, import_node_path2.resolve)(process.cwd(), template);
65
67
  engine?.addContext(this.nodeContext, templateFile);
66
68
  }
67
69
  if (engine) {
@@ -1,5 +1,5 @@
1
1
  import { ShikiTransformer } from 'shiki';
2
- import { C as CustomConverterFactoryFn, c as ShikiTransformerFactoryFn } from '../../../index-Bbw2Lfb5.cjs';
2
+ import { C as CustomConverterFactoryFn, c as ShikiTransformerFactoryFn } from '../../../index-CA5P6VBa.cjs';
3
3
  import 'asciidoctor';
4
4
  import 'astro/zod';
5
5
 
@@ -1,5 +1,5 @@
1
1
  import { ShikiTransformer } from 'shiki';
2
- import { C as CustomConverterFactoryFn, c as ShikiTransformerFactoryFn } from '../../../index-Bbw2Lfb5.js';
2
+ import { C as CustomConverterFactoryFn, c as ShikiTransformerFactoryFn } from '../../../index-CA5P6VBa.js';
3
3
  import 'asciidoctor';
4
4
  import 'astro/zod';
5
5
 
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  sourceCodeConverter
3
- } from "../../../chunk-SUQERYPQ.js";
4
- import "../../../chunk-2UGTFP4R.js";
3
+ } from "../../../chunk-YJTKOSBY.js";
4
+ import "../../../chunk-37PTJDHV.js";
5
5
  export {
6
6
  sourceCodeConverter
7
7
  };
@@ -1,4 +1,4 @@
1
- import { A as AbstractEngine, b as AsciidocTemplate, T as TemplateModule, F as FilesystemTemplate, R as RawTemplate } from '../../../../index-Bbw2Lfb5.cjs';
1
+ import { A as AbstractEngine, b as AsciidocTemplate, T as TemplateModule, F as FilesystemTemplate, R as RawTemplate } from '../../../../index-CA5P6VBa.cjs';
2
2
  import { AbstractBlock } from 'asciidoctor';
3
3
  import 'shiki';
4
4
  import 'astro/zod';
@@ -1,4 +1,4 @@
1
- import { A as AbstractEngine, b as AsciidocTemplate, T as TemplateModule, F as FilesystemTemplate, R as RawTemplate } from '../../../../index-Bbw2Lfb5.js';
1
+ import { A as AbstractEngine, b as AsciidocTemplate, T as TemplateModule, F as FilesystemTemplate, R as RawTemplate } from '../../../../index-CA5P6VBa.js';
2
2
  import { AbstractBlock } from 'asciidoctor';
3
3
  import 'shiki';
4
4
  import 'astro/zod';
@@ -25,7 +25,9 @@ __export(transformers_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(transformers_exports);
27
27
 
28
- // src/lib/utils.ts
28
+ // src/utils.ts
29
+ var import_node_path = require("path");
30
+ var import_tinyglobby = require("tinyglobby");
29
31
  function escapeRegexCharacters(str) {
30
32
  const re = /[-\\^$*+?.()|\[\]{}]/g;
31
33
  return str.replace(re, "\\$&");
@@ -1,4 +1,4 @@
1
- import { S as ShikiTransformerFactory } from '../../../index-Bbw2Lfb5.cjs';
1
+ import { S as ShikiTransformerFactory } from '../../../index-CA5P6VBa.cjs';
2
2
  import { BundledLanguage } from 'shiki';
3
3
  import 'asciidoctor';
4
4
  import 'astro/zod';
@@ -1,4 +1,4 @@
1
- import { S as ShikiTransformerFactory } from '../../../index-Bbw2Lfb5.js';
1
+ import { S as ShikiTransformerFactory } from '../../../index-CA5P6VBa.js';
2
2
  import { BundledLanguage } from 'shiki';
3
3
  import 'asciidoctor';
4
4
  import 'astro/zod';
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  transformAsciidocCallout,
3
3
  transformerPrompt
4
- } from "../../../chunk-KPBQHX55.js";
5
- import "../../../chunk-2UGTFP4R.js";
4
+ } from "../../../chunk-FVYBQ3D5.js";
5
+ import "../../../chunk-37PTJDHV.js";
6
6
  export {
7
7
  transformAsciidocCallout,
8
8
  transformerPrompt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d10f/asciidoc-astro-loader",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "An Astro collections loader for Asciidoc files",
5
5
  "author": "D10f",
6
6
  "license": "MIT",
@@ -52,7 +52,8 @@
52
52
  "dependencies": {
53
53
  "@shikijs/transformers": "^4.5.0",
54
54
  "asciidoctor": "^3.0.4",
55
- "shiki": "^4.5.0"
55
+ "shiki": "^4.5.0",
56
+ "tinyglobby": "^0.2.17"
56
57
  },
57
58
  "peerDependencies": {
58
59
  "astro": "^6.3.1"