@contentful/rich-text-html-renderer
Advanced tools
Comparing version 2.9.0 to 2.9.1
@@ -27,5 +27,5 @@ "use strict"; | ||
_a[rich_text_types_1.BLOCKS.HR] = function () { return '<hr/>'; }, | ||
_a[rich_text_types_1.INLINES.ASSET_HYPERLINK] = function (node) { return exports.defaultInline(rich_text_types_1.INLINES.ASSET_HYPERLINK, node); }, | ||
_a[rich_text_types_1.INLINES.ENTRY_HYPERLINK] = function (node) { return exports.defaultInline(rich_text_types_1.INLINES.ENTRY_HYPERLINK, node); }, | ||
_a[rich_text_types_1.INLINES.EMBEDDED_ENTRY] = function (node) { return exports.defaultInline(rich_text_types_1.INLINES.EMBEDDED_ENTRY, node); }, | ||
_a[rich_text_types_1.INLINES.ASSET_HYPERLINK] = function (node) { return defaultInline(rich_text_types_1.INLINES.ASSET_HYPERLINK, node); }, | ||
_a[rich_text_types_1.INLINES.ENTRY_HYPERLINK] = function (node) { return defaultInline(rich_text_types_1.INLINES.ENTRY_HYPERLINK, node); }, | ||
_a[rich_text_types_1.INLINES.EMBEDDED_ENTRY] = function (node) { return defaultInline(rich_text_types_1.INLINES.EMBEDDED_ENTRY, node); }, | ||
_a[rich_text_types_1.INLINES.HYPERLINK] = function (node, next) { return "<a href=\"" + node.data.uri + "\">" + next(node.content) + "</a>"; }, | ||
@@ -39,8 +39,11 @@ _a); | ||
_b); | ||
exports.defaultInline = function (type, node) { | ||
var defaultInline = function (type, node) { | ||
return "<span>type: " + type + " id: " + node.data.target.sys.id + "</span>"; | ||
}; | ||
function documentToHtmlString(doc, options) { | ||
/** | ||
* Serialize a Contentful Rich Text `document` to an html string. | ||
*/ | ||
function documentToHtmlString(richTextDocument, options) { | ||
if (options === void 0) { options = {}; } | ||
return nodeListToHtmlString(doc.content, { | ||
return nodeListToHtmlString(richTextDocument.content, { | ||
renderNode: __assign({}, defaultNodeRenderers, options.renderNode), | ||
@@ -56,7 +59,6 @@ renderMark: __assign({}, defaultMarkRenderers, options.renderMark), | ||
function nodeToHtmlString(node, _a) { | ||
var _b = _a.renderNode, renderNode = _b === void 0 ? {} : _b, _c = _a.renderMark, renderMark = _c === void 0 ? {} : _c; | ||
if (node.nodeType === 'text') { | ||
var textNode = node; | ||
if (textNode.marks.length > 0) { | ||
return textNode.marks.reduce(function (value, mark) { | ||
var renderNode = _a.renderNode, renderMark = _a.renderMark; | ||
if (isText(node)) { | ||
if (node.marks.length > 0) { | ||
return node.marks.reduce(function (value, mark) { | ||
if (!renderMark[mark.type]) { | ||
@@ -66,5 +68,5 @@ return value; | ||
return renderMark[mark.type](value); | ||
}, textNode.value); | ||
}, node.value); | ||
} | ||
return textNode.value; | ||
return node.value; | ||
} | ||
@@ -80,2 +82,5 @@ else { | ||
} | ||
function isText(node) { | ||
return node.nodeType === 'text'; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -164,5 +164,8 @@ 'use strict'; | ||
}; | ||
function documentToHtmlString(doc, options) { | ||
/** | ||
* Serialize a Contentful Rich Text `document` to an html string. | ||
*/ | ||
function documentToHtmlString(richTextDocument, options) { | ||
if (options === void 0) { options = {}; } | ||
return nodeListToHtmlString(doc.content, { | ||
return nodeListToHtmlString(richTextDocument.content, { | ||
renderNode: __assign({}, defaultNodeRenderers, options.renderNode), | ||
@@ -177,7 +180,6 @@ renderMark: __assign({}, defaultMarkRenderers, options.renderMark), | ||
function nodeToHtmlString(node, _a) { | ||
var _b = _a.renderNode, renderNode = _b === void 0 ? {} : _b, _c = _a.renderMark, renderMark = _c === void 0 ? {} : _c; | ||
if (node.nodeType === 'text') { | ||
var textNode = node; | ||
if (textNode.marks.length > 0) { | ||
return textNode.marks.reduce(function (value, mark) { | ||
var renderNode = _a.renderNode, renderMark = _a.renderMark; | ||
if (isText(node)) { | ||
if (node.marks.length > 0) { | ||
return node.marks.reduce(function (value, mark) { | ||
if (!renderMark[mark.type]) { | ||
@@ -187,5 +189,5 @@ return value; | ||
return renderMark[mark.type](value); | ||
}, textNode.value); | ||
}, node.value); | ||
} | ||
return textNode.value; | ||
return node.value; | ||
} | ||
@@ -201,5 +203,7 @@ else { | ||
} | ||
function isText(node) { | ||
return node.nodeType === 'text'; | ||
} | ||
exports.defaultInline = defaultInline; | ||
exports.documentToHtmlString = documentToHtmlString; | ||
//# sourceMappingURL=rich-text-html-renderer.es5.js.map |
@@ -1,9 +0,8 @@ | ||
import { Document, Node, Block, Inline } from '@contentful/rich-text-types'; | ||
export declare type NonTextNode = Block | Inline; | ||
export declare const defaultInline: (type: string, node: NonTextNode) => string; | ||
import { Document, Text, Block, Inline } from '@contentful/rich-text-types'; | ||
export declare type CommonNode = Text | Block | Inline; | ||
export interface Next { | ||
(nodes: Node[]): string; | ||
(nodes: CommonNode[]): string; | ||
} | ||
export interface NodeRenderer { | ||
(node: NonTextNode, next: Next): string; | ||
(node: Block | Inline, next: Next): string; | ||
} | ||
@@ -13,12 +12,18 @@ export interface RenderNode { | ||
} | ||
export interface MarkRenderer { | ||
(text: string): string; | ||
} | ||
export interface RenderMark { | ||
[k: string]: MarkRenderer; | ||
[k: string]: (text: string) => string; | ||
} | ||
export interface Options { | ||
/** | ||
* Node renderers | ||
*/ | ||
renderNode?: RenderNode; | ||
/** | ||
* Mark renderers | ||
*/ | ||
renderMark?: RenderMark; | ||
} | ||
export declare function documentToHtmlString(doc: Document, options?: Partial<Options>): string; | ||
/** | ||
* Serialize a Contentful Rich Text `document` to an html string. | ||
*/ | ||
export declare function documentToHtmlString(richTextDocument: Document, options?: Partial<Options>): string; |
{ | ||
"name": "@contentful/rich-text-html-renderer", | ||
"version": "2.9.0", | ||
"version": "2.9.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
# rich-text-html-renderer | ||
HTML renderer for the Rich Text document. | ||
## Installation | ||
Using [npm](http://npmjs.org/): | ||
@@ -12,2 +14,3 @@ | ||
Using [yarn](https://yarnpkg.com/): | ||
```javascript | ||
@@ -24,10 +27,7 @@ yarn add @contentful/rich-text-html-renderer | ||
nodeType: 'document', | ||
nodeClass: 'document', | ||
content: [ | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'paragraph', | ||
content: [ | ||
{ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -39,3 +39,3 @@ value: 'Hello world!', | ||
}, | ||
] | ||
], | ||
}; | ||
@@ -51,23 +51,19 @@ | ||
nodeType: 'document', | ||
nodeClass: 'document', | ||
content: [ | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'paragraph', | ||
content: [ | ||
{ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
value: 'Hello', | ||
marks: [{ nodeType: 'bold' }] | ||
marks: [{ nodeType: 'bold' }], | ||
}, | ||
{ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
value: ' world!', | ||
marks: [{ nodeType: 'italic' }] | ||
marks: [{ nodeType: 'italic' }], | ||
}, | ||
], | ||
}, | ||
] | ||
], | ||
}; | ||
@@ -86,10 +82,7 @@ | ||
nodeType: 'document', | ||
nodeClass: 'document', | ||
content: [ | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'paragraph', | ||
content: [ | ||
{ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -100,3 +93,2 @@ value: 'Hello', | ||
{ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -132,6 +124,4 @@ value: ' world!', | ||
nodeType: 'document', | ||
nodeClass: 'document', | ||
content: [ | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'embedded-entry-block', | ||
@@ -156,28 +146,30 @@ data: { | ||
The `renderNode` keys should be one of the following `BLOCKS` and `INLINES` properties as defined in [`@contentful/rich-text-types`](https://www.npmjs.com/package/@contentful/rich-text-types): | ||
* `BLOCKS` | ||
* `DOCUMENT` | ||
* `PARAGRAPH` | ||
* `HEADING_1` | ||
* `HEADING_2` | ||
* `HEADING_3` | ||
* `HEADING_4` | ||
* `HEADING_5` | ||
* `HEADING_6` | ||
* `UL_LIST` | ||
* `OL_LIST` | ||
* `LIST_ITEM` | ||
* `QUOTE` | ||
* `HR` | ||
* `EMBDEDDED_ENTRY` | ||
* `INLINES` | ||
* `EMBEDDED_ENTRY` (this is different from the `BLOCKS.EMBEDDED_ENTRY`) | ||
* `HYPERLINK` | ||
* `ENTRY_HYPERLINK` | ||
* `ASSET_HYPERLINK` | ||
- `BLOCKS` | ||
- `DOCUMENT` | ||
- `PARAGRAPH` | ||
- `HEADING_1` | ||
- `HEADING_2` | ||
- `HEADING_3` | ||
- `HEADING_4` | ||
- `HEADING_5` | ||
- `HEADING_6` | ||
- `UL_LIST` | ||
- `OL_LIST` | ||
- `LIST_ITEM` | ||
- `QUOTE` | ||
- `HR` | ||
- `EMBEDDED_ENTRY` | ||
- `EMBEDDED_ASSET` | ||
- `INLINES` | ||
- `EMBEDDED_ENTRY` (this is different from the `BLOCKS.EMBEDDED_ENTRY`) | ||
- `HYPERLINK` | ||
- `ENTRY_HYPERLINK` | ||
- `ASSET_HYPERLINK` | ||
The `renderMark` keys should be one of the following `MARKS` properties as defined in [`@contentful/rich-text-types`](https://www.npmjs.com/package/@contentful/rich-text-types): | ||
* `BOLD` | ||
* `ITALIC` | ||
* `UNDERLINE` | ||
* `CODE` | ||
- `BOLD` | ||
- `ITALIC` | ||
- `UNDERLINE` | ||
- `CODE` |
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
910
69135
168