@octanejs/docusaurus 0.0.1
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/LICENSE +21 -0
- package/README.md +100 -0
- package/package.json +77 -0
- package/src/bin.js +70 -0
- package/src/index.js +13 -0
- package/src/load-site.js +36 -0
- package/src/manifest.js +261 -0
- package/src/mdx.js +319 -0
- package/src/version.js +68 -0
- package/src/vite.js +202 -0
- package/types/index.d.ts +107 -0
- package/types/mdx.d.ts +60 -0
- package/types/vite.d.ts +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dominic Gannaway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# `@octanejs/docusaurus`
|
|
2
|
+
|
|
3
|
+
The headless Docusaurus content and MDX bridge for Octane.
|
|
4
|
+
|
|
5
|
+
Docusaurus has a useful boundary before React: its Node-side config, presets,
|
|
6
|
+
and plugins load content, create data modules, and emit a serializable route
|
|
7
|
+
tree. This package adopts that graph, normalizes it into a renderer-neutral
|
|
8
|
+
manifest, resolves Docusaurus module aliases for Vite, and compiles
|
|
9
|
+
Docusaurus-shaped MDX exports into real Octane components.
|
|
10
|
+
|
|
11
|
+
It does **not** run React themes or React-authored swizzles unchanged. Octane is
|
|
12
|
+
compiler-first; the renderer and theme layers must be authored for Octane.
|
|
13
|
+
|
|
14
|
+
## Version contract
|
|
15
|
+
|
|
16
|
+
The headless loader is intentionally pinned to `@docusaurus/core@3.10.1`.
|
|
17
|
+
Docusaurus does not publish the `server/site` loader as a stable public entry,
|
|
18
|
+
so accepting an untested minor would turn an internal upstream refactor into a
|
|
19
|
+
silent route-data corruption. The loader checks both the package version and
|
|
20
|
+
Docusaurus's Node `>=20.0` runtime requirement before importing that seam. This
|
|
21
|
+
package retains Octane's repository-wide Node `>=22` baseline.
|
|
22
|
+
|
|
23
|
+
`allowUnsupportedVersion: true` is available only for explicit compatibility
|
|
24
|
+
experiments.
|
|
25
|
+
|
|
26
|
+
## Inspect the headless site graph
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import {
|
|
30
|
+
createDocusaurusManifest,
|
|
31
|
+
loadDocusaurusSite,
|
|
32
|
+
} from '@octanejs/docusaurus';
|
|
33
|
+
|
|
34
|
+
const loaded = await loadDocusaurusSite({ siteDir: process.cwd() });
|
|
35
|
+
const manifest = await createDocusaurusManifest(loaded);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The manifest contains:
|
|
39
|
+
|
|
40
|
+
- nested routes with `component`, `modules`, `props`, and plugin context;
|
|
41
|
+
- generated global data and per-document metadata;
|
|
42
|
+
- `@site`, `@generated`, `~docs`, `@theme`, `@theme-original`, and
|
|
43
|
+
`@theme-init` resolution;
|
|
44
|
+
- the exact Docusaurus version and route-path inventory.
|
|
45
|
+
|
|
46
|
+
The CLI exposes the same boundary:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
octane-docusaurus inspect --site-dir . --out .octane-docusaurus/manifest.json
|
|
50
|
+
octane-docusaurus clear --site-dir .
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Vite
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { defineConfig } from 'vite';
|
|
57
|
+
import { octane } from 'octane/compiler/vite';
|
|
58
|
+
import { docusaurus } from '@octanejs/docusaurus/vite';
|
|
59
|
+
|
|
60
|
+
export default defineConfig({
|
|
61
|
+
plugins: [...docusaurus(), octane()],
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The bridge publishes `virtual:octane-docusaurus-manifest` and resolves
|
|
66
|
+
Docusaurus aliases. Its MDX plugin chooses Octane client/server compilation per
|
|
67
|
+
Vite environment and injects metadata discovered by the content plugins.
|
|
68
|
+
|
|
69
|
+
## Docusaurus-aware MDX
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { compileDocusaurusMdx } from '@octanejs/docusaurus/mdx';
|
|
73
|
+
|
|
74
|
+
const result = await compileDocusaurusMdx(source, '/docs/intro.mdx', {
|
|
75
|
+
metadata: docMetadata,
|
|
76
|
+
resolveMarkdownLink: ({ linkPathname }) =>
|
|
77
|
+
linkPathname.startsWith('./') ? `/guide/${linkPathname.slice(2)}` : null,
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Alongside the default document component, compiled modules export Docusaurus's
|
|
82
|
+
document shape:
|
|
83
|
+
|
|
84
|
+
- `frontMatter` (plus `@octanejs/mdx`'s existing `frontmatter`);
|
|
85
|
+
- `contentTitle`;
|
|
86
|
+
- `toc`;
|
|
87
|
+
- `metadata`;
|
|
88
|
+
- `assets`.
|
|
89
|
+
|
|
90
|
+
GitHub-compatible heading IDs (including explicit Docusaurus IDs), duplicate
|
|
91
|
+
slugs, first-title wrapping/removal, TOC bounds, and Markdown link/image
|
|
92
|
+
rewriting happen before Octane compilation. User remark/rehype/recma plugins
|
|
93
|
+
remain composable.
|
|
94
|
+
|
|
95
|
+
## Current scope
|
|
96
|
+
|
|
97
|
+
Phases 1–3 are implemented here: headless loading, manifest/Vite integration,
|
|
98
|
+
and MDX compilation. Client routing, static generation, hydration, and an
|
|
99
|
+
Octane classic theme are deliberately left to the renderer/theme phases; the
|
|
100
|
+
CLI therefore does not present `start` or `build` as working commands yet.
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/docusaurus",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Docusaurus content and MDX integration for Octane",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22"
|
|
9
|
+
},
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Dominic Gannaway",
|
|
12
|
+
"email": "dg@domgan.com"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
20
|
+
"directory": "packages/docusaurus"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"types",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"bin": {
|
|
29
|
+
"octane-docusaurus": "src/bin.js"
|
|
30
|
+
},
|
|
31
|
+
"main": "src/index.js",
|
|
32
|
+
"module": "src/index.js",
|
|
33
|
+
"types": "types/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./types/index.d.ts",
|
|
37
|
+
"import": "./src/index.js",
|
|
38
|
+
"default": "./src/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./mdx": {
|
|
41
|
+
"types": "./types/mdx.d.ts",
|
|
42
|
+
"import": "./src/mdx.js",
|
|
43
|
+
"default": "./src/mdx.js"
|
|
44
|
+
},
|
|
45
|
+
"./vite": {
|
|
46
|
+
"types": "./types/vite.d.ts",
|
|
47
|
+
"import": "./src/vite.js",
|
|
48
|
+
"default": "./src/vite.js"
|
|
49
|
+
},
|
|
50
|
+
"./package.json": "./package.json"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"github-slugger": "1.5.0",
|
|
54
|
+
"@octanejs/mdx": "0.1.14"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@docusaurus/core": "3.10.1",
|
|
58
|
+
"vite": ">=7.0.0",
|
|
59
|
+
"octane": "0.1.17"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"vite": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@docusaurus/core": "3.10.1",
|
|
68
|
+
"@docusaurus/plugin-content-docs": "3.10.1",
|
|
69
|
+
"@types/node": "^24.13.3",
|
|
70
|
+
"vite": "^8.1.5",
|
|
71
|
+
"vitest": "^4.1.10",
|
|
72
|
+
"octane": "0.1.17"
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"test": "cd ../.. && vitest run --project docusaurus"
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/bin.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { rm } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createDocusaurusManifest, loadDocusaurusSite, writeDocusaurusManifest } from './index.js';
|
|
6
|
+
|
|
7
|
+
function usage() {
|
|
8
|
+
return `Usage:
|
|
9
|
+
octane-docusaurus inspect [--site-dir DIR] [--config FILE] [--locale LOCALE] [--out FILE]
|
|
10
|
+
octane-docusaurus clear [--site-dir DIR]
|
|
11
|
+
|
|
12
|
+
The phase 1-3 command inspects Docusaurus's headless route/data graph. Static
|
|
13
|
+
site build, hydration, and theme commands arrive with the renderer phases.
|
|
14
|
+
`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function parseArguments(argv) {
|
|
18
|
+
const command = argv[0];
|
|
19
|
+
const options = {};
|
|
20
|
+
for (let index = 1; index < argv.length; index++) {
|
|
21
|
+
const argument = argv[index];
|
|
22
|
+
if (argument === '--allow-unsupported-version') {
|
|
23
|
+
options.allowUnsupportedVersion = true;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (!argument.startsWith('--')) throw new Error(`Unexpected argument: ${argument}`);
|
|
27
|
+
const value = argv[++index];
|
|
28
|
+
if (value === undefined) throw new Error(`Missing value for ${argument}`);
|
|
29
|
+
if (argument === '--site-dir') options.siteDir = value;
|
|
30
|
+
else if (argument === '--config') options.config = value;
|
|
31
|
+
else if (argument === '--locale') options.locale = value;
|
|
32
|
+
else if (argument === '--out') options.out = value;
|
|
33
|
+
else throw new Error(`Unknown option: ${argument}`);
|
|
34
|
+
}
|
|
35
|
+
return { command, options };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function inspect(options) {
|
|
39
|
+
const loaded = await loadDocusaurusSite(options);
|
|
40
|
+
const manifest = await createDocusaurusManifest(loaded);
|
|
41
|
+
if (options.out) {
|
|
42
|
+
const filename = await writeDocusaurusManifest(manifest, options.out);
|
|
43
|
+
process.stdout.write(`${filename}\n`);
|
|
44
|
+
} else {
|
|
45
|
+
process.stdout.write(`${JSON.stringify(manifest, null, 2)}\n`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function clear(options) {
|
|
50
|
+
const siteDir = path.resolve(options.siteDir ?? process.cwd());
|
|
51
|
+
await Promise.all([
|
|
52
|
+
rm(path.join(siteDir, '.docusaurus'), { recursive: true, force: true }),
|
|
53
|
+
rm(path.join(siteDir, '.octane-docusaurus'), { recursive: true, force: true }),
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function main() {
|
|
58
|
+
const { command, options } = parseArguments(process.argv.slice(2));
|
|
59
|
+
if (command === 'inspect') await inspect(options);
|
|
60
|
+
else if (command === 'clear') await clear(options);
|
|
61
|
+
else {
|
|
62
|
+
process.stderr.write(usage());
|
|
63
|
+
process.exitCode = command === '--help' || command === '-h' ? 0 : 2;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
main().catch((error) => {
|
|
68
|
+
process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`);
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
});
|
package/src/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {
|
|
2
|
+
MINIMUM_DOCUSARUS_NODE_VERSION,
|
|
3
|
+
SUPPORTED_DOCUSARUS_VERSION,
|
|
4
|
+
assertSupportedDocusaurusRuntime,
|
|
5
|
+
resolveDocusaurusCore,
|
|
6
|
+
} from './version.js';
|
|
7
|
+
export { loadDocusaurusSite } from './load-site.js';
|
|
8
|
+
export {
|
|
9
|
+
createDocusaurusManifest,
|
|
10
|
+
readDocusaurusManifest,
|
|
11
|
+
resolveDocusaurusId,
|
|
12
|
+
writeDocusaurusManifest,
|
|
13
|
+
} from './manifest.js';
|
package/src/load-site.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import { assertSupportedDocusaurusRuntime, resolveDocusaurusCore } from './version.js';
|
|
4
|
+
|
|
5
|
+
export async function loadDocusaurusSite(options = {}) {
|
|
6
|
+
const siteDir = path.resolve(options.siteDir ?? process.cwd());
|
|
7
|
+
const core = await resolveDocusaurusCore(siteDir);
|
|
8
|
+
assertSupportedDocusaurusRuntime(core, options);
|
|
9
|
+
|
|
10
|
+
const siteModuleUrl = pathToFileURL(path.join(core.packageRoot, 'lib/server/site.js')).href;
|
|
11
|
+
const siteModule = await import(siteModuleUrl);
|
|
12
|
+
if (typeof siteModule.loadSite !== 'function') {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`[@octanejs/docusaurus] @docusaurus/core@${core.version} no longer exposes the ` +
|
|
15
|
+
'internal server/site loadSite() seam.',
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const site = await siteModule.loadSite({
|
|
20
|
+
siteDir,
|
|
21
|
+
...(options.outDir === undefined ? {} : { outDir: options.outDir }),
|
|
22
|
+
...(options.config === undefined ? {} : { config: options.config }),
|
|
23
|
+
...(options.locale === undefined ? {} : { locale: options.locale }),
|
|
24
|
+
...(options.automaticBaseUrlLocalizationDisabled === undefined
|
|
25
|
+
? {}
|
|
26
|
+
: {
|
|
27
|
+
automaticBaseUrlLocalizationDisabled: options.automaticBaseUrlLocalizationDisabled,
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
corePath: core.packageRoot,
|
|
33
|
+
docusaurusVersion: core.version,
|
|
34
|
+
site,
|
|
35
|
+
};
|
|
36
|
+
}
|
package/src/manifest.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const SOURCE_EXTENSIONS = ['.tsrx', '.tsx', '.jsx', '.ts', '.js', '.mjs', '.cjs'];
|
|
6
|
+
const RESOLVE_EXTENSIONS = [...SOURCE_EXTENSIONS, '.json', '.mdx', '.md', '.css'];
|
|
7
|
+
|
|
8
|
+
function toJsonValue(value, trail = '$', active = new WeakSet()) {
|
|
9
|
+
if (
|
|
10
|
+
value === null ||
|
|
11
|
+
typeof value === 'string' ||
|
|
12
|
+
typeof value === 'number' ||
|
|
13
|
+
typeof value === 'boolean'
|
|
14
|
+
) {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
if (value === undefined) return undefined;
|
|
18
|
+
if (typeof value === 'bigint' || typeof value === 'symbol' || typeof value === 'function') {
|
|
19
|
+
throw new TypeError(`${trail} is not serializable in a Docusaurus route manifest.`);
|
|
20
|
+
}
|
|
21
|
+
if (active.has(value)) {
|
|
22
|
+
throw new TypeError(`${trail} is cyclic and cannot be serialized.`);
|
|
23
|
+
}
|
|
24
|
+
active.add(value);
|
|
25
|
+
let result;
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
result = value.map((item, index) => toJsonValue(item, `${trail}[${index}]`, active));
|
|
28
|
+
} else {
|
|
29
|
+
result = {};
|
|
30
|
+
for (const [key, item] of Object.entries(value)) {
|
|
31
|
+
const serialized = toJsonValue(item, `${trail}.${key}`, active);
|
|
32
|
+
if (serialized !== undefined) result[key] = serialized;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
active.delete(value);
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function moduleId(module) {
|
|
40
|
+
if (typeof module === 'string') return module;
|
|
41
|
+
if (module && typeof module === 'object' && module.__import === true) {
|
|
42
|
+
return {
|
|
43
|
+
__import: true,
|
|
44
|
+
path: String(module.path),
|
|
45
|
+
...(module.query === undefined ? {} : { query: toJsonValue(module.query) }),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
throw new TypeError(`Expected a Docusaurus module reference, received ${String(module)}.`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeRoute(route, index, parentId = 'root') {
|
|
52
|
+
const id = `${parentId}/${index}:${route.path}`;
|
|
53
|
+
const modules = {};
|
|
54
|
+
for (const [name, value] of Object.entries(route.modules ?? {})) {
|
|
55
|
+
modules[name] = Array.isArray(value) ? value.map(moduleId) : moduleId(value);
|
|
56
|
+
}
|
|
57
|
+
const known = new Set([
|
|
58
|
+
'path',
|
|
59
|
+
'component',
|
|
60
|
+
'modules',
|
|
61
|
+
'context',
|
|
62
|
+
'props',
|
|
63
|
+
'routes',
|
|
64
|
+
'exact',
|
|
65
|
+
'priority',
|
|
66
|
+
'metadata',
|
|
67
|
+
'plugin',
|
|
68
|
+
]);
|
|
69
|
+
const attributes = {};
|
|
70
|
+
for (const [name, value] of Object.entries(route)) {
|
|
71
|
+
if (known.has(name)) continue;
|
|
72
|
+
const serialized = toJsonValue(value, `route(${route.path}).${name}`);
|
|
73
|
+
if (serialized !== undefined) attributes[name] = serialized;
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
id,
|
|
77
|
+
path: String(route.path),
|
|
78
|
+
component: moduleId(route.component),
|
|
79
|
+
exact: route.exact === true,
|
|
80
|
+
...(route.priority === undefined ? {} : { priority: Number(route.priority) }),
|
|
81
|
+
...(Object.keys(modules).length === 0 ? {} : { modules }),
|
|
82
|
+
...(route.context === undefined
|
|
83
|
+
? {}
|
|
84
|
+
: { context: toJsonValue(route.context, `route(${route.path}).context`) }),
|
|
85
|
+
...(route.props === undefined
|
|
86
|
+
? {}
|
|
87
|
+
: { props: toJsonValue(route.props, `route(${route.path}).props`) }),
|
|
88
|
+
...(route.metadata === undefined
|
|
89
|
+
? {}
|
|
90
|
+
: { metadata: toJsonValue(route.metadata, `route(${route.path}).metadata`) }),
|
|
91
|
+
...(route.plugin === undefined
|
|
92
|
+
? {}
|
|
93
|
+
: { plugin: toJsonValue(route.plugin, `route(${route.path}).plugin`) }),
|
|
94
|
+
...(Object.keys(attributes).length === 0 ? {} : { attributes }),
|
|
95
|
+
children: (route.routes ?? []).map((child, childIndex) =>
|
|
96
|
+
normalizeRoute(child, childIndex, id),
|
|
97
|
+
),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function listFiles(directory) {
|
|
102
|
+
if (!existsSync(directory)) return [];
|
|
103
|
+
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
104
|
+
const absolute = path.join(directory, entry.name);
|
|
105
|
+
return entry.isDirectory() ? listFiles(absolute) : [absolute];
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function themeKey(file, root) {
|
|
110
|
+
const relative = path.relative(root, file).split(path.sep).join('/');
|
|
111
|
+
if (relative.endsWith('.d.ts')) return null;
|
|
112
|
+
const extension = SOURCE_EXTENSIONS.find((candidate) => relative.endsWith(candidate));
|
|
113
|
+
if (!extension) return null;
|
|
114
|
+
let key = relative.slice(0, -extension.length);
|
|
115
|
+
if (key.endsWith('/index')) key = key.slice(0, -'/index'.length);
|
|
116
|
+
return key;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function themeLayers(plugins) {
|
|
120
|
+
const layers = [];
|
|
121
|
+
for (const plugin of plugins) {
|
|
122
|
+
if (typeof plugin.getThemePath !== 'function') continue;
|
|
123
|
+
const supplied = await plugin.getThemePath();
|
|
124
|
+
if (typeof supplied !== 'string') continue;
|
|
125
|
+
layers.push(path.isAbsolute(supplied) ? supplied : path.resolve(plugin.path, supplied));
|
|
126
|
+
}
|
|
127
|
+
return layers;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function createThemeAliases(plugins, siteDir) {
|
|
131
|
+
const providers = new Map();
|
|
132
|
+
for (const root of await themeLayers(plugins)) {
|
|
133
|
+
for (const file of listFiles(root)) {
|
|
134
|
+
const key = themeKey(file, root);
|
|
135
|
+
if (key === null) continue;
|
|
136
|
+
const entries = providers.get(key) ?? [];
|
|
137
|
+
entries.push(file);
|
|
138
|
+
providers.set(key, entries);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const theme = {};
|
|
142
|
+
const themeOriginal = {};
|
|
143
|
+
const themeInit = {};
|
|
144
|
+
for (const [key, entries] of providers) {
|
|
145
|
+
theme[key] = entries.at(-1);
|
|
146
|
+
themeOriginal[key] = entries.at(-1);
|
|
147
|
+
if (entries.length > 1) themeInit[key] = entries[0];
|
|
148
|
+
}
|
|
149
|
+
const siteTheme = path.join(siteDir, 'src/theme');
|
|
150
|
+
for (const file of listFiles(siteTheme)) {
|
|
151
|
+
const key = themeKey(file, siteTheme);
|
|
152
|
+
if (key !== null) theme[key] = file;
|
|
153
|
+
}
|
|
154
|
+
return { theme, themeOriginal, themeInit };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function readJsonIfPresent(filename, fallback) {
|
|
158
|
+
if (!existsSync(filename)) return fallback;
|
|
159
|
+
return JSON.parse(readFileSync(filename, 'utf8'));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function contentMetadata(generatedFilesDir) {
|
|
163
|
+
const roots = [
|
|
164
|
+
path.join(generatedFilesDir, 'docusaurus-plugin-content-docs'),
|
|
165
|
+
path.join(generatedFilesDir, 'docusaurus-plugin-content-pages'),
|
|
166
|
+
];
|
|
167
|
+
const content = {};
|
|
168
|
+
for (const root of roots) {
|
|
169
|
+
for (const file of listFiles(root)) {
|
|
170
|
+
if (!file.endsWith('.json') || path.basename(file).startsWith('__')) continue;
|
|
171
|
+
let value;
|
|
172
|
+
try {
|
|
173
|
+
value = JSON.parse(readFileSync(file, 'utf8'));
|
|
174
|
+
} catch {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (value && typeof value === 'object' && typeof value.source === 'string') {
|
|
178
|
+
content[value.source] = value;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return content;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function createDocusaurusManifest(loaded) {
|
|
186
|
+
const { props } = loaded.site;
|
|
187
|
+
const { siteDir, generatedFilesDir } = props;
|
|
188
|
+
const themeAliases = await createThemeAliases(props.plugins ?? [], siteDir);
|
|
189
|
+
return {
|
|
190
|
+
schemaVersion: 1,
|
|
191
|
+
docusaurusVersion: loaded.docusaurusVersion,
|
|
192
|
+
siteDir,
|
|
193
|
+
generatedFilesDir,
|
|
194
|
+
outDir: props.outDir,
|
|
195
|
+
baseUrl: props.baseUrl,
|
|
196
|
+
routesPaths: [...props.routesPaths],
|
|
197
|
+
routes: props.routes.map((route, index) => normalizeRoute(route, index)),
|
|
198
|
+
globalData: readJsonIfPresent(path.join(generatedFilesDir, 'globalData.json'), {}),
|
|
199
|
+
content: contentMetadata(generatedFilesDir),
|
|
200
|
+
aliases: {
|
|
201
|
+
site: siteDir,
|
|
202
|
+
generated: generatedFilesDir,
|
|
203
|
+
docs: path.join(generatedFilesDir, 'docusaurus-plugin-content-docs'),
|
|
204
|
+
...themeAliases,
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function resolvedFile(candidate) {
|
|
210
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
211
|
+
for (const extension of RESOLVE_EXTENSIONS) {
|
|
212
|
+
const filename = candidate + extension;
|
|
213
|
+
if (existsSync(filename) && statSync(filename).isFile()) return filename;
|
|
214
|
+
}
|
|
215
|
+
if (existsSync(candidate) && statSync(candidate).isDirectory()) {
|
|
216
|
+
for (const extension of RESOLVE_EXTENSIONS) {
|
|
217
|
+
const filename = path.join(candidate, `index${extension}`);
|
|
218
|
+
if (existsSync(filename) && statSync(filename).isFile()) return filename;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function within(root, relativeId) {
|
|
225
|
+
const candidate = path.resolve(root, relativeId);
|
|
226
|
+
const relative = path.relative(root, candidate);
|
|
227
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) return null;
|
|
228
|
+
return resolvedFile(candidate);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function resolveDocusaurusId(id, manifest) {
|
|
232
|
+
const queryIndex = id.indexOf('?');
|
|
233
|
+
const source = queryIndex === -1 ? id : id.slice(0, queryIndex);
|
|
234
|
+
const query = queryIndex === -1 ? '' : id.slice(queryIndex);
|
|
235
|
+
let resolved = null;
|
|
236
|
+
if (source.startsWith('@site/')) {
|
|
237
|
+
resolved = within(manifest.aliases.site, source.slice('@site/'.length));
|
|
238
|
+
} else if (source.startsWith('@generated/')) {
|
|
239
|
+
resolved = within(manifest.aliases.generated, source.slice('@generated/'.length));
|
|
240
|
+
} else if (source.startsWith('~docs/')) {
|
|
241
|
+
resolved = within(manifest.aliases.docs, source.slice('~docs/'.length));
|
|
242
|
+
} else if (source.startsWith('@theme-original/')) {
|
|
243
|
+
resolved = manifest.aliases.themeOriginal[source.slice('@theme-original/'.length)] ?? null;
|
|
244
|
+
} else if (source.startsWith('@theme-init/')) {
|
|
245
|
+
resolved = manifest.aliases.themeInit[source.slice('@theme-init/'.length)] ?? null;
|
|
246
|
+
} else if (source.startsWith('@theme/')) {
|
|
247
|
+
resolved = manifest.aliases.theme[source.slice('@theme/'.length)] ?? null;
|
|
248
|
+
}
|
|
249
|
+
return resolved === null ? null : resolved + query;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export async function writeDocusaurusManifest(manifest, filename) {
|
|
253
|
+
const target = path.resolve(filename);
|
|
254
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
255
|
+
await writeFile(target, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
256
|
+
return target;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export async function readDocusaurusManifest(filename) {
|
|
260
|
+
return JSON.parse(await readFile(filename, 'utf8'));
|
|
261
|
+
}
|