@t8/docsgen 0.3.33 → 0.3.35
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 +58 -50
- package/package.json +1 -1
- package/src/bin/content/getCSSRoot.ts +16 -10
- package/src/bin/content/getSectionContent.ts +52 -52
- package/src/bin/fetchContent.ts +13 -4
package/dist/bin.js
CHANGED
|
@@ -116,18 +116,23 @@ var import_args_json = __toESM(require_dist(), 1);
|
|
|
116
116
|
|
|
117
117
|
// src/bin/fetchContent.ts
|
|
118
118
|
import { readFile } from "node:fs/promises";
|
|
119
|
+
var cachedContent = /* @__PURE__ */ new Map();
|
|
119
120
|
async function fetchContent(location) {
|
|
120
121
|
if (!location) return "";
|
|
122
|
+
let content = cachedContent.get(location);
|
|
123
|
+
if (content !== void 0) return content;
|
|
121
124
|
if (/^https?:\/\//.test(location)) {
|
|
122
125
|
try {
|
|
123
|
-
|
|
126
|
+
content = await (await fetch(location, { cache: "no-cache" })).text();
|
|
124
127
|
} catch {
|
|
125
128
|
console.warn(`Failed to fetch content from '${location}'`);
|
|
126
|
-
return "";
|
|
127
129
|
}
|
|
130
|
+
} else {
|
|
131
|
+
let locationPath = location.replace(/^\//, "");
|
|
132
|
+
content = (await readFile(locationPath)).toString();
|
|
128
133
|
}
|
|
129
|
-
|
|
130
|
-
return
|
|
134
|
+
cachedContent.set(location, content ??= "");
|
|
135
|
+
return content;
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
// src/bin/getLocation.ts
|
|
@@ -572,6 +577,7 @@ var packageName = "@t8/docsgen";
|
|
|
572
577
|
var exec = promisify(defaultExec);
|
|
573
578
|
var __filename = fileURLToPath(import.meta.url);
|
|
574
579
|
var __dirname = dirname(__filename);
|
|
580
|
+
var packageURL = "";
|
|
575
581
|
async function getCSSRoot(ctx, type) {
|
|
576
582
|
let { dir = "", assetsDir } = ctx;
|
|
577
583
|
let cssRoot = {
|
|
@@ -586,10 +592,12 @@ async function getCSSRoot(ctx, type) {
|
|
|
586
592
|
recursive: true
|
|
587
593
|
});
|
|
588
594
|
} else {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
595
|
+
if (!packageURL) {
|
|
596
|
+
let packageVersion = (await exec(`npm view ${packageName} version`)).stdout.trim().split(".").slice(0, 2).join(".");
|
|
597
|
+
packageURL = `https://unpkg.com/${packageName}@${packageVersion}`;
|
|
598
|
+
}
|
|
599
|
+
cssRoot.index = `${packageURL}/dist/css`;
|
|
600
|
+
cssRoot.content = `${packageURL}/dist/css`;
|
|
593
601
|
}
|
|
594
602
|
return cssRoot[type];
|
|
595
603
|
}
|
|
@@ -880,53 +888,53 @@ async function getSectionContent(ctx, index) {
|
|
|
880
888
|
let navContent = await getNav(ctx, nav);
|
|
881
889
|
let plainTitle = await getPlainTitle(ctx);
|
|
882
890
|
return toFileContent(`
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
891
|
+
<!DOCTYPE html>
|
|
892
|
+
<html lang="en" data-layout="section">
|
|
893
|
+
<head>
|
|
894
|
+
${getInjectedContent(ctx, "section", "head", "prepend")}
|
|
895
|
+
<meta charset="utf-8">
|
|
896
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
897
|
+
<meta name="description" content="${plainTitle}: ${escapeHTML(stripHTML(nav[index]?.title, true))}">
|
|
898
|
+
<title>${escapeHTML(stripHTML(nav[index]?.title, true))} | ${plainTitle}</title>
|
|
899
|
+
<link rel="stylesheet" href="${cssRoot}/base.css">
|
|
900
|
+
<link rel="stylesheet" href="${cssRoot}/section.css">
|
|
901
|
+
${getIconTag(ctx)}
|
|
902
|
+
${nav[index + 1]?.id ? `<link rel="prefetch" href="${root}${contentDir}/${nav[index + 1]?.id}">` : ""}
|
|
903
|
+
${nav[index - 1]?.id ? `<link rel="prefetch" href="${root}${contentDir}/${nav[index - 1]?.id}">` : ""}
|
|
904
|
+
${getInjectedContent(ctx, "section", "head", "append")}
|
|
905
|
+
</head>
|
|
906
|
+
<body>
|
|
907
|
+
${getInjectedContent(ctx, "section", "body", "prepend")}
|
|
908
|
+
<div class="layout">
|
|
909
|
+
<div class="${navContent ? "" : "no-nav "}body">
|
|
910
|
+
<main>
|
|
911
|
+
<h1><a href="${root}">${plainTitle}</a></h1>
|
|
912
|
+
${content}
|
|
913
|
+
|
|
914
|
+
<p class="pagenav">
|
|
915
|
+
<span class="prev">
|
|
916
|
+
<span class="icon">\u2190</span>
|
|
917
|
+
${nav[index - 1]?.id ? `<a href="${root}${contentDir}/${nav[index - 1]?.id}">${nav[index - 1]?.title}</a>` : `<a href="${root}">Intro</a>`}
|
|
918
|
+
</span>
|
|
919
|
+
<span class="sep">|</span>
|
|
920
|
+
${nav[index + 1]?.id ? `<span class="next"><a href="${root}${contentDir}/${nav[index + 1]?.id}">${nav[index + 1]?.title}</a> <span class="icon">\u2192</span></span>` : `<span class="repo">${getRepoLink(ctx)}</span>`}
|
|
921
|
+
</p>
|
|
922
|
+
</main>
|
|
923
|
+
${navContent ? "<hr>" : ""}
|
|
924
|
+
${navContent.replace(
|
|
917
925
|
new RegExp(
|
|
918
926
|
`(<li data-id="${escapeRegExp(nav[index]?.id)}">)<a href="[^"]+">([^<]+)</a>`
|
|
919
927
|
),
|
|
920
928
|
"$1<strong>$2</strong>"
|
|
921
929
|
)}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
+
</div>
|
|
931
|
+
</div>
|
|
932
|
+
|
|
933
|
+
${content.includes("<pre><code ") ? getInjectedContent(ctx, "section", ":has-code", "append") || getDefaultCodeStyleContent(cssRoot) : ""}
|
|
934
|
+
${getCounterContent(ctx)}
|
|
935
|
+
${getInjectedContent(ctx, "section", "body", "append")}
|
|
936
|
+
</body>
|
|
937
|
+
</html>
|
|
930
938
|
`);
|
|
931
939
|
}
|
|
932
940
|
|
package/package.json
CHANGED
|
@@ -11,6 +11,8 @@ const exec = promisify(defaultExec);
|
|
|
11
11
|
const __filename = fileURLToPath(import.meta.url);
|
|
12
12
|
const __dirname = dirname(__filename);
|
|
13
13
|
|
|
14
|
+
let packageURL = "";
|
|
15
|
+
|
|
14
16
|
export async function getCSSRoot(ctx: Context, type: "index" | "content") {
|
|
15
17
|
let { dir = "", assetsDir } = ctx;
|
|
16
18
|
|
|
@@ -28,16 +30,20 @@ export async function getCSSRoot(ctx: Context, type: "index" | "content") {
|
|
|
28
30
|
recursive: true,
|
|
29
31
|
});
|
|
30
32
|
} else {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
if (!packageURL) {
|
|
34
|
+
let packageVersion = (
|
|
35
|
+
await exec(`npm view ${packageName} version`)
|
|
36
|
+
).stdout
|
|
37
|
+
.trim()
|
|
38
|
+
.split(".")
|
|
39
|
+
.slice(0, 2)
|
|
40
|
+
.join(".");
|
|
41
|
+
|
|
42
|
+
packageURL = `https://unpkg.com/${packageName}@${packageVersion}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
cssRoot.index = `${packageURL}/dist/css`;
|
|
46
|
+
cssRoot.content = `${packageURL}/dist/css`;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
return cssRoot[type];
|
|
@@ -24,57 +24,57 @@ export async function getSectionContent(ctx: Context, index: number) {
|
|
|
24
24
|
let plainTitle = await getPlainTitle(ctx);
|
|
25
25
|
|
|
26
26
|
return toFileContent(`
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
27
|
+
<!DOCTYPE html>
|
|
28
|
+
<html lang="en" data-layout="section">
|
|
29
|
+
<head>
|
|
30
|
+
${getInjectedContent(ctx, "section", "head", "prepend")}
|
|
31
|
+
<meta charset="utf-8">
|
|
32
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
33
|
+
<meta name="description" content="${plainTitle}: ${escapeHTML(stripHTML(nav[index]?.title, true))}">
|
|
34
|
+
<title>${escapeHTML(stripHTML(nav[index]?.title, true))} | ${plainTitle}</title>
|
|
35
|
+
<link rel="stylesheet" href="${cssRoot}/base.css">
|
|
36
|
+
<link rel="stylesheet" href="${cssRoot}/section.css">
|
|
37
|
+
${getIconTag(ctx)}
|
|
38
|
+
${nav[index + 1]?.id ? `<link rel="prefetch" href="${root}${contentDir}/${nav[index + 1]?.id}">` : ""}
|
|
39
|
+
${nav[index - 1]?.id ? `<link rel="prefetch" href="${root}${contentDir}/${nav[index - 1]?.id}">` : ""}
|
|
40
|
+
${getInjectedContent(ctx, "section", "head", "append")}
|
|
41
|
+
</head>
|
|
42
|
+
<body>
|
|
43
|
+
${getInjectedContent(ctx, "section", "body", "prepend")}
|
|
44
|
+
<div class="layout">
|
|
45
|
+
<div class="${navContent ? "" : "no-nav "}body">
|
|
46
|
+
<main>
|
|
47
|
+
<h1><a href="${root}">${plainTitle}</a></h1>
|
|
48
|
+
${content}
|
|
49
|
+
|
|
50
|
+
<p class="pagenav">
|
|
51
|
+
<span class="prev">
|
|
52
|
+
<span class="icon">←</span>
|
|
53
|
+
${nav[index - 1]?.id ? `<a href="${root}${contentDir}/${nav[index - 1]?.id}">${nav[index - 1]?.title}</a>` : `<a href="${root}">Intro</a>`}
|
|
54
|
+
</span>
|
|
55
|
+
<span class="sep">|</span>
|
|
56
|
+
${nav[index + 1]?.id ? `<span class="next"><a href="${root}${contentDir}/${nav[index + 1]?.id}">${nav[index + 1]?.title}</a> <span class="icon">→</span></span>` : `<span class="repo">${getRepoLink(ctx)}</span>`}
|
|
57
|
+
</p>
|
|
58
|
+
</main>
|
|
59
|
+
${navContent ? "<hr>" : ""}
|
|
60
|
+
${navContent.replace(
|
|
61
|
+
new RegExp(
|
|
62
|
+
`(<li data-id="${escapeRegExp(nav[index]?.id)}">)<a href="[^"]+">([^<]+)</a>`,
|
|
63
|
+
),
|
|
64
|
+
"$1<strong>$2</strong>",
|
|
65
|
+
)}
|
|
66
|
+
</div>
|
|
67
|
+
</div>
|
|
68
|
+
|
|
69
|
+
${
|
|
70
|
+
content.includes("<pre><code ")
|
|
71
|
+
? getInjectedContent(ctx, "section", ":has-code", "append") ||
|
|
72
|
+
getDefaultCodeStyleContent(cssRoot)
|
|
73
|
+
: ""
|
|
74
|
+
}
|
|
75
|
+
${getCounterContent(ctx)}
|
|
76
|
+
${getInjectedContent(ctx, "section", "body", "append")}
|
|
77
|
+
</body>
|
|
78
|
+
</html>
|
|
79
79
|
`);
|
|
80
80
|
}
|
package/src/bin/fetchContent.ts
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
|
|
3
|
+
let cachedContent = new Map<string, string>();
|
|
4
|
+
|
|
3
5
|
export async function fetchContent(location: string | undefined) {
|
|
4
6
|
if (!location) return "";
|
|
5
7
|
|
|
8
|
+
let content = cachedContent.get(location);
|
|
9
|
+
|
|
10
|
+
if (content !== undefined) return content;
|
|
11
|
+
|
|
6
12
|
if (/^https?:\/\//.test(location)) {
|
|
7
13
|
try {
|
|
8
|
-
|
|
14
|
+
content = await (await fetch(location, { cache: "no-cache" })).text();
|
|
9
15
|
} catch {
|
|
10
16
|
console.warn(`Failed to fetch content from '${location}'`);
|
|
11
|
-
return "";
|
|
12
17
|
}
|
|
18
|
+
} else {
|
|
19
|
+
let locationPath = location.replace(/^\//, "");
|
|
20
|
+
|
|
21
|
+
content = (await readFile(locationPath)).toString();
|
|
13
22
|
}
|
|
14
23
|
|
|
15
|
-
|
|
24
|
+
cachedContent.set(location, (content ??= ""));
|
|
16
25
|
|
|
17
|
-
return
|
|
26
|
+
return content;
|
|
18
27
|
}
|