Skip to content

Commit f5c89c6

Browse files
authored
add content with code input to sanity (#1077)
1 parent c5f7ac3 commit f5c89c6

File tree

11 files changed

+663
-22
lines changed

11 files changed

+663
-22
lines changed

apps/docs/app/routes/_docs/komponenter/$slug.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import * as badgeExamples from '@/examples/badge';
22
import * as buttonExamples from '@/examples/button';
33
import { sanityFetch } from '@/lib/sanity';
44
import { ComponentPreview } from '@/ui/component-preview';
5+
import { Content } from '@/ui/content';
56
import { PropsTable } from '@/ui/props-table';
67
import { createFileRoute, notFound } from '@tanstack/react-router';
78
import * as props from 'docgen';
89
import { defineQuery } from 'groq';
910

1011
const COMPONENT_QUERY = defineQuery(
11-
`*[_type == "component" && slug.current == $slug][0]{ "name": coalesce(name, '') }`,
12+
`*[_type == "component" && slug.current == $slug][0]{ content, "name": coalesce(name, '') }`,
1213
);
1314

1415
export const Route = createFileRoute('/_docs/komponenter/$slug')({
@@ -43,6 +44,8 @@ function Page() {
4344
<>
4445
<h1 className="heading-l mb-12 mt-9">{data.name}</h1>
4546

47+
<Content content={data.content ?? []} />
48+
4649
{examples.map(({ title, code }) => (
4750
<ComponentPreview scope={scope} key={title} title={title} code={code} />
4851
))}

apps/docs/app/ui/content.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { PortableText } from '@portabletext/react';
2+
import type { Content as IContent } from 'sanity.types';
3+
4+
const components = {};
5+
6+
export function Content({ content }: { content: IContent }) {
7+
return (
8+
<div className="prose">
9+
<PortableText value={content} components={components} />
10+
</div>
11+
);
12+
}

apps/docs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"@obosbbl/grunnmuren-icons-react": "workspace:*",
2121
"@obosbbl/grunnmuren-icons-svg": "workspace:*",
2222
"@obosbbl/grunnmuren-react": "workspace:*",
23+
"@portabletext/react": "3.2.0",
2324
"@sanity/client": "6.24.1",
25+
"@sanity/code-input": "5.1.2",
2426
"@sanity/vision": "3.68.3",
2527
"@tanstack/react-router": "1.95.1",
2628
"@tanstack/start": "1.95.1",

apps/docs/sanity.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { obosAuthStore } from '@code-obos/sanity-auth';
2+
import { codeInput } from '@sanity/code-input';
23
import { visionTool } from '@sanity/vision';
34
import { defineConfig } from 'sanity';
45
import { structureTool } from 'sanity/structure';
@@ -12,7 +13,7 @@ export default defineConfig({
1213
basePath: '/studio',
1314
title: 'Grunnmuren - Sanity Studio',
1415
auth: obosAuthStore({ dataset }),
15-
plugins: [structureTool(), visionTool()],
16+
plugins: [structureTool(), visionTool(), codeInput()],
1617
schema: {
1718
types: schemaTypes,
1819
},

apps/docs/sanity.types.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@ export type SanityAssetSourceData = {
125125
url?: string;
126126
};
127127

128+
export type CodeBlock = {
129+
_type: 'code-block';
130+
code?: Code;
131+
caption?: string;
132+
};
133+
134+
export type Content = Array<
135+
| {
136+
children?: Array<{
137+
marks?: Array<string>;
138+
text?: string;
139+
_type: 'span';
140+
_key: string;
141+
}>;
142+
style?: 'normal' | 'h2' | 'h3' | 'h4' | 'h5' | 'blockquote';
143+
listItem?: 'bullet' | 'number';
144+
markDefs?: Array<{
145+
href?: string;
146+
_type: 'link';
147+
_key: string;
148+
}>;
149+
level?: number;
150+
_type: 'block';
151+
_key: string;
152+
}
153+
| ({
154+
_key: string;
155+
} & CodeBlock)
156+
>;
157+
128158
export type Component = {
129159
_id: string;
130160
_type: 'component';
@@ -133,6 +163,7 @@ export type Component = {
133163
_rev: string;
134164
name?: string;
135165
slug?: Slug;
166+
content?: Content;
136167
};
137168

138169
export type Slug = {
@@ -141,6 +172,14 @@ export type Slug = {
141172
source?: string;
142173
};
143174

175+
export type Code = {
176+
_type: 'code';
177+
language?: string;
178+
filename?: string;
179+
code?: string;
180+
highlightedLines?: Array<number>;
181+
};
182+
144183
export type AllSanitySchemaTypes =
145184
| SanityImagePaletteSwatch
146185
| SanityImagePalette
@@ -152,8 +191,11 @@ export type AllSanitySchemaTypes =
152191
| SanityImageMetadata
153192
| Geopoint
154193
| SanityAssetSourceData
194+
| CodeBlock
195+
| Content
155196
| Component
156-
| Slug;
197+
| Slug
198+
| Code;
157199
export declare const internalGroqTypeReferenceTo: unique symbol;
158200
// Source: ./app/routes/_docs.tsx
159201
// Variable: COMPONENTS_NAVIGATION_QUERY
@@ -164,10 +206,11 @@ export type COMPONENTS_NAVIGATION_QUERYResult = Array<{
164206
slug: string | '';
165207
}>;
166208

167-
// Source: ./app/routes/_docs/komponenter.$slug.tsx
209+
// Source: ./app/routes/_docs/komponenter/$slug.tsx
168210
// Variable: COMPONENT_QUERY
169-
// Query: *[_type == "component" && slug.current == $slug][0]{ "name": coalesce(name, '') }
211+
// Query: *[_type == "component" && slug.current == $slug][0]{ content, "name": coalesce(name, '') }
170212
export type COMPONENT_QUERYResult = {
213+
content: Content | null;
171214
name: string | '';
172215
} | null;
173216

@@ -187,6 +230,6 @@ declare module '@sanity/client' {
187230
"*[_type == \"component\"]{ _id, name, 'slug': coalesce(slug.current, '')} | order(name asc)":
188231
| COMPONENTS_NAVIGATION_QUERYResult
189232
| COMPONENTS_INDEX_QUERYResult;
190-
'*[_type == "component" && slug.current == $slug][0]{ "name": coalesce(name, \'\') }': COMPONENT_QUERYResult;
233+
'*[_type == "component" && slug.current == $slug][0]{ content, "name": coalesce(name, \'\') }': COMPONENT_QUERYResult;
191234
}
192235
}

0 commit comments

Comments
 (0)