@vx-oss/docs-sanity 1.0.3
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/dist/client.d.mts +10 -0
- package/dist/client.mjs +14 -0
- package/dist/index.d.mts +53 -0
- package/dist/index.mjs +122 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Fuma
|
|
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.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { PortableTextBlock } from "@portabletext/react";
|
|
3
|
+
import { TOCItemType } from "@vx-oss/docs-core/toc";
|
|
4
|
+
//#region src/client.d.ts
|
|
5
|
+
declare function renderToc(opts: {
|
|
6
|
+
toc: PortableTextBlock[];
|
|
7
|
+
render: (body: PortableTextBlock) => ReactNode;
|
|
8
|
+
}): TOCItemType[];
|
|
9
|
+
//#endregion
|
|
10
|
+
export { renderToc };
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/client.ts
|
|
2
|
+
function renderToc(opts) {
|
|
3
|
+
const { render, toc } = opts;
|
|
4
|
+
return toc.map((item) => ({
|
|
5
|
+
depth: Number(item.level ?? 0),
|
|
6
|
+
title: render({
|
|
7
|
+
...item,
|
|
8
|
+
style: void 0
|
|
9
|
+
}),
|
|
10
|
+
url: `#${item._key}`
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { renderToc };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { PortableTextBlock } from "@portabletext/react";
|
|
3
|
+
import { TOCItemType } from "@vx-oss/docs-core/toc";
|
|
4
|
+
import { StructuredData } from "@vx-oss/docs-core/mdx-plugins/remark-structure";
|
|
5
|
+
import { DynamicSource, MetaData } from "@vx-oss/docs-core/source";
|
|
6
|
+
import { DefinedFetchType } from "next-sanity/live";
|
|
7
|
+
import { SlugValue } from "@sanity/types";
|
|
8
|
+
import { SanityClient } from "@sanity/client";
|
|
9
|
+
//#region src/index.d.ts
|
|
10
|
+
type SanityOptions<Doc extends BaseDoc = BaseDoc> = GenericSanityOptions<Doc> | NextSanityOptions<Doc>;
|
|
11
|
+
interface BaseSanityOptions<Doc extends BaseDoc> {
|
|
12
|
+
/** document name for docs pages */
|
|
13
|
+
docType: string;
|
|
14
|
+
/** generate [virtual file path](https://fumadocs.dev/docs/headless/source-api/source#static-source) from document */
|
|
15
|
+
generatePath?: (doc: ShallowDoc<Doc>) => string;
|
|
16
|
+
/** base directory for the virutal file paths */
|
|
17
|
+
baseDir?: string;
|
|
18
|
+
}
|
|
19
|
+
interface GenericSanityOptions<Doc extends BaseDoc = BaseDoc> extends BaseSanityOptions<Doc> {
|
|
20
|
+
client: SanityClient;
|
|
21
|
+
}
|
|
22
|
+
interface NextSanityOptions<Doc extends BaseDoc = BaseDoc> extends BaseSanityOptions<Doc> {
|
|
23
|
+
client?: SanityClient;
|
|
24
|
+
/** the `sanityFetch` from `next-sanity/live` */
|
|
25
|
+
sanityFetch: DefinedFetchType;
|
|
26
|
+
}
|
|
27
|
+
/** your page document must align with this type */
|
|
28
|
+
interface BaseDoc {
|
|
29
|
+
_id: string;
|
|
30
|
+
_type: string;
|
|
31
|
+
_updatedAt?: string;
|
|
32
|
+
title?: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
slug?: SlugValue;
|
|
35
|
+
}
|
|
36
|
+
type ShallowDoc<Doc extends BaseDoc> = Pick<Doc, '_id' | '_type' | '_updatedAt' | 'title' | 'slug' | 'description'>;
|
|
37
|
+
type DocToPage<Doc extends BaseDoc> = ShallowDoc<Doc> & {
|
|
38
|
+
title: string;
|
|
39
|
+
load: () => Promise<DocToPageLoaded<Doc>>;
|
|
40
|
+
structuredData: () => Promise<StructuredData>;
|
|
41
|
+
};
|
|
42
|
+
type DocToPageLoaded<Doc extends BaseDoc> = Doc & {
|
|
43
|
+
_toc: PortableTextBlock[];
|
|
44
|
+
renderToc: (opts: {
|
|
45
|
+
render: (body: PortableTextBlock) => ReactNode;
|
|
46
|
+
}) => TOCItemType[];
|
|
47
|
+
};
|
|
48
|
+
declare function createSanitySource<Doc extends BaseDoc>(options: SanityOptions<Doc>): DynamicSource<{
|
|
49
|
+
pageData: DocToPage<Doc>;
|
|
50
|
+
metaData: MetaData;
|
|
51
|
+
}>;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { BaseDoc, DocToPage, DocToPageLoaded, GenericSanityOptions, NextSanityOptions, SanityOptions, createSanitySource };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { renderToc } from "./client.mjs";
|
|
2
|
+
import { cache } from "react";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
function createSanitySource(options) {
|
|
6
|
+
const { docType, baseDir, generatePath } = options;
|
|
7
|
+
let sanityFetch;
|
|
8
|
+
const useNextCache = "sanityFetch" in options;
|
|
9
|
+
let fileCache = /* @__PURE__ */ new Map();
|
|
10
|
+
if ("sanityFetch" in options) {
|
|
11
|
+
const fn = options.sanityFetch;
|
|
12
|
+
sanityFetch = async (query, params) => {
|
|
13
|
+
return (await fn({
|
|
14
|
+
query,
|
|
15
|
+
params
|
|
16
|
+
})).data;
|
|
17
|
+
};
|
|
18
|
+
} else {
|
|
19
|
+
const client = options.client;
|
|
20
|
+
sanityFetch = client.fetch.bind(client);
|
|
21
|
+
}
|
|
22
|
+
function toVirtualFile(file) {
|
|
23
|
+
const slugs = file.slug?.current?.split("/").filter((v) => v.length > 0) ?? [];
|
|
24
|
+
let filePath = generatePath ? generatePath(file) : slugs.length === 0 ? "index.mdx" : `${slugs.join("/")}.mdx`;
|
|
25
|
+
if (baseDir) filePath = path.join(baseDir, filePath);
|
|
26
|
+
return {
|
|
27
|
+
type: "page",
|
|
28
|
+
data: {
|
|
29
|
+
...file,
|
|
30
|
+
title: file.title ?? file._id,
|
|
31
|
+
load: cache(async () => {
|
|
32
|
+
const data = await sanityFetch(`*[_type == $docType && _id == $id][0]{
|
|
33
|
+
...,
|
|
34
|
+
"_toc": body[style in ["h1", "h2", "h3", "h4", "h5", "h6"]]
|
|
35
|
+
}`, {
|
|
36
|
+
id: file._id,
|
|
37
|
+
docType
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
...data,
|
|
41
|
+
_toc: data._toc ?? [],
|
|
42
|
+
renderToc(opts) {
|
|
43
|
+
if (!data._toc) return [];
|
|
44
|
+
return renderToc({
|
|
45
|
+
toc: data._toc,
|
|
46
|
+
...opts
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}),
|
|
51
|
+
structuredData: cache(async () => {
|
|
52
|
+
return getStructuredData((await sanityFetch(`*[_type == $docType && _id == $id][0]{
|
|
53
|
+
"structuredBody": body[]{
|
|
54
|
+
...,
|
|
55
|
+
_type == "block" => {
|
|
56
|
+
"heading": select(
|
|
57
|
+
style == "h1" => "h1",
|
|
58
|
+
style == "h2" => "h2",
|
|
59
|
+
style == "h3" => "h3",
|
|
60
|
+
style == "h4" => "h4",
|
|
61
|
+
style == "h5" => "h5",
|
|
62
|
+
style == "h6" => "h6",
|
|
63
|
+
null
|
|
64
|
+
),
|
|
65
|
+
"content": pt::text(@)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}`, {
|
|
69
|
+
docType,
|
|
70
|
+
id: file._id
|
|
71
|
+
}))?.structuredBody ?? []);
|
|
72
|
+
})
|
|
73
|
+
},
|
|
74
|
+
slugs,
|
|
75
|
+
path: filePath
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
cache: useNextCache ? "custom" : "memory",
|
|
80
|
+
async files() {
|
|
81
|
+
const data = await sanityFetch(`*[_type == $docType]{ _id, _type, _updatedAt, title, slug, description }`, { docType });
|
|
82
|
+
const next = /* @__PURE__ */ new Map();
|
|
83
|
+
const files = data.map((doc) => {
|
|
84
|
+
const key = `${doc._id}:${doc._updatedAt ?? JSON.stringify(doc)}`;
|
|
85
|
+
const file = fileCache.get(key) ?? toVirtualFile(doc);
|
|
86
|
+
next.set(key, file);
|
|
87
|
+
return file;
|
|
88
|
+
});
|
|
89
|
+
fileCache = next;
|
|
90
|
+
return files;
|
|
91
|
+
},
|
|
92
|
+
invalidate() {
|
|
93
|
+
fileCache.clear();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function getStructuredData(blocks = []) {
|
|
98
|
+
const structuredData = {
|
|
99
|
+
headings: [],
|
|
100
|
+
contents: []
|
|
101
|
+
};
|
|
102
|
+
let lastHeading;
|
|
103
|
+
for (const block of blocks) {
|
|
104
|
+
const content = block.content?.trim();
|
|
105
|
+
if (!content) continue;
|
|
106
|
+
if (block.heading && block._key) {
|
|
107
|
+
structuredData.headings.push({
|
|
108
|
+
id: block._key,
|
|
109
|
+
content
|
|
110
|
+
});
|
|
111
|
+
lastHeading = block._key;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
structuredData.contents.push({
|
|
115
|
+
heading: lastHeading,
|
|
116
|
+
content
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return structuredData;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { createSanitySource };
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vx-oss/docs-sanity",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "The official Sanity integration for Fumadocs",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Docs",
|
|
7
|
+
"Fumadocs"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://vezham.com",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "Fuma Nama",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/vezham/fumadocs",
|
|
15
|
+
"directory": "packages/sanity"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.mjs",
|
|
23
|
+
"./client": "./dist/client.mjs",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@portabletext/react": "^8.0.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@sanity/client": "^8.3.0",
|
|
34
|
+
"@sanity/types": "^6.11.0",
|
|
35
|
+
"@types/node": "26.3.0",
|
|
36
|
+
"@types/react": "^19.2.18",
|
|
37
|
+
"fuma-cli": "^0.1.1",
|
|
38
|
+
"next-sanity": "^13.3.3",
|
|
39
|
+
"react": "^19.2.8",
|
|
40
|
+
"sanity": "^6.11.0",
|
|
41
|
+
"tsdown": "0.22.14",
|
|
42
|
+
"typescript": "^6.0.3",
|
|
43
|
+
"vitest": "^4.1.11",
|
|
44
|
+
"@vx-oss/docs-core": "1.0.3",
|
|
45
|
+
"tsconfig": "0.0.1",
|
|
46
|
+
"@vx-oss/docs-react": "1.0.3"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@sanity/client": "^7.22.0 || ^8.0.0",
|
|
50
|
+
"@sanity/types": "*",
|
|
51
|
+
"@types/react": "*",
|
|
52
|
+
"next-sanity": "^12.4.0 || ^13.0.0",
|
|
53
|
+
"react": "^19.2.0",
|
|
54
|
+
"react-dom": "^19.2.0",
|
|
55
|
+
"@vx-oss/docs-core": "^1.0.1"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"@sanity/types": {
|
|
59
|
+
"optional": true
|
|
60
|
+
},
|
|
61
|
+
"@sanity/client": {
|
|
62
|
+
"optional": true
|
|
63
|
+
},
|
|
64
|
+
"next-sanity": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"@types/react": {
|
|
68
|
+
"optional": true
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsdown",
|
|
73
|
+
"clean": "rimraf dist",
|
|
74
|
+
"dev": "tsdown --watch --clean false",
|
|
75
|
+
"lint": "oxlint .",
|
|
76
|
+
"types:check": "tsc --noEmit",
|
|
77
|
+
"test": "vitest run"
|
|
78
|
+
}
|
|
79
|
+
}
|