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