datocms-structured-text-utils
Version:
A set of Typescript types and helpers to work with DatoCMS Structured Text fields.
97 lines (88 loc) • 2.79 kB
text/typescript
import { DefaultMark, NodeType } from './types';
export const blockquoteNodeType = 'blockquote' as const;
export const blockNodeType = 'block' as const;
export const inlineBlockNodeType = 'inlineBlock' as const;
export const codeNodeType = 'code' as const;
export const headingNodeType = 'heading' as const;
export const inlineItemNodeType = 'inlineItem' as const;
export const itemLinkNodeType = 'itemLink' as const;
export const linkNodeType = 'link' as const;
export const listItemNodeType = 'listItem' as const;
export const listNodeType = 'list' as const;
export const paragraphNodeType = 'paragraph' as const;
export const rootNodeType = 'root' as const;
export const spanNodeType = 'span' as const;
export const thematicBreakNodeType = 'thematicBreak' as const;
export const allowedNodeTypes = [
blockquoteNodeType,
blockNodeType,
inlineBlockNodeType,
codeNodeType,
headingNodeType,
inlineItemNodeType,
itemLinkNodeType,
linkNodeType,
listItemNodeType,
listNodeType,
paragraphNodeType,
rootNodeType,
spanNodeType,
thematicBreakNodeType,
];
export type AllowedChildren = Record<NodeType, 'inlineNodes' | NodeType[]>;
export const allowedChildren: AllowedChildren = {
[ ]: [paragraphNodeType],
[ ]: [],
[ ]: [],
[ ]: [],
[ ]: 'inlineNodes',
[ ]: [],
[ ]: 'inlineNodes',
[ ]: 'inlineNodes',
[ ]: [paragraphNodeType, listNodeType],
[ ]: [listItemNodeType],
[ ]: 'inlineNodes',
[ ]: [
blockquoteNodeType,
codeNodeType,
listNodeType,
paragraphNodeType,
headingNodeType,
blockNodeType,
thematicBreakNodeType,
],
[ ]: [],
[ ]: [],
};
export const inlineNodeTypes = [
spanNodeType,
linkNodeType,
itemLinkNodeType,
inlineItemNodeType,
inlineBlockNodeType,
];
export type AllowedAttributes = Record<NodeType, string[]>;
export const allowedAttributes: AllowedAttributes = {
[ ]: ['children', 'attribution'],
[ ]: ['item'],
[ ]: ['item'],
[ ]: ['language', 'highlight', 'code'],
[ ]: ['level', 'children', 'style'],
[ ]: ['item'],
[ ]: ['item', 'children', 'meta'],
[ ]: ['url', 'children', 'meta'],
[ ]: ['children'],
[ ]: ['style', 'children'],
[ ]: ['children', 'style'],
[ ]: ['children'],
[ ]: ['value', 'marks'],
[ ]: [],
};
export const defaultMarks: DefaultMark[] = [
'strong',
'code',
'emphasis',
'underline',
'strikethrough',
'highlight',
];