@t8/docsgen 0.4.21 → 0.4.23

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
@@ -432,12 +432,37 @@ function getPreviewContent(url, title) {
432
432
  let { pathname, searchParams } = new URL(url);
433
433
  let sandboxId = pathname.split("/").at(-1);
434
434
  let file = searchParams.get("file");
435
+ let height = searchParams.get("h");
435
436
  let previewURL = new URL(`https://codesandbox.io/embed/${sandboxId}`);
436
- let previewContent = title ? `<legend>${title}</legend>` : "";
437
+ let content = title ? `<legend>${title}</legend>` : "";
437
438
  previewURL.searchParams.set("view", "preview");
438
439
  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;
440
+ let attrs = [`src="${previewURL.href}"`];
441
+ if (title) attrs.push(`title="${title}"`);
442
+ if (height) attrs.push(`style="height: ${height}px;"`);
443
+ content += `<iframe ${attrs.join(" ")} sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" loading="lazy"></iframe>`;
444
+ return content;
445
+ }
446
+ function isEmpty(element) {
447
+ return element.innerHTML.trim() === "";
448
+ }
449
+ function insertPreview(preview, a) {
450
+ let p = a.closest("p");
451
+ if (p?.parentElement) {
452
+ p.parentElement.insertBefore(preview, p);
453
+ if (a.nextElementSibling?.matches("br")) a.nextElementSibling.remove();
454
+ a.remove();
455
+ if (isEmpty(p)) p.remove();
456
+ return;
457
+ }
458
+ let li = a.closest("li");
459
+ let list = li?.closest("ul, ol");
460
+ if (li && list?.parentElement) {
461
+ list.parentElement.insertBefore(preview, list);
462
+ a.remove();
463
+ if (isEmpty(li)) li.remove();
464
+ if (isEmpty(list)) list.remove();
465
+ }
441
466
  }
442
467
  function getSectionPostprocess(linkMap) {
443
468
  return (content) => {
@@ -448,14 +473,7 @@ function getSectionPostprocess(linkMap) {
448
473
  if (isPreviewURL(href)) {
449
474
  let preview = document.createElement("fieldset");
450
475
  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
- }
476
+ insertPreview(preview, a);
459
477
  } else {
460
478
  let nextHref = linkMap[href] ?? href;
461
479
  a.setAttribute("href", nextHref);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/docsgen",
3
- "version": "0.4.21",
3
+ "version": "0.4.23",
4
4
  "description": "",
5
5
  "main": "dist/bin.js",
6
6
  "type": "module",
@@ -7,18 +7,57 @@ function isPreviewURL(url: string) {
7
7
  function getPreviewContent(url: string, title?: string) {
8
8
  let { pathname, searchParams } = new URL(url);
9
9
  let sandboxId = pathname.split("/").at(-1);
10
+
10
11
  let file = searchParams.get("file");
12
+ let height = searchParams.get("h");
11
13
 
12
14
  let previewURL = new URL(`https://codesandbox.io/embed/${sandboxId}`);
13
- let previewContent = title ? `<legend>${title}</legend>` : "";
15
+ let content = title ? `<legend>${title}</legend>` : "";
14
16
 
15
17
  previewURL.searchParams.set("view", "preview");
16
18
 
17
19
  if (file) previewURL.searchParams.set("module", file);
18
20
 
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>`;
21
+ let attrs = [`src="${previewURL.href}"`];
22
+
23
+ if (title) attrs.push(`title="${title}"`);
24
+ if (height) attrs.push(`style="height: ${height}px;"`);
25
+
26
+ content += `<iframe ${attrs.join(" ")} sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" loading="lazy"></iframe>`;
20
27
 
21
- return previewContent;
28
+ return content;
29
+ }
30
+
31
+ function isEmpty(element: Element) {
32
+ return element.innerHTML.trim() === "";
33
+ }
34
+
35
+ function insertPreview(preview: HTMLElement, a: HTMLAnchorElement) {
36
+ let p: HTMLElement | null = a.closest("p");
37
+
38
+ if (p?.parentElement) {
39
+ p.parentElement.insertBefore(preview, p);
40
+
41
+ if (a.nextElementSibling?.matches("br")) a.nextElementSibling.remove();
42
+
43
+ a.remove();
44
+
45
+ if (isEmpty(p)) p.remove();
46
+
47
+ return;
48
+ }
49
+
50
+ let li = a.closest("li");
51
+ let list = li?.closest("ul, ol");
52
+
53
+ if (li && list?.parentElement) {
54
+ list.parentElement.insertBefore(preview, list);
55
+
56
+ a.remove();
57
+
58
+ if (isEmpty(li)) li.remove();
59
+ if (isEmpty(list)) list.remove();
60
+ }
22
61
  }
23
62
 
24
63
  export function getSectionPostprocess(
@@ -37,22 +76,12 @@ export function getSectionPostprocess(
37
76
 
38
77
  preview.innerHTML = getPreviewContent(href, a.innerHTML);
39
78
 
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();
49
-
50
- if (p.innerHTML.trim() === "") p.remove();
51
- }
79
+ insertPreview(preview, a);
52
80
  } else {
53
81
  let nextHref = linkMap[href] ?? href;
54
82
 
55
83
  a.setAttribute("href", nextHref);
84
+
56
85
  if (/^(https?:)?\/\//.test(nextHref))
57
86
  a.setAttribute("target", "_blank");
58
87
  }