@contentful/rich-text-html-renderer
Advanced tools
Comparing version 16.2.0 to 16.3.0
@@ -23,4 +23,4 @@ "use strict"; | ||
Object.defineProperty(exports, "embeddedEntryDoc", { enumerable: true, get: function () { return __importDefault(embedded_entry_1).default; } }); | ||
var embedded_entry_2 = require("./embedded-entry"); | ||
Object.defineProperty(exports, "embeddedResourceDoc", { enumerable: true, get: function () { return __importDefault(embedded_entry_2).default; } }); | ||
var embedded_resource_1 = require("./embedded-resource"); | ||
Object.defineProperty(exports, "embeddedResourceDoc", { enumerable: true, get: function () { return __importDefault(embedded_resource_1).default; } }); | ||
var ol_1 = require("./ol"); | ||
@@ -27,0 +27,0 @@ Object.defineProperty(exports, "olDoc", { enumerable: true, get: function () { return __importDefault(ol_1).default; } }); |
@@ -34,2 +34,18 @@ "use strict"; | ||
}, | ||
{ | ||
doc: (0, documents_1.headingDoc)(rich_text_types_1.BLOCKS.HEADING_3), | ||
expected: '<h3>hello world</h3>', | ||
}, | ||
{ | ||
doc: (0, documents_1.headingDoc)(rich_text_types_1.BLOCKS.HEADING_4), | ||
expected: '<h4>hello world</h4>', | ||
}, | ||
{ | ||
doc: (0, documents_1.headingDoc)(rich_text_types_1.BLOCKS.HEADING_5), | ||
expected: '<h5>hello world</h5>', | ||
}, | ||
{ | ||
doc: (0, documents_1.headingDoc)(rich_text_types_1.BLOCKS.HEADING_6), | ||
expected: '<h6>hello world</h6>', | ||
}, | ||
]; | ||
@@ -164,3 +180,3 @@ docs.forEach(function (_a) { | ||
sys: { | ||
urn: 'crn:contentful:::content:spaces/6fqi4ljzyr0e/entries/9mpxT4zsRi6Iwukey8KeM', | ||
urn: 'crn:contentful:::content:spaces/6fqi4ljzyr0e/environments/master/entries/9mpxT4zsRi6Iwukey8KeM', | ||
type: 'ResourceLink', | ||
@@ -235,4 +251,4 @@ linkType: 'Contentful:Entry', | ||
id: '9mpxT4zsRi6Iwukey8KeM', | ||
link: 'Link', | ||
type: 'Asset', | ||
type: 'Link', | ||
linkType: 'Asset', | ||
}, | ||
@@ -250,4 +266,4 @@ }, | ||
id: '9mpxT4zsRi6Iwukey8KeM', | ||
link: 'Link', | ||
type: 'Entry', | ||
type: 'Link', | ||
linkType: 'Entry', | ||
}, | ||
@@ -260,2 +276,16 @@ }, | ||
}); | ||
it('renders resource hyperlink', function () { | ||
var entry = { | ||
target: { | ||
sys: { | ||
urn: 'crn:contentful:::content:spaces/6fqi4ljzyr0e/environments/master/entries/9mpxT4zsRi6Iwukey8KeM', | ||
type: 'ResourceLink', | ||
linkType: 'Contentful:Entry', | ||
}, | ||
}, | ||
}; | ||
var document = (0, inline_entity_1.default)(entry, rich_text_types_1.INLINES.RESOURCE_HYPERLINK); | ||
var expected = "<p><span>type: ".concat(rich_text_types_1.INLINES.RESOURCE_HYPERLINK, " urn: ").concat(entry.target.sys.urn, "</span></p>"); | ||
expect((0, index_1.documentToHtmlString)(document)).toEqual(expected); | ||
}); | ||
it('renders embedded entry', function () { | ||
@@ -266,4 +296,4 @@ var entry = { | ||
id: '9mpxT4zsRi6Iwukey8KeM', | ||
link: 'Link', | ||
type: 'Entry', | ||
type: 'Link', | ||
linkType: 'Entry', | ||
}, | ||
@@ -276,2 +306,16 @@ }, | ||
}); | ||
it('renders embedded resource', function () { | ||
var entry = { | ||
target: { | ||
sys: { | ||
urn: 'crn:contentful:::content:spaces/6fqi4ljzyr0e/environments/master/entries/9mpxT4zsRi6Iwukey8KeM', | ||
type: 'Link', | ||
linkType: 'Contentful:Entry', | ||
}, | ||
}, | ||
}; | ||
var document = (0, inline_entity_1.default)(entry, rich_text_types_1.INLINES.EMBEDDED_RESOURCE); | ||
var expected = "<p><span>type: ".concat(rich_text_types_1.INLINES.EMBEDDED_RESOURCE, " urn: ").concat(entry.target.sys.urn, "</span></p>"); | ||
expect((0, index_1.documentToHtmlString)(document)).toEqual(expected); | ||
}); | ||
it('does not crash with empty documents', function () { | ||
@@ -278,0 +322,0 @@ expect((0, index_1.documentToHtmlString)({})).toEqual(''); |
@@ -43,3 +43,9 @@ "use strict"; | ||
_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.RESOURCE_HYPERLINK] = function (node) { | ||
return defaultInlineResource(rich_text_types_1.INLINES.RESOURCE_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.EMBEDDED_RESOURCE] = function (node) { | ||
return defaultInlineResource(rich_text_types_1.INLINES.EMBEDDED_RESOURCE, node); | ||
}, | ||
_a[rich_text_types_1.INLINES.HYPERLINK] = function (node, next) { | ||
@@ -61,2 +67,5 @@ var href = typeof node.data.uri === 'string' ? node.data.uri : ''; | ||
}; | ||
var defaultInlineResource = function (type, node) { | ||
return "<span>type: ".concat((0, escape_html_1.default)(type), " urn: ").concat((0, escape_html_1.default)(node.data.target.sys.urn), "</span>"); | ||
}; | ||
/** | ||
@@ -63,0 +72,0 @@ * Serialize a Contentful Rich Text `document` to an html string. |
@@ -511,3 +511,9 @@ 'use strict'; | ||
_a[dist_4.ENTRY_HYPERLINK] = function (node) { return defaultInline(dist_4.ENTRY_HYPERLINK, node); }, | ||
_a[dist_4.RESOURCE_HYPERLINK] = function (node) { | ||
return defaultInlineResource(dist_4.RESOURCE_HYPERLINK, node); | ||
}, | ||
_a[dist_4.EMBEDDED_ENTRY] = function (node) { return defaultInline(dist_4.EMBEDDED_ENTRY, node); }, | ||
_a[dist_4.EMBEDDED_RESOURCE] = function (node) { | ||
return defaultInlineResource(dist_4.EMBEDDED_RESOURCE, node); | ||
}, | ||
_a[dist_4.HYPERLINK] = function (node, next) { | ||
@@ -529,2 +535,5 @@ var href = typeof node.data.uri === 'string' ? node.data.uri : ''; | ||
}; | ||
var defaultInlineResource = function (type, node) { | ||
return "<span>type: ".concat(escapeHtml_1(type), " urn: ").concat(escapeHtml_1(node.data.target.sys.urn), "</span>"); | ||
}; | ||
/** | ||
@@ -531,0 +540,0 @@ * Serialize a Contentful Rich Text `document` to an html string. |
@@ -9,3 +9,3 @@ export { default as hrDoc } from './hr'; | ||
export { default as embeddedEntryDoc } from './embedded-entry'; | ||
export { default as embeddedResourceDoc } from './embedded-entry'; | ||
export { default as embeddedResourceDoc } from './embedded-resource'; | ||
export { default as olDoc } from './ol'; | ||
@@ -12,0 +12,0 @@ export { default as ulDoc } from './ul'; |
{ | ||
"name": "@contentful/rich-text-html-renderer", | ||
"version": "16.2.0", | ||
"version": "16.3.0", | ||
"main": "dist/rich-text-html-renderer.es5.js", | ||
@@ -47,3 +47,3 @@ "typings": "dist/types/index.d.ts", | ||
}, | ||
"gitHead": "fdf1744e4680b38e9f855a84220c7eead05f4775" | ||
"gitHead": "3664afadae95d9a499abc96985abfaaa5d6967fe" | ||
} |
@@ -166,5 +166,7 @@ # rich-text-html-renderer | ||
- `EMBEDDED_ENTRY` (this is different from the `BLOCKS.EMBEDDED_ENTRY`) | ||
- `EMBEDDED_RESOURCE` | ||
- `HYPERLINK` | ||
- `ENTRY_HYPERLINK` | ||
- `ASSET_HYPERLINK` | ||
- `RESOURCE_HYPERLINK` | ||
@@ -179,1 +181,34 @@ 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): | ||
- `SUBSCRIPT` | ||
#### Preserving Whitespace | ||
In your HTML rendering options, you can utilize the `preserveWhitespace` boolean flag. When set to `true`, this flag ensures that spaces and line breaks in the Contentful rich text content are preserved in the rendered HTML. Specifically, it replaces consecutive spaces with ` ` entities and retains line breaks using `<br />` tags. This capability is particularly beneficial for content that has specific formatting requirements involving spaces and line breaks. | ||
```javascript | ||
import { documentToHtmlString } from '@contentful/rich-text-html-renderer'; | ||
const document = { | ||
nodeType: 'document', | ||
content: [ | ||
{ | ||
nodeType: 'paragraph', | ||
content: [ | ||
{ | ||
nodeType: 'text', | ||
value: 'Hello world!', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
const options = { | ||
preserveWhitespace: true, | ||
}; | ||
documentToHtmlString(document, options); | ||
// -> <p>Hello world!</p> | ||
``` | ||
With this configuration, the HTML output retains the spaces found between "Hello" and "world!". |
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
132952
1798
213