Skip to content

Commit 15ac23d

Browse files
author
Alex Patterson
committed
add html
1 parent fc61515 commit 15ac23d

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎components/html-embed.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function HTMLEmbed(props: any) {
2+
const { html } = props;
3+
if (!html) {
4+
return <div>Hello World!</div>;
5+
}
6+
return <div dangerouslySetInnerHTML={{ __html: html }} />
7+
}

‎components/portable-text.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import BlockCode from "@/components/block-code";
2020
import TwitterEmbed from "@/components/twitter-embed";
2121
import CodePenEmbed from "@/components/codepen-embed";
2222
import CodeSandboxEmbed from "./codesandbox-embed";
23+
import HTMLEmbed from "@/components/html-embed";
24+
2325

2426
export default function CustomPortableText({
2527
className,
@@ -36,6 +38,7 @@ export default function CustomPortableText({
3638
codepen: ({ value }) => <CodePenEmbed {...value} />,
3739
codesandbox: ({ value }) => <CodeSandboxEmbed {...value} />,
3840
twitter: ({ value }) => <TwitterEmbed {...value} />,
41+
htmlBlock: ({ value }) => <HTMLEmbed {...value} />,
3942
},
4043
block: {
4144
h5: ({ children }) => (

‎sanity/components/HTMLPreview.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
// TODO: Add stronger typing
3+
const HTMLPreview = (props: any) => {
4+
const { html } = props;
5+
if (!html) {
6+
return <div>Add some HTML</div>;
7+
}
8+
return <div dangerouslySetInnerHTML={{ __html: html }} />
9+
};
10+
11+
const preview = (props: any) => <HTMLPreview {...props} />;
12+
13+
export default preview;

‎sanity/schemas/custom/html.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineType } from "sanity";
2+
import { FaHtml5 } from "react-icons/fa6";
3+
import preview from "../../components/HTMLPreview";
4+
5+
export default defineType({
6+
name: "htmlBlock",
7+
type: "object",
8+
icon: FaHtml5,
9+
components: {
10+
preview,
11+
},
12+
preview: {
13+
select: {
14+
html: "html",
15+
},
16+
},
17+
fields: [
18+
{
19+
name: "html",
20+
type: "text",
21+
title: "Raw HTML",
22+
},
23+
],
24+
initialValue: {
25+
html: "<div>Hello World</div>",
26+
},
27+
});

‎sanity/schemas/partials/base.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import internalLink from "../custom/internalLink";
1212
// Custom schemas (need imported in sanity.config.ts)
1313
import codepen from "../custom/codepen";
1414
import codesandbox from "../custom/codesandbox";
15+
import html from "../custom/html";
1516
import twitter from "../custom/twitter-embed";
1617

1718
const baseType = defineType({
@@ -94,6 +95,7 @@ const baseType = defineType({
9495
defineArrayMember(codepen),
9596
defineArrayMember(codesandbox),
9697
defineArrayMember(twitter),
98+
defineArrayMember(html),
9799
],
98100
}),
99101
],

0 commit comments

Comments
 (0)