@graphql-markdown/nuxt-theme 0.2.2 → 0.2.4

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.
@@ -20,18 +20,20 @@
20
20
 
21
21
  <div class="relative overflow-x-auto p-5 font-mono text-sm">
22
22
  <!-- Inside a code group the tab bar carries the label, so the card drops
23
- its header. The kind badge and copy button float over the snippet
24
- instead, so the kind stays visible for every type. -->
23
+ its header. Only the copy button floats over the snippet; the entry
24
+ heading already shows the kind badge. -->
25
25
  <div
26
26
  v-if="hideHeader"
27
27
  class="absolute top-3 right-3 z-10 flex items-center gap-2"
28
28
  >
29
- <UBadge color="primary" variant="subtle" size="sm">{{ kind }}</UBadge>
30
-
31
29
  <UButton v-bind="copyButtonProps" @click="copy(code)" />
32
30
  </div>
33
31
 
34
- <div class="blueprint-code" v-html="html" />
32
+ <div
33
+ class="blueprint-code"
34
+ :class="{ 'pr-12': hideHeader }"
35
+ v-html="html"
36
+ />
35
37
  </div>
36
38
 
37
39
  <!-- Example variables stack under the query, with their own copy button. -->
@@ -16,10 +16,10 @@ import type {
16
16
  ApiNavigationNode,
17
17
  } from "./useApiNavigation";
18
18
 
19
- /** Anvil's own order: queries, then mutations, then subscriptions, then types. */
19
+ /** Anvil's own order: queries, then mutations, then subscriptions, then types, then directives. */
20
20
  const OPERATION_KINDS = ["queries", "mutations", "subscriptions"] as const;
21
21
  type OperationKind = (typeof OPERATION_KINDS)[number];
22
- type BucketId = OperationKind | "types";
22
+ type BucketId = OperationKind | "directives" | "types";
23
23
 
24
24
  /**
25
25
  * One entry of the single-page view. Typed by the swizzlable
@@ -71,6 +71,9 @@ export const anchorIdFor = (page: { path: string; kind?: string }): string => {
71
71
  };
72
72
 
73
73
  const bucketIdFor = (kind: string | undefined): BucketId => {
74
+ if (kind === "directives") {
75
+ return "directives";
76
+ }
74
77
  return kind && (OPERATION_KINDS as readonly string[]).includes(kind)
75
78
  ? (kind as OperationKind)
76
79
  : "types";
@@ -81,6 +84,7 @@ const BUCKET_TITLES: Record<BucketId, string> = {
81
84
  mutations: "Mutations",
82
85
  subscriptions: "Subscriptions",
83
86
  types: "Types",
87
+ directives: "Directives",
84
88
  };
85
89
 
86
90
  const buildEntry = async (
@@ -129,7 +133,7 @@ const buildEntry = async (
129
133
  * Everything the single-page reference view needs: every generated page's
130
134
  * full body, fixed up and split into lead/sections the same way
131
135
  * `useApiDocument` does for one page at a time, grouped into
132
- * Queries/Mutations/Subscriptions/Types buckets in Anvil's order, with every
136
+ * Queries/Mutations/Subscriptions/Types/Directives buckets in Anvil's order, with every
133
137
  * definition and example already Shiki-highlighted. Rendered at the base
134
138
  * route (`[...slug].vue`'s landing branch) whenever `useHierarchyMode()`
135
139
  * detects flat hierarchy, in place of the per-type catalog.
@@ -206,7 +210,15 @@ export const useApiSinglePage = async (
206
210
  }),
207
211
  );
208
212
 
209
- return (["queries", "mutations", "subscriptions", "types"] as const)
213
+ return (
214
+ [
215
+ "queries",
216
+ "mutations",
217
+ "subscriptions",
218
+ "types",
219
+ "directives",
220
+ ] as const
221
+ )
210
222
  .map((id) => {
211
223
  return {
212
224
  id,
@@ -76,9 +76,11 @@
76
76
  <!-- Nuxt UI reads the dialog's accessible name from `contentSearch.title`
77
77
  and `contentSearch.description`, which its bundled locales do not
78
78
  define — without these props the modal announces the raw keys. -->
79
+ <!-- Search groups are keyed by `path`; branches have none, so give them one
80
+ here (the sidebar keeps the raw tree). -->
79
81
  <UContentSearch
80
- :navigation="navigationSections as unknown as ContentNavigationItem[]"
81
- :files="searchFiles ?? []"
82
+ :navigation="searchNavigation as unknown as ContentNavigationItem[]"
83
+ :files="singlePageSearchFiles"
82
84
  title="Search the API reference"
83
85
  description="Find an operation, type, field, or example by name."
84
86
  />
@@ -89,6 +91,10 @@
89
91
  <script setup lang="ts">
90
92
  import type { ContentNavigationItem } from "@nuxt/content";
91
93
  import { useApiSinglePageNavigation } from "~/composables/useApiSinglePage";
94
+ import {
95
+ toSinglePageSearchSections,
96
+ withSearchGroupIds,
97
+ } from "~/utils/search-sections";
92
98
  import { scrollToIntro } from "~/utils/scroll-to-intro";
93
99
 
94
100
  const config = useAppConfig();
@@ -121,6 +127,10 @@ const navigationSections = computed(() =>
121
127
  : sections.value,
122
128
  );
123
129
 
130
+ const searchNavigation = computed(() =>
131
+ withSearchGroupIds(navigationSections.value),
132
+ );
133
+
124
134
  // Keep the highlighted sidebar link visible as the page scrolls.
125
135
  watch(
126
136
  activeAnchor,
@@ -140,7 +150,13 @@ watch(
140
150
  );
141
151
 
142
152
  const { data: searchFiles } = await useAsyncData("api-reference-search", () =>
143
- queryCollectionSearchSections("content"),
153
+ queryCollectionSearchSections("content", { ignoredTags: ["style"] }),
154
+ );
155
+
156
+ // Flat hierarchy's sidebar leaves are `#anchor` links, so search results must
157
+ // carry the same `#anchor` ids for Nuxt UI to match them.
158
+ const singlePageSearchFiles = computed(() =>
159
+ toSinglePageSearchSections(searchFiles.value ?? [], isFlat.value),
144
160
  );
145
161
  </script>
146
162
 
@@ -0,0 +1,88 @@
1
+ import { extractAnchor } from "./api-document";
2
+
3
+ /** The subset of a `queryCollectionSearchSections` item this helper touches. */
4
+ export interface SearchSection {
5
+ [key: string]: unknown;
6
+ id: string;
7
+ title: string;
8
+ titles: string[];
9
+ level: number;
10
+ content: string;
11
+ }
12
+
13
+ const stripAnchorMarker = (value: string): string => {
14
+ const anchor = extractAnchor(value);
15
+ return anchor ? anchor.text.trim() : value;
16
+ };
17
+
18
+ /**
19
+ * Removes Shiki theme CSS (`html .shiki span {...}`) that leaks into indexed
20
+ * content. Plain index scanning rather than a regex, so there is no
21
+ * backtracking risk.
22
+ */
23
+ export const stripShikiCss = (content: string): string => {
24
+ let result = content;
25
+ let from = 0;
26
+ for (;;) {
27
+ const marker = result.indexOf(".shiki", from);
28
+ if (marker === -1) break;
29
+ const open = result.indexOf("{", marker);
30
+ const close = open === -1 ? -1 : result.indexOf("}", open);
31
+ if (close === -1) break;
32
+ let start = result.lastIndexOf("html", marker);
33
+ if (start === -1 || result.slice(start, marker).includes("}")) {
34
+ start = marker;
35
+ }
36
+ result = `${result.slice(0, start)} ${result.slice(close + 1)}`;
37
+ from = start;
38
+ }
39
+ return result.split(/\s+/).filter(Boolean).join(" ");
40
+ };
41
+
42
+ const anchorFromId = (id: string): string => {
43
+ const base = id.split("#")[0] ?? id;
44
+ return base.split("/").findLast(Boolean) ?? base;
45
+ };
46
+
47
+ /**
48
+ * Cleans search sections and, in flat (single-page) mode, points every
49
+ * section at its entry's `#<anchorId>` so it matches the single-page
50
+ * sidebar's leaf paths (Nuxt UI buckets files by `id.split("#")[0]`, which
51
+ * is empty for a bare hash id and falls back to the whole id).
52
+ */
53
+ export const toSinglePageSearchSections = <T extends SearchSection>(
54
+ files: readonly T[],
55
+ isFlat: boolean,
56
+ ): T[] => {
57
+ return files.map((file) => {
58
+ const cleaned = {
59
+ ...file,
60
+ title: stripAnchorMarker(file.title),
61
+ titles: file.titles.map(stripAnchorMarker),
62
+ content: stripShikiCss(file.content),
63
+ };
64
+ return isFlat ? { ...cleaned, id: `#${anchorFromId(file.id)}` } : cleaned;
65
+ });
66
+ };
67
+
68
+ /**
69
+ * Gives every top-level navigation branch without a `path` a synthetic one so
70
+ * Nuxt UI's UContentSearch (which keys each CommandPalette group by
71
+ * `group.path` and silently drops groups without an `id`) keeps its results.
72
+ * Returns a shallow copy; the input is not mutated.
73
+ */
74
+ export const withSearchGroupIds = <
75
+ T extends { title?: string; path?: string; children?: unknown[] },
76
+ >(
77
+ navigation: readonly T[],
78
+ ): T[] => {
79
+ return navigation.map((node, index) => {
80
+ if (!node.children || node.path) {
81
+ return { ...node };
82
+ }
83
+ const slug =
84
+ (node.title ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-") ||
85
+ String(index);
86
+ return { ...node, path: `search-group-${slug}` };
87
+ });
88
+ };
@@ -0,0 +1,35 @@
1
+ import { defineNuxtModule, useNuxt } from "@nuxt/kit";
2
+ import type { Component } from "nuxt/schema";
3
+
4
+ const MDC_INJECTED_COMPONENTS = new Set(["UBadge", "UAlert"]);
5
+
6
+ /**
7
+ * Marks the MDC components that api-document.ts injects into rendered markdown
8
+ * at runtime as global.
9
+ *
10
+ * api-document.ts injects these into rendered markdown at runtime, so Nuxt
11
+ * Content never sees them in the content manifest; production builds only
12
+ * bundle manifest or global components, leaving raw <UBadge>/<UAlert>.
13
+ */
14
+ export const markInjectedComponentsGlobal = (components: Component[]): void => {
15
+ for (const component of components) {
16
+ if (MDC_INJECTED_COMPONENTS.has(component.pascalName)) {
17
+ component.global = true;
18
+ }
19
+ }
20
+ };
21
+
22
+ export { MDC_INJECTED_COMPONENTS };
23
+
24
+ export default defineNuxtModule({
25
+ meta: {
26
+ name: "gqlmd-mdc-components",
27
+ },
28
+ setup() {
29
+ const nuxt = useNuxt();
30
+
31
+ nuxt.hook("components:extend", (components: Component[]) => {
32
+ markInjectedComponentsGlobal(components);
33
+ });
34
+ },
35
+ });
package/nuxt.config.ts CHANGED
@@ -7,6 +7,7 @@ const layerDir = fileURLToPath(new URL(".", import.meta.url));
7
7
  export default defineNuxtConfig({
8
8
  modules: [
9
9
  join(layerDir, "modules/generate"),
10
+ join(layerDir, "modules/mdc-components"),
10
11
  "@nuxt/content",
11
12
  "@nuxt/ui",
12
13
  join(layerDir, "modules/prerender"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-markdown/nuxt-theme",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A Nuxt layer for building GraphQL API reference sites",
5
5
  "homepage": "https://graphql-markdown.dev",
6
6
  "bugs": {
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@clack/prompts": "^1.0.0",
49
- "@graphql-markdown/cli": "^1.2.2",
49
+ "@graphql-markdown/cli": "^1.2.3",
50
50
  "@nuxt/content": "^3.16.2",
51
51
  "@nuxt/kit": "^4.6.4",
52
52
  "@nuxt/ui": "^4.11.0",