@ox-content/vite-plugin 2.11.0 → 2.13.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/dist/github.mjs CHANGED
@@ -1,640 +1,2 @@
1
- import { c as __exportAll } from "./mermaid.mjs";
2
- import { unified } from "unified";
3
- import rehypeParse from "rehype-parse";
4
- import rehypeStringify from "rehype-stringify";
5
- import { Buffer } from "node:buffer";
6
- //#region src/plugins/github.ts
7
- /**
8
- * GitHub Plugin - Repository and source code embedding
9
- *
10
- * Transforms <GitHub> components into static repository and source code cards
11
- * by fetching data from GitHub API at build time.
12
- */
13
- var github_exports = /* @__PURE__ */ __exportAll({
14
- collectGitHubRepos: () => collectGitHubRepos,
15
- collectGitHubSources: () => collectGitHubSources,
16
- createGitHubPermalink: () => createGitHubPermalink,
17
- fetchGitHubSource: () => fetchGitHubSource,
18
- fetchRepoData: () => fetchRepoData,
19
- isSafeGitHubRepo: () => isSafeGitHubRepo,
20
- parseGitHubLineRange: () => parseGitHubLineRange,
21
- parseGitHubPermalink: () => parseGitHubPermalink,
22
- prefetchGitHubRepos: () => prefetchGitHubRepos,
23
- prefetchGitHubSources: () => prefetchGitHubSources,
24
- transformGitHub: () => transformGitHub
25
- });
26
- const defaultOptions = {
27
- token: "",
28
- cache: true,
29
- cacheTTL: 36e5,
30
- maxSourceBytes: 2e5,
31
- maxSourceLines: 120
32
- };
33
- const repoCache = /* @__PURE__ */ new Map();
34
- const sourceCache = /* @__PURE__ */ new Map();
35
- const GITHUB_REPO_RE = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
36
- const GITHUB_COMPONENT_RE = /<github\b([^>]*)>/gi;
37
- const ATTRIBUTE_RE = /([:\w-]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>/]+)))?/g;
38
- const CONTROL_CHAR_RE = /[\u0000-\u001f\u007f]/;
39
- const EXTENSION_LANGUAGE_MAP = new Map([
40
- ["cjs", "javascript"],
41
- ["css", "css"],
42
- ["go", "go"],
43
- ["html", "html"],
44
- ["js", "javascript"],
45
- ["json", "json"],
46
- ["jsx", "jsx"],
47
- ["md", "markdown"],
48
- ["mdx", "mdx"],
49
- ["mjs", "javascript"],
50
- ["py", "python"],
51
- ["rb", "ruby"],
52
- ["rs", "rust"],
53
- ["sh", "shell"],
54
- ["svelte", "svelte"],
55
- ["toml", "toml"],
56
- ["ts", "typescript"],
57
- ["tsx", "tsx"],
58
- ["vue", "vue"],
59
- ["yaml", "yaml"],
60
- ["yml", "yaml"]
61
- ]);
62
- function isSafeGitHubRepo(repo) {
63
- return GITHUB_REPO_RE.test(repo) && !repo.split("/").some((part) => part === "." || part === "..");
64
- }
65
- function isSafeGitHubRef(ref) {
66
- return Boolean(ref) && !CONTROL_CHAR_RE.test(ref) && !hasUnsafePathSegment(ref);
67
- }
68
- function isSafeGitHubPath(path) {
69
- return Boolean(path) && !CONTROL_CHAR_RE.test(path) && !hasUnsafePathSegment(path);
70
- }
71
- function hasUnsafePathSegment(value) {
72
- return value.split("/").some((part) => !part || part === "." || part === ".." || part.includes("\\"));
73
- }
74
- function encodePath(path) {
75
- return path.split("/").map(encodeURIComponent).join("/");
76
- }
77
- function sourceKey(source) {
78
- return `${source.repo}@${source.ref}:${source.path}`;
79
- }
80
- function formatLineRange(lines) {
81
- return lines.start === lines.end ? `L${lines.start}` : `L${lines.start}-L${lines.end}`;
82
- }
83
- function parseGitHubLineRange(value) {
84
- if (!value) return void 0;
85
- const match = value.trim().match(/^#?L?(\d+)(?:-L?(\d+))?$/i);
86
- if (!match) return void 0;
87
- const start = Number.parseInt(match[1], 10);
88
- const end = match[2] ? Number.parseInt(match[2], 10) : start;
89
- if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 1 || end < start) return;
90
- return {
91
- start,
92
- end
93
- };
94
- }
95
- function createGitHubPermalink(source) {
96
- const fragment = source.lines ? `#${formatLineRange(source.lines)}` : "";
97
- return `https://github.com/${source.repo}/blob/${encodeURIComponent(source.ref)}/${encodePath(source.path)}${fragment}`;
98
- }
99
- function parseGitHubPermalink(value) {
100
- let url;
101
- try {
102
- url = new URL(value);
103
- } catch {
104
- return null;
105
- }
106
- if (url.protocol !== "https:" || url.hostname !== "github.com") return null;
107
- let parts;
108
- try {
109
- parts = url.pathname.split("/").filter(Boolean).map((part) => decodeURIComponent(part));
110
- } catch {
111
- return null;
112
- }
113
- if (parts.length < 5 || parts[2] !== "blob") return null;
114
- const repo = `${parts[0]}/${parts[1]}`;
115
- const ref = parts[3];
116
- const path = parts.slice(4).join("/");
117
- if (!isSafeGitHubRepo(repo) || !isSafeGitHubRef(ref) || !isSafeGitHubPath(path)) return null;
118
- const source = {
119
- repo,
120
- ref,
121
- path,
122
- lines: parseGitHubLineRange(url.hash)
123
- };
124
- return {
125
- ...source,
126
- permalink: createGitHubPermalink(source)
127
- };
128
- }
129
- /**
130
- * Get element attribute value.
131
- */
132
- function getAttribute(el, name) {
133
- const value = el.properties?.[name];
134
- if (typeof value === "string") return value;
135
- if (Array.isArray(value)) return value.join(" ");
136
- }
137
- /**
138
- * Format number with K/M suffix.
139
- */
140
- function formatNumber(num) {
141
- if (num >= 1e6) return `${(num / 1e6).toFixed(1)}M`;
142
- if (num >= 1e3) return `${(num / 1e3).toFixed(1)}k`;
143
- return String(num);
144
- }
145
- /**
146
- * Fetch repository data from GitHub API.
147
- */
148
- async function fetchRepoData(repo, options) {
149
- if (!isSafeGitHubRepo(repo)) return null;
150
- if (options.cache) {
151
- const cached = repoCache.get(repo);
152
- if (cached && Date.now() - cached.timestamp < options.cacheTTL) return cached.data;
153
- }
154
- try {
155
- const headers = {
156
- Accept: "application/vnd.github.v3+json",
157
- "User-Agent": "ox-content-github-plugin"
158
- };
159
- if (options.token) headers.Authorization = `Bearer ${options.token}`;
160
- const response = await fetch(`https://api.github.com/repos/${repo}`, { headers });
161
- if (!response.ok) {
162
- console.warn(`Failed to fetch GitHub repo ${repo}: ${response.status}`);
163
- return null;
164
- }
165
- const data = await response.json();
166
- if (options.cache) repoCache.set(repo, {
167
- data,
168
- timestamp: Date.now()
169
- });
170
- return data;
171
- } catch (error) {
172
- console.warn(`Error fetching GitHub repo ${repo}:`, error);
173
- return null;
174
- }
175
- }
176
- /**
177
- * Fetch source file data from GitHub API.
178
- */
179
- async function fetchGitHubSource(source, options) {
180
- if (!isSafeGitHubRepo(source.repo) || !isSafeGitHubRef(source.ref) || !isSafeGitHubPath(source.path)) return null;
181
- const key = sourceKey(source);
182
- if (options.cache) {
183
- const cached = sourceCache.get(key);
184
- if (cached && Date.now() - cached.timestamp < options.cacheTTL) return cached.data;
185
- }
186
- try {
187
- const headers = {
188
- Accept: "application/vnd.github.v3+json",
189
- "User-Agent": "ox-content-github-plugin"
190
- };
191
- if (options.token) headers.Authorization = `Bearer ${options.token}`;
192
- const apiUrl = `https://api.github.com/repos/${source.repo}/contents/${encodePath(source.path)}?ref=${encodeURIComponent(source.ref)}`;
193
- const response = await fetch(apiUrl, { headers });
194
- if (!response.ok) {
195
- console.warn(`Failed to fetch GitHub source ${source.permalink}: ${response.status}`);
196
- return null;
197
- }
198
- const data = await response.json();
199
- if (data.type !== "file" || data.encoding !== "base64" || !data.content || (data.size ?? 0) > options.maxSourceBytes) return null;
200
- const content = Buffer.from(data.content.replace(/\s/g, ""), "base64").toString("utf8");
201
- if (Buffer.byteLength(content) > options.maxSourceBytes) return null;
202
- const sourceData = {
203
- repo: source.repo,
204
- ref: source.ref,
205
- path: source.path,
206
- permalink: source.permalink,
207
- content,
208
- size: data.size ?? Buffer.byteLength(content),
209
- html_url: data.html_url ?? source.permalink,
210
- language: inferLanguage(source.path)
211
- };
212
- if (options.cache) sourceCache.set(key, {
213
- data: sourceData,
214
- timestamp: Date.now()
215
- });
216
- return sourceData;
217
- } catch (error) {
218
- console.warn(`Error fetching GitHub source ${source.permalink}:`, error);
219
- return null;
220
- }
221
- }
222
- /**
223
- * Create GitHub card element from repo data.
224
- */
225
- function createGitHubCard(repoData) {
226
- const statsChildren = [];
227
- if (repoData.language) statsChildren.push({
228
- type: "element",
229
- tagName: "span",
230
- properties: { className: ["ox-github-language"] },
231
- children: [{
232
- type: "element",
233
- tagName: "span",
234
- properties: {
235
- className: ["ox-github-language-color"],
236
- "data-lang": repoData.language.toLowerCase()
237
- },
238
- children: []
239
- }, {
240
- type: "text",
241
- value: repoData.language
242
- }]
243
- });
244
- statsChildren.push({
245
- type: "element",
246
- tagName: "span",
247
- properties: { className: ["ox-github-stat"] },
248
- children: [{
249
- type: "element",
250
- tagName: "svg",
251
- properties: {
252
- viewBox: "0 0 16 16",
253
- fill: "currentColor"
254
- },
255
- children: [{
256
- type: "element",
257
- tagName: "path",
258
- properties: { d: "M8 .25a.75.75 0 0 1 .673.418l1.882 3.815 4.21.612a.75.75 0 0 1 .416 1.279l-3.046 2.97.719 4.192a.751.751 0 0 1-1.088.791L8 12.347l-3.766 1.98a.75.75 0 0 1-1.088-.79l.72-4.194L.818 6.374a.75.75 0 0 1 .416-1.28l4.21-.611L7.327.668A.75.75 0 0 1 8 .25Z" },
259
- children: []
260
- }]
261
- }, {
262
- type: "text",
263
- value: formatNumber(repoData.stargazers_count)
264
- }]
265
- });
266
- statsChildren.push({
267
- type: "element",
268
- tagName: "span",
269
- properties: { className: ["ox-github-stat"] },
270
- children: [{
271
- type: "element",
272
- tagName: "svg",
273
- properties: {
274
- viewBox: "0 0 16 16",
275
- fill: "currentColor"
276
- },
277
- children: [{
278
- type: "element",
279
- tagName: "path",
280
- properties: { d: "M5 5.372v.878c0 .414.336.75.75.75h4.5a.75.75 0 0 0 .75-.75v-.878a2.25 2.25 0 1 1 1.5 0v.878a2.25 2.25 0 0 1-2.25 2.25h-1.5v2.128a2.251 2.251 0 1 1-1.5 0V8.5h-1.5A2.25 2.25 0 0 1 3.5 6.25v-.878a2.25 2.25 0 1 1 1.5 0ZM5 3.25a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Zm6.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Zm-3 8.75a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Z" },
281
- children: []
282
- }]
283
- }, {
284
- type: "text",
285
- value: formatNumber(repoData.forks_count)
286
- }]
287
- });
288
- return {
289
- type: "element",
290
- tagName: "a",
291
- properties: {
292
- className: ["ox-github-card"],
293
- href: repoData.html_url,
294
- target: "_blank",
295
- rel: "noopener noreferrer"
296
- },
297
- children: [
298
- {
299
- type: "element",
300
- tagName: "div",
301
- properties: { className: ["ox-github-header"] },
302
- children: [{
303
- type: "element",
304
- tagName: "svg",
305
- properties: {
306
- className: ["ox-github-icon"],
307
- viewBox: "0 0 16 16",
308
- fill: "currentColor"
309
- },
310
- children: [{
311
- type: "element",
312
- tagName: "path",
313
- properties: { d: "M2 2.5A2.5 2.5 0 0 1 4.5 0h8.75a.75.75 0 0 1 .75.75v12.5a.75.75 0 0 1-.75.75h-2.5a.75.75 0 0 1 0-1.5h1.75v-2h-8a1 1 0 0 0-.714 1.7.75.75 0 1 1-1.072 1.05A2.495 2.495 0 0 1 2 11.5Zm10.5-1h-8a1 1 0 0 0-1 1v6.708A2.486 2.486 0 0 1 4.5 9h8ZM5 12.25a.25.25 0 0 1 .25-.25h3.5a.25.25 0 0 1 .25.25v3.25a.25.25 0 0 1-.4.2l-1.45-1.087a.249.249 0 0 0-.3 0L5.4 15.7a.25.25 0 0 1-.4-.2Z" },
314
- children: []
315
- }]
316
- }, {
317
- type: "element",
318
- tagName: "span",
319
- properties: { className: ["ox-github-repo"] },
320
- children: [{
321
- type: "text",
322
- value: repoData.full_name
323
- }]
324
- }]
325
- },
326
- ...repoData.description ? [{
327
- type: "element",
328
- tagName: "p",
329
- properties: { className: ["ox-github-description"] },
330
- children: [{
331
- type: "text",
332
- value: repoData.description
333
- }]
334
- }] : [],
335
- {
336
- type: "element",
337
- tagName: "div",
338
- properties: { className: ["ox-github-stats"] },
339
- children: statsChildren
340
- }
341
- ]
342
- };
343
- }
344
- /**
345
- * Create fallback element when repo data is unavailable.
346
- */
347
- function createFallbackCard(repo) {
348
- return {
349
- type: "element",
350
- tagName: "a",
351
- properties: {
352
- className: ["ox-github-card", "error"],
353
- href: isSafeGitHubRepo(repo) ? `https://github.com/${repo}` : "#",
354
- target: "_blank",
355
- rel: "noopener noreferrer"
356
- },
357
- children: [{
358
- type: "element",
359
- tagName: "div",
360
- properties: { className: ["ox-github-header"] },
361
- children: [{
362
- type: "element",
363
- tagName: "svg",
364
- properties: {
365
- className: ["ox-github-icon"],
366
- viewBox: "0 0 16 16",
367
- fill: "currentColor"
368
- },
369
- children: [{
370
- type: "element",
371
- tagName: "path",
372
- properties: { d: "M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z" },
373
- children: []
374
- }]
375
- }, {
376
- type: "element",
377
- tagName: "span",
378
- properties: { className: ["ox-github-repo"] },
379
- children: [{
380
- type: "text",
381
- value: repo
382
- }]
383
- }]
384
- }]
385
- };
386
- }
387
- function inferLanguage(path) {
388
- const fileName = path.split("/").at(-1)?.toLowerCase() ?? "";
389
- if (fileName === "dockerfile") return "dockerfile";
390
- if (fileName === "makefile") return "makefile";
391
- const extension = fileName.includes(".") ? fileName.split(".").at(-1) : void 0;
392
- return extension ? EXTENSION_LANGUAGE_MAP.get(extension) ?? extension : null;
393
- }
394
- function normalizeSourceLines(content) {
395
- const lines = content.replace(/\r\n?/g, "\n").split("\n");
396
- if (lines.length > 1 && lines.at(-1) === "") lines.pop();
397
- return lines.length > 0 ? lines : [""];
398
- }
399
- function createGitHubSourceCard(source, lines, options) {
400
- const allLines = normalizeSourceLines(source.content);
401
- const start = Math.min(lines?.start ?? 1, allLines.length);
402
- const end = lines ? Math.min(lines.end, allLines.length) : Math.min(allLines.length, options.maxSourceLines);
403
- const selectedLines = allLines.slice(start - 1, end);
404
- const lineRange = {
405
- start,
406
- end
407
- };
408
- const loc = selectedLines.length;
409
- const rangeLabel = formatLineRange(lineRange);
410
- const locLabel = !lines && end < allLines.length ? `${rangeLabel} of ${allLines.length} LOC` : `${rangeLabel} - ${loc} LOC`;
411
- const languageClass = source.language ? [`language-${source.language}`] : [];
412
- return {
413
- type: "element",
414
- tagName: "figure",
415
- properties: {
416
- className: ["ox-github-code"],
417
- "data-loc": String(loc),
418
- "data-source": source.permalink
419
- },
420
- children: [{
421
- type: "element",
422
- tagName: "figcaption",
423
- properties: { className: ["ox-github-code-header"] },
424
- children: [{
425
- type: "element",
426
- tagName: "a",
427
- properties: {
428
- className: ["ox-github-code-title"],
429
- href: source.permalink,
430
- target: "_blank",
431
- rel: "noopener noreferrer"
432
- },
433
- children: [{
434
- type: "text",
435
- value: `${source.repo}/${source.path}`
436
- }]
437
- }, {
438
- type: "element",
439
- tagName: "span",
440
- properties: { className: ["ox-github-code-loc"] },
441
- children: [{
442
- type: "text",
443
- value: locLabel
444
- }]
445
- }]
446
- }, {
447
- type: "element",
448
- tagName: "pre",
449
- properties: {
450
- className: ["ox-github-code-block", ...languageClass],
451
- ...source.language ? { "data-language": source.language } : {}
452
- },
453
- children: [{
454
- type: "element",
455
- tagName: "code",
456
- properties: { className: languageClass },
457
- children: selectedLines.map((line, index) => {
458
- const lineNumber = start + index;
459
- return {
460
- type: "element",
461
- tagName: "span",
462
- properties: {
463
- className: ["line", "ox-github-code-line"],
464
- "data-line": String(lineNumber)
465
- },
466
- children: [{
467
- type: "element",
468
- tagName: "span",
469
- properties: { className: ["ox-github-code-line-number"] },
470
- children: [{
471
- type: "text",
472
- value: String(lineNumber)
473
- }]
474
- }, {
475
- type: "element",
476
- tagName: "span",
477
- properties: { className: ["ox-github-code-line-content"] },
478
- children: [{
479
- type: "text",
480
- value: line || " "
481
- }]
482
- }]
483
- };
484
- })
485
- }]
486
- }]
487
- };
488
- }
489
- /**
490
- * Collect all GitHub repos from HTML for pre-fetching.
491
- */
492
- async function collectGitHubRepos(html) {
493
- const repos = [];
494
- GITHUB_COMPONENT_RE.lastIndex = 0;
495
- let match;
496
- while ((match = GITHUB_COMPONENT_RE.exec(html)) !== null) {
497
- const attrs = parseAttributes(match[1]);
498
- if (attrs.path || attrs.file || attrs.permalink || attrs.url || attrs.href) continue;
499
- const repo = attrs.repo;
500
- if (repo && isSafeGitHubRepo(repo)) repos.push(repo);
501
- }
502
- return repos;
503
- }
504
- /**
505
- * Collect all GitHub source references from HTML for pre-fetching.
506
- */
507
- async function collectGitHubSources(html) {
508
- const sources = [];
509
- GITHUB_COMPONENT_RE.lastIndex = 0;
510
- let match;
511
- while ((match = GITHUB_COMPONENT_RE.exec(html)) !== null) {
512
- const source = sourceRefFromAttributes(parseAttributes(match[1]));
513
- if (source) sources.push(source);
514
- }
515
- return sources;
516
- }
517
- function parseAttributes(raw) {
518
- const attrs = {};
519
- ATTRIBUTE_RE.lastIndex = 0;
520
- let match;
521
- while ((match = ATTRIBUTE_RE.exec(raw)) !== null) attrs[match[1].toLowerCase()] = match[2] ?? match[3] ?? match[4] ?? "";
522
- return attrs;
523
- }
524
- function attributesFromElement(el) {
525
- const attrs = {};
526
- for (const name of [
527
- "permalink",
528
- "url",
529
- "href",
530
- "repo",
531
- "path",
532
- "file",
533
- "ref",
534
- "sha",
535
- "branch",
536
- "loc",
537
- "lines",
538
- "line"
539
- ]) {
540
- const value = getAttribute(el, name);
541
- if (value !== void 0) attrs[name] = value;
542
- }
543
- return attrs;
544
- }
545
- function sourceRefFromAttributes(attrs) {
546
- const permalink = attrs.permalink ?? attrs.url ?? attrs.href;
547
- if (permalink) return parseGitHubPermalink(permalink);
548
- const repo = attrs.repo;
549
- const path = attrs.path ?? attrs.file;
550
- if (!repo || !path || !isSafeGitHubRepo(repo) || !isSafeGitHubPath(path)) return null;
551
- const ref = attrs.ref ?? attrs.sha ?? attrs.branch ?? "main";
552
- if (!isSafeGitHubRef(ref)) return null;
553
- const source = {
554
- repo,
555
- ref,
556
- path,
557
- lines: parseGitHubLineRange(attrs.loc ?? attrs.lines ?? attrs.line)
558
- };
559
- return {
560
- ...source,
561
- permalink: createGitHubPermalink(source)
562
- };
563
- }
564
- /**
565
- * Pre-fetch all GitHub repos data.
566
- */
567
- async function prefetchGitHubRepos(repos, options) {
568
- const mergedOptions = {
569
- ...defaultOptions,
570
- ...options
571
- };
572
- const results = /* @__PURE__ */ new Map();
573
- await Promise.all(Array.from(new Set(repos)).map(async (repo) => {
574
- const data = await fetchRepoData(repo, mergedOptions);
575
- results.set(repo, data);
576
- }));
577
- return results;
578
- }
579
- /**
580
- * Pre-fetch all GitHub source files.
581
- */
582
- async function prefetchGitHubSources(sources, options) {
583
- const mergedOptions = {
584
- ...defaultOptions,
585
- ...options
586
- };
587
- const results = /* @__PURE__ */ new Map();
588
- const uniqueSources = Array.from(new Map(sources.map((source) => [sourceKey(source), source])).values());
589
- await Promise.all(uniqueSources.map(async (source) => {
590
- const data = await fetchGitHubSource(source, mergedOptions);
591
- results.set(sourceKey(source), data);
592
- }));
593
- return results;
594
- }
595
- /**
596
- * Rehype plugin to transform GitHub components.
597
- */
598
- function rehypeGitHub(repoDataMap, sourceDataMap, options) {
599
- return (tree) => {
600
- const visit = (node) => {
601
- if ("children" in node) for (let i = 0; i < node.children.length; i++) {
602
- const child = node.children[i];
603
- if (child.type === "element") if (child.tagName.toLowerCase() === "github") {
604
- const attrs = attributesFromElement(child);
605
- const source = sourceRefFromAttributes(attrs);
606
- if (source) {
607
- const sourceData = sourceDataMap.get(sourceKey(source));
608
- node.children[i] = sourceData ? createGitHubSourceCard(sourceData, source.lines, options) : createFallbackCard(source.permalink);
609
- continue;
610
- }
611
- const repo = attrs.repo;
612
- if (repo) {
613
- const repoData = repoDataMap.get(repo);
614
- const cardElement = repoData ? createGitHubCard(repoData) : createFallbackCard(repo);
615
- node.children[i] = cardElement;
616
- }
617
- } else visit(child);
618
- }
619
- };
620
- visit(tree);
621
- };
622
- }
623
- /**
624
- * Transform GitHub components in HTML.
625
- */
626
- async function transformGitHub(html, repoDataMap, options) {
627
- const mergedOptions = {
628
- ...defaultOptions,
629
- ...options
630
- };
631
- let dataMap = repoDataMap;
632
- if (!dataMap) dataMap = await prefetchGitHubRepos(await collectGitHubRepos(html), mergedOptions);
633
- const sourceDataMap = await prefetchGitHubSources(await collectGitHubSources(html), mergedOptions);
634
- const result = await unified().use(rehypeParse, { fragment: true }).use(rehypeGitHub, dataMap, sourceDataMap, mergedOptions).use(rehypeStringify).process(html);
635
- return String(result);
636
- }
637
- //#endregion
638
- export { github_exports as a, prefetchGitHubRepos as c, fetchRepoData as i, prefetchGitHubSources as l, collectGitHubSources as n, parseGitHubLineRange as o, fetchGitHubSource as r, parseGitHubPermalink as s, collectGitHubRepos as t, transformGitHub as u };
639
-
640
- //# sourceMappingURL=github.mjs.map
1
+ import { d as transformGitHub } from "./github2.mjs";
2
+ export { transformGitHub };