@t8/docsgen 0.4.20 → 0.4.22

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,16 @@ 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;
441
445
  }
442
446
  function getSectionPostprocess(linkMap) {
443
447
  return (content) => {
@@ -445,11 +449,17 @@ function getSectionPostprocess(linkMap) {
445
449
  for (let a of document.querySelectorAll("a")) {
446
450
  let href = a.getAttribute("href");
447
451
  if (href === null) continue;
448
- if (isPreviewURL(href) && a.parentElement) {
452
+ if (isPreviewURL(href)) {
449
453
  let preview = document.createElement("fieldset");
450
454
  preview.innerHTML = getPreviewContent(href, a.innerHTML);
451
- a.parentElement.insertBefore(preview, a);
452
- a.remove();
455
+ let p = a.closest("p");
456
+ if (p?.parentElement) {
457
+ p.parentElement.insertBefore(preview, p);
458
+ if (a.nextElementSibling?.matches("br"))
459
+ a.nextElementSibling.remove();
460
+ a.remove();
461
+ if (p.innerHTML.trim() === "") p.remove();
462
+ }
453
463
  } else {
454
464
  let nextHref = linkMap[href] ?? href;
455
465
  a.setAttribute("href", nextHref);
@@ -252,6 +252,7 @@ main h2:first-child {
252
252
  }
253
253
  main fieldset {
254
254
  border: 0.05em solid var(--secondary-color);
255
+ margin: var(--block-margin-y) 0;
255
256
  }
256
257
  main fieldset legend {
257
258
  font-size: 0.8em;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/docsgen",
3
- "version": "0.4.20",
3
+ "version": "0.4.22",
4
4
  "description": "",
5
5
  "main": "dist/bin.js",
6
6
  "type": "module",
@@ -7,18 +7,25 @@ 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;"`);
20
25
 
21
- return previewContent;
26
+ content += `<iframe ${attrs.join(" ")} sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" loading="lazy"></iframe>`;
27
+
28
+ return content;
22
29
  }
23
30
 
24
31
  export function getSectionPostprocess(
@@ -32,17 +39,28 @@ export function getSectionPostprocess(
32
39
 
33
40
  if (href === null) continue;
34
41
 
35
- if (isPreviewURL(href) && a.parentElement) {
42
+ if (isPreviewURL(href)) {
36
43
  let preview = document.createElement("fieldset");
37
44
 
38
45
  preview.innerHTML = getPreviewContent(href, a.innerHTML);
39
46
 
40
- a.parentElement.insertBefore(preview, a);
41
- a.remove();
47
+ let p = a.closest("p");
48
+
49
+ if (p?.parentElement) {
50
+ p.parentElement.insertBefore(preview, p);
51
+
52
+ if (a.nextElementSibling?.matches("br"))
53
+ a.nextElementSibling.remove();
54
+
55
+ a.remove();
56
+
57
+ if (p.innerHTML.trim() === "") p.remove();
58
+ }
42
59
  } else {
43
60
  let nextHref = linkMap[href] ?? href;
44
61
 
45
62
  a.setAttribute("href", nextHref);
63
+
46
64
  if (/^(https?:)?\/\//.test(nextHref))
47
65
  a.setAttribute("target", "_blank");
48
66
  }
@@ -252,6 +252,7 @@ main h2:first-child {
252
252
  }
253
253
  main fieldset {
254
254
  border: 0.05em solid var(--secondary-color);
255
+ margin: var(--block-margin-y) 0;
255
256
  }
256
257
  main fieldset legend {
257
258
  font-size: 0.8em;