@t8/docsgen 0.1.5 → 0.1.7

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
@@ -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 "";
@@ -119,14 +130,21 @@ function toConfig(metadata) {
119
130
  }
120
131
 
121
132
  // src/bin/getConfig.ts
122
- var config = null;
123
- async function getConfig() {
124
- if (config) return config;
125
- let metadata = {};
133
+ async function addMetadata(config2) {
126
134
  try {
127
- metadata = JSON.parse(await fetchText("./package.json"));
135
+ let rawContent = await fetchText(getLocation(config2, "package.json"));
136
+ let metadata = JSON.parse(rawContent);
137
+ return {
138
+ ...toConfig(metadata),
139
+ ...config2
140
+ };
128
141
  } catch {
142
+ return config2;
129
143
  }
144
+ }
145
+ var config = null;
146
+ async function getConfig() {
147
+ if (config) return config;
130
148
  let localConfig = {};
131
149
  try {
132
150
  localConfig = JSON.parse(
@@ -142,10 +160,12 @@ async function getConfig() {
142
160
  mainBranch: "main",
143
161
  root: "/",
144
162
  contentDir: "x",
145
- ...toConfig(metadata),
146
163
  ...localConfig,
147
164
  ...A(args)
148
165
  };
166
+ if (config.entries)
167
+ config.entries = await Promise.all(config.entries.map(addMetadata));
168
+ else await addMetadata(config);
149
169
  if (!config.root?.endsWith("/")) config.root = `${config.root ?? ""}/`;
150
170
  return config;
151
171
  }
@@ -324,16 +344,6 @@ function getSlug(title) {
324
344
  return slug;
325
345
  }
326
346
 
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
347
  // src/bin/getParsedContent.ts
338
348
  var md = new import_markdown_it.default({
339
349
  html: true
@@ -405,7 +415,8 @@ function getSectionPostprocess(linkMap) {
405
415
  }
406
416
  async function getParsedContent(ctx) {
407
417
  let { singlePage } = ctx;
408
- let content = md.render(await fetchText(getSource(ctx)));
418
+ let rawContent = await fetchText(getLocation(ctx, "README.md", ctx.source));
419
+ let content = md.render(rawContent);
409
420
  let dom = new import_jsdom2.JSDOM(content);
410
421
  let { nav, linkMap } = buildNav(ctx, dom);
411
422
  let badges = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/docsgen",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "",
5
5
  "main": "dist/bin.js",
6
6
  "bin": {
@@ -1,20 +1,30 @@
1
1
  import { parseArgs } from "args-json";
2
2
  import type { BinConfig } from "../types/BinConfig";
3
+ import type { EntryConfig } from "../types/EntryConfig";
3
4
  import type { PackageMetadata } from "../types/PackageMetadata";
4
5
  import { fetchText } from "./fetchText";
6
+ import { getLocation } from "./getLocation";
5
7
  import { toConfig } from "./toConfig";
6
8
 
9
+ async function addMetadata(config: EntryConfig) {
10
+ try {
11
+ let rawContent = await fetchText(getLocation(config, "package.json"));
12
+ let metadata = JSON.parse(rawContent) as PackageMetadata;
13
+
14
+ return {
15
+ ...toConfig(metadata),
16
+ ...config,
17
+ };
18
+ } catch {
19
+ return config;
20
+ }
21
+ }
22
+
7
23
  let config: BinConfig | null = null;
8
24
 
9
25
  export async function getConfig(): Promise<BinConfig> {
10
26
  if (config) return config;
11
27
 
12
- let metadata: PackageMetadata = {};
13
-
14
- try {
15
- metadata = JSON.parse(await fetchText("./package.json")) as PackageMetadata;
16
- } catch {}
17
-
18
28
  let localConfig: BinConfig = {};
19
29
 
20
30
  try {
@@ -33,11 +43,14 @@ export async function getConfig(): Promise<BinConfig> {
33
43
  mainBranch: "main",
34
44
  root: "/",
35
45
  contentDir: "x",
36
- ...toConfig(metadata),
37
46
  ...localConfig,
38
47
  ...parseArgs<BinConfig>(args),
39
48
  };
40
49
 
50
+ if (config.entries)
51
+ config.entries = await Promise.all(config.entries.map(addMetadata));
52
+ else await addMetadata(config);
53
+
41
54
  if (!config.root?.endsWith("/")) config.root = `${config.root ?? ""}/`;
42
55
 
43
56
  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 content = md.render(await fetchText(getSource(ctx)));
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);
@@ -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
- }