Skip to content

Instantly share code, notes, and snippets.

@mitchuman
Last active April 16, 2024 00:02
Show Gist options
  • Save mitchuman/95842862783c3b0cea9160a8c557acef to your computer and use it in GitHub Desktop.
Save mitchuman/95842862783c3b0cea9160a8c557acef to your computer and use it in GitHub Desktop.
Sanity utils
import type {
Divider,
ListItem,
ListItemBuilder,
StructureBuilder,
View,
ViewBuilder,
} from "sanity/structure";
export const singleton = (
S: StructureBuilder,
title: string,
id: string,
views: Array<View | ViewBuilder> = []
): ListItemBuilder =>
S.listItem()
.title(title)
.id(id)
.child(
S.document()
.schemaType(id)
.documentId(id)
.views([S.view.form(), ...views])
);
export const group = (
S: StructureBuilder,
title: string,
items: (ListItemBuilder | ListItem | Divider)[]
): ListItemBuilder =>
S.listItem().title(title).child(S.list().title(title).items(items));
/**
* Return the text of a block type as a single string. Use in schema previews.
*/
export function getBlockText(
block?: {
children?: {
text: string;
}[];
}[],
lineBreakChar: string = "↵ "
) {
return (
block?.reduce((a, c, i) => {
const text = c.children?.flatMap((c) => c.text).join("") || "";
return a + text + (i !== block.length - 1 ? lineBreakChar : "");
}, "") || ""
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment