@uxfront/layer-docs 0.8.3 → 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 CHANGED
@@ -47,7 +47,7 @@ export default defineAppConfig({
47
47
  });
48
48
  ```
49
49
 
50
- The wordmark links home, and its accessible name is Docus's `header.title`, or the site name. The byline sits beside the link, not inside it, and phones leave it out so the header's buttons keep their room. With neither set, the header shows Docus's own title or logo.
50
+ The wordmark links home, and the link's accessible name is the text it shows, like "OpenComponents", so someone who uses voice control can say what they see to follow it. The byline sits beside the link, not inside it, and phones leave it out so the header's buttons keep their room. With neither set, the header shows Docus's own title or logo.
51
51
 
52
52
  ## Header links
53
53
 
@@ -142,6 +142,114 @@ export default defineNuxtConfig({
142
142
  });
143
143
  ```
144
144
 
145
+ ## Code themes
146
+
147
+ The layer highlights code in GitHub's high-contrast themes, `github-light-high-contrast` in light mode and `github-dark-high-contrast` in dark mode. Every token colour in them reaches 4.5:1 on the code blocks' background, the contrast [WCAG 1.4.3](https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html) asks of text, whichever neutral colour the app gives Nuxt UI. The Material themes Nuxt UI sets otherwise go as low as 2.18:1, so some comments and strings are hard to read.
148
+
149
+ To use other themes, set all three keys in the app's `nuxt.config.ts`. Shiki writes a rule for `light` that outranks `default`, so if you leave `light` out, light mode keeps the layer's theme:
150
+
151
+ ```ts
152
+ // nuxt.config.ts
153
+ export default defineNuxtConfig({
154
+ content: {
155
+ build: {
156
+ markdown: {
157
+ highlight: {
158
+ theme: { default: "github-light", light: "github-light", dark: "github-dark" },
159
+ },
160
+ },
161
+ },
162
+ },
163
+ });
164
+ ```
165
+
166
+ ## Framework URLs
167
+
168
+ By default, every docs page has one URL for all your frameworks, and the reader's pick lives in their browser. So search engines only ever see the first framework's examples, and a link someone shares opens on whatever the next reader picked last. Framework URLs give each page its own URL for each framework, like `/docs/vue/components/button`, built from the same markdown file, so a search for "vue button" can land on a page that shows Vue.
169
+
170
+ Turn them on in the app's `nuxt.config.ts`, since the routes are decided when the site builds:
171
+
172
+ ```ts
173
+ // nuxt.config.ts
174
+ export default defineNuxtConfig({
175
+ extends: ["@uxfront/layer-docs"],
176
+ frameworkUrls: { enabled: true },
177
+ });
178
+ ```
179
+
180
+ Keep listing the frameworks in `docsTheme.frameworks` in `app/app.config.ts`, as above. The build reads them from there, along with any list that file imports, like `./data/frameworks`. It stops with a message if the list is empty, if the docs aren't in `content/docs/`, or if the app uses Docus's i18n locales, which framework URLs don't support yet.
181
+
182
+ ### What changes
183
+
184
+ With framework URLs on:
185
+
186
+ - **Every docs page is served under each framework.** `content/docs/components/button.md` becomes `/docs/react/components/button`, `/docs/vue/components/button` and so on, and the build writes every one of them, whether or not anything links to it. The unprefixed `/docs/components/button` isn't built. Only the framework values match the segment, so keep content folders from sharing a name with a framework.
187
+ - **The URL decides the framework.** On `/docs/vue/…`, `useFramework()` returns Vue on the server and in the browser alike, and a stored pick never overrides it, so the page hydrates without a flash of another framework.
188
+ - **The select goes to the same page under the new framework.** Picking Svelte on `/docs/vue/components/button` opens `/docs/svelte/components/button`, from the sidebar and from the header's menu alike, and stores the pick.
189
+ - **Each page carries only one framework's examples.** Every `::framework-switcher` keeps only the slot it shows, in the HTML and in the page's payload, and the table of contents drops the headings of the other slots.
190
+ - **Links keep the framework.** The sidebar, the header's menu, previous and next, the breadcrumb and its structured data, search results, the page header's `links` from the frontmatter, links in your markdown (`[Button](/docs/components/button)` and `::card{to="/docs/…"}` alike) and `docsTheme.headerLinks` all point under the current framework. A link that already has a framework in it, like `/docs/react/…`, stays as it is, and so does `/docs` itself. On a page that isn't under a framework, like the homepage or the 404 page, they take the first framework, and so do the links in `content/index.md` when Docus's landing page renders it.
191
+ - **Each page's markdown is served at its framework path too.** Docus's "Copy page" button and `/raw/docs/vue/components/button.md` return the same markdown as `/raw/docs/components/button.md`, with every framework's examples in it.
192
+
193
+ A few things stay as they are, so keep them in mind:
194
+
195
+ - **Docus's sub-navigation mode** (`navigation.sub` in `app.config.ts`) isn't supported: its section bar and mobile bar keep their links unprefixed.
196
+ - **A `content/docs/index.md` page** is served at `/docs/<framework>`, but links to `/docs` stay `/docs`, so a crawler that follows one builds `/docs` with every framework's examples. Link to `docsPath("/")` instead.
197
+ - **Your own pages under `/docs/`**, like `app/pages/docs/changelog.vue`, get their links put under a framework and aren't built. Keep app pages outside `/docs/`.
198
+ - **Your own pages that render content** with `ContentRenderer` keep their markdown links as they are. Link to the docs from them with `docsPath()`.
199
+
200
+ ### Links outside the docs
201
+
202
+ Links the layer doesn't render, like a "Read the docs" button on your own homepage, take their URL from `docsPath()`, which every component and composable can call:
203
+
204
+ ```vue
205
+ <!-- app/pages/index.vue -->
206
+ <template>
207
+ <UButton :to="docsPath('/getting-started/introduction')">Read the docs</UButton>
208
+ </template>
209
+ ```
210
+
211
+ | Argument | Type | What it does |
212
+ | ----------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
213
+ | `page` | `string` | The page's path below `/docs`, like `/components/button`. |
214
+ | `framework` | `string` | The framework to link to. Leave it out to use the current route's framework, or the first framework on a page that isn't under one, like a homepage. |
215
+
216
+ It returns `/docs/<framework>/<page>`, like `/docs/react/components/button`. With framework URLs off, it returns `/docs/<page>` and ignores `framework`, so you can use it before you turn them on.
217
+
218
+ ### Coverage and search engines
219
+
220
+ A page **covers** a framework when every `::framework-switcher` on it has a slot for it. A page with no switcher covers all of them. A page is still built under every framework, so the select never leads to a 404, and the head tags tell search engines which version to show:
221
+
222
+ - **A page that covers the framework** is canonical at its own URL, and has a `<link rel="alternate">` to its version under each other framework that covers it.
223
+ - **A page that doesn't** shows the first framework it covers, with the switcher's "Not available for Solid, showing React." note. It's marked `<meta name="robots" content="noindex, follow">`, and its canonical points to the first framework that covers it, so search engines index the page that really has the examples.
224
+
225
+ `docsPageFrameworks(page, frameworks)` returns the frameworks a page covers, in the order of `frameworks`. It's auto-imported in the app and in your server routes, and works with framework URLs off too.
226
+
227
+ ### The sitemap
228
+
229
+ With framework URLs on, the layer serves `/sitemap.xml` in place of Docus's: each docs page once for each framework that covers it, with the date part of the page's `modifiedAt` as `<lastmod>`. Pages with `sitemap: false` in their frontmatter, `.navigation` files and the versions a page doesn't cover stay out. Docus's landing page, `/`, is listed when your app uses it, as Docus does. The URLs are absolute when the site has a URL, from `site.url` or `NUXT_SITE_URL`, read when the site builds.
230
+
231
+ A site that has its own `server/routes/sitemap.xml.ts` keeps it, and the layer adds nothing there. To list the docs pages in it, call `getDocsSitemapEntries(event)`, which is auto-imported in your server routes:
232
+
233
+ ```ts
234
+ // server/routes/sitemap.xml.ts
235
+ export default defineEventHandler(async (event) => {
236
+ // Your own homepage, app/pages/index.vue, comes first. With Docus's landing
237
+ // page instead, the entries already list `/`.
238
+ const entries = [{ loc: "/" }, ...(await getDocsSitemapEntries(event))];
239
+ // Write `entries` as XML, with your site's URL before each `loc`…
240
+ });
241
+ ```
242
+
243
+ Each entry has these fields:
244
+
245
+ | Field | Type | What it holds |
246
+ | --------- | -------- | --------------------------------------------------------------------------------------------------------------------------- |
247
+ | `loc` | `string` | The page's URL path: `/docs/vue/components/button` for a docs page, and `/` for Docus's landing page. |
248
+ | `lastmod` | `string` | The date part of the page's `modifiedAt`, like `2026-11-19`. It's left out when the page has none. |
249
+ | `path` | `string` | The page's content path, like `/docs/components/button`, the same for each framework's entry, and `/` for the landing page. |
250
+
251
+ With framework URLs off, it lists each page once, with `loc` the same as `path`. Docus's landing page is in the list whenever your app uses it, so only add `/` yourself when your app has its own `app/pages/index.vue`.
252
+
145
253
  ## What it adds
146
254
 
147
255
  - **A Framework select above the sidebar.** It picks the framework for the whole site, and lines up with the navigation's pages below it. It replaces Docus's `DocsAsideLeftTop` and renders Docus's below it.
@@ -149,10 +257,15 @@ export default defineNuxtConfig({
149
257
  - **The links and the same select in the header's menu.** On smaller screens, where Docus hides the sidebar, the menu that stands in for it starts with the header links below `768px`, then the select. It replaces Docus's `AppHeaderBody` and renders Docus's below them.
150
258
  - **`AppHeaderLinks` and `AppHeaderFramework`.** The links and the Framework select on their own, for a site that replaces `AppHeaderCTA` or `AppHeaderBody`.
151
259
  - **`FrameworkSwitcher`.** It shows the slot for the reader's framework, with no tabs of its own, since the select already picks one for every page. A page doesn't have to cover every framework: a missing one shows the first one the page has, with a note saying so.
260
+ - **A named menu toggle.** On smaller screens, the header's menu button says what it does, "Open menu" or "Close menu", in Nuxt UI's words, translated with its locale. Docus's has no name, which [WCAG 4.1.2](https://www.w3.org/WAI/WCAG22/Understanding/name-role-value.html) asks every control to have, so screen readers announce it only as "button". It replaces Docus's `IconMenuToggle`, and its lines don't move for readers who ask for reduced motion.
152
261
  - **A header wordmark and byline.** It replaces Docus's `AppHeaderLeft`, and renders Docus's unless the app sets a wordmark.
153
262
  - **Typed app config.** `useAppConfig()` knows `docsTheme`, so `wordmark`, `byline` and `headerLinks` typecheck even in an app that leaves them unset.
154
- - **`useFramework()`.** The reader's pick, shared by every switcher and the select, and kept in `localStorage` across visits. It's read once the page is mounted, so the prerendered HTML shows the default framework and hydrates cleanly.
263
+ - **`useFramework()`.** The reader's pick, shared by every switcher and the select, and kept in `localStorage` across visits. It's read once the page is mounted, so the prerendered HTML shows the default framework and hydrates cleanly. With framework URLs on, the route's framework wins on a framework route. It returns `{ framework, frameworks, current }`.
264
+ - **Framework URLs, when you turn them on.** Every docs page at `/docs/<framework>/<page>`, built once per framework with only that framework's examples (see [Framework URLs](#framework-urls)). They add a route with a copy of Docus's docs page, use a copy of Docus's landing page, and replace Docus's `DocsAsideLeftBody` and `AppSearch` to put their links under the framework. Off, those two render Docus's own.
265
+ - **`docsPath()` and `docsPageFrameworks()`.** A docs page's URL for links outside the Docus shell, and the frameworks a page covers. Both are auto-imported, and `docsPageFrameworks` and `getDocsSitemapEntries` are auto-imported in server routes too.
155
266
  - **Highlighting for the frameworks' languages.** `tsx`, `svelte`, `angular-html`, `angular-ts` and `astro`, on top of Docus's.
267
+ - **Readable code colours.** GitHub's high-contrast themes, whose every colour reaches 4.5:1 on the code blocks' background (see [Code themes](#code-themes)).
156
268
  - **Bundled icons.** Nuxt Icon bundles the icons named in app config too, so the select's icons don't come from the Iconify API.
269
+ - **Ids that survive hydration.** Ids from `useId()` come out the same in the prerendered page and in the browser, so the select's label and the sidebar's accordions still point at elements that exist once the page hydrates. Nuxt removes `onServerPrefetch` calls from production client builds, and Nuxt Icon's CSS icons make one, so without this every id after the header's first icon changed in the browser. The layer keeps those calls in the client build, where they never run.
157
270
 
158
271
  Everything else is Docus's: configure it as its [docs](https://docus.dev) describe.
@@ -0,0 +1,82 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Replaces Docus's mobile menu toggle (docus/app/components/IconMenuToggle.vue),
4
+ * which its header (docus/app/components/app/AppHeader.vue) renders in place of
5
+ * Nuxt UI's, to name it. Docus's is an icon-only button with no name, so screen
6
+ * readers announce just "button". This one says what it does, "Open menu" or
7
+ * "Close menu", in Nuxt UI's words for its own toggle, translated with its locale.
8
+ *
9
+ * Docus springs the three lines into a cross with motion-v, which isn't the
10
+ * layer's to import, so they turn with a CSS transition here, and don't move for
11
+ * readers who ask for reduced motion. Keep the props and the button in step with
12
+ * Docus's. Docus 5.15.0 names its own toggle, so this file can go once the layer
13
+ * requires that version or later.
14
+ */
15
+ import { useLocale } from "#ui/composables/useLocale";
16
+
17
+ defineProps<{
18
+ open: boolean;
19
+ }>();
20
+
21
+ const { t } = useLocale();
22
+ </script>
23
+
24
+ <template>
25
+ <UButton
26
+ size="sm"
27
+ variant="ghost"
28
+ color="neutral"
29
+ class="-me-1.5"
30
+ square
31
+ :aria-label="open ? t('header.close') : t('header.open')"
32
+ >
33
+ <svg
34
+ xmlns="http://www.w3.org/2000/svg"
35
+ class="docs-menu-toggle size-5"
36
+ :class="{ 'docs-menu-toggle--open': open }"
37
+ viewBox="0 0 24 24"
38
+ fill="none"
39
+ stroke="currentColor"
40
+ stroke-width="2"
41
+ stroke-linecap="round"
42
+ stroke-linejoin="round"
43
+ aria-hidden="true"
44
+ >
45
+ <line x1="4" y1="6" x2="20" y2="6" />
46
+ <line x1="4" y1="12" x2="20" y2="12" />
47
+ <line x1="4" y1="18" x2="20" y2="18" />
48
+ </svg>
49
+ </UButton>
50
+ </template>
51
+
52
+ <style scoped>
53
+ /* Styled here rather than with Tailwind utilities: Docus's stylesheet doesn't
54
+ list this layer among Tailwind's sources, so only utilities Docus happens to
55
+ use itself, like the button's `-me-1.5` and the icon's `size-5`, are generated. */
56
+ .docs-menu-toggle line {
57
+ transform-box: fill-box;
58
+ transform-origin: center;
59
+ transition:
60
+ transform 0.2s ease-out,
61
+ opacity 0.2s ease-out;
62
+ }
63
+
64
+ /* The top and bottom lines meet in the middle and cross, and the middle one fades. */
65
+ .docs-menu-toggle--open line:nth-child(1) {
66
+ transform: translateY(6px) rotate(45deg);
67
+ }
68
+
69
+ .docs-menu-toggle--open line:nth-child(2) {
70
+ opacity: 0;
71
+ }
72
+
73
+ .docs-menu-toggle--open line:nth-child(3) {
74
+ transform: translateY(-6px) rotate(-45deg);
75
+ }
76
+
77
+ @media (prefers-reduced-motion: reduce) {
78
+ .docs-menu-toggle line {
79
+ transition: none;
80
+ }
81
+ }
82
+ </style>
@@ -1,11 +1,24 @@
1
1
  <script setup lang="ts">
2
+ import DocusAppHeaderBody from "docus/app/components/app/AppHeaderBody.vue";
2
3
  // Replaces Docus's to start the header's menu, which stands in for the sidebar
3
4
  // on smaller screens, with `docsTheme.headerLinks` below 768px, where
4
5
  // AppHeaderCTA hides them, and the framework select, as DocsAsideLeftTop puts
5
- // it above the sidebar. Docus's renders below them.
6
- import DocusAppHeaderBody from "docus/app/components/app/AppHeaderBody.vue";
6
+ // it above the sidebar. Docus's renders below them, with the navigation under
7
+ // the route's framework when framework URLs are on.
8
+ import type { Ref } from "vue";
9
+
10
+ import type { NavigationItem } from "../../../modules/framework-urls/utils";
11
+ import { useFrameworkRoute } from "../../internal/frameworkRoute";
7
12
 
8
13
  const appConfig = useAppConfig();
14
+ const navigation = inject<Ref<NavigationItem[] | null>>("navigation");
15
+ const { enabled, navigationFor } = useFrameworkRoute();
16
+
17
+ if (enabled)
18
+ provide(
19
+ "navigation",
20
+ computed(() => navigationFor(navigation?.value)),
21
+ );
9
22
  </script>
10
23
 
11
24
  <template>
@@ -2,20 +2,20 @@
2
2
  // Replaces Docus's, which prints the site name as plain text, to set the app's
3
3
  // wordmark (`docsTheme.wordmark`) and sign it "by UXFront" (`docsTheme.byline`),
4
4
  // the way the UXFront homepage headers do. With neither, it renders Docus's own.
5
+ // The link has no `aria-label`, so its name is the wordmark it shows, as WCAG
6
+ // 2.5.3 asks: someone who says "click OpenComponents" reaches it.
5
7
  import UxFrontMark from "@uxfront/ui/components/icons/UxFrontMark.vue";
6
8
  import DocusAppHeaderLeft from "docus/app/components/app/AppHeaderLeft.vue";
7
9
 
8
10
  const appConfig = useAppConfig();
9
- const site = useSiteConfig();
10
11
  const { localePath } = useDocusI18n();
11
12
 
12
13
  const wordmark = computed(() => appConfig.docsTheme.wordmark);
13
- const ariaLabel = computed(() => appConfig.header?.title || site.name);
14
14
  </script>
15
15
 
16
16
  <template>
17
17
  <div class="docs-header-left">
18
- <NuxtLink v-if="wordmark" :to="localePath('/')" :aria-label="ariaLabel" class="docs-wordmark">
18
+ <NuxtLink v-if="wordmark" :to="localePath('/')" class="docs-wordmark">
19
19
  <strong>{{ wordmark.bold }}</strong
20
20
  >{{ wordmark.regular }}
21
21
  </NuxtLink>
@@ -2,8 +2,10 @@
2
2
  // The header's links, from `docsTheme.headerLinks`, or `items` for a site that
3
3
  // splits them around controls of its own, like a Version select. A link with
4
4
  // `activeFor` is active on that path and below it, so the docs link stays
5
- // highlighted on every docs page, not only the one it points to.
5
+ // highlighted on every docs page, not only the one it points to. With framework
6
+ // URLs on, a link to a docs page goes to it under the route's framework.
6
7
  import type { HeaderLink } from "../../composables/useFramework";
8
+ import { useFrameworkRoute } from "../../internal/frameworkRoute";
7
9
 
8
10
  const props = defineProps<{
9
11
  orientation: "horizontal" | "vertical";
@@ -13,9 +15,11 @@ const props = defineProps<{
13
15
 
14
16
  const appConfig = useAppConfig();
15
17
  const route = useRoute();
18
+ const { toFramework } = useFrameworkRoute();
16
19
 
17
20
  const links = computed(() =>
18
- (props.items ?? appConfig.docsTheme.headerLinks ?? []).map(({ activeFor, ...link }) => {
21
+ (props.items ?? appConfig.docsTheme.headerLinks ?? []).map(({ activeFor, ...item }) => {
22
+ const link = { ...item, to: toFramework(item.to) };
19
23
  if (activeFor === undefined) return link;
20
24
  const active = route.path === activeFor || route.path.startsWith(`${activeFor}/`);
21
25
  return { ...link, active, "aria-current": active ? ("page" as const) : undefined };
@@ -0,0 +1,21 @@
1
+ <script setup lang="ts">
2
+ // Replaces Docus's to keep search results and navigation under the route's
3
+ // framework when framework URLs are on. Without them, it renders Docus's own,
4
+ // so a site that doesn't opt in gets Docus's search as it ships.
5
+ import DocusAppSearch from "docus/app/components/app/AppSearch.vue";
6
+
7
+ import type { NavigationItem } from "../../../modules/framework-urls/utils";
8
+ import { useFrameworkRoute } from "../../internal/frameworkRoute";
9
+ import FrameworkSearch from "../../internal/FrameworkSearch.vue";
10
+
11
+ defineProps<{
12
+ navigation?: NavigationItem[] | null;
13
+ }>();
14
+
15
+ const { enabled } = useFrameworkRoute();
16
+ </script>
17
+
18
+ <template>
19
+ <FrameworkSearch v-if="enabled" :navigation="navigation" />
20
+ <DocusAppSearch v-else :navigation="navigation" />
21
+ </template>
@@ -0,0 +1,23 @@
1
+ <script setup lang="ts">
2
+ import DocusAsideLeftBody from "docus/app/components/docs/DocsAsideLeftBody.vue";
3
+ // Replaces Docus's to give the sidebar's navigation the framework of the route,
4
+ // with framework URLs on: Docus's renders below, reading the navigation this
5
+ // provides. Without them, it gets Docus's navigation as it is.
6
+ import type { Ref } from "vue";
7
+
8
+ import type { NavigationItem } from "../../../modules/framework-urls/utils";
9
+ import { useFrameworkRoute } from "../../internal/frameworkRoute";
10
+
11
+ const navigation = inject<Ref<NavigationItem[] | null>>("navigation");
12
+ const { enabled, navigationFor } = useFrameworkRoute();
13
+
14
+ if (enabled)
15
+ provide(
16
+ "navigation",
17
+ computed(() => navigationFor(navigation?.value)),
18
+ );
19
+ </script>
20
+
21
+ <template>
22
+ <DocusAsideLeftBody />
23
+ </template>
@@ -1,4 +1,7 @@
1
1
  import { useLocalStorage } from "@vueuse/core";
2
+ import type { Ref } from "vue";
3
+
4
+ import { useFrameworkRoute } from "../internal/frameworkRoute";
2
5
 
3
6
  /**
4
7
  * A framework offered by the `FrameworkSwitcher` content component and the
@@ -76,16 +79,39 @@ declare module "@nuxt/schema" {
76
79
  *
77
80
  * The stored pick is only read once mounted, so the prerendered HTML and the
78
81
  * first client render both show the default, and hydrate cleanly.
82
+ *
83
+ * With framework URLs on, a framework route, like `/docs/vue/…`, decides the
84
+ * framework instead, on the server and in the browser alike, and a stored pick
85
+ * never overrides it. Setting `framework` there stores the pick and goes to the
86
+ * same page under the new framework.
79
87
  */
80
88
  export function useFramework() {
81
89
  const appConfig = useAppConfig();
90
+ const route = useRoute();
91
+ const { routeFramework, values } = useFrameworkRoute();
82
92
 
83
93
  const frameworks = computed<FrameworkOption[]>(() => appConfig.docsTheme.frameworks);
84
94
 
85
- const framework = useLocalStorage("docs-theme:framework", frameworks.value[0]?.value ?? "", {
95
+ const stored = useLocalStorage("docs-theme:framework", frameworks.value[0]?.value ?? "", {
86
96
  initOnMounted: true,
87
97
  });
88
98
 
99
+ const framework: Ref<string> = values.length
100
+ ? computed({
101
+ get: () => routeFramework.value ?? stored.value,
102
+ set: (value) => {
103
+ stored.value = value;
104
+ const from = routeFramework.value;
105
+ if (!from || value === from || !values.includes(value)) return;
106
+ void navigateTo({
107
+ path: `/docs/${value}${route.path.slice(`/docs/${from}`.length)}`,
108
+ query: route.query,
109
+ hash: route.hash,
110
+ });
111
+ },
112
+ })
113
+ : stored;
114
+
89
115
  // A stored pick that's no longer on the list falls back to the default.
90
116
  const current = computed(
91
117
  () =>
@@ -0,0 +1,97 @@
1
+ <script setup lang="ts">
2
+ import type { NavigationItem } from "../../modules/framework-urls/utils";
3
+ // Docus's search (docus/app/components/app/AppSearch.vue), with its results
4
+ // and navigation under the route's framework, so a search opened on a
5
+ // `/docs/vue/…` page leads to `/docs/vue/…`. AppSearch renders it only with
6
+ // framework URLs on, and Docus's own otherwise.
7
+ //
8
+ // Upgrading Docus: this copies Docus 5.13.0's app/components/app/AppSearch.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 "./frameworkRoute";
12
+
13
+ const props = defineProps<{
14
+ navigation?: NavigationItem[] | null;
15
+ }>();
16
+
17
+ interface SearchSection {
18
+ id: string;
19
+ [key: string]: unknown;
20
+ }
21
+
22
+ const appConfig = useAppConfig();
23
+ const { forced: forcedColorMode } = useDocusColorMode();
24
+ const { locale, isEnabled } = useDocusI18n();
25
+ const { enabled, toFramework, navigationFor } = useFrameworkRoute();
26
+
27
+ const collectionName = computed(() => (isEnabled.value ? `docs_${locale.value}` : "docs") as never);
28
+ const useFts = appConfig.search.fts;
29
+
30
+ const { data: rawFiles } = useFts
31
+ ? { data: ref(null) }
32
+ : useLazyAsyncData(
33
+ `search_${collectionName.value}`,
34
+ () => queryCollectionSearchSections(collectionName.value),
35
+ {
36
+ server: false,
37
+ watch: [locale],
38
+ },
39
+ );
40
+
41
+ const files = computed(() =>
42
+ enabled && rawFiles.value
43
+ ? (rawFiles.value as SearchSection[]).map((file) => ({ ...file, id: toFramework(file.id) }))
44
+ : rawFiles.value,
45
+ );
46
+
47
+ const {
48
+ search: rawSearch,
49
+ status: searchStatus,
50
+ init,
51
+ } = useFts
52
+ ? useSearchCollection(collectionName, { immediate: false, ignoredTags: ["style"] })
53
+ : { search: undefined, status: ref(undefined), init: () => {} };
54
+
55
+ const search =
56
+ enabled && rawSearch
57
+ ? async (...args: Parameters<typeof rawSearch>) =>
58
+ ((await rawSearch(...args)) as SearchSection[]).map((result) => ({
59
+ ...result,
60
+ id: toFramework(result.id),
61
+ }))
62
+ : rawSearch;
63
+
64
+ if (useFts) {
65
+ const { open } = useContentSearch();
66
+ watch(open, (value) => {
67
+ if (value && searchStatus.value === "idle") {
68
+ init();
69
+ }
70
+ });
71
+ }
72
+
73
+ const navigation = computed(() => (enabled ? navigationFor(props.navigation) : props.navigation));
74
+
75
+ const links = computed(() =>
76
+ useFts
77
+ ? navigation.value
78
+ ?.filter((item) => item.children?.length)
79
+ .map((item) => ({
80
+ label: item.title,
81
+ icon: item.icon,
82
+ to: item.children![0]!.path,
83
+ }))
84
+ : undefined,
85
+ );
86
+ </script>
87
+
88
+ <template>
89
+ <LazyUContentSearch
90
+ :files="files"
91
+ :search="search"
92
+ :search-status="searchStatus"
93
+ :links="links"
94
+ :navigation="navigation"
95
+ :color-mode="!forcedColorMode"
96
+ />
97
+ </template>
@@ -0,0 +1,39 @@
1
+ import {
2
+ FRAMEWORK_ROUTE_NAME,
3
+ prefixDocsPath,
4
+ prefixNavigation,
5
+ type NavigationItem,
6
+ } from "../../modules/framework-urls/utils";
7
+
8
+ /**
9
+ * Where framework URLs stand on the current route: whether they're on, the
10
+ * route's framework, and the framework links take. Off a framework route,
11
+ * links take the first framework, so a page built once, like the 404 page,
12
+ * never depends on the reader's stored pick.
13
+ */
14
+ export function useFrameworkRoute() {
15
+ const route = useRoute();
16
+ const config = useRuntimeConfig().public.docsFrameworkUrls;
17
+ const enabled = !!config;
18
+ const values = config?.frameworks ?? [];
19
+
20
+ const routeFramework = computed(() => {
21
+ if (!enabled || route.name !== FRAMEWORK_ROUTE_NAME) return undefined;
22
+ const value = route.params.framework;
23
+ return typeof value === "string" && values.includes(value) ? value : undefined;
24
+ });
25
+
26
+ const linkFramework = computed(() => routeFramework.value ?? values[0]);
27
+
28
+ /** A same-site link to a docs page, under the framework links take. */
29
+ const toFramework = <T>(link: T): T =>
30
+ enabled && linkFramework.value ? prefixDocsPath(link, linkFramework.value, values) : link;
31
+
32
+ /** A navigation tree with every path under the framework links take. */
33
+ const navigationFor = <T extends NavigationItem>(items: T[] | null | undefined) =>
34
+ enabled && linkFramework.value
35
+ ? prefixNavigation(items, linkFramework.value, values)
36
+ : (items ?? []);
37
+
38
+ return { enabled, values, routeFramework, linkFramework, toFramework, navigationFor };
39
+ }
@@ -0,0 +1,31 @@
1
+ import { pageFrameworks, type MinimarkBody } from "../../modules/framework-urls/utils";
2
+ import { useFrameworkRoute } from "../internal/frameworkRoute";
3
+
4
+ /**
5
+ * The URL of a docs page, for links outside the Docus shell, like a homepage's
6
+ * "Read the docs" button: `docsPath("/components/button")` is
7
+ * `/docs/react/components/button` on a site with framework URLs, under the
8
+ * framework of the current route, or the first framework off a framework
9
+ * route. Pass `framework` to pick one. Without framework URLs, it's
10
+ * `/docs/components/button`.
11
+ */
12
+ export function docsPath(page: string, framework?: string): string {
13
+ const path = page.startsWith("/") ? page : `/${page}`;
14
+ const { enabled, linkFramework } = useFrameworkRoute();
15
+ const value = framework ?? linkFramework.value;
16
+ return enabled && value
17
+ ? `/docs/${value}${path === "/" ? "" : path}`
18
+ : `/docs${path === "/" ? "" : path}`;
19
+ }
20
+
21
+ /**
22
+ * The frameworks a docs page covers, in the order of `frameworks`: those that
23
+ * every `::framework-switcher` on it has a slot for. A page with no switcher
24
+ * covers them all.
25
+ */
26
+ export function docsPageFrameworks(
27
+ page: { body?: MinimarkBody | null } | null | undefined,
28
+ frameworks: readonly string[],
29
+ ): string[] {
30
+ return pageFrameworks(page?.body, frameworks);
31
+ }