@t8/docsgen 0.1.5 → 0.1.6
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 +22 -17
- package/package.json +1 -1
- package/src/bin/getConfig.ts +11 -7
- package/src/bin/getLocation.ts +22 -0
- package/src/bin/getParsedContent.ts +3 -2
- package/src/bin/getSource.ts +0 -16
package/dist/bin.js
CHANGED
|
@@ -92,6 +92,17 @@ async function fetchText(location) {
|
|
|
92
92
|
return (await (0, import_promises.readFile)(location)).toString();
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
// src/bin/getLocation.ts
|
|
96
|
+
function getLocation(ctx, path, preferredLocation) {
|
|
97
|
+
let { repo, mainBranch = "main" } = ctx;
|
|
98
|
+
if (preferredLocation) return preferredLocation;
|
|
99
|
+
let ghId = repo?.startsWith("https://github.com/") ? repo.replace("https://github.com/", "").split("/").slice(0, 2).join("/") : "";
|
|
100
|
+
let urlPath = path.replace(/^\.?\//, "");
|
|
101
|
+
if (ghId)
|
|
102
|
+
return `https://raw.githubusercontent.com/${ghId}/refs/heads/${mainBranch}/${urlPath}`;
|
|
103
|
+
return path;
|
|
104
|
+
}
|
|
105
|
+
|
|
95
106
|
// src/bin/toRepoURL.ts
|
|
96
107
|
function toRepoURL(x) {
|
|
97
108
|
if (!x) return "";
|
|
@@ -122,11 +133,6 @@ function toConfig(metadata) {
|
|
|
122
133
|
var config = null;
|
|
123
134
|
async function getConfig() {
|
|
124
135
|
if (config) return config;
|
|
125
|
-
let metadata = {};
|
|
126
|
-
try {
|
|
127
|
-
metadata = JSON.parse(await fetchText("./package.json"));
|
|
128
|
-
} catch {
|
|
129
|
-
}
|
|
130
136
|
let localConfig = {};
|
|
131
137
|
try {
|
|
132
138
|
localConfig = JSON.parse(
|
|
@@ -142,10 +148,18 @@ async function getConfig() {
|
|
|
142
148
|
mainBranch: "main",
|
|
143
149
|
root: "/",
|
|
144
150
|
contentDir: "x",
|
|
145
|
-
...toConfig(metadata),
|
|
146
151
|
...localConfig,
|
|
147
152
|
...A(args)
|
|
148
153
|
};
|
|
154
|
+
try {
|
|
155
|
+
let rawContent = await fetchText(getLocation(config, "package.json"));
|
|
156
|
+
let metadata = JSON.parse(rawContent);
|
|
157
|
+
config = {
|
|
158
|
+
...toConfig(metadata),
|
|
159
|
+
...config
|
|
160
|
+
};
|
|
161
|
+
} catch {
|
|
162
|
+
}
|
|
149
163
|
if (!config.root?.endsWith("/")) config.root = `${config.root ?? ""}/`;
|
|
150
164
|
return config;
|
|
151
165
|
}
|
|
@@ -324,16 +338,6 @@ function getSlug(title) {
|
|
|
324
338
|
return slug;
|
|
325
339
|
}
|
|
326
340
|
|
|
327
|
-
// src/bin/getSource.ts
|
|
328
|
-
function getSource(ctx) {
|
|
329
|
-
let { source, repo, mainBranch = "main" } = ctx;
|
|
330
|
-
if (source) return source;
|
|
331
|
-
let ghId = repo?.startsWith("https://github.com/") ? repo.replace("https://github.com/", "").split("/").slice(0, 2).join("/") : "";
|
|
332
|
-
if (ghId)
|
|
333
|
-
return `https://raw.githubusercontent.com/${ghId}/refs/heads/${mainBranch}/README.md`;
|
|
334
|
-
return "README.md";
|
|
335
|
-
}
|
|
336
|
-
|
|
337
341
|
// src/bin/getParsedContent.ts
|
|
338
342
|
var md = new import_markdown_it.default({
|
|
339
343
|
html: true
|
|
@@ -405,7 +409,8 @@ function getSectionPostprocess(linkMap) {
|
|
|
405
409
|
}
|
|
406
410
|
async function getParsedContent(ctx) {
|
|
407
411
|
let { singlePage } = ctx;
|
|
408
|
-
let
|
|
412
|
+
let rawContent = await fetchText(getLocation(ctx, "README.md", ctx.source));
|
|
413
|
+
let content = md.render(rawContent);
|
|
409
414
|
let dom = new import_jsdom2.JSDOM(content);
|
|
410
415
|
let { nav, linkMap } = buildNav(ctx, dom);
|
|
411
416
|
let badges = [];
|
package/package.json
CHANGED
package/src/bin/getConfig.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { parseArgs } from "args-json";
|
|
|
2
2
|
import type { BinConfig } from "../types/BinConfig";
|
|
3
3
|
import type { PackageMetadata } from "../types/PackageMetadata";
|
|
4
4
|
import { fetchText } from "./fetchText";
|
|
5
|
+
import { getLocation } from "./getLocation";
|
|
5
6
|
import { toConfig } from "./toConfig";
|
|
6
7
|
|
|
7
8
|
let config: BinConfig | null = null;
|
|
@@ -9,12 +10,6 @@ let config: BinConfig | null = null;
|
|
|
9
10
|
export async function getConfig(): Promise<BinConfig> {
|
|
10
11
|
if (config) return config;
|
|
11
12
|
|
|
12
|
-
let metadata: PackageMetadata = {};
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
metadata = JSON.parse(await fetchText("./package.json")) as PackageMetadata;
|
|
16
|
-
} catch {}
|
|
17
|
-
|
|
18
13
|
let localConfig: BinConfig = {};
|
|
19
14
|
|
|
20
15
|
try {
|
|
@@ -33,11 +28,20 @@ export async function getConfig(): Promise<BinConfig> {
|
|
|
33
28
|
mainBranch: "main",
|
|
34
29
|
root: "/",
|
|
35
30
|
contentDir: "x",
|
|
36
|
-
...toConfig(metadata),
|
|
37
31
|
...localConfig,
|
|
38
32
|
...parseArgs<BinConfig>(args),
|
|
39
33
|
};
|
|
40
34
|
|
|
35
|
+
try {
|
|
36
|
+
let rawContent = await fetchText(getLocation(config, "package.json"));
|
|
37
|
+
let metadata = JSON.parse(rawContent) as PackageMetadata;
|
|
38
|
+
|
|
39
|
+
config = {
|
|
40
|
+
...toConfig(metadata),
|
|
41
|
+
...config,
|
|
42
|
+
};
|
|
43
|
+
} catch {}
|
|
44
|
+
|
|
41
45
|
if (!config.root?.endsWith("/")) config.root = `${config.root ?? ""}/`;
|
|
42
46
|
|
|
43
47
|
return config;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Context } from "../types/Context";
|
|
2
|
+
|
|
3
|
+
export function getLocation(
|
|
4
|
+
ctx: Context,
|
|
5
|
+
path: string,
|
|
6
|
+
preferredLocation?: string | undefined,
|
|
7
|
+
) {
|
|
8
|
+
let { repo, mainBranch = "main" } = ctx;
|
|
9
|
+
|
|
10
|
+
if (preferredLocation) return preferredLocation;
|
|
11
|
+
|
|
12
|
+
let ghId = repo?.startsWith("https://github.com/")
|
|
13
|
+
? repo.replace("https://github.com/", "").split("/").slice(0, 2).join("/")
|
|
14
|
+
: "";
|
|
15
|
+
|
|
16
|
+
let urlPath = path.replace(/^\.?\//, "");
|
|
17
|
+
|
|
18
|
+
if (ghId)
|
|
19
|
+
return `https://raw.githubusercontent.com/${ghId}/refs/heads/${mainBranch}/${urlPath}`;
|
|
20
|
+
|
|
21
|
+
return path;
|
|
22
|
+
}
|
|
@@ -3,8 +3,8 @@ import Markdown from "markdown-it";
|
|
|
3
3
|
import type { Context } from "../types/Context";
|
|
4
4
|
import type { NavItem } from "../types/NavItem";
|
|
5
5
|
import { fetchText } from "./fetchText";
|
|
6
|
+
import { getLocation } from "./getLocation";
|
|
6
7
|
import { getSlug } from "./getSlug";
|
|
7
|
-
import { getSource } from "./getSource";
|
|
8
8
|
|
|
9
9
|
const md = new Markdown({
|
|
10
10
|
html: true,
|
|
@@ -105,7 +105,8 @@ function getSectionPostprocess(linkMap: Record<string, string>) {
|
|
|
105
105
|
|
|
106
106
|
export async function getParsedContent(ctx: Context) {
|
|
107
107
|
let { singlePage } = ctx;
|
|
108
|
-
let
|
|
108
|
+
let rawContent = await fetchText(getLocation(ctx, "README.md", ctx.source));
|
|
109
|
+
let content = md.render(rawContent);
|
|
109
110
|
let dom = new JSDOM(content);
|
|
110
111
|
|
|
111
112
|
let { nav, linkMap } = buildNav(ctx, dom);
|
package/src/bin/getSource.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Context } from "../types/Context";
|
|
2
|
-
|
|
3
|
-
export function getSource(ctx: Context) {
|
|
4
|
-
let { source, repo, mainBranch = "main" } = ctx;
|
|
5
|
-
|
|
6
|
-
if (source) return source;
|
|
7
|
-
|
|
8
|
-
let ghId = repo?.startsWith("https://github.com/")
|
|
9
|
-
? repo.replace("https://github.com/", "").split("/").slice(0, 2).join("/")
|
|
10
|
-
: "";
|
|
11
|
-
|
|
12
|
-
if (ghId)
|
|
13
|
-
return `https://raw.githubusercontent.com/${ghId}/refs/heads/${mainBranch}/README.md`;
|
|
14
|
-
|
|
15
|
-
return "README.md";
|
|
16
|
-
}
|