Skip to content

Commit 57f4cce

Browse files
committed
feat: add rich table support
1 parent 1b3413d commit 57f4cce

6 files changed

Lines changed: 163 additions & 0 deletions

File tree

astro/src/components/PortableText.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
22
import {PortableText as PortableTextInternal} from 'astro-portabletext'
33
import PortableTextImage from './PortableTextImage.astro'
4+
import Table from './Table.astro'
45
56
const {portableText} = Astro.props
67
78
const components = {
89
type: {
910
image: PortableTextImage,
11+
richTableBlock: Table,
1012
},
1113
}
1214
---

astro/src/components/Table.astro

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
import type {RichTableType} from 'sanity-plugin-rich-table'
3+
import PortableText from './PortableText.astro'
4+
5+
// Registered as a `richTableBlock` type component on PortableText.astro,
6+
// so the table data arrives on `Astro.props.node`.
7+
const {rows, columnHeaders, hasColumnTitles, hasRowTitles} = Astro.props
8+
.node as RichTableType
9+
---
10+
11+
<table>
12+
{
13+
hasColumnTitles && (
14+
<thead>
15+
<tr>
16+
{hasRowTitles && <th />}
17+
{columnHeaders?.map((header) => <th>{header.title}</th>)}
18+
</tr>
19+
</thead>
20+
)
21+
}
22+
<tbody>
23+
{
24+
(rows ?? []).map((row) => (
25+
<tr>
26+
{hasRowTitles && <th scope="row">{row.title}</th>}
27+
{row.cells?.map((cell) => (
28+
<td>
29+
<PortableText portableText={cell.content} />
30+
</td>
31+
))}
32+
</tr>
33+
))
34+
}
35+
</tbody>
36+
</table>

pnpm-lock.yaml

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

studio/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"rxjs": "^7.8.2",
3434
"sanity": "^5.30.0",
3535
"sanity-plugin-bulk-delete": "^1.1.1",
36+
"sanity-plugin-rich-table": "^1.1.1",
3637
"sanity-plugin-taxonomy-manager": "^4.7.0",
3738
"styled-components": "^6.3.11"
3839
},

studio/sanity.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {RiBubbleChartFill} from 'react-icons/ri'
1414
import {NodeTree} from './static/NodeTree'
1515
import {presentationTool} from 'sanity/presentation'
1616
import {resolve} from './resolve'
17+
import {richTablePlugin} from 'sanity-plugin-rich-table'
1718

1819
const hiddenDocTypes = (listItem: any) =>
1920
![
@@ -101,6 +102,7 @@ export default defineConfig([
101102
embeddingsIndexDashboard(),
102103
embeddingsIndexReferenceInput(),
103104
assist(),
105+
richTablePlugin({}),
104106
],
105107

106108
schema: {
@@ -136,6 +138,7 @@ export default defineConfig([
136138
}),
137139
embeddingsIndexDashboard(),
138140
assist(),
141+
richTablePlugin({}),
139142
],
140143

141144
schema: {
@@ -181,6 +184,7 @@ export default defineConfig([
181184
// roles: ['administrator', 'editor'], // Optionally restrict to specific roles
182185
}),
183186
assist(),
187+
richTablePlugin({}),
184188
],
185189

186190
schema: {

studio/schemaTypes/objects/bodyPortableText.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@ export default defineType({
4545
defineArrayMember({
4646
type: 'bodyImage',
4747
}),
48+
defineArrayMember({
49+
name: 'richTableBlock',
50+
title: 'Rich Table Block',
51+
type: 'richTableBlock', // Use the rich table block type
52+
}),
4853
],
4954
})

0 commit comments

Comments
 (0)