datocms-structured-text-utils
Version:
A set of Typescript types and helpers to work with DatoCMS Structured Text fields.
89 lines • 2.88 kB
JavaScript
import { allowedNodeTypes, blockNodeType, blockquoteNodeType, codeNodeType, headingNodeType, inlineBlockNodeType, inlineItemNodeType, inlineNodeTypes, itemLinkNodeType, linkNodeType, listItemNodeType, listNodeType, paragraphNodeType, rootNodeType, spanNodeType, thematicBreakNodeType, } from './definitions';
export function hasChildren(node) {
return 'children' in node;
}
export function isInlineNode(node) {
return inlineNodeTypes.includes(node.type);
}
export function isHeading(node) {
return node.type === headingNodeType;
}
export function isSpan(node) {
return node.type === spanNodeType;
}
export function isRoot(node) {
return node.type === rootNodeType;
}
export function isParagraph(node) {
return node.type === paragraphNodeType;
}
export function isList(node) {
return node.type === listNodeType;
}
export function isListItem(node) {
return node.type === listItemNodeType;
}
export function isBlockquote(node) {
return node.type === blockquoteNodeType;
}
export function isBlock(node) {
return node.type === blockNodeType;
}
export function isInlineBlock(node) {
return node.type === inlineBlockNodeType;
}
export function isCode(node) {
return node.type === codeNodeType;
}
export function isLink(node) {
return node.type === linkNodeType;
}
export function isItemLink(node) {
return node.type === itemLinkNodeType;
}
export function isInlineItem(node) {
return node.type === inlineItemNodeType;
}
export function isThematicBreak(node) {
return node.type === thematicBreakNodeType;
}
function isObject(obj) {
return Boolean(typeof obj === 'object' && obj);
}
export function isNodeType(value) {
return allowedNodeTypes.includes(value);
}
export function isNode(obj) {
return Boolean(isObject(obj) &&
'type' in obj &&
typeof obj.type === 'string' &&
isNodeType(obj.type));
}
export function isStructuredText(obj) {
return Boolean(isObject(obj) && 'value' in obj && isDocument(obj.value));
}
export function isDocument(obj) {
return Boolean(isObject(obj) &&
'schema' in obj &&
'document' in obj &&
obj.schema === 'dast');
}
export function isEmptyDocument(obj) {
if (!obj) {
return true;
}
var document = isStructuredText(obj) && isDocument(obj.value)
? obj.value
: isDocument(obj)
? obj
: null;
if (!document) {
throw new Error('Passed object is neither null, a Structured Text value or a DAST document');
}
return (document.document.children.length === 1 &&
document.document.children[0].type === 'paragraph' &&
document.document.children[0].children.length === 1 &&
document.document.children[0].children[0].type === 'span' &&
document.document.children[0].children[0].value === '');
}
//# sourceMappingURL=guards.js.map