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