@t8/docsgen 0.4.19 → 0.4.21

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/bin.js CHANGED
@@ -349,7 +349,7 @@ async function getRepoMetadata({
349
349
  }
350
350
 
351
351
  // src/bin/parsing/getParsedContent.ts
352
- import { JSDOM as JSDOM2 } from "jsdom";
352
+ import { JSDOM as JSDOM3 } from "jsdom";
353
353
  import Markdown from "markdown-it";
354
354
 
355
355
  // src/bin/getSlug.ts
@@ -424,15 +424,46 @@ function getInstallationCode(element) {
424
424
  }
425
425
 
426
426
  // src/bin/parsing/getSectionPostprocess.ts
427
+ import { JSDOM as JSDOM2 } from "jsdom";
428
+ function isPreviewURL(url) {
429
+ return url.startsWith("https://codesandbox.io/p/sandbox/");
430
+ }
431
+ function getPreviewContent(url, title) {
432
+ let { pathname, searchParams } = new URL(url);
433
+ let sandboxId = pathname.split("/").at(-1);
434
+ let file = searchParams.get("file");
435
+ let previewURL = new URL(`https://codesandbox.io/embed/${sandboxId}`);
436
+ let previewContent = title ? `<legend>${title}</legend>` : "";
437
+ previewURL.searchParams.set("view", "preview");
438
+ if (file) previewURL.searchParams.set("module", file);
439
+ previewContent += `<iframe src="${previewURL.href}"${title ? ` title="${title}"` : ""} sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" loading="lazy"></iframe>`;
440
+ return previewContent;
441
+ }
427
442
  function getSectionPostprocess(linkMap) {
428
443
  return (content) => {
429
- let s = content;
430
- s = s.replace(/<a href="([^"]+)">/g, (_, url) => {
431
- let nextURL = linkMap[url] ?? url;
432
- let attrs = /^(https?:)?\/\//.test(nextURL) ? ' target="_blank"' : "";
433
- return `<a href="${nextURL}"${attrs}>`;
434
- });
435
- return s;
444
+ let { document } = new JSDOM2(content).window;
445
+ for (let a of document.querySelectorAll("a")) {
446
+ let href = a.getAttribute("href");
447
+ if (href === null) continue;
448
+ if (isPreviewURL(href)) {
449
+ let preview = document.createElement("fieldset");
450
+ preview.innerHTML = getPreviewContent(href, a.innerHTML);
451
+ let p = a.closest("p");
452
+ if (p?.parentElement) {
453
+ p.parentElement.insertBefore(preview, p);
454
+ if (a.nextElementSibling?.matches("br"))
455
+ a.nextElementSibling.remove();
456
+ a.remove();
457
+ if (p.innerHTML.trim() === "") p.remove();
458
+ }
459
+ } else {
460
+ let nextHref = linkMap[href] ?? href;
461
+ a.setAttribute("href", nextHref);
462
+ if (/^(https?:)?\/\//.test(nextHref))
463
+ a.setAttribute("target", "_blank");
464
+ }
465
+ }
466
+ return document.body.innerHTML;
436
467
  };
437
468
  }
438
469
 
@@ -485,7 +516,7 @@ async function getParsedContent(ctx) {
485
516
  let { singlePage, firstLineDescription, linkMap } = ctx;
486
517
  let rawContent = await fetchContent(contentLocation);
487
518
  let content = md.render(preprocessContent(rawContent));
488
- let dom = new JSDOM2(content);
519
+ let dom = new JSDOM3(content);
489
520
  let { nav, linkMap: navLinkMap } = buildNav(ctx, dom);
490
521
  let badges = "";
491
522
  let title = "";
@@ -802,7 +833,7 @@ function escapeRegExp(s) {
802
833
  }
803
834
 
804
835
  // src/bin/content/getNav.ts
805
- import { JSDOM as JSDOM3 } from "jsdom";
836
+ import { JSDOM as JSDOM4 } from "jsdom";
806
837
 
807
838
  // src/bin/getNpmLink.ts
808
839
  function getNpmLink({ npm }, className) {
@@ -816,7 +847,7 @@ async function getNav(ctx, navItems) {
816
847
  let navContent = await fetchContent(nav);
817
848
  let s = "";
818
849
  if (navContent) {
819
- let navDom = new JSDOM3(navContent).window.document.body;
850
+ let navDom = new JSDOM4(navContent).window.document.body;
820
851
  for (let link of navDom.querySelectorAll("a")) {
821
852
  if (link.dataset.name === name) {
822
853
  let parent = link.parentElement;
@@ -250,6 +250,21 @@ main h2 {
250
250
  main h2:first-child {
251
251
  margin-top: 0;
252
252
  }
253
+ main fieldset {
254
+ border: 0.05em solid var(--secondary-color);
255
+ margin: var(--block-margin-y) 0;
256
+ }
257
+ main fieldset legend {
258
+ font-size: 0.8em;
259
+ color: var(--secondary-color);
260
+ }
261
+ main fieldset iframe {
262
+ width: 100%;
263
+ height: 360px;
264
+ background: var(--code-block-background);
265
+ border: none;
266
+ overflow: hidden;
267
+ }
253
268
 
254
269
  .pagenav {
255
270
  --icon-width: 1.25em;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/docsgen",
3
- "version": "0.4.19",
3
+ "version": "0.4.21",
4
4
  "description": "",
5
5
  "main": "dist/bin.js",
6
6
  "type": "module",
@@ -1,16 +1,63 @@
1
+ import { JSDOM } from "jsdom";
2
+
3
+ function isPreviewURL(url: string) {
4
+ return url.startsWith("https://codesandbox.io/p/sandbox/");
5
+ }
6
+
7
+ function getPreviewContent(url: string, title?: string) {
8
+ let { pathname, searchParams } = new URL(url);
9
+ let sandboxId = pathname.split("/").at(-1);
10
+ let file = searchParams.get("file");
11
+
12
+ let previewURL = new URL(`https://codesandbox.io/embed/${sandboxId}`);
13
+ let previewContent = title ? `<legend>${title}</legend>` : "";
14
+
15
+ previewURL.searchParams.set("view", "preview");
16
+
17
+ if (file) previewURL.searchParams.set("module", file);
18
+
19
+ previewContent += `<iframe src="${previewURL.href}"${title ? ` title="${title}"` : ""} sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" loading="lazy"></iframe>`;
20
+
21
+ return previewContent;
22
+ }
23
+
1
24
  export function getSectionPostprocess(
2
25
  linkMap: Record<string, string | undefined>,
3
26
  ) {
4
27
  return (content: string) => {
5
- let s = content;
28
+ let { document } = new JSDOM(content).window;
29
+
30
+ for (let a of document.querySelectorAll("a")) {
31
+ let href = a.getAttribute("href");
32
+
33
+ if (href === null) continue;
34
+
35
+ if (isPreviewURL(href)) {
36
+ let preview = document.createElement("fieldset");
37
+
38
+ preview.innerHTML = getPreviewContent(href, a.innerHTML);
39
+
40
+ let p = a.closest("p");
41
+
42
+ if (p?.parentElement) {
43
+ p.parentElement.insertBefore(preview, p);
44
+
45
+ if (a.nextElementSibling?.matches("br"))
46
+ a.nextElementSibling.remove();
47
+
48
+ a.remove();
6
49
 
7
- s = s.replace(/<a href="([^"]+)">/g, (_, url) => {
8
- let nextURL = linkMap[url] ?? url;
9
- let attrs = /^(https?:)?\/\//.test(nextURL) ? ' target="_blank"' : "";
50
+ if (p.innerHTML.trim() === "") p.remove();
51
+ }
52
+ } else {
53
+ let nextHref = linkMap[href] ?? href;
10
54
 
11
- return `<a href="${nextURL}"${attrs}>`;
12
- });
55
+ a.setAttribute("href", nextHref);
56
+ if (/^(https?:)?\/\//.test(nextHref))
57
+ a.setAttribute("target", "_blank");
58
+ }
59
+ }
13
60
 
14
- return s;
61
+ return document.body.innerHTML;
15
62
  };
16
63
  }
@@ -250,6 +250,21 @@ main h2 {
250
250
  main h2:first-child {
251
251
  margin-top: 0;
252
252
  }
253
+ main fieldset {
254
+ border: 0.05em solid var(--secondary-color);
255
+ margin: var(--block-margin-y) 0;
256
+ }
257
+ main fieldset legend {
258
+ font-size: 0.8em;
259
+ color: var(--secondary-color);
260
+ }
261
+ main fieldset iframe {
262
+ width: 100%;
263
+ height: 360px;
264
+ background: var(--code-block-background);
265
+ border: none;
266
+ overflow: hidden;
267
+ }
253
268
 
254
269
  .pagenav {
255
270
  --icon-width: 1.25em;