@t8/docsgen 0.3.34 → 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 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
- return await (await fetch(location, { cache: "no-cache" })).text();
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
- let locationPath = location.replace(/^\//, "");
130
- return (await readFile(locationPath)).toString();
134
+ cachedContent.set(location, content ??= "");
135
+ return content;
131
136
  }
132
137
 
133
138
  // src/bin/getLocation.ts
@@ -801,33 +806,9 @@ function getNpmLink({ npm }, className) {
801
806
  }
802
807
 
803
808
  // src/bin/content/getNav.ts
804
- var cachedNavContent = /* @__PURE__ */ new Map();
805
- async function getNavContent({ name, nav }) {
806
- if (!nav) return "";
807
- let navContent = cachedNavContent.get(nav);
808
- if (navContent !== void 0) return navContent;
809
- navContent = await fetchContent(nav);
810
- if (navContent) {
811
- let navDom = new JSDOM3(navContent).window.document.body;
812
- for (let link of navDom.querySelectorAll("a")) {
813
- if (link.dataset.name === name) {
814
- let parent = link.parentElement;
815
- link.remove();
816
- while (parent && parent.innerHTML.trim() === "") {
817
- let nextParent = parent.parentElement;
818
- parent.remove();
819
- parent = nextParent;
820
- }
821
- }
822
- }
823
- navContent = navDom.innerHTML;
824
- }
825
- cachedNavContent.set(nav, navContent);
826
- return navContent;
827
- }
828
809
  async function getNav(ctx, navItems) {
829
- let { name, root, contentDir, backstory } = ctx;
830
- let navContent = await getNavContent(ctx);
810
+ let { name, root, contentDir, backstory, nav } = ctx;
811
+ let navContent = await fetchContent(nav);
831
812
  let s = "";
832
813
  if (navContent) {
833
814
  let navDom = new JSDOM3(navContent).window.document.body;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/docsgen",
3
- "version": "0.3.34",
3
+ "version": "0.3.35",
4
4
  "description": "",
5
5
  "main": "dist/bin.js",
6
6
  "type": "module",
@@ -5,46 +5,9 @@ import { fetchContent } from "../fetchContent.ts";
5
5
  import { getNpmLink } from "../getNpmLink.ts";
6
6
  import { getRepoLink } from "../getRepoLink.ts";
7
7
 
8
- let cachedNavContent = new Map<string, string>();
9
-
10
- async function getNavContent({ name, nav }: Context) {
11
- if (!nav) return "";
12
-
13
- let navContent = cachedNavContent.get(nav);
14
-
15
- if (navContent !== undefined) return navContent;
16
-
17
- navContent = await fetchContent(nav);
18
-
19
- if (navContent) {
20
- let navDom = new JSDOM(navContent).window.document.body;
21
-
22
- for (let link of navDom.querySelectorAll("a")) {
23
- if (link.dataset.name === name) {
24
- let parent = link.parentElement;
25
-
26
- link.remove();
27
-
28
- while (parent && parent.innerHTML.trim() === "") {
29
- let nextParent = parent.parentElement;
30
-
31
- parent.remove();
32
- parent = nextParent;
33
- }
34
- }
35
- }
36
-
37
- navContent = navDom.innerHTML;
38
- }
39
-
40
- cachedNavContent.set(nav, navContent);
41
-
42
- return navContent;
43
- }
44
-
45
8
  export async function getNav(ctx: Context, navItems: NavItem[]) {
46
- let { name, root, contentDir, backstory } = ctx;
47
- let navContent = await getNavContent(ctx);
9
+ let { name, root, contentDir, backstory, nav } = ctx;
10
+ let navContent = await fetchContent(nav);
48
11
  let s = "";
49
12
 
50
13
  if (navContent) {
@@ -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
- return await (await fetch(location, { cache: "no-cache" })).text();
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
- let locationPath = location.replace(/^\//, "");
24
+ cachedContent.set(location, (content ??= ""));
16
25
 
17
- return (await readFile(locationPath)).toString();
26
+ return content;
18
27
  }