Skip to content

Commit 49f5da2

Browse files
Support both static and dynamic code snippets (#1081)
* Support both static and dynamic code snippets * Orginze imports
1 parent 74702be commit 49f5da2

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

apps/docs/schema.json

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@
663663
}
664664
},
665665
{
666-
"name": "code-block",
666+
"name": "live-code-block",
667667
"type": "type",
668668
"value": {
669669
"type": "object",
@@ -672,7 +672,38 @@
672672
"type": "objectAttribute",
673673
"value": {
674674
"type": "string",
675-
"value": "code-block"
675+
"value": "live-code-block"
676+
}
677+
},
678+
"code": {
679+
"type": "objectAttribute",
680+
"value": {
681+
"type": "inline",
682+
"name": "code"
683+
},
684+
"optional": true
685+
},
686+
"caption": {
687+
"type": "objectAttribute",
688+
"value": {
689+
"type": "string"
690+
},
691+
"optional": true
692+
}
693+
}
694+
}
695+
},
696+
{
697+
"name": "static-code-block",
698+
"type": "type",
699+
"value": {
700+
"type": "object",
701+
"attributes": {
702+
"_type": {
703+
"type": "objectAttribute",
704+
"value": {
705+
"type": "string",
706+
"value": "static-code-block"
676707
}
677708
},
678709
"code": {
@@ -877,7 +908,22 @@
877908
},
878909
"rest": {
879910
"type": "inline",
880-
"name": "code-block"
911+
"name": "live-code-block"
912+
}
913+
},
914+
{
915+
"type": "object",
916+
"attributes": {
917+
"_key": {
918+
"type": "objectAttribute",
919+
"value": {
920+
"type": "string"
921+
}
922+
}
923+
},
924+
"rest": {
925+
"type": "inline",
926+
"name": "static-code-block"
881927
}
882928
}
883929
]

apps/docs/studio/objects/content.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const content = defineType({
1616
{ title: 'Quote', value: 'blockquote' },
1717
],
1818
},
19-
{ type: 'code-block' },
19+
{ type: 'live-code-block' },
20+
{ type: 'static-code-block' },
2021
],
2122
});
2223

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { defineField, defineType } from 'sanity';
2+
3+
const codeBlock = defineType({
4+
name: 'live-code-block',
5+
title: 'Live Code Block',
6+
description:
7+
"For editable code snippets that also can control rendering. Use 'Static Code Block' for static, non editable code snippets.",
8+
type: 'object',
9+
fields: [
10+
defineField({
11+
name: 'code',
12+
title: 'Code',
13+
type: 'code',
14+
options: {
15+
language: 'tsx',
16+
languageAlternatives: [{ title: 'TypeScript / React', value: 'tsx' }],
17+
},
18+
validation: (rule) => rule.required(),
19+
}),
20+
defineField({
21+
name: 'caption',
22+
title: 'Caption',
23+
description: 'A description or summary of the code',
24+
type: 'string',
25+
}),
26+
],
27+
preview: {
28+
select: {
29+
title: 'caption',
30+
},
31+
},
32+
});
33+
34+
export { codeBlock as default };

apps/docs/studio/objects/code-block.ts renamed to apps/docs/studio/objects/static-code-block.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { defineField, defineType } from 'sanity';
22

33
const codeBlock = defineType({
4-
name: 'code-block',
5-
title: 'Code Block',
4+
name: 'static-code-block',
5+
title: 'Static Code Block',
66
description:
77
"Great for simple code snippets that doesn't vary based on the language selected",
88
type: 'object',
@@ -18,6 +18,7 @@ const codeBlock = defineType({
1818
{ title: 'Bash', value: 'bash' },
1919
],
2020
},
21+
validation: (rule) => rule.required(),
2122
}),
2223
defineField({
2324
name: 'caption',

apps/docs/studio/schema-types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import component from './documents/component';
2-
import codeBlock from './objects/code-block';
32
import content from './objects/content';
3+
import liveCodeBlock from './objects/live-code-block';
4+
import staticCodeBlock from './objects/static-code-block';
45

56
// Export an array of all the schema types. This is used in the Sanity Studio configuration. https://www.sanity.io/docs/schema-types
6-
export const schemaTypes = [component, content, codeBlock];
7+
export const schemaTypes = [component, content, staticCodeBlock, liveCodeBlock];

0 commit comments

Comments
 (0)