@theholocron/docs-theme 1.0.9 → 1.2.0
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/loader.d.mts +34 -0
- package/dist/loader.mjs +88 -0
- package/package.json +9 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Loader } from "astro/loaders";
|
|
2
|
+
//#region src/loader.d.ts
|
|
3
|
+
type DocsSource = {
|
|
4
|
+
dir: string;
|
|
5
|
+
slug: string;
|
|
6
|
+
} | {
|
|
7
|
+
package: string;
|
|
8
|
+
slug: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Loads Markdown/MDX files from local directories or installed npm content
|
|
12
|
+
* packages into a Starlight docs collection.
|
|
13
|
+
*
|
|
14
|
+
* Each source is either a local directory path or an npm package name whose
|
|
15
|
+
* published `content/` directory will be resolved at build time.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // content.config.ts
|
|
19
|
+
* import { createDocsLoader } from "@theholocron/docs-theme/loader";
|
|
20
|
+
* import { fileURLToPath } from "node:url";
|
|
21
|
+
*
|
|
22
|
+
* export const collections = {
|
|
23
|
+
* docs: defineCollection({
|
|
24
|
+
* loader: createDocsLoader([
|
|
25
|
+
* { dir: fileURLToPath(new URL("content/docs", import.meta.url)), slug: "" },
|
|
26
|
+
* { package: "@theholocron/clients-docs", slug: "projects/clients" },
|
|
27
|
+
* ]),
|
|
28
|
+
* schema: docsSchema(),
|
|
29
|
+
* }),
|
|
30
|
+
* };
|
|
31
|
+
*/
|
|
32
|
+
declare function createDocsLoader(sources: DocsSource[]): Loader;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { DocsSource, createDocsLoader };
|
package/dist/loader.mjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
5
|
+
import { dirname, extname, join, relative } from "node:path";
|
|
6
|
+
import matter from "gray-matter";
|
|
7
|
+
//#region src/loader.ts
|
|
8
|
+
const EXTENSIONS = /* @__PURE__ */ new Set([".md", ".mdx"]);
|
|
9
|
+
async function* walk(dir) {
|
|
10
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
11
|
+
if (entry.name.startsWith("_")) continue;
|
|
12
|
+
const full = join(dir, entry.name);
|
|
13
|
+
if (entry.isDirectory()) yield* walk(full);
|
|
14
|
+
else if (EXTENSIONS.has(extname(entry.name))) yield full;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function computeId(filePath, baseDir, slugPrefix) {
|
|
18
|
+
const noIndex = relative(baseDir, filePath).replace(/\\/g, "/").replace(/\.(mdx?)$/, "").replace(/\/index$/, "").replace(/^index$/, "");
|
|
19
|
+
if (noIndex) return slugPrefix ? `${slugPrefix}/${noIndex}` : noIndex;
|
|
20
|
+
return slugPrefix || "index";
|
|
21
|
+
}
|
|
22
|
+
function resolvePackageContentDir(packageName, root) {
|
|
23
|
+
let dir = dirname(createRequire(root).resolve(packageName));
|
|
24
|
+
while (dir !== dirname(dir)) {
|
|
25
|
+
if (existsSync(join(dir, "package.json"))) return join(dir, "content");
|
|
26
|
+
dir = dirname(dir);
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`Could not resolve content directory for ${packageName}`);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Loads Markdown/MDX files from local directories or installed npm content
|
|
32
|
+
* packages into a Starlight docs collection.
|
|
33
|
+
*
|
|
34
|
+
* Each source is either a local directory path or an npm package name whose
|
|
35
|
+
* published `content/` directory will be resolved at build time.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // content.config.ts
|
|
39
|
+
* import { createDocsLoader } from "@theholocron/docs-theme/loader";
|
|
40
|
+
* import { fileURLToPath } from "node:url";
|
|
41
|
+
*
|
|
42
|
+
* export const collections = {
|
|
43
|
+
* docs: defineCollection({
|
|
44
|
+
* loader: createDocsLoader([
|
|
45
|
+
* { dir: fileURLToPath(new URL("content/docs", import.meta.url)), slug: "" },
|
|
46
|
+
* { package: "@theholocron/clients-docs", slug: "projects/clients" },
|
|
47
|
+
* ]),
|
|
48
|
+
* schema: docsSchema(),
|
|
49
|
+
* }),
|
|
50
|
+
* };
|
|
51
|
+
*/
|
|
52
|
+
function createDocsLoader(sources) {
|
|
53
|
+
return {
|
|
54
|
+
name: "docs-loader",
|
|
55
|
+
async load(ctx) {
|
|
56
|
+
ctx.store.clear();
|
|
57
|
+
const siteRoot = fileURLToPath(ctx.config.root);
|
|
58
|
+
const resolved = sources.map((s) => "package" in s ? {
|
|
59
|
+
dir: resolvePackageContentDir(s.package, ctx.config.root),
|
|
60
|
+
slug: s.slug
|
|
61
|
+
} : s);
|
|
62
|
+
for (const { dir, slug: slugPrefix } of resolved) for await (const absPath of walk(dir)) {
|
|
63
|
+
const id = computeId(absPath, dir, slugPrefix);
|
|
64
|
+
const raw = await readFile(absPath, "utf-8");
|
|
65
|
+
const { data: frontmatter, content: body } = matter(raw);
|
|
66
|
+
const digest = ctx.generateDigest(raw);
|
|
67
|
+
const filePath = relative(siteRoot, absPath);
|
|
68
|
+
const data = await ctx.parseData({
|
|
69
|
+
id,
|
|
70
|
+
data: frontmatter,
|
|
71
|
+
filePath
|
|
72
|
+
});
|
|
73
|
+
const rendered = await ctx.renderMarkdown(body, { fileURL: pathToFileURL(absPath) });
|
|
74
|
+
ctx.store.set({
|
|
75
|
+
id,
|
|
76
|
+
data,
|
|
77
|
+
body,
|
|
78
|
+
filePath,
|
|
79
|
+
digest,
|
|
80
|
+
rendered
|
|
81
|
+
});
|
|
82
|
+
ctx.watcher?.add(absPath);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
export { createDocsLoader };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/docs-theme",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Starlight plugin providing the shared docs theme.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -30,6 +30,11 @@
|
|
|
30
30
|
"import": "./dist/index.mjs",
|
|
31
31
|
"default": "./dist/index.mjs"
|
|
32
32
|
},
|
|
33
|
+
"./loader": {
|
|
34
|
+
"types": "./dist/loader.d.mts",
|
|
35
|
+
"import": "./dist/loader.mjs",
|
|
36
|
+
"default": "./dist/loader.mjs"
|
|
37
|
+
},
|
|
33
38
|
"./styles.css": "./dist/styles.css"
|
|
34
39
|
},
|
|
35
40
|
"main": "./dist/index.mjs",
|
|
@@ -37,6 +42,9 @@
|
|
|
37
42
|
"dist",
|
|
38
43
|
"README.md"
|
|
39
44
|
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"gray-matter": "^4.0.3"
|
|
47
|
+
},
|
|
40
48
|
"devDependencies": {
|
|
41
49
|
"@astrojs/starlight": "^0.41.5",
|
|
42
50
|
"@theholocron/eslint-config": "^7.6.0",
|