Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@prismicio/richtext

Package Overview
Dependencies
Maintainers
22
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prismicio/richtext - npm Package Compare versions

Comparing version 2.1.1 to 2.1.2

dist/asText.cjs

137

dist/index.d.ts

@@ -1,130 +0,7 @@

import { RichTextNodeType, RTAnyNode, RTHeading1Node, RTHeading2Node, RTHeading3Node, RTHeading4Node, RTHeading5Node, RTHeading6Node, RTParagraphNode, RTPreformattedNode, RTStrongNode, RTEmNode, RTListItemNode, RTOListItemNode, RTListNode, RTOListNode, RTImageNode, RTEmbedNode, RTLinkNode, RTLabelNode, RTSpanNode, RTNode, RichTextField } from '@prismicio/types';
export { RichTextNodeType as Element } from '@prismicio/types';
/**
* Serializes a node from a rich text or title field with a function
*
* @typeParam ReturnType - Return type of the function serializer
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
declare type RichTextFunctionSerializer<ReturnType> = (type: typeof RichTextNodeType[keyof typeof RichTextNodeType], node: RTAnyNode, text: string | undefined, children: ReturnType[], key: string) => ReturnType | null | undefined;
/**
* Map serializer's tag function serializer, can be helpful for typing those handlers
*
* @typeParam ReturnType - Return type of the tag serializer
*/
declare type RichTextMapSerializerFunction<ReturnType, Node extends RTAnyNode = RTAnyNode, TextType = string | undefined> = (payload: {
type: Node["type"];
node: Node;
text: TextType;
children: ReturnType[];
key: string;
}) => ReturnType | null | undefined;
/**
* Serializes a node from a rich text or title field with a map
*
* @remarks
* This type of serializer needs to be processed through
* {@link wrapMapSerializer} before being used with {@link serialize}
* @typeParam ReturnType - Return type of the map serializer
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
declare type RichTextMapSerializer<ReturnType> = {
heading1?: RichTextMapSerializerFunction<ReturnType, RTHeading1Node, undefined>;
heading2?: RichTextMapSerializerFunction<ReturnType, RTHeading2Node, undefined>;
heading3?: RichTextMapSerializerFunction<ReturnType, RTHeading3Node, undefined>;
heading4?: RichTextMapSerializerFunction<ReturnType, RTHeading4Node, undefined>;
heading5?: RichTextMapSerializerFunction<ReturnType, RTHeading5Node, undefined>;
heading6?: RichTextMapSerializerFunction<ReturnType, RTHeading6Node, undefined>;
paragraph?: RichTextMapSerializerFunction<ReturnType, RTParagraphNode, undefined>;
preformatted?: RichTextMapSerializerFunction<ReturnType, RTPreformattedNode, undefined>;
strong?: RichTextMapSerializerFunction<ReturnType, RTStrongNode, string>;
em?: RichTextMapSerializerFunction<ReturnType, RTEmNode, string>;
listItem?: RichTextMapSerializerFunction<ReturnType, RTListItemNode, undefined>;
oListItem?: RichTextMapSerializerFunction<ReturnType, RTOListItemNode, undefined>;
list?: RichTextMapSerializerFunction<ReturnType, RTListNode, undefined>;
oList?: RichTextMapSerializerFunction<ReturnType, RTOListNode, undefined>;
image?: RichTextMapSerializerFunction<ReturnType, RTImageNode, undefined>;
embed?: RichTextMapSerializerFunction<ReturnType, RTEmbedNode, undefined>;
hyperlink?: RichTextMapSerializerFunction<ReturnType, RTLinkNode, string>;
label?: RichTextMapSerializerFunction<ReturnType, RTLabelNode, string>;
span?: RichTextMapSerializerFunction<ReturnType, RTSpanNode, string>;
};
interface Tree {
key: string;
children: TreeNode[];
}
interface TreeNode {
key: string;
type: typeof RichTextNodeType[keyof typeof RichTextNodeType];
text?: string;
node: RTAnyNode;
children: TreeNode[];
}
/**
* Parses a rich text or title field into a tree
*
* @remarks
* This is a low level helper mainly intended to be used by higher level
* packages. Most users aren't expected to this function directly.
* @param nodes - A rich text or title field from Prismic
*
* @returns Tree from given rich text or title field
*/
declare const asTree: (nodes: RTNode[]) => Tree;
/**
* Serializes a rich text or title field to a plain text string
*
* @param richTextField - A rich text or title field from Prismic
* @param separator - Separator used to join each element, defaults to a space
*
* @returns Plain text equivalent of the provided rich text or title field
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
declare const asText: (richTextField: RichTextField, separator?: string) => string;
/**
* Serializes a rich text or title field with a given serializer
*
* @remarks
* This is a low level helper mainly intended to be used by higher level
* packages Most users aren't expected to this function directly
* @typeParam SerializerReturnType - Return type of the serializer
* @param richTextField - A rich text or title field from Prismic
* @param serializer - A function serializer to apply
*
* @returns An array of serialized nodes
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
declare const serialize: <SerializerReturnType>(richTextField: RichTextField, serializer: RichTextFunctionSerializer<SerializerReturnType>) => SerializerReturnType[];
/**
* Wraps a map serializer into a regular function serializer
*
* @remarks
* This is a low level helper mainly intended to be used by higher level
* packages Most users aren't expected to this function directly
* @typeParam SerializerReturnType - Return type of the map serializer
* @param mapSerializer - Map serializer to wrap
*
* @returns A regular function serializer
*/
declare const wrapMapSerializer: <SerializerReturnType>(mapSerializer: RichTextMapSerializer<SerializerReturnType>) => RichTextFunctionSerializer<SerializerReturnType>;
/**
* Takes an array of serializers and returns a serializer applying provided
* serializers sequentially until a result is returned
*
* @remarks
* This is a low level helper mainly intended to be used by higher level
* packages Most users aren't expected to this function directly
* @typeParam SerializerReturnType - Return type of serializers
* @param serializers - Serializers to compose
*
* @returns Composed serializer
*/
declare const composeSerializers: <SerializerReturnType>(...serializers: (RichTextFunctionSerializer<SerializerReturnType> | undefined)[]) => RichTextFunctionSerializer<SerializerReturnType>;
export { RichTextFunctionSerializer, RichTextMapSerializer, RichTextMapSerializerFunction, asText, asTree, composeSerializers, serialize, wrapMapSerializer };
export { asTree } from "./asTree";
export { asText } from "./asText";
export { serialize } from "./serialize";
export { wrapMapSerializer } from "./wrapMapSerializer";
export { composeSerializers } from "./composeSerializers";
export { RichTextNodeType as Element } from "@prismicio/types";
export type { RichTextFunctionSerializer, RichTextMapSerializer, RichTextMapSerializerFunction, } from "./types";

@@ -1,202 +0,15 @@

import { RichTextNodeType } from '@prismicio/types';
export { RichTextNodeType as Element } from '@prismicio/types';
const uuid = () => {
return (++uuid.i).toString();
import { asTree } from "./asTree.js";
import { asText } from "./asText.js";
import { serialize } from "./serialize.js";
import { wrapMapSerializer } from "./wrapMapSerializer.js";
import { composeSerializers } from "./composeSerializers.js";
import { RichTextNodeType } from "@prismicio/types";
export {
RichTextNodeType as Element,
asText,
asTree,
composeSerializers,
serialize,
wrapMapSerializer
};
uuid.i = 0;
const asTree = (nodes) => {
const preparedNodes = prepareNodes(nodes);
const children = [];
for (let i = 0; i < preparedNodes.length; i++) {
children.push(nodeToTreeNode(preparedNodes[i]));
}
return {
key: uuid(),
children
};
};
const createTreeNode = (node, children = []) => {
return {
key: uuid(),
type: node.type,
text: "text" in node ? node.text : void 0,
node,
children
};
};
const createTextTreeNode = (text) => {
return createTreeNode({
type: RichTextNodeType.span,
text,
spans: []
});
};
const prepareNodes = (nodes) => {
const mutNodes = nodes.slice(0);
for (let i = 0; i < mutNodes.length; i++) {
const node = mutNodes[i];
if (node.type === RichTextNodeType.listItem || node.type === RichTextNodeType.oListItem) {
const items = [
node
];
while (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {
items.push(mutNodes[i + 1]);
mutNodes.splice(i, 1);
}
if (node.type === RichTextNodeType.listItem) {
mutNodes[i] = {
type: RichTextNodeType.list,
items
};
} else {
mutNodes[i] = {
type: RichTextNodeType.oList,
items
};
}
}
}
return mutNodes;
};
const nodeToTreeNode = (node) => {
if ("text" in node) {
return createTreeNode(
node,
textNodeSpansToTreeNodeChildren(node.spans, node)
);
}
if ("items" in node) {
const children = [];
for (let i = 0; i < node.items.length; i++) {
children.push(nodeToTreeNode(node.items[i]));
}
return createTreeNode(node, children);
}
return createTreeNode(node);
};
const textNodeSpansToTreeNodeChildren = (spans, node, parentSpan) => {
if (!spans.length) {
return [createTextTreeNode(node.text)];
}
const mutSpans = spans.slice(0);
mutSpans.sort((a, b) => a.start - b.start || b.end - a.end);
const children = [];
for (let i = 0; i < mutSpans.length; i++) {
const span = mutSpans[i];
const parentSpanStart = parentSpan && parentSpan.start || 0;
const spanStart = span.start - parentSpanStart;
const spanEnd = span.end - parentSpanStart;
const text = node.text.slice(spanStart, spanEnd);
const childSpans = [];
for (let j = i; j < mutSpans.length; j++) {
const siblingSpan = mutSpans[j];
if (siblingSpan !== span && siblingSpan.start >= span.start && siblingSpan.end <= span.end) {
childSpans.push(siblingSpan);
mutSpans.splice(j, 1);
j--;
}
}
if (i === 0 && spanStart > 0) {
children.push(createTextTreeNode(node.text.slice(0, spanStart)));
}
const spanWithText = { ...span, text };
children.push(
createTreeNode(
spanWithText,
textNodeSpansToTreeNodeChildren(
childSpans,
{
...node,
text
},
span
)
)
);
if (spanEnd < node.text.length) {
children.push(
createTextTreeNode(
node.text.slice(
spanEnd,
mutSpans[i + 1] ? mutSpans[i + 1].start - parentSpanStart : void 0
)
)
);
}
}
return children;
};
const asText = (richTextField, separator = " ") => {
let result = "";
for (let i = 0; i < richTextField.length; i++) {
if ("text" in richTextField[i]) {
result += (result ? separator : "") + richTextField[i].text;
}
}
return result;
};
const serialize = (richTextField, serializer) => {
return serializeTreeNodes(
asTree(richTextField).children,
serializer
);
};
const serializeTreeNodes = (nodes, serializer) => {
const serializedTreeNodes = [];
for (let i = 0; i < nodes.length; i++) {
const treeNode = nodes[i];
const serializedTreeNode = serializer(
treeNode.type,
treeNode.node,
treeNode.text,
serializeTreeNodes(treeNode.children, serializer),
treeNode.key
);
if (serializedTreeNode != null) {
serializedTreeNodes.push(serializedTreeNode);
}
}
return serializedTreeNodes;
};
const RichTextReversedNodeType = {
[RichTextNodeType.listItem]: "listItem",
[RichTextNodeType.oListItem]: "oListItem",
[RichTextNodeType.list]: "list",
[RichTextNodeType.oList]: "oList"
};
const wrapMapSerializer = (mapSerializer) => {
return (type, node, text, children, key) => {
const tagSerializer = mapSerializer[RichTextReversedNodeType[type] || type];
if (tagSerializer) {
return tagSerializer({
type,
node,
text,
children,
key
});
}
};
};
const composeSerializers = (...serializers) => {
return (...args) => {
for (let i = 0; i < serializers.length; i++) {
const serializer = serializers[i];
if (serializer) {
const res = serializer(...args);
if (res != null) {
return res;
}
}
}
};
};
export { asText, asTree, composeSerializers, serialize, wrapMapSerializer };
//# sourceMappingURL=index.js.map
{
"name": "@prismicio/richtext",
"version": "2.1.1",
"version": "2.1.2",
"description": "A parser and serializer for Prismic's Rich Text format",

@@ -33,4 +33,4 @@ "keywords": [

"scripts": {
"build": "siroc build",
"dev": "siroc build --watch",
"build": "vite build",
"dev": "vite build --watch",
"format": "prettier --write .",

@@ -49,20 +49,21 @@ "lint": "eslint --ext .js,.ts .",

"dependencies": {
"@prismicio/types": "^0.2.0"
"@prismicio/types": "^0.2.5"
},
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.8",
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"c8": "^7.12.0",
"eslint": "^8.20.0",
"@size-limit/preset-small-lib": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"@vitest/coverage-c8": "^0.26.0",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-tsdoc": "^0.2.16",
"prettier": "^2.7.1",
"prettier-plugin-jsdoc": "^0.3.38",
"siroc": "^0.16.0",
"size-limit": "^7.0.8",
"eslint-plugin-tsdoc": "^0.2.17",
"prettier": "^2.8.1",
"prettier-plugin-jsdoc": "^0.4.2",
"size-limit": "^8.1.0",
"standard-version": "^9.5.0",
"typescript": "^4.7.4",
"vitest": "^0.19.1"
"typescript": "^4.9.4",
"vite": "^4.0.2",
"vite-plugin-sdk": "^0.1.0",
"vitest": "^0.26.0"
},

@@ -69,0 +70,0 @@ "engines": {

@@ -42,3 +42,4 @@ import {

/**
* Map serializer's tag function serializer, can be helpful for typing those handlers
* Map serializer's tag function serializer, can be helpful for typing those
* handlers
*

@@ -45,0 +46,0 @@ * @typeParam ReturnType - Return type of the tag serializer

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc