@d10f/asciidoc-astro-loader 0.0.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.
- package/LICENSE +21 -0
- package/README.md +286 -0
- package/dist/chunk-2F52PMNV.js +132 -0
- package/dist/chunk-DDIUST2Z.js +113 -0
- package/dist/chunk-HAZIHU2Y.js +56 -0
- package/dist/index-Cf7MF6tZ.d.cts +325 -0
- package/dist/index-Cf7MF6tZ.d.ts +325 -0
- package/dist/index.cjs +708 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +402 -0
- package/dist/lib/asciidoc/converters/index.cjs +178 -0
- package/dist/lib/asciidoc/converters/index.d.cts +13 -0
- package/dist/lib/asciidoc/converters/index.d.ts +13 -0
- package/dist/lib/asciidoc/converters/index.js +7 -0
- package/dist/lib/asciidoc/templates/engines/index.cjs +191 -0
- package/dist/lib/asciidoc/templates/engines/index.d.cts +30 -0
- package/dist/lib/asciidoc/templates/engines/index.d.ts +30 -0
- package/dist/lib/asciidoc/templates/engines/index.js +30 -0
- package/dist/lib/shiki/transformers/index.cjs +127 -0
- package/dist/lib/shiki/transformers/index.d.cts +59 -0
- package/dist/lib/shiki/transformers/index.d.ts +59 -0
- package/dist/lib/shiki/transformers/index.js +8 -0
- package/package.json +58 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LoaderContext } from 'astro/loaders';
|
|
2
|
+
import { A as AsciidocLoader } from './index-Cf7MF6tZ.cjs';
|
|
3
|
+
export { a as AsciidocTemplate, C as CustomConverterFactoryFn, F as FilesystemTemplate, N as NodeContext, R as RawTemplate } from './index-Cf7MF6tZ.cjs';
|
|
4
|
+
import 'shiki';
|
|
5
|
+
import 'asciidoctor';
|
|
6
|
+
import 'zod';
|
|
7
|
+
|
|
8
|
+
declare function asciidocLoader(options: AsciidocLoader): {
|
|
9
|
+
name: string;
|
|
10
|
+
load({ config, parseData, store, logger }: LoaderContext): Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { asciidocLoader };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LoaderContext } from 'astro/loaders';
|
|
2
|
+
import { A as AsciidocLoader } from './index-Cf7MF6tZ.js';
|
|
3
|
+
export { a as AsciidocTemplate, C as CustomConverterFactoryFn, F as FilesystemTemplate, N as NodeContext, R as RawTemplate } from './index-Cf7MF6tZ.js';
|
|
4
|
+
import 'shiki';
|
|
5
|
+
import 'asciidoctor';
|
|
6
|
+
import 'zod';
|
|
7
|
+
|
|
8
|
+
declare function asciidocLoader(options: AsciidocLoader): {
|
|
9
|
+
name: string;
|
|
10
|
+
load({ config, parseData, store, logger }: LoaderContext): Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { asciidocLoader };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sourceCodeConverter
|
|
3
|
+
} from "./chunk-HAZIHU2Y.js";
|
|
4
|
+
import {
|
|
5
|
+
slugify,
|
|
6
|
+
splitFilenameComponents
|
|
7
|
+
} from "./chunk-DDIUST2Z.js";
|
|
8
|
+
import {
|
|
9
|
+
HandlebarsEngine,
|
|
10
|
+
NunjucksEngine
|
|
11
|
+
} from "./chunk-2F52PMNV.js";
|
|
12
|
+
|
|
13
|
+
// src/loader.ts
|
|
14
|
+
import { readdirSync as readdirSync2, realpathSync } from "fs";
|
|
15
|
+
import { nextTick } from "process";
|
|
16
|
+
|
|
17
|
+
// src/lib/asciidoc/AsciidocDocument.ts
|
|
18
|
+
import Asciidoctor from "asciidoctor";
|
|
19
|
+
|
|
20
|
+
// src/lib/asciidoc/BaseConverter.ts
|
|
21
|
+
var BaseConverter = class {
|
|
22
|
+
constructor(defaultConverter) {
|
|
23
|
+
this.defaultConverter = defaultConverter;
|
|
24
|
+
this.customConverters = /* @__PURE__ */ new Map();
|
|
25
|
+
this.templateEngine = void 0;
|
|
26
|
+
}
|
|
27
|
+
customConverters;
|
|
28
|
+
templateEngine;
|
|
29
|
+
/**
|
|
30
|
+
* Registers a new custom converter to act upon a specific block type.
|
|
31
|
+
*/
|
|
32
|
+
register(converter) {
|
|
33
|
+
const context = converter.nodeContext;
|
|
34
|
+
this.customConverters.set(context, converter);
|
|
35
|
+
}
|
|
36
|
+
registerTemplateEngine(templateEngine) {
|
|
37
|
+
this.templateEngine = templateEngine;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Converts the node to an HTML string. If the node's context matches one of
|
|
41
|
+
* the registered converters, it will use that converter.
|
|
42
|
+
*/
|
|
43
|
+
convert(node) {
|
|
44
|
+
const context = node.getNodeName();
|
|
45
|
+
const converter = this.customConverters.get(context);
|
|
46
|
+
if (converter) {
|
|
47
|
+
const output = converter.convert(node, this.templateEngine);
|
|
48
|
+
if (output) return output;
|
|
49
|
+
}
|
|
50
|
+
if (this.templateEngine) {
|
|
51
|
+
const output = this.templateEngine.convert(node);
|
|
52
|
+
if (output) return output;
|
|
53
|
+
}
|
|
54
|
+
return this.defaultConverter.convert(node);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/lib/asciidoc/TemplateEngineRegistry.ts
|
|
59
|
+
import { readdirSync } from "fs";
|
|
60
|
+
var TemplateEngineRegistry = class _TemplateEngineRegistry {
|
|
61
|
+
/**
|
|
62
|
+
* A list of supported templating engines, their associated extensions
|
|
63
|
+
* and associatied template files to render.
|
|
64
|
+
*/
|
|
65
|
+
static modules = [];
|
|
66
|
+
/**
|
|
67
|
+
* A list of all the node existing contexts an Asciidoc node can have.
|
|
68
|
+
*/
|
|
69
|
+
static nodeContexts = [
|
|
70
|
+
"admonition",
|
|
71
|
+
"audio",
|
|
72
|
+
"colist",
|
|
73
|
+
"dlist",
|
|
74
|
+
"document",
|
|
75
|
+
"embedded",
|
|
76
|
+
"example",
|
|
77
|
+
"floating-title",
|
|
78
|
+
"image",
|
|
79
|
+
"inline_anchor",
|
|
80
|
+
"inline_break",
|
|
81
|
+
"inline_button",
|
|
82
|
+
"inline_callout",
|
|
83
|
+
"inline_footnote",
|
|
84
|
+
"inline_image",
|
|
85
|
+
"inline_indexterm",
|
|
86
|
+
"inline_kbd",
|
|
87
|
+
"inline_menu",
|
|
88
|
+
"inline_quoted",
|
|
89
|
+
"listing",
|
|
90
|
+
"literal",
|
|
91
|
+
"olist",
|
|
92
|
+
"open",
|
|
93
|
+
"outline",
|
|
94
|
+
"page_break",
|
|
95
|
+
"paragraph",
|
|
96
|
+
"preamble",
|
|
97
|
+
"quote",
|
|
98
|
+
"section",
|
|
99
|
+
"sidebar",
|
|
100
|
+
"stem",
|
|
101
|
+
"table",
|
|
102
|
+
"thematic_break",
|
|
103
|
+
"toc",
|
|
104
|
+
"ulist",
|
|
105
|
+
"verse",
|
|
106
|
+
"video"
|
|
107
|
+
];
|
|
108
|
+
constructor(templateEngines, options) {
|
|
109
|
+
for (let i = 0; i < templateEngines.length; i++) {
|
|
110
|
+
const current = templateEngines[i];
|
|
111
|
+
for (let j = 0; j < i; j++) {
|
|
112
|
+
const previous = templateEngines[j];
|
|
113
|
+
if (current.name === previous.name) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`Engine named "${current.name}" is already registered.`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
current.extensions.forEach((currentExt) => {
|
|
119
|
+
if (previous.extensions.includes(currentExt)) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`Extension "${currentExt}" is already handled by engine "${previous.name}".`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
_TemplateEngineRegistry.modules.push(templateEngines[i]);
|
|
127
|
+
}
|
|
128
|
+
this.loadTemplates(options);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Asynchronously loads the necessary third party modules.
|
|
132
|
+
*/
|
|
133
|
+
async loadEngines() {
|
|
134
|
+
_TemplateEngineRegistry.modules.forEach(async (m) => {
|
|
135
|
+
if (m.canLoad) {
|
|
136
|
+
await m.load();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Returns the module with the given name.
|
|
142
|
+
*/
|
|
143
|
+
getEngineByName(name) {
|
|
144
|
+
return _TemplateEngineRegistry.modules.find((m) => m.name === name);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Returns the module that supports the given extension.
|
|
148
|
+
*/
|
|
149
|
+
getEngineByExtension(ext) {
|
|
150
|
+
return _TemplateEngineRegistry.modules.find(
|
|
151
|
+
(m) => m.extensions.includes(ext)
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Converts the provided node using one of the registered template
|
|
156
|
+
* engine modules. For more granular control, gain access to the
|
|
157
|
+
* engine itself with _getEngineByName_ or _getEngineByExtension_
|
|
158
|
+
* and use one of its render methods directly.
|
|
159
|
+
*/
|
|
160
|
+
convert(node, options = {}) {
|
|
161
|
+
const context = node.getNodeName();
|
|
162
|
+
return _TemplateEngineRegistry.modules.find((m) => m.hasContext(context))?.renderNode(node, options);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Scans the templates directory and creates an index of all templates
|
|
166
|
+
* based on supported block names and extensions.
|
|
167
|
+
*
|
|
168
|
+
* @see https://docs.asciidoctor.org/asciidoctor.js/latest/extend/converter/template-converter/#naming-convention
|
|
169
|
+
*/
|
|
170
|
+
loadTemplates({ rootDir, recursive }) {
|
|
171
|
+
readdirSync(rootDir, {
|
|
172
|
+
encoding: "utf8",
|
|
173
|
+
recursive
|
|
174
|
+
}).forEach((template) => {
|
|
175
|
+
const { filename, extension } = splitFilenameComponents(
|
|
176
|
+
template
|
|
177
|
+
);
|
|
178
|
+
if (!filename || !extension) return false;
|
|
179
|
+
if (!_TemplateEngineRegistry.nodeContexts.includes(filename))
|
|
180
|
+
return false;
|
|
181
|
+
const engineModule = _TemplateEngineRegistry.modules.find((m) => {
|
|
182
|
+
return m.supportsExt(extension);
|
|
183
|
+
});
|
|
184
|
+
if (engineModule) {
|
|
185
|
+
engineModule.addContext(filename, `${rootDir}/${template}`);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// src/lib/asciidoc/AsciidocDocument.ts
|
|
192
|
+
var AsciidocDocument = class {
|
|
193
|
+
document;
|
|
194
|
+
baseConverter;
|
|
195
|
+
constructor(filepath) {
|
|
196
|
+
const asciidoc = Asciidoctor();
|
|
197
|
+
this.baseConverter = new BaseConverter(
|
|
198
|
+
asciidoc.Html5Converter.create()
|
|
199
|
+
);
|
|
200
|
+
asciidoc.ConverterFactory.register(this.baseConverter, ["html5"]);
|
|
201
|
+
this.document = asciidoc.loadFile(filepath);
|
|
202
|
+
}
|
|
203
|
+
get documentTitle() {
|
|
204
|
+
return this.document.getDocumentTitle({
|
|
205
|
+
partition: true
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
get title() {
|
|
209
|
+
return this.documentTitle.getMain();
|
|
210
|
+
}
|
|
211
|
+
get subtitle() {
|
|
212
|
+
return this.documentTitle.getSubtitle();
|
|
213
|
+
}
|
|
214
|
+
get slug() {
|
|
215
|
+
return slugify(this.title);
|
|
216
|
+
}
|
|
217
|
+
get preamble() {
|
|
218
|
+
const [documentBlock] = this.document.getBlocks();
|
|
219
|
+
const [preambleBlock] = documentBlock?.getBlocks();
|
|
220
|
+
if (preambleBlock) return preambleBlock.getSourceLines()[0];
|
|
221
|
+
}
|
|
222
|
+
get version() {
|
|
223
|
+
return this.document.getRevisionNumber();
|
|
224
|
+
}
|
|
225
|
+
get createdAt() {
|
|
226
|
+
return this.document.getRevisionDate();
|
|
227
|
+
}
|
|
228
|
+
get keywords() {
|
|
229
|
+
return this.document.getAttribute("keywords");
|
|
230
|
+
}
|
|
231
|
+
get languages() {
|
|
232
|
+
return this.document.findBy({ context: "listing", style: "source" }).reduce((acc, cur) => {
|
|
233
|
+
const lang = cur.getAttribute("language");
|
|
234
|
+
if (!lang) return acc;
|
|
235
|
+
return acc.add(lang);
|
|
236
|
+
}, /* @__PURE__ */ new Set());
|
|
237
|
+
}
|
|
238
|
+
convert(converters = [], templateEngine) {
|
|
239
|
+
converters.forEach((converter) => {
|
|
240
|
+
this.baseConverter.register(converter);
|
|
241
|
+
});
|
|
242
|
+
if (templateEngine) {
|
|
243
|
+
this.baseConverter.registerTemplateEngine(templateEngine);
|
|
244
|
+
}
|
|
245
|
+
return this.document.getContent();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
// src/schemas/index.ts
|
|
250
|
+
import { z as z3 } from "zod";
|
|
251
|
+
|
|
252
|
+
// src/schemas/document.ts
|
|
253
|
+
import { resolve } from "path";
|
|
254
|
+
import { z } from "zod";
|
|
255
|
+
var documentOptionsSchema = z.looseObject({
|
|
256
|
+
mode: z.union([z.literal("safe"), z.literal("unsafe")]).default("safe"),
|
|
257
|
+
template: z.string().default("").transform((val) => resolve(process.cwd(), val)),
|
|
258
|
+
recursive: z.boolean().default(false)
|
|
259
|
+
}).default({
|
|
260
|
+
mode: "safe",
|
|
261
|
+
template: "",
|
|
262
|
+
recursive: false
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// src/schemas/shiki.ts
|
|
266
|
+
import { z as z2 } from "zod";
|
|
267
|
+
var shikiOptionsSchema = z2.object({
|
|
268
|
+
theme: z2.union([
|
|
269
|
+
z2.string(),
|
|
270
|
+
z2.object({
|
|
271
|
+
light: z2.string(),
|
|
272
|
+
dark: z2.string()
|
|
273
|
+
}).catchall(z2.string())
|
|
274
|
+
]).transform(transformThemeProp).default({
|
|
275
|
+
light: "catppuccin-latte",
|
|
276
|
+
dark: "catppuccin-macchiato"
|
|
277
|
+
}),
|
|
278
|
+
defaultColor: z2.string().default("light-dark()"),
|
|
279
|
+
cssVariablePrefix: z2.string().default("--shiki-"),
|
|
280
|
+
mergeWhitespaces: z2.union([z2.boolean(), z2.literal("never")]).default(true),
|
|
281
|
+
tabindex: z2.union([z2.number(), z2.string(), z2.literal(false)]).default(false)
|
|
282
|
+
}).transform(({ theme, ...rest }) => ({
|
|
283
|
+
themes: theme,
|
|
284
|
+
...rest
|
|
285
|
+
})).default({
|
|
286
|
+
mergeWhitespaces: true,
|
|
287
|
+
cssVariablePrefix: "--shiki-",
|
|
288
|
+
defaultColor: "light-dark()",
|
|
289
|
+
tabindex: false,
|
|
290
|
+
theme: {
|
|
291
|
+
light: "catppuccin-latte",
|
|
292
|
+
dark: "catppuccin-macchiato"
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
function transformThemeProp(value) {
|
|
296
|
+
return typeof value === "string" ? { light: value, dark: value } : value;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/schemas/index.ts
|
|
300
|
+
var preambleOptionsSchema = z3.object({
|
|
301
|
+
tableOfContents: z3.boolean().default(true),
|
|
302
|
+
maxLevel: z3.number().max(6).min(1).default(3),
|
|
303
|
+
position: z3.union([z3.literal("before"), z3.literal("after")]).default("after")
|
|
304
|
+
}).default({
|
|
305
|
+
tableOfContents: true,
|
|
306
|
+
maxLevel: 3,
|
|
307
|
+
position: "after"
|
|
308
|
+
});
|
|
309
|
+
var loaderOptionsSchema = z3.object({
|
|
310
|
+
base: z3.string(),
|
|
311
|
+
document: documentOptionsSchema,
|
|
312
|
+
syntaxHighlighting: shikiOptionsSchema,
|
|
313
|
+
preamble: preambleOptionsSchema
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// src/lib/shiki/Highlighter.ts
|
|
317
|
+
import { createHighlighterCore, createJavaScriptRegexEngine } from "shiki";
|
|
318
|
+
async function createHighlighter(documents, themes) {
|
|
319
|
+
const langs = documents.reduce((acc, cur) => {
|
|
320
|
+
cur.languages.forEach((lang) => {
|
|
321
|
+
acc.add(lang);
|
|
322
|
+
});
|
|
323
|
+
return acc;
|
|
324
|
+
}, /* @__PURE__ */ new Set());
|
|
325
|
+
const highlighter = await createHighlighterCore({
|
|
326
|
+
themes: Object.values(themes).map((theme) => {
|
|
327
|
+
return import(`@shikijs/themes/${theme}`);
|
|
328
|
+
}),
|
|
329
|
+
langs: Array.from(langs).map((lang) => {
|
|
330
|
+
return import(`@shikijs/langs/${lang}`);
|
|
331
|
+
}),
|
|
332
|
+
engine: createJavaScriptRegexEngine()
|
|
333
|
+
});
|
|
334
|
+
return Object.freeze(highlighter);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/loader.ts
|
|
338
|
+
function asciidocLoader(options) {
|
|
339
|
+
const parsedOpts = loaderOptionsSchema.parse(options);
|
|
340
|
+
if (parsedOpts.document.converters === void 0) {
|
|
341
|
+
parsedOpts.document.converters = [
|
|
342
|
+
sourceCodeConverter({ nodeContext: "listing" })
|
|
343
|
+
];
|
|
344
|
+
}
|
|
345
|
+
if (parsedOpts.document.templateEngines === void 0) {
|
|
346
|
+
parsedOpts.document.templateEngines = [
|
|
347
|
+
new HandlebarsEngine(),
|
|
348
|
+
new NunjucksEngine()
|
|
349
|
+
];
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
name: "asciidoc-loader",
|
|
353
|
+
async load({ config, parseData, store, logger }) {
|
|
354
|
+
const root = config.root.pathname;
|
|
355
|
+
const base = parsedOpts.base.startsWith(".") ? realpathSync(options.base) : root + options.base;
|
|
356
|
+
const docs = readdirSync2(base, "utf8").filter((file) => file.match(/(?:\.a(?:scii)?doc)$/)).map((filename) => new AsciidocDocument(`${base}/${filename}`));
|
|
357
|
+
if (docs.length === 0) {
|
|
358
|
+
logger.warn("No documents found for this collection.");
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
let templateEngine = void 0;
|
|
362
|
+
if (parsedOpts.document.template) {
|
|
363
|
+
templateEngine = new TemplateEngineRegistry(
|
|
364
|
+
parsedOpts.document.templateEngines,
|
|
365
|
+
{
|
|
366
|
+
rootDir: parsedOpts.document.template,
|
|
367
|
+
recursive: parsedOpts.document.recursive
|
|
368
|
+
}
|
|
369
|
+
);
|
|
370
|
+
await templateEngine.loadEngines();
|
|
371
|
+
}
|
|
372
|
+
const highlighter = await createHighlighter(
|
|
373
|
+
docs,
|
|
374
|
+
parsedOpts.syntaxHighlighting.themes
|
|
375
|
+
);
|
|
376
|
+
const converters = parsedOpts.document.converters.map(
|
|
377
|
+
(converter) => converter(parsedOpts, highlighter)
|
|
378
|
+
);
|
|
379
|
+
docs.forEach(async (doc) => {
|
|
380
|
+
const data = await parseData({
|
|
381
|
+
id: doc.slug,
|
|
382
|
+
data: {
|
|
383
|
+
title: doc.title,
|
|
384
|
+
createdAt: new Date(doc.createdAt),
|
|
385
|
+
description: doc.preamble
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
store.set({
|
|
389
|
+
id: doc.slug,
|
|
390
|
+
data,
|
|
391
|
+
rendered: {
|
|
392
|
+
html: doc.convert(converters, templateEngine)
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
nextTick(highlighter.dispose);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
export {
|
|
401
|
+
asciidocLoader
|
|
402
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/asciidoc/converters/index.ts
|
|
21
|
+
var converters_exports = {};
|
|
22
|
+
__export(converters_exports, {
|
|
23
|
+
sourceCodeConverter: () => sourceCodeConverter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(converters_exports);
|
|
26
|
+
|
|
27
|
+
// src/lib/asciidoc/converters/sourceCodeConverter.ts
|
|
28
|
+
var import_transformers = require("@shikijs/transformers");
|
|
29
|
+
var import_node_path = require("path");
|
|
30
|
+
|
|
31
|
+
// src/lib/utils.ts
|
|
32
|
+
function escapeRegexCharacters(str) {
|
|
33
|
+
const re = /[-\\^$*+?.()|\[\]{}]/g;
|
|
34
|
+
return str.replace(re, "\\$&");
|
|
35
|
+
}
|
|
36
|
+
function splitFilenameComponents(filename) {
|
|
37
|
+
const match = filename.match(/^(?<path>.*\/)*(?<name>[^\.]+)\.(?<ext>.*)$/);
|
|
38
|
+
return {
|
|
39
|
+
filepath: match?.groups?.path ?? null,
|
|
40
|
+
filename: match?.groups?.name ?? null,
|
|
41
|
+
extension: match?.groups?.ext ?? null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/lib/shiki/transformers/transformAsciidocCallout.ts
|
|
46
|
+
function transformAsciidocCallout({
|
|
47
|
+
node,
|
|
48
|
+
cssClasses = "pointer-events-none select-none ml-2"
|
|
49
|
+
}) {
|
|
50
|
+
const lineComments = ["//", "#", ";;"];
|
|
51
|
+
const customLineComment = node.getAttribute("line-comment");
|
|
52
|
+
if (customLineComment) {
|
|
53
|
+
lineComments.push(escapeRegexCharacters(customLineComment));
|
|
54
|
+
}
|
|
55
|
+
const calloutReList = [
|
|
56
|
+
// Handles C-style and similar comments like Perl, Python...
|
|
57
|
+
new RegExp(`(?:${lineComments.join("|")})((?:\\s+<(\\d+)>)+)`),
|
|
58
|
+
// Handles XML comments
|
|
59
|
+
new RegExp(/((?:\s*<!--(\d+)-->)+)/)
|
|
60
|
+
];
|
|
61
|
+
const linesWithCallout = {};
|
|
62
|
+
return {
|
|
63
|
+
preprocess(code) {
|
|
64
|
+
return code.split("\n").map((line, idx) => {
|
|
65
|
+
for (const re of calloutReList) {
|
|
66
|
+
const match = line.match(re);
|
|
67
|
+
if (!match) continue;
|
|
68
|
+
const callouts = match[0].trim().replaceAll(/(?:<!--|-->|[<>])/g, "").split(" ");
|
|
69
|
+
linesWithCallout[idx + 1] = callouts;
|
|
70
|
+
return line.replace(re, "");
|
|
71
|
+
}
|
|
72
|
+
return line;
|
|
73
|
+
}).join("\n");
|
|
74
|
+
},
|
|
75
|
+
line(hast, line) {
|
|
76
|
+
const callouts = linesWithCallout[line];
|
|
77
|
+
if (!callouts) return;
|
|
78
|
+
callouts.forEach((calloutId) => {
|
|
79
|
+
hast.properties[`data-callout-${calloutId}`] = "";
|
|
80
|
+
hast.children.push({
|
|
81
|
+
type: "element",
|
|
82
|
+
tagName: "i",
|
|
83
|
+
properties: {
|
|
84
|
+
class: `conum ${cssClasses}`,
|
|
85
|
+
"data-value": calloutId
|
|
86
|
+
},
|
|
87
|
+
children: [
|
|
88
|
+
{
|
|
89
|
+
type: "element",
|
|
90
|
+
tagName: "b",
|
|
91
|
+
properties: {},
|
|
92
|
+
children: [
|
|
93
|
+
{
|
|
94
|
+
type: "text",
|
|
95
|
+
value: calloutId
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/lib/shiki/transformers/transformConsoleCodeBlock.ts
|
|
107
|
+
function transformConsoleCodeBlock(options = {
|
|
108
|
+
cssClasses: "pointer-events-none select-none mr-2 opacity-50"
|
|
109
|
+
}) {
|
|
110
|
+
const unselectablePrompt = `<span $1 class="${options.cssClasses}">$</span>`;
|
|
111
|
+
const linePrefix = '<span class="line[^>]+?>';
|
|
112
|
+
const splitPrompt = new RegExp(
|
|
113
|
+
`(?<=${linePrefix})(?:<span (style="[^"]*?")>\\s*?\\$\\s+?([^<]))`
|
|
114
|
+
);
|
|
115
|
+
const trimWhitespace = new RegExp(
|
|
116
|
+
`(?<=${linePrefix})(?:<span (style="[^"]*?")>\\s*?\\$\\s*?<\\/span>(?:<span>\\s+<\\/span>)?)`
|
|
117
|
+
);
|
|
118
|
+
const trimWhitespaceAhead = new RegExp(
|
|
119
|
+
`(?<=${linePrefix}<span [^>]+?>\\$<\\/span>)(<span style="[^"]+?">)\\s+?`
|
|
120
|
+
);
|
|
121
|
+
return {
|
|
122
|
+
postprocess: (html, { lang }) => {
|
|
123
|
+
if (lang === "console") {
|
|
124
|
+
return html.split("\n").map((line) => {
|
|
125
|
+
return line.replace(
|
|
126
|
+
splitPrompt,
|
|
127
|
+
unselectablePrompt + "<span $1>$2"
|
|
128
|
+
).replace(trimWhitespace, unselectablePrompt).replace(trimWhitespaceAhead, "$1");
|
|
129
|
+
}).join("\n");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/lib/asciidoc/converters/sourceCodeConverter.ts
|
|
136
|
+
var sourceCodeConverter = ({ nodeContext, transformers, template }) => {
|
|
137
|
+
return (options, highlighter) => {
|
|
138
|
+
return {
|
|
139
|
+
nodeContext: nodeContext ?? "listing",
|
|
140
|
+
convert(node, templateEngine) {
|
|
141
|
+
const input = node.getSourceLines().join("\n");
|
|
142
|
+
const lang = node.getAttribute("language");
|
|
143
|
+
const output = highlighter.codeToHtml(input, {
|
|
144
|
+
...options.syntaxHighlighting,
|
|
145
|
+
lang,
|
|
146
|
+
transformers: [
|
|
147
|
+
...transformers ?? [],
|
|
148
|
+
(0, import_transformers.transformerNotationDiff)(),
|
|
149
|
+
(0, import_transformers.transformerNotationHighlight)(),
|
|
150
|
+
(0, import_transformers.transformerNotationFocus)(),
|
|
151
|
+
transformAsciidocCallout({ node }),
|
|
152
|
+
transformConsoleCodeBlock()
|
|
153
|
+
]
|
|
154
|
+
});
|
|
155
|
+
if (templateEngine && template) {
|
|
156
|
+
const { extension } = splitFilenameComponents(template);
|
|
157
|
+
const engine = templateEngine.getEngineByExtension(
|
|
158
|
+
extension || ""
|
|
159
|
+
);
|
|
160
|
+
if (engine) {
|
|
161
|
+
const templateFile = (0, import_node_path.resolve)(process.cwd(), template);
|
|
162
|
+
if (engine.canRenderFile) {
|
|
163
|
+
return engine.renderFile(templateFile, {
|
|
164
|
+
content: output,
|
|
165
|
+
lang
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return output;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
176
|
+
0 && (module.exports = {
|
|
177
|
+
sourceCodeConverter
|
|
178
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ShikiTransformer } from 'shiki';
|
|
2
|
+
import { C as CustomConverterFactoryFn, N as NodeContext } from '../../../index-Cf7MF6tZ.cjs';
|
|
3
|
+
import 'asciidoctor';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
type CodeBlockConverterOptions = {
|
|
7
|
+
nodeContext: NodeContext;
|
|
8
|
+
transformers: ShikiTransformer[];
|
|
9
|
+
template: string;
|
|
10
|
+
};
|
|
11
|
+
declare const sourceCodeConverter: CustomConverterFactoryFn<Partial<CodeBlockConverterOptions>>;
|
|
12
|
+
|
|
13
|
+
export { sourceCodeConverter };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ShikiTransformer } from 'shiki';
|
|
2
|
+
import { C as CustomConverterFactoryFn, N as NodeContext } from '../../../index-Cf7MF6tZ.js';
|
|
3
|
+
import 'asciidoctor';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
type CodeBlockConverterOptions = {
|
|
7
|
+
nodeContext: NodeContext;
|
|
8
|
+
transformers: ShikiTransformer[];
|
|
9
|
+
template: string;
|
|
10
|
+
};
|
|
11
|
+
declare const sourceCodeConverter: CustomConverterFactoryFn<Partial<CodeBlockConverterOptions>>;
|
|
12
|
+
|
|
13
|
+
export { sourceCodeConverter };
|