@contentful/rich-text-plain-text-renderer
Advanced tools
Comparing version 10.0.0 to 10.0.1
@@ -7,6 +7,6 @@ "use strict"; | ||
* | ||
* NB: This can be applied to any block node of a structured text document, | ||
* NB: This can be applied to non text node of a structured text document, | ||
* hence the flexible typing. | ||
*/ | ||
function documentToPlainTextString(rootRichTextNode, blockDivisor) { | ||
function documentToPlainTextString(rootNode, blockDivisor) { | ||
if (blockDivisor === void 0) { blockDivisor = ' '; } | ||
@@ -21,10 +21,13 @@ /** | ||
* nodeType: BLOCKS.UL_LIST, | ||
* data: {}, | ||
* content: [ | ||
* { | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [{ | ||
* nodeType: BLOCKS.PARAGRAPH, | ||
* data: {}, | ||
* content: [ | ||
* { nodeType: 'text', value: 'List ', marks: [] }, | ||
* { nodeType: 'text', value: 'item', marks: [{ type: 'bold' }] } | ||
* { nodeType: 'text', data: {}, value: 'List ', marks: [] }, | ||
* { nodeType: 'text', data: {}, value: 'item', marks: [{ type: 'bold' }] } | ||
* ] | ||
@@ -34,7 +37,9 @@ * }] | ||
* { | ||
* nodeType: BLOCKS.OL_LIST, | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [{ | ||
* nodeType: BLOCKS.PARAGRAPH, | ||
* data: {}, | ||
* content: [ | ||
* { nodeType: 'text', value: 'Another list item', marks: [] } | ||
* { nodeType: 'text', data: {}, value: 'Another list item', marks: [] } | ||
* ] | ||
@@ -44,12 +49,18 @@ * }] | ||
* { | ||
* nodeType: BLOCKS.HR, | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [], | ||
* content: [{ | ||
* nodeType: BLOCKS.HR, | ||
* data: {}, | ||
* content: [], | ||
* }] | ||
* }, | ||
* { | ||
* nodeType: BLOCKS.OL_LIST, | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [{ | ||
* nodeType: BLOCKS.PARAGRAPH, | ||
* data: | ||
* content: [ | ||
* { nodeType: 'text', value: 'Yet another list item', marks: [] } | ||
* { nodeType: 'text', data: {}, value: 'Yet another list item', marks: [] } | ||
* ] | ||
@@ -68,26 +79,20 @@ * }] | ||
*/ | ||
var childNodeList = rootRichTextNode.content; | ||
return childNodeList.reduce(function (textValue, node, i) { | ||
var nodeIsText = isText(node); | ||
var nodeTextValue = nodeIsText | ||
? node.value | ||
: documentToPlainTextString(node, blockDivisor); | ||
if (!nodeIsText && !nodeTextValue.length) { | ||
return textValue; | ||
return rootNode.content.reduce(function (acc, node, i) { | ||
var nodeTextValue; | ||
if (rich_text_types_1.helpers.isText(node)) { | ||
nodeTextValue = node.value; | ||
} | ||
else { | ||
var nextNode = childNodeList[i + 1]; | ||
var nodeIsInBlockSequence = nextNode && !isText(nextNode) && isBlock(nextNode); | ||
var divisor = nodeIsInBlockSequence ? blockDivisor : ''; | ||
return textValue + nodeTextValue + divisor; | ||
else if (rich_text_types_1.helpers.isBlock(node) || rich_text_types_1.helpers.isInline(node)) { | ||
nodeTextValue = documentToPlainTextString(node, blockDivisor); | ||
if (!nodeTextValue.length) { | ||
return acc; | ||
} | ||
} | ||
var nextNode = rootNode.content[i + 1]; | ||
var isNextNodeBlock = nextNode && rich_text_types_1.helpers.isBlock(nextNode); | ||
var divisor = isNextNodeBlock ? blockDivisor : ''; | ||
return acc + nodeTextValue + divisor; | ||
}, ''); | ||
} | ||
exports.documentToPlainTextString = documentToPlainTextString; | ||
function isBlock(node) { | ||
return Object.values(rich_text_types_1.BLOCKS).includes(node.nodeType); | ||
} | ||
function isText(node) { | ||
return node.nodeType === 'text'; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -41,3 +41,3 @@ 'use strict'; | ||
*/ | ||
var inlines = { | ||
var INLINES = { | ||
HYPERLINK: 'hyperlink', | ||
@@ -93,4 +93,30 @@ ENTRY_HYPERLINK: 'entry-hyperlink', | ||
/** | ||
* Checks if the node is an instance of Inline. | ||
*/ | ||
function isInline(node) { | ||
return Object.values(INLINES).includes(node.nodeType); | ||
} | ||
/** | ||
* Checks if the node is an instance of Block. | ||
*/ | ||
function isBlock(node) { | ||
return Object.values(BLOCKS).includes(node.nodeType); | ||
} | ||
/** | ||
* Checks if the node is an instance of Text. | ||
*/ | ||
function isText(node) { | ||
return node.nodeType === 'text'; | ||
} | ||
var helpers = /*#__PURE__*/Object.freeze({ | ||
isInline: isInline, | ||
isBlock: isBlock, | ||
isText: isText | ||
}); | ||
exports.helpers = helpers; | ||
exports.BLOCKS = BLOCKS; | ||
exports.INLINES = inlines; | ||
exports.INLINES = INLINES; | ||
exports.MARKS = marks; | ||
@@ -104,8 +130,9 @@ exports.TOP_LEVEL_BLOCKS = TOP_LEVEL_BLOCKS; | ||
unwrapExports(richTextTypes_es5); | ||
var richTextTypes_es5_1 = richTextTypes_es5.BLOCKS; | ||
var richTextTypes_es5_2 = richTextTypes_es5.INLINES; | ||
var richTextTypes_es5_3 = richTextTypes_es5.MARKS; | ||
var richTextTypes_es5_4 = richTextTypes_es5.TOP_LEVEL_BLOCKS; | ||
var richTextTypes_es5_5 = richTextTypes_es5.VOID_BLOCKS; | ||
var richTextTypes_es5_6 = richTextTypes_es5.CONTAINERS; | ||
var richTextTypes_es5_1 = richTextTypes_es5.helpers; | ||
var richTextTypes_es5_2 = richTextTypes_es5.BLOCKS; | ||
var richTextTypes_es5_3 = richTextTypes_es5.INLINES; | ||
var richTextTypes_es5_4 = richTextTypes_es5.MARKS; | ||
var richTextTypes_es5_5 = richTextTypes_es5.TOP_LEVEL_BLOCKS; | ||
var richTextTypes_es5_6 = richTextTypes_es5.VOID_BLOCKS; | ||
var richTextTypes_es5_7 = richTextTypes_es5.CONTAINERS; | ||
@@ -115,6 +142,6 @@ /** | ||
* | ||
* NB: This can be applied to any block node of a structured text document, | ||
* NB: This can be applied to non text node of a structured text document, | ||
* hence the flexible typing. | ||
*/ | ||
function documentToPlainTextString(rootRichTextNode, blockDivisor) { | ||
function documentToPlainTextString(rootNode, blockDivisor) { | ||
if (blockDivisor === void 0) { blockDivisor = ' '; } | ||
@@ -129,10 +156,13 @@ /** | ||
* nodeType: BLOCKS.UL_LIST, | ||
* data: {}, | ||
* content: [ | ||
* { | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [{ | ||
* nodeType: BLOCKS.PARAGRAPH, | ||
* data: {}, | ||
* content: [ | ||
* { nodeType: 'text', value: 'List ', marks: [] }, | ||
* { nodeType: 'text', value: 'item', marks: [{ type: 'bold' }] } | ||
* { nodeType: 'text', data: {}, value: 'List ', marks: [] }, | ||
* { nodeType: 'text', data: {}, value: 'item', marks: [{ type: 'bold' }] } | ||
* ] | ||
@@ -142,7 +172,9 @@ * }] | ||
* { | ||
* nodeType: BLOCKS.OL_LIST, | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [{ | ||
* nodeType: BLOCKS.PARAGRAPH, | ||
* data: {}, | ||
* content: [ | ||
* { nodeType: 'text', value: 'Another list item', marks: [] } | ||
* { nodeType: 'text', data: {}, value: 'Another list item', marks: [] } | ||
* ] | ||
@@ -152,12 +184,18 @@ * }] | ||
* { | ||
* nodeType: BLOCKS.HR, | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [], | ||
* content: [{ | ||
* nodeType: BLOCKS.HR, | ||
* data: {}, | ||
* content: [], | ||
* }] | ||
* }, | ||
* { | ||
* nodeType: BLOCKS.OL_LIST, | ||
* nodeType: BLOCKS.LIST_ITEM, | ||
* data: {}, | ||
* content: [{ | ||
* nodeType: BLOCKS.PARAGRAPH, | ||
* data: | ||
* content: [ | ||
* { nodeType: 'text', value: 'Yet another list item', marks: [] } | ||
* { nodeType: 'text', data: {}, value: 'Yet another list item', marks: [] } | ||
* ] | ||
@@ -176,27 +214,21 @@ * }] | ||
*/ | ||
var childNodeList = rootRichTextNode.content; | ||
return childNodeList.reduce(function (textValue, node, i) { | ||
var nodeIsText = isText(node); | ||
var nodeTextValue = nodeIsText | ||
? node.value | ||
: documentToPlainTextString(node, blockDivisor); | ||
if (!nodeIsText && !nodeTextValue.length) { | ||
return textValue; | ||
return rootNode.content.reduce(function (acc, node, i) { | ||
var nodeTextValue; | ||
if (richTextTypes_es5_1.isText(node)) { | ||
nodeTextValue = node.value; | ||
} | ||
else { | ||
var nextNode = childNodeList[i + 1]; | ||
var nodeIsInBlockSequence = nextNode && !isText(nextNode) && isBlock(nextNode); | ||
var divisor = nodeIsInBlockSequence ? blockDivisor : ''; | ||
return textValue + nodeTextValue + divisor; | ||
else if (richTextTypes_es5_1.isBlock(node) || richTextTypes_es5_1.isInline(node)) { | ||
nodeTextValue = documentToPlainTextString(node, blockDivisor); | ||
if (!nodeTextValue.length) { | ||
return acc; | ||
} | ||
} | ||
var nextNode = rootNode.content[i + 1]; | ||
var isNextNodeBlock = nextNode && richTextTypes_es5_1.isBlock(nextNode); | ||
var divisor = isNextNodeBlock ? blockDivisor : ''; | ||
return acc + nodeTextValue + divisor; | ||
}, ''); | ||
} | ||
function isBlock(node) { | ||
return Object.values(richTextTypes_es5_1).includes(node.nodeType); | ||
} | ||
function isText(node) { | ||
return node.nodeType === 'text'; | ||
} | ||
exports.documentToPlainTextString = documentToPlainTextString; | ||
//# sourceMappingURL=rich-text-plain-text-renderer.es5.js.map |
@@ -1,10 +0,8 @@ | ||
import { Document, Block, Inline } from '@contentful/rich-text-types'; | ||
declare type NonTextNode = Block | Inline; | ||
import { Block, Inline } from '@contentful/rich-text-types'; | ||
/** | ||
* Returns the text value of a rich text document. | ||
* | ||
* NB: This can be applied to any block node of a structured text document, | ||
* NB: This can be applied to non text node of a structured text document, | ||
* hence the flexible typing. | ||
*/ | ||
export declare function documentToPlainTextString(rootRichTextNode: Document | NonTextNode, blockDivisor?: string): string; | ||
export {}; | ||
export declare function documentToPlainTextString(rootNode: Block | Inline, blockDivisor?: string): string; |
{ | ||
"name": "@contentful/rich-text-plain-text-renderer", | ||
"version": "10.0.0", | ||
"version": "10.0.1", | ||
"main": "dist/rich-text-plain-text-renderer.es5.js", | ||
@@ -26,3 +26,3 @@ "typings": "dist/types/rich-text-plain-text-renderer.d.ts", | ||
"dependencies": { | ||
"@contentful/rich-text-types": "^10.0.0" | ||
"@contentful/rich-text-types": "^10.0.1" | ||
}, | ||
@@ -41,3 +41,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "4531fd40a4e459fda941272917d3e42bdacd9f00" | ||
"gitHead": "0c308468f7858c0c8e9391cdf95601e8dd75f9f9" | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
33277
484
0