datocms-structured-text-utils
Advanced tools
Comparing version 4.0.0 to 4.0.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isEmptyDocument = exports.isDocument = exports.isStructuredText = exports.isThematicBreak = exports.isInlineItem = exports.isItemLink = exports.isLink = exports.isCode = exports.isBlock = exports.isBlockquote = exports.isListItem = exports.isList = exports.isParagraph = exports.isRoot = exports.isSpan = exports.isHeading = exports.isInlineNode = exports.hasChildren = void 0; | ||
exports.isEmptyDocument = exports.isDocument = exports.isStructuredText = exports.isNode = exports.isNodeType = exports.isThematicBreak = exports.isInlineItem = exports.isItemLink = exports.isLink = exports.isCode = exports.isBlock = exports.isBlockquote = exports.isListItem = exports.isList = exports.isParagraph = exports.isRoot = exports.isSpan = exports.isHeading = exports.isInlineNode = exports.hasChildren = void 0; | ||
var definitions_1 = require("./definitions"); | ||
@@ -65,21 +65,32 @@ function hasChildren(node) { | ||
exports.isThematicBreak = isThematicBreak; | ||
function isStructuredText( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj) { | ||
return obj && 'value' in obj && isDocument(obj.value); | ||
function isObject(obj) { | ||
return Boolean(typeof obj === 'object' && obj); | ||
} | ||
function isNodeType(value) { | ||
return definitions_1.allowedNodeTypes.includes(value); | ||
} | ||
exports.isNodeType = isNodeType; | ||
function isNode(obj) { | ||
return Boolean(isObject(obj) && | ||
'type' in obj && | ||
typeof obj.type === 'string' && | ||
isNodeType(obj.type)); | ||
} | ||
exports.isNode = isNode; | ||
function isStructuredText(obj) { | ||
return Boolean(isObject(obj) && 'value' in obj && isDocument(obj.value)); | ||
} | ||
exports.isStructuredText = isStructuredText; | ||
function isDocument( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj) { | ||
return obj && 'schema' in obj && 'document' in obj; | ||
function isDocument(obj) { | ||
return Boolean(isObject(obj) && | ||
'schema' in obj && | ||
'document' in obj && | ||
obj.schema === 'dast'); | ||
} | ||
exports.isDocument = isDocument; | ||
function isEmptyDocument( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj) { | ||
function isEmptyDocument(obj) { | ||
if (!obj) { | ||
return true; | ||
} | ||
var document = isStructuredText(obj) | ||
var document = isStructuredText(obj) && isDocument(obj.value) | ||
? obj.value | ||
@@ -92,4 +103,3 @@ : isDocument(obj) | ||
} | ||
return (document.schema === 'dast' && | ||
document.document.children.length === 1 && | ||
return (document.document.children.length === 1 && | ||
document.document.children[0].type === 'paragraph' && | ||
@@ -96,0 +106,0 @@ document.document.children[0].children.length === 1 && |
@@ -58,5 +58,3 @@ "use strict"; | ||
} | ||
else { | ||
throw new RenderError("Don't know how to render a node with type \"" + node.type + "\". Please specify a custom renderRule for it!", node); | ||
} | ||
throw new RenderError("Don't know how to render a node with type \"" + node.type + "\". Please specify a custom renderRule for it!", node); | ||
} | ||
@@ -68,7 +66,14 @@ exports.transformNode = transformNode; | ||
} | ||
var result = transformNode(adapter, guards_1.isStructuredText(structuredTextOrNode) | ||
var node = guards_1.isStructuredText(structuredTextOrNode) && | ||
guards_1.isDocument(structuredTextOrNode.value) | ||
? structuredTextOrNode.value.document | ||
: guards_1.isDocument(structuredTextOrNode) | ||
? structuredTextOrNode.document | ||
: structuredTextOrNode, 't-0', [], renderRules); | ||
: guards_1.isNode(structuredTextOrNode) | ||
? structuredTextOrNode | ||
: undefined; | ||
if (!node) { | ||
throw new Error('Passed object is neither null, a Structured Text value, a DAST document or a DAST node'); | ||
} | ||
var result = transformNode(adapter, node, 't-0', [], renderRules); | ||
return result; | ||
@@ -75,0 +80,0 @@ } |
@@ -18,5 +18,5 @@ import { DefaultMark, NodeType } from './types'; | ||
export declare const allowedChildren: AllowedChildren; | ||
export declare const inlineNodeTypes: ("link" | "span" | "itemLink" | "inlineItem")[]; | ||
export declare const inlineNodeTypes: ("span" | "link" | "itemLink" | "inlineItem")[]; | ||
export declare type AllowedAttributes = Record<NodeType, string[]>; | ||
export declare const allowedAttributes: AllowedAttributes; | ||
export declare const defaultMarks: DefaultMark[]; |
@@ -1,2 +0,2 @@ | ||
import { Root, List, Blockquote, Block, Link, ItemLink, InlineItem, Code, ListItem, Paragraph, Heading, Node, Span, WithChildrenNode, InlineNode, Record, StructuredText, ThematicBreak, Document } from './types'; | ||
import { Root, List, Blockquote, Block, Link, ItemLink, InlineItem, Code, ListItem, Paragraph, Heading, Node, Span, WithChildrenNode, InlineNode, NodeType, Record as DatoCmsRecord, StructuredText, ThematicBreak, Document } from './types'; | ||
export declare function hasChildren(node: Node): node is WithChildrenNode; | ||
@@ -17,4 +17,6 @@ export declare function isInlineNode(node: Node): node is InlineNode; | ||
export declare function isThematicBreak(node: Node): node is ThematicBreak; | ||
export declare function isStructuredText<R1 extends Record, R2 extends Record = R1>(obj: any): obj is StructuredText<R1, R2>; | ||
export declare function isDocument(obj: any): obj is Document; | ||
export declare function isEmptyDocument(obj: any): boolean; | ||
export declare function isNodeType(value: string): value is NodeType; | ||
export declare function isNode(obj: unknown): obj is Node; | ||
export declare function isStructuredText<R1 extends DatoCmsRecord, R2 extends DatoCmsRecord = R1>(obj: unknown): obj is StructuredText<R1, R2>; | ||
export declare function isDocument(obj: unknown): obj is Document; | ||
export declare function isEmptyDocument(obj: unknown): boolean; |
@@ -1,2 +0,2 @@ | ||
import { headingNodeType, spanNodeType, rootNodeType, paragraphNodeType, listNodeType, listItemNodeType, blockquoteNodeType, blockNodeType, codeNodeType, linkNodeType, itemLinkNodeType, inlineItemNodeType, inlineNodeTypes, thematicBreakNodeType, } from './definitions'; | ||
import { allowedNodeTypes, headingNodeType, spanNodeType, rootNodeType, paragraphNodeType, listNodeType, listItemNodeType, blockquoteNodeType, blockNodeType, codeNodeType, linkNodeType, itemLinkNodeType, inlineItemNodeType, inlineNodeTypes, thematicBreakNodeType, } from './definitions'; | ||
export function hasChildren(node) { | ||
@@ -47,19 +47,28 @@ return 'children' in node; | ||
} | ||
export function isStructuredText( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj) { | ||
return obj && 'value' in obj && isDocument(obj.value); | ||
function isObject(obj) { | ||
return Boolean(typeof obj === 'object' && obj); | ||
} | ||
export function isDocument( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj) { | ||
return obj && 'schema' in obj && 'document' in obj; | ||
export function isNodeType(value) { | ||
return allowedNodeTypes.includes(value); | ||
} | ||
export function isEmptyDocument( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj) { | ||
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) | ||
var document = isStructuredText(obj) && isDocument(obj.value) | ||
? obj.value | ||
@@ -72,4 +81,3 @@ : isDocument(obj) | ||
} | ||
return (document.schema === 'dast' && | ||
document.document.children.length === 1 && | ||
return (document.document.children.length === 1 && | ||
document.document.children[0].type === 'paragraph' && | ||
@@ -76,0 +84,0 @@ document.document.children[0].children.length === 1 && |
@@ -21,3 +21,3 @@ var __extends = (this && this.__extends) || (function () { | ||
}; | ||
import { hasChildren, isDocument, isStructuredText } from './guards'; | ||
import { hasChildren, isDocument, isNode, isStructuredText } from './guards'; | ||
import { flatten } from 'array-flatten'; | ||
@@ -55,5 +55,3 @@ var RenderError = /** @class */ (function (_super) { | ||
} | ||
else { | ||
throw new RenderError("Don't know how to render a node with type \"" + node.type + "\". Please specify a custom renderRule for it!", node); | ||
} | ||
throw new RenderError("Don't know how to render a node with type \"" + node.type + "\". Please specify a custom renderRule for it!", node); | ||
} | ||
@@ -64,9 +62,16 @@ export function render(adapter, structuredTextOrNode, renderRules) { | ||
} | ||
var result = transformNode(adapter, isStructuredText(structuredTextOrNode) | ||
var node = isStructuredText(structuredTextOrNode) && | ||
isDocument(structuredTextOrNode.value) | ||
? structuredTextOrNode.value.document | ||
: isDocument(structuredTextOrNode) | ||
? structuredTextOrNode.document | ||
: structuredTextOrNode, 't-0', [], renderRules); | ||
: isNode(structuredTextOrNode) | ||
? structuredTextOrNode | ||
: undefined; | ||
if (!node) { | ||
throw new Error('Passed object is neither null, a Structured Text value, a DAST document or a DAST node'); | ||
} | ||
var result = transformNode(adapter, node, 't-0', [], renderRules); | ||
return result; | ||
} | ||
//# sourceMappingURL=render.js.map |
@@ -373,3 +373,19 @@ export declare type Node = BlockNode | InlineNode; | ||
export declare type StructuredText<R1 extends Record = Record, R2 extends Record = R1> = { | ||
/** A DatoCMS compatible document */ | ||
/** | ||
* A DatoCMS "dast" document | ||
* | ||
* https://www.datocms.com/docs/structured-text/dast | ||
*/ | ||
value: Document | unknown; | ||
/** Blocks associated with the Structured Text */ | ||
blocks?: R1[]; | ||
/** Links associated with the Structured Text */ | ||
links?: R2[]; | ||
}; | ||
export declare type TypesafeStructuredText<R1 extends Record = Record, R2 extends Record = R1> = { | ||
/** | ||
* A DatoCMS "dast" document | ||
* | ||
* https://www.datocms.com/docs/structured-text/dast | ||
*/ | ||
value: Document; | ||
@@ -376,0 +392,0 @@ /** Blocks associated with the Structured Text */ |
@@ -18,5 +18,5 @@ import { DefaultMark, NodeType } from './types'; | ||
export declare const allowedChildren: AllowedChildren; | ||
export declare const inlineNodeTypes: ("link" | "span" | "itemLink" | "inlineItem")[]; | ||
export declare const inlineNodeTypes: ("span" | "link" | "itemLink" | "inlineItem")[]; | ||
export declare type AllowedAttributes = Record<NodeType, string[]>; | ||
export declare const allowedAttributes: AllowedAttributes; | ||
export declare const defaultMarks: DefaultMark[]; |
@@ -1,2 +0,2 @@ | ||
import { Root, List, Blockquote, Block, Link, ItemLink, InlineItem, Code, ListItem, Paragraph, Heading, Node, Span, WithChildrenNode, InlineNode, Record, StructuredText, ThematicBreak, Document } from './types'; | ||
import { Root, List, Blockquote, Block, Link, ItemLink, InlineItem, Code, ListItem, Paragraph, Heading, Node, Span, WithChildrenNode, InlineNode, NodeType, Record as DatoCmsRecord, StructuredText, ThematicBreak, Document } from './types'; | ||
export declare function hasChildren(node: Node): node is WithChildrenNode; | ||
@@ -17,4 +17,6 @@ export declare function isInlineNode(node: Node): node is InlineNode; | ||
export declare function isThematicBreak(node: Node): node is ThematicBreak; | ||
export declare function isStructuredText<R1 extends Record, R2 extends Record = R1>(obj: any): obj is StructuredText<R1, R2>; | ||
export declare function isDocument(obj: any): obj is Document; | ||
export declare function isEmptyDocument(obj: any): boolean; | ||
export declare function isNodeType(value: string): value is NodeType; | ||
export declare function isNode(obj: unknown): obj is Node; | ||
export declare function isStructuredText<R1 extends DatoCmsRecord, R2 extends DatoCmsRecord = R1>(obj: unknown): obj is StructuredText<R1, R2>; | ||
export declare function isDocument(obj: unknown): obj is Document; | ||
export declare function isEmptyDocument(obj: unknown): boolean; |
@@ -373,3 +373,19 @@ export declare type Node = BlockNode | InlineNode; | ||
export declare type StructuredText<R1 extends Record = Record, R2 extends Record = R1> = { | ||
/** A DatoCMS compatible document */ | ||
/** | ||
* A DatoCMS "dast" document | ||
* | ||
* https://www.datocms.com/docs/structured-text/dast | ||
*/ | ||
value: Document | unknown; | ||
/** Blocks associated with the Structured Text */ | ||
blocks?: R1[]; | ||
/** Links associated with the Structured Text */ | ||
links?: R2[]; | ||
}; | ||
export declare type TypesafeStructuredText<R1 extends Record = Record, R2 extends Record = R1> = { | ||
/** | ||
* A DatoCMS "dast" document | ||
* | ||
* https://www.datocms.com/docs/structured-text/dast | ||
*/ | ||
value: Document; | ||
@@ -376,0 +392,0 @@ /** Blocks associated with the Structured Text */ |
{ | ||
"name": "datocms-structured-text-utils", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "A set of Typescript types and helpers to work with DatoCMS Structured Text fields.", | ||
@@ -38,3 +38,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "0a5daee77a4164adcbed9b7c6c6aff00f77199b1" | ||
"gitHead": "d9061b4584ccd5dde261d28b1005cd34b9a282c9" | ||
} |
@@ -18,3 +18,3 @@ import { | ||
NodeType, | ||
Record, | ||
Record as DatoCmsRecord, | ||
StructuredText, | ||
@@ -26,2 +26,3 @@ ThematicBreak, | ||
import { | ||
allowedNodeTypes, | ||
headingNodeType, | ||
@@ -103,20 +104,36 @@ spanNodeType, | ||
export function isStructuredText<R1 extends Record, R2 extends Record = R1>( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj: any, | ||
): obj is StructuredText<R1, R2> { | ||
return obj && 'value' in obj && isDocument(obj.value); | ||
function isObject(obj: unknown): obj is Record<string, unknown> { | ||
return Boolean(typeof obj === 'object' && obj); | ||
} | ||
export function isDocument( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj: any, | ||
): obj is Document { | ||
return obj && 'schema' in obj && 'document' in obj; | ||
export function isNodeType(value: string): value is NodeType { | ||
return allowedNodeTypes.includes(value as NodeType); | ||
} | ||
export function isEmptyDocument( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
obj: any, | ||
): boolean { | ||
export function isNode(obj: unknown): obj is Node { | ||
return Boolean( | ||
isObject(obj) && | ||
'type' in obj && | ||
typeof obj.type === 'string' && | ||
isNodeType(obj.type), | ||
); | ||
} | ||
export function isStructuredText< | ||
R1 extends DatoCmsRecord, | ||
R2 extends DatoCmsRecord = R1 | ||
>(obj: unknown): obj is StructuredText<R1, R2> { | ||
return Boolean(isObject(obj) && 'value' in obj && isDocument(obj.value)); | ||
} | ||
export function isDocument(obj: unknown): obj is Document { | ||
return Boolean( | ||
isObject(obj) && | ||
'schema' in obj && | ||
'document' in obj && | ||
obj.schema === 'dast', | ||
); | ||
} | ||
export function isEmptyDocument(obj: unknown): boolean { | ||
if (!obj) { | ||
@@ -126,7 +143,8 @@ return true; | ||
const document = isStructuredText(obj) | ||
? obj.value | ||
: isDocument(obj) | ||
? obj | ||
: null; | ||
const document = | ||
isStructuredText(obj) && isDocument(obj.value) | ||
? obj.value | ||
: isDocument(obj) | ||
? obj | ||
: null; | ||
@@ -140,3 +158,2 @@ if (!document) { | ||
return ( | ||
document.schema === 'dast' && | ||
document.document.children.length === 1 && | ||
@@ -143,0 +160,0 @@ document.document.children[0].type === 'paragraph' && |
import { Node, Record, Document, StructuredText } from './types'; | ||
import { hasChildren, isDocument, isStructuredText } from './guards'; | ||
import { hasChildren, isDocument, isNode, isStructuredText } from './guards'; | ||
import { flatten } from 'array-flatten'; | ||
@@ -93,8 +93,7 @@ | ||
return matchingTransform.apply({ adapter, node, children, key, ancestors }); | ||
} else { | ||
throw new RenderError( | ||
`Don't know how to render a node with type "${node.type}". Please specify a custom renderRule for it!`, | ||
node, | ||
); | ||
} | ||
throw new RenderError( | ||
`Don't know how to render a node with type "${node.type}". Please specify a custom renderRule for it!`, | ||
node, | ||
); | ||
} | ||
@@ -132,16 +131,21 @@ | ||
const result = transformNode( | ||
adapter, | ||
isStructuredText<R1, R2>(structuredTextOrNode) | ||
const node = | ||
isStructuredText<R1, R2>(structuredTextOrNode) && | ||
isDocument(structuredTextOrNode.value) | ||
? structuredTextOrNode.value.document | ||
: isDocument(structuredTextOrNode) | ||
? structuredTextOrNode.document | ||
: structuredTextOrNode, | ||
: isNode(structuredTextOrNode) | ||
? structuredTextOrNode | ||
: undefined; | ||
't-0', | ||
[], | ||
renderRules, | ||
); | ||
if (!node) { | ||
throw new Error( | ||
'Passed object is neither null, a Structured Text value, a DAST document or a DAST node', | ||
); | ||
} | ||
const result = transformNode(adapter, node, 't-0', [], renderRules); | ||
return result; | ||
} |
@@ -443,4 +443,27 @@ export type Node = BlockNode | InlineNode; | ||
export type StructuredText<R1 extends Record = Record, R2 extends Record = R1> = { | ||
/** A DatoCMS compatible document */ | ||
export type StructuredText< | ||
R1 extends Record = Record, | ||
R2 extends Record = R1 | ||
> = { | ||
/** | ||
* A DatoCMS "dast" document | ||
* | ||
* https://www.datocms.com/docs/structured-text/dast | ||
*/ | ||
value: Document | unknown; | ||
/** Blocks associated with the Structured Text */ | ||
blocks?: R1[]; | ||
/** Links associated with the Structured Text */ | ||
links?: R2[]; | ||
}; | ||
export type TypesafeStructuredText< | ||
R1 extends Record = Record, | ||
R2 extends Record = R1 | ||
> = { | ||
/** | ||
* A DatoCMS "dast" document | ||
* | ||
* https://www.datocms.com/docs/structured-text/dast | ||
*/ | ||
value: Document; | ||
@@ -447,0 +470,0 @@ /** Blocks associated with the Structured Text */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112590
2644