@uxfront/layer-docs 0.8.4 → 0.9.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/README.md +90 -1
- package/app/components/app/AppHeaderBody.vue +15 -2
- package/app/components/app/AppHeaderLinks.vue +6 -2
- package/app/components/app/AppSearch.vue +21 -0
- package/app/components/docs/DocsAsideLeftBody.vue +23 -0
- package/app/composables/useFramework.ts +27 -1
- package/app/internal/FrameworkSearch.vue +97 -0
- package/app/internal/frameworkRoute.ts +39 -0
- package/app/utils/frameworkUrls.ts +31 -0
- package/modules/framework-urls/index.ts +248 -0
- package/modules/framework-urls/runtime/landing.vue +57 -0
- package/modules/framework-urls/runtime/page.vue +211 -0
- package/modules/framework-urls/runtime/server/helpers.ts +49 -0
- package/modules/framework-urls/runtime/server/raw.ts +30 -0
- package/modules/framework-urls/runtime/server/routes.ts +22 -0
- package/modules/framework-urls/runtime/server/sitemap.ts +15 -0
- package/modules/framework-urls/utils.ts +407 -0
- package/nuxt.config.ts +15 -1
- package/package.json +5 -3
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
addServerHandler,
|
|
7
|
+
addServerImports,
|
|
8
|
+
createResolver,
|
|
9
|
+
defineNuxtModule,
|
|
10
|
+
extendPages,
|
|
11
|
+
} from "@nuxt/kit";
|
|
12
|
+
import { createJiti } from "jiti";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
FRAMEWORK_ROUTE_NAME,
|
|
16
|
+
frameworkPathOf,
|
|
17
|
+
frameworkRoutePath,
|
|
18
|
+
isUnprefixedDocsPath,
|
|
19
|
+
keepsLayerSitemap,
|
|
20
|
+
type ScannedHandler,
|
|
21
|
+
} from "./utils";
|
|
22
|
+
|
|
23
|
+
/** `frameworkUrls` in `nuxt.config.ts`. */
|
|
24
|
+
export interface FrameworkUrlsOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Serves every docs page at `/docs/<framework>/<page>`, once for each of
|
|
27
|
+
* `docsTheme.frameworks`, and builds each of them. Off by default.
|
|
28
|
+
*/
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What the module passes to the app and the server at runtime, with framework URLs on. */
|
|
33
|
+
export interface FrameworkUrlsRuntimeConfig {
|
|
34
|
+
/** The values of `docsTheme.frameworks`, in order, as the build read them. */
|
|
35
|
+
frameworks: string[];
|
|
36
|
+
/** The site's URL, for the absolute URLs in the sitemap. */
|
|
37
|
+
siteUrl?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare module "@nuxt/schema" {
|
|
41
|
+
interface NuxtConfig {
|
|
42
|
+
frameworkUrls?: FrameworkUrlsOptions;
|
|
43
|
+
}
|
|
44
|
+
interface NuxtOptions {
|
|
45
|
+
frameworkUrls?: FrameworkUrlsOptions;
|
|
46
|
+
}
|
|
47
|
+
interface PublicRuntimeConfig {
|
|
48
|
+
docsFrameworkUrls?: FrameworkUrlsRuntimeConfig;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The route the prerenderer asks for the list of docs pages. It's never written to disk. */
|
|
53
|
+
export const ROUTES_SEED = "/__docs-framework-routes";
|
|
54
|
+
|
|
55
|
+
const APP_CONFIG_EXTENSIONS = ["ts", "mts", "js", "mjs"];
|
|
56
|
+
|
|
57
|
+
/** A path with its symlinks resolved, or the path itself when it doesn't exist. */
|
|
58
|
+
function realpathOrSelf(path: string): string {
|
|
59
|
+
try {
|
|
60
|
+
return realpathSync(path);
|
|
61
|
+
} catch {
|
|
62
|
+
return path;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface Layer {
|
|
67
|
+
config: { srcDir?: string; rootDir?: string };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Reads `docsTheme.frameworks` from the app config files of the app and its
|
|
72
|
+
* layers, the way Nuxt merges them: arrays add up, the app's first. The files
|
|
73
|
+
* run in a sandbox where `defineAppConfig` returns what it's given, so a list
|
|
74
|
+
* imported from another file, like Open Components' `./data/frameworks`, works.
|
|
75
|
+
*/
|
|
76
|
+
export async function readFrameworks(
|
|
77
|
+
layers: readonly Layer[],
|
|
78
|
+
alias: Record<string, string>,
|
|
79
|
+
inline: unknown,
|
|
80
|
+
): Promise<string[]> {
|
|
81
|
+
const jiti = createJiti(import.meta.url, { alias, moduleCache: false, fsCache: false });
|
|
82
|
+
const global = globalThis as { defineAppConfig?: unknown };
|
|
83
|
+
const previous = global.defineAppConfig;
|
|
84
|
+
global.defineAppConfig = <T>(config: T) => config;
|
|
85
|
+
const values: string[] = [];
|
|
86
|
+
const collect = (config: unknown) => {
|
|
87
|
+
const frameworks = (config as { docsTheme?: { frameworks?: unknown } } | undefined)?.docsTheme
|
|
88
|
+
?.frameworks;
|
|
89
|
+
if (!Array.isArray(frameworks)) return;
|
|
90
|
+
for (const option of frameworks) {
|
|
91
|
+
const value = (option as { value?: unknown } | undefined)?.value;
|
|
92
|
+
if (typeof value === "string" && value && !values.includes(value)) values.push(value);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
for (const layer of layers) {
|
|
97
|
+
const srcDir = layer.config.srcDir;
|
|
98
|
+
if (!srcDir) continue;
|
|
99
|
+
const file = APP_CONFIG_EXTENSIONS.map((ext) => join(srcDir, `app.config.${ext}`)).find(
|
|
100
|
+
(path) => existsSync(path),
|
|
101
|
+
);
|
|
102
|
+
if (file) collect(await jiti.import(file, { default: true }));
|
|
103
|
+
}
|
|
104
|
+
collect(inline);
|
|
105
|
+
} finally {
|
|
106
|
+
global.defineAppConfig = previous;
|
|
107
|
+
}
|
|
108
|
+
return values;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Framework URLs: every docs page at `/docs/<framework>/<page>`, rendered from
|
|
113
|
+
* the content page at `/docs/<page>` with only that framework's examples, and
|
|
114
|
+
* prerendered once per framework. Unless `frameworkUrls.enabled` is set, it
|
|
115
|
+
* adds nothing but the server helpers, which work without it too.
|
|
116
|
+
*/
|
|
117
|
+
export default defineNuxtModule<FrameworkUrlsOptions>({
|
|
118
|
+
meta: { name: "framework-urls", configKey: "frameworkUrls" },
|
|
119
|
+
defaults: { enabled: false },
|
|
120
|
+
async setup(options, nuxt) {
|
|
121
|
+
const resolver = createResolver(import.meta.url);
|
|
122
|
+
const resolve = (path: string) => resolver.resolve(path);
|
|
123
|
+
const docusDir = dirname(createRequire(import.meta.url).resolve("docus/package.json"));
|
|
124
|
+
|
|
125
|
+
// Auto-imported in Nitro with framework URLs off too, so a site's own
|
|
126
|
+
// sitemap route can call them either way.
|
|
127
|
+
addServerImports([
|
|
128
|
+
{ name: "getDocsSitemapEntries", from: resolve("./runtime/server/helpers") },
|
|
129
|
+
{ name: "docsPageFrameworks", from: resolve("./runtime/server/helpers") },
|
|
130
|
+
]);
|
|
131
|
+
|
|
132
|
+
if (!options.enabled) return;
|
|
133
|
+
|
|
134
|
+
const rootDir = nuxt.options.rootDir;
|
|
135
|
+
if ((nuxt.options as { i18n?: { locales?: unknown } }).i18n?.locales) {
|
|
136
|
+
throw new Error(
|
|
137
|
+
"[@uxfront/layer-docs] frameworkUrls doesn't support Docus's i18n locales yet.",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
if (!existsSync(join(rootDir, "content", "docs"))) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
"[@uxfront/layer-docs] frameworkUrls serves the pages in content/docs/, and this app has none.",
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const frameworks = await readFrameworks(
|
|
147
|
+
nuxt.options._layers,
|
|
148
|
+
nuxt.options.alias,
|
|
149
|
+
(nuxt.options.appConfig as Record<string, unknown>)?.docsTheme
|
|
150
|
+
? nuxt.options.appConfig
|
|
151
|
+
: undefined,
|
|
152
|
+
);
|
|
153
|
+
if (!frameworks.length) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
"[@uxfront/layer-docs] frameworkUrls needs the frameworks listed in docsTheme.frameworks, in app/app.config.ts.",
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const runtime: FrameworkUrlsRuntimeConfig = { frameworks };
|
|
160
|
+
nuxt.options.runtimeConfig.public.docsFrameworkUrls = runtime;
|
|
161
|
+
// Docus fills in the site's URL from the environment in its own module.
|
|
162
|
+
nuxt.hook("modules:done", () => {
|
|
163
|
+
const site = (nuxt.options as { site?: { url?: string } }).site;
|
|
164
|
+
if (site?.url) runtime.siteUrl = site.url;
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
extendPages((pages) => {
|
|
168
|
+
pages.push({
|
|
169
|
+
name: FRAMEWORK_ROUTE_NAME,
|
|
170
|
+
path: frameworkRoutePath(frameworks),
|
|
171
|
+
file: resolve("./runtime/page.vue"),
|
|
172
|
+
meta: { layout: "docs" },
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Docus's landing page, for an app without its own app/pages/index.vue,
|
|
177
|
+
// renders content/index.md as it is, so its links to docs pages would point
|
|
178
|
+
// at pages that aren't built. The layer's copy puts them under the first
|
|
179
|
+
// framework. Swapped once every module has added its pages.
|
|
180
|
+
const docusLanding = join(docusDir, "app", "templates", "landing.vue");
|
|
181
|
+
nuxt.hook("pages:resolved", (pages) => {
|
|
182
|
+
for (const page of pages) {
|
|
183
|
+
if (page.file && realpathOrSelf(page.file) === realpathOrSelf(docusLanding)) {
|
|
184
|
+
page.file = resolve("./runtime/landing.vue");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const sitemapHandler = resolve("./runtime/server/sitemap");
|
|
190
|
+
addServerHandler({ route: "/sitemap.xml", handler: sitemapHandler });
|
|
191
|
+
addServerHandler({ middleware: true, handler: resolve("./runtime/server/raw") });
|
|
192
|
+
addServerHandler({ route: ROUTES_SEED, handler: resolve("./runtime/server/routes") });
|
|
193
|
+
|
|
194
|
+
nuxt.hook("nitro:config", (config) => {
|
|
195
|
+
config.prerender ||= {};
|
|
196
|
+
config.prerender.ignore ||= [];
|
|
197
|
+
// Unprefixed docs pages aren't built: every link points under a framework.
|
|
198
|
+
config.prerender.ignore.push((path: string) => isUnprefixedDocsPath(path, frameworks));
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// A site's own server/routes/sitemap.xml wins over the layer's, which only
|
|
202
|
+
// replaces Docus's. A handler a module adds shadows a scanned one, so the
|
|
203
|
+
// layer takes its own out when the site has one.
|
|
204
|
+
let ownSitemap = false;
|
|
205
|
+
const removeLayerSitemap = (handlers: { route?: string; handler?: string }[] | undefined) => {
|
|
206
|
+
const index = handlers?.findIndex(
|
|
207
|
+
(handler) => handler.route === "/sitemap.xml" && handler.handler === sitemapHandler,
|
|
208
|
+
);
|
|
209
|
+
if (handlers && index !== undefined && index !== -1) handlers.splice(index, 1);
|
|
210
|
+
};
|
|
211
|
+
nuxt.hook("nitro:build:before", (nitro) => {
|
|
212
|
+
const scanned = (nitro as unknown as { scannedHandlers: ScannedHandler[] }).scannedHandlers;
|
|
213
|
+
ownSitemap = !keepsLayerSitemap(scanned, docusDir, realpathOrSelf);
|
|
214
|
+
if (ownSitemap) removeLayerSitemap(nitro.options.handlers);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
nuxt.hook("nitro:init", (nitro) => {
|
|
218
|
+
// The prerenderer is a Nitro of its own, built from the original config.
|
|
219
|
+
nitro.hooks.hook("prerender:config", (config) => {
|
|
220
|
+
if (ownSitemap) removeLayerSitemap(config.handlers);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// Every docs page × every framework, listed explicitly rather than found by
|
|
224
|
+
// crawling: the seed route answers with the content paths, from the content
|
|
225
|
+
// database, so the list is the same with or without a warm parse cache.
|
|
226
|
+
let routes: Set<string> | undefined;
|
|
227
|
+
nitro.hooks.hook("prerender:routes", (set) => {
|
|
228
|
+
routes = set;
|
|
229
|
+
set.add(ROUTES_SEED);
|
|
230
|
+
});
|
|
231
|
+
nitro.hooks.hook("prerender:generate", (route) => {
|
|
232
|
+
if (route.route !== ROUTES_SEED) return;
|
|
233
|
+
route.skip = true;
|
|
234
|
+
// Docus prerenders with `failOnError: false`, so a failure here would
|
|
235
|
+
// otherwise leave the site without a single docs page, and say nothing.
|
|
236
|
+
if (route.error || !routes || !route.contents) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
`[@uxfront/layer-docs] frameworkUrls couldn't list the docs pages to build: ${route.error?.message ?? "no response"}`,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
const paths = JSON.parse(route.contents) as string[];
|
|
242
|
+
for (const path of paths) {
|
|
243
|
+
for (const framework of frameworks) routes.add(frameworkPathOf(path, framework));
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
},
|
|
248
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// Docus's landing page (docus/app/templates/landing.vue), which renders
|
|
3
|
+
// content/index.md at `/` for an app without its own app/pages/index.vue. With
|
|
4
|
+
// framework URLs on, its links to docs pages, from markdown links and component
|
|
5
|
+
// props like `:u-button{to}` alike, go under the first framework, since
|
|
6
|
+
// unprefixed docs pages aren't built.
|
|
7
|
+
//
|
|
8
|
+
// Upgrading Docus: this copies Docus 5.13.0's app/templates/landing.vue.
|
|
9
|
+
// When you raise the `docus` range in package.json, diff that file against
|
|
10
|
+
// this one, carry its changes over, and run `pnpm test:build`.
|
|
11
|
+
import { useFrameworkRoute } from "../../../app/internal/frameworkRoute";
|
|
12
|
+
import { prefixBodyLinks, type MinimarkBody } from "../utils";
|
|
13
|
+
|
|
14
|
+
interface LandingPage {
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
body?: MinimarkBody;
|
|
18
|
+
seo?: { title?: string; description?: string; ogImage?: string };
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const route = useRoute();
|
|
23
|
+
const { values, linkFramework } = useFrameworkRoute();
|
|
24
|
+
const query = queryCollection as unknown as (collection: string) => {
|
|
25
|
+
path: (path: string) => { first: () => Promise<LandingPage | null> };
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const { data: page } = await useAsyncData("landing", async () => {
|
|
29
|
+
const landing = await query("landing").path(route.path).first();
|
|
30
|
+
if (!landing || !linkFramework.value) return landing;
|
|
31
|
+
return { ...landing, body: prefixBodyLinks(landing.body, linkFramework.value, values) };
|
|
32
|
+
});
|
|
33
|
+
if (!page.value) {
|
|
34
|
+
throw createError({ statusCode: 404, statusMessage: "Page not found", fatal: true });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const title = page.value.seo?.title || page.value.title;
|
|
38
|
+
const description = page.value.seo?.description || page.value.description;
|
|
39
|
+
|
|
40
|
+
useSeo({
|
|
41
|
+
title,
|
|
42
|
+
description,
|
|
43
|
+
type: "website",
|
|
44
|
+
ogImage: page.value.seo?.ogImage,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (!page.value.seo?.ogImage) {
|
|
48
|
+
defineOgImage("Landing", {
|
|
49
|
+
title: title?.slice(0, 60),
|
|
50
|
+
description: formatOgDescription(title, description),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
</script>
|
|
54
|
+
|
|
55
|
+
<template>
|
|
56
|
+
<ContentRenderer v-if="page" :value="page" />
|
|
57
|
+
</template>
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// Docus's docs page (docus/app/pages/[[lang]]/[...slug].vue), for
|
|
3
|
+
// `/docs/<framework>/<page>`: it renders the content page at `/docs/<page>`,
|
|
4
|
+
// with only that framework's examples in the page data, links and breadcrumbs
|
|
5
|
+
// under the framework, and the head tags for the frameworks the page covers.
|
|
6
|
+
//
|
|
7
|
+
// Upgrading Docus: this copies Docus 5.13.0's app/pages/[[lang]]/[...slug].vue.
|
|
8
|
+
// When you raise the `docus` range in package.json, diff that file against
|
|
9
|
+
// this one, carry its changes over, and run `pnpm test:build`.
|
|
10
|
+
import { useFrameworkRoute } from "../../../app/internal/frameworkRoute";
|
|
11
|
+
import {
|
|
12
|
+
absoluteUrl,
|
|
13
|
+
contentPathOf,
|
|
14
|
+
findHeadline,
|
|
15
|
+
frameworkPathOf,
|
|
16
|
+
pageFrameworks,
|
|
17
|
+
prefixPageLinks,
|
|
18
|
+
trimPageBody,
|
|
19
|
+
type MinimarkBody,
|
|
20
|
+
type NavigationItem,
|
|
21
|
+
} from "../utils";
|
|
22
|
+
|
|
23
|
+
definePageMeta({
|
|
24
|
+
layout: "docs",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
interface DocsPage {
|
|
28
|
+
path: string;
|
|
29
|
+
title: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
stem?: string;
|
|
32
|
+
extension?: string;
|
|
33
|
+
body?: MinimarkBody;
|
|
34
|
+
seo?: { title?: string; description?: string };
|
|
35
|
+
links?: { to?: unknown; [key: string]: unknown }[];
|
|
36
|
+
modifiedAt?: string;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const route = useRoute();
|
|
41
|
+
const { t } = useDocusI18n();
|
|
42
|
+
const { isOpen } = useAssistant();
|
|
43
|
+
const appConfig = useAppConfig();
|
|
44
|
+
const site = useSiteConfig();
|
|
45
|
+
const navigation = inject<Ref<NavigationItem[] | null>>("navigation");
|
|
46
|
+
const { values } = useFrameworkRoute();
|
|
47
|
+
|
|
48
|
+
const framework = String(route.params.framework);
|
|
49
|
+
const path = contentPathOf(route.params.slug as string[] | undefined);
|
|
50
|
+
const query = queryCollection as unknown as (collection: string) => {
|
|
51
|
+
path: (path: string) => { first: () => Promise<DocsPage | null> };
|
|
52
|
+
};
|
|
53
|
+
const surroundings = queryCollectionItemSurroundings as unknown as (
|
|
54
|
+
collection: string,
|
|
55
|
+
path: string,
|
|
56
|
+
options: { fields: string[] },
|
|
57
|
+
) => Promise<({ path: string } | null)[]>;
|
|
58
|
+
|
|
59
|
+
const [{ data }, { data: surround }] = await Promise.all([
|
|
60
|
+
useAsyncData(`docs-framework-${route.path}`, async () => {
|
|
61
|
+
const page = await query("docs").path(path).first();
|
|
62
|
+
if (!page) return null;
|
|
63
|
+
// Coverage comes from the whole page, and the data keeps one framework's
|
|
64
|
+
// slots. The frontmatter's links, shown in the page header, keep the framework.
|
|
65
|
+
return {
|
|
66
|
+
covers: pageFrameworks(page.body, values),
|
|
67
|
+
page: {
|
|
68
|
+
...page,
|
|
69
|
+
body: trimPageBody(page.body, framework, values),
|
|
70
|
+
links: prefixPageLinks(page.links, framework, values),
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
useAsyncData(`docs-framework-${route.path}-surround`, async () => {
|
|
75
|
+
const items = await surroundings("docs", path, { fields: ["description"] });
|
|
76
|
+
return items.map((item) => item && { ...item, path: frameworkPathOf(item.path, framework) });
|
|
77
|
+
}),
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
if (!data.value) {
|
|
81
|
+
throw createError({ statusCode: 404, statusMessage: "Page not found", fatal: true });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const page = computed(() => data.value!.page);
|
|
85
|
+
const title = page.value.seo?.title || page.value.title;
|
|
86
|
+
const description = page.value.seo?.description || page.value.description;
|
|
87
|
+
|
|
88
|
+
const headline = ref(findHeadline(navigation?.value, path));
|
|
89
|
+
watch(
|
|
90
|
+
() => navigation?.value,
|
|
91
|
+
() => {
|
|
92
|
+
headline.value = findHeadline(navigation?.value, path) || headline.value;
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const breadcrumbs = computed(() =>
|
|
97
|
+
findPageBreadcrumbs(navigation?.value ?? undefined, path)?.map((item) => ({
|
|
98
|
+
...item,
|
|
99
|
+
path: frameworkPathOf(item.path, framework),
|
|
100
|
+
})),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
useSeo({
|
|
104
|
+
title,
|
|
105
|
+
description,
|
|
106
|
+
type: "article",
|
|
107
|
+
modifiedAt: page.value.modifiedAt,
|
|
108
|
+
breadcrumbs,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// A covered page is canonical at its own URL, which `useSeo` sets, and names
|
|
112
|
+
// its versions under the other frameworks that cover it. An uncovered one
|
|
113
|
+
// shows another framework's examples, so it stays out of search results, and
|
|
114
|
+
// its canonical is the first framework that covers it.
|
|
115
|
+
const covers = data.value.covers;
|
|
116
|
+
const urlFor = (value: string) => absoluteUrl(site.url, frameworkPathOf(path, value));
|
|
117
|
+
if (covers.includes(framework)) {
|
|
118
|
+
useHead({
|
|
119
|
+
link: covers
|
|
120
|
+
.filter((value) => value !== framework)
|
|
121
|
+
.map((value) => ({ rel: "alternate", href: urlFor(value), key: `alternate-${value}` })),
|
|
122
|
+
});
|
|
123
|
+
} else {
|
|
124
|
+
useSeoMeta({ robots: "noindex, follow" });
|
|
125
|
+
useHead({ link: [{ rel: "canonical", href: urlFor(covers[0] ?? values[0] ?? framework) }] });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
defineOgImage("Docs", {
|
|
129
|
+
headline: headline.value,
|
|
130
|
+
title: title?.slice(0, 60),
|
|
131
|
+
description: formatOgDescription(title, description),
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const github = computed(() => (appConfig.github ? appConfig.github : null));
|
|
135
|
+
|
|
136
|
+
const editLink = computed(() => {
|
|
137
|
+
if (!github.value) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return [
|
|
142
|
+
github.value.url,
|
|
143
|
+
"edit",
|
|
144
|
+
github.value.branch,
|
|
145
|
+
github.value.rootDir,
|
|
146
|
+
"content",
|
|
147
|
+
`${page.value.stem}.${page.value.extension}`,
|
|
148
|
+
]
|
|
149
|
+
.filter(Boolean)
|
|
150
|
+
.join("/");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// The page's markdown, which the layer serves at the framework's path too.
|
|
154
|
+
addPrerenderPath(`/raw${route.path}.md`);
|
|
155
|
+
</script>
|
|
156
|
+
|
|
157
|
+
<template>
|
|
158
|
+
<UPage v-if="page" :ui="isOpen ? { center: 'lg:col-span-10' } : undefined">
|
|
159
|
+
<UPageHeader
|
|
160
|
+
:title="page.title"
|
|
161
|
+
:description="page.description"
|
|
162
|
+
:headline="headline"
|
|
163
|
+
:ui="{
|
|
164
|
+
wrapper: 'flex-row items-center flex-wrap justify-between',
|
|
165
|
+
}"
|
|
166
|
+
>
|
|
167
|
+
<template #links>
|
|
168
|
+
<UButton v-for="(link, index) in page.links" :key="index" size="sm" v-bind="link" />
|
|
169
|
+
|
|
170
|
+
<DocsPageHeaderLinks />
|
|
171
|
+
</template>
|
|
172
|
+
</UPageHeader>
|
|
173
|
+
|
|
174
|
+
<UPageBody>
|
|
175
|
+
<ContentRenderer v-if="page" :value="page" />
|
|
176
|
+
|
|
177
|
+
<USeparator v-if="github">
|
|
178
|
+
<div class="flex items-center gap-2 text-sm text-muted">
|
|
179
|
+
<UButton
|
|
180
|
+
variant="link"
|
|
181
|
+
color="neutral"
|
|
182
|
+
:to="editLink"
|
|
183
|
+
target="_blank"
|
|
184
|
+
icon="i-lucide-pen"
|
|
185
|
+
:ui="{ leadingIcon: 'size-4' }"
|
|
186
|
+
>
|
|
187
|
+
{{ t("docs.edit") }}
|
|
188
|
+
</UButton>
|
|
189
|
+
<template v-if="github?.url">
|
|
190
|
+
<span>{{ t("common.or") }}</span>
|
|
191
|
+
<UButton
|
|
192
|
+
variant="link"
|
|
193
|
+
color="neutral"
|
|
194
|
+
:to="`${github.url}/issues/new/choose`"
|
|
195
|
+
target="_blank"
|
|
196
|
+
icon="i-lucide-alert-circle"
|
|
197
|
+
:ui="{ leadingIcon: 'size-4' }"
|
|
198
|
+
>
|
|
199
|
+
{{ t("docs.report") }}
|
|
200
|
+
</UButton>
|
|
201
|
+
</template>
|
|
202
|
+
</div>
|
|
203
|
+
</USeparator>
|
|
204
|
+
<UContentSurround :surround="surround" />
|
|
205
|
+
</UPageBody>
|
|
206
|
+
|
|
207
|
+
<template v-if="!isOpen" #right>
|
|
208
|
+
<DocsAsideRight :page="page" />
|
|
209
|
+
</template>
|
|
210
|
+
</UPage>
|
|
211
|
+
</template>
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
|
|
3
|
+
import { queryCollection, useRuntimeConfig } from "#imports";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
pageFrameworks,
|
|
7
|
+
sitemapEntries,
|
|
8
|
+
type DocsSitemapEntry,
|
|
9
|
+
type MinimarkBody,
|
|
10
|
+
type SitemapPage,
|
|
11
|
+
} from "../../utils";
|
|
12
|
+
|
|
13
|
+
export type { DocsSitemapEntry };
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The frameworks a docs page covers, in the order of `frameworks`: those that
|
|
17
|
+
* every `::framework-switcher` on it has a slot for. A page with no switcher
|
|
18
|
+
* covers them all.
|
|
19
|
+
*/
|
|
20
|
+
export function docsPageFrameworks(
|
|
21
|
+
page: { body?: MinimarkBody | null } | null | undefined,
|
|
22
|
+
frameworks: readonly string[],
|
|
23
|
+
): string[] {
|
|
24
|
+
return pageFrameworks(page?.body, frameworks);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The sitemap's entries, as paths: each docs page once for each framework it
|
|
29
|
+
* covers with framework URLs on, or once at its own path with them off. Pages
|
|
30
|
+
* with `sitemap: false` and navigation-only paths are left out, and
|
|
31
|
+
* `modifiedAt` sets `lastmod`. The landing page, `/`, comes last when Docus
|
|
32
|
+
* renders it.
|
|
33
|
+
*/
|
|
34
|
+
export async function getDocsSitemapEntries(event: H3Event): Promise<DocsSitemapEntry[]> {
|
|
35
|
+
const frameworks = useRuntimeConfig(event).public.docsFrameworkUrls?.frameworks ?? [];
|
|
36
|
+
const query = queryCollection as unknown as (
|
|
37
|
+
event: H3Event,
|
|
38
|
+
collection: string,
|
|
39
|
+
) => { all: () => Promise<SitemapPage[]> };
|
|
40
|
+
const pages: SitemapPage[] = [];
|
|
41
|
+
for (const collection of ["docs", "landing"]) {
|
|
42
|
+
try {
|
|
43
|
+
pages.push(...(await query(event, collection).all()));
|
|
44
|
+
} catch {
|
|
45
|
+
// Docus defines `landing` only when the app has no app/pages/index.vue.
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return sitemapEntries(pages, frameworks);
|
|
49
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createError, defineEventHandler, setResponseHeader, useRuntimeConfig } from "#imports";
|
|
2
|
+
|
|
3
|
+
import { rawContentPath } from "../../utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Serves `/raw/docs/<framework>/<page>.md`, the markdown Docus links from each
|
|
7
|
+
* page, with Nuxt Content's `/raw/docs/<page>.md`. The markdown holds every
|
|
8
|
+
* framework's examples. Nitro's router matches the path the request came in
|
|
9
|
+
* with, so the middleware asks for the content path itself rather than
|
|
10
|
+
* rewriting the URL.
|
|
11
|
+
*/
|
|
12
|
+
export default defineEventHandler(async (event) => {
|
|
13
|
+
const frameworks = useRuntimeConfig(event).public.docsFrameworkUrls?.frameworks ?? [];
|
|
14
|
+
const path = rawContentPath(event.node.req.url ?? "", frameworks);
|
|
15
|
+
if (!path) return undefined;
|
|
16
|
+
try {
|
|
17
|
+
const markdown = await event.$fetch<string>(path, { responseType: "text" });
|
|
18
|
+
setResponseHeader(event, "content-type", "text/markdown; charset=utf-8");
|
|
19
|
+
return markdown;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
const statusCode =
|
|
22
|
+
error &&
|
|
23
|
+
typeof error === "object" &&
|
|
24
|
+
"statusCode" in error &&
|
|
25
|
+
typeof error.statusCode === "number"
|
|
26
|
+
? error.statusCode
|
|
27
|
+
: 500;
|
|
28
|
+
throw createError({ statusCode, statusMessage: statusCode === 404 ? "Page not found" : "" });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createError, defineEventHandler, queryCollection } from "#imports";
|
|
2
|
+
|
|
3
|
+
import { isNavigationPath } from "../../utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The content paths of every docs page, for the prerenderer, which builds each
|
|
7
|
+
* of them once per framework. It answers only while prerendering.
|
|
8
|
+
*/
|
|
9
|
+
export default defineEventHandler(async (event) => {
|
|
10
|
+
if (!import.meta.prerender) throw createError({ statusCode: 404 });
|
|
11
|
+
const query = queryCollection as unknown as (
|
|
12
|
+
event: unknown,
|
|
13
|
+
collection: string,
|
|
14
|
+
) => { select: (field: "path") => { all: () => Promise<{ path?: string }[]> } };
|
|
15
|
+
const pages = await query(event, "docs").select("path").all();
|
|
16
|
+
return pages
|
|
17
|
+
.map((page) => page.path)
|
|
18
|
+
.filter(
|
|
19
|
+
(path): path is string =>
|
|
20
|
+
!!path && (path === "/docs" || path.startsWith("/docs/")) && !isNavigationPath(path),
|
|
21
|
+
);
|
|
22
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineEventHandler, setResponseHeader, useRuntimeConfig } from "#imports";
|
|
2
|
+
|
|
3
|
+
import { sitemapXml } from "../../utils";
|
|
4
|
+
import { getDocsSitemapEntries } from "./helpers";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `/sitemap.xml`, with framework URLs on, in place of Docus's: each docs page
|
|
8
|
+
* once for each framework it covers. A site's own `server/routes/sitemap.xml`
|
|
9
|
+
* replaces this one, and can call `getDocsSitemapEntries` for the same list.
|
|
10
|
+
*/
|
|
11
|
+
export default defineEventHandler(async (event) => {
|
|
12
|
+
const { siteUrl } = useRuntimeConfig(event).public.docsFrameworkUrls ?? {};
|
|
13
|
+
setResponseHeader(event, "content-type", "application/xml");
|
|
14
|
+
return sitemapXml(await getDocsSitemapEntries(event), siteUrl);
|
|
15
|
+
});
|