Skip to content

Commit b9491e5

Browse files
authored
Merge pull request #258 from oleast/feat-default-tag-converter--rebase
2 parents 17c277c + 28fc09e commit b9491e5

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,38 @@ const converter = (node, next) => ({
120120
});
121121
```
122122

123-
Skipping an element can be done by returning the result of the `next`-function. Ignoring an element AND its' children can be done by just returning an empty array. **Skipping is the default behavior of any tag that is not supported.**
123+
Skipping an element can be done by returning the result of the `next`-function. Ignoring an element AND its' children can be done by just returning an empty array.
124124

125125
```typescript
126126
const skippingConverter = (node, next) => next(node);
127127

128128
const ignoringConverter = (node, next) => [];
129129
```
130130

131+
### Unsupported tags
132+
133+
Skipping an element is the default behavior of any tag that is not supported. However, this can be overridden as follows:
134+
135+
```typescript
136+
import {
137+
htmlStringToDocument,
138+
Options,
139+
TagConverter,
140+
} from "contentful-rich-text-html-parser";
141+
import { convertTagToChildren } from "contentful-rich-text-html-parser/converters";
142+
143+
const logAndConvertTagToChildren: TagConverter = (node, next) => {
144+
console.log(`Unsupported tag: ${node.tagName}`);
145+
return convertTagToChildren(node, next); // skip element
146+
};
147+
148+
const options: Options = {
149+
defaultTagConverter: logAndConvertTagToChildren,
150+
};
151+
152+
htmlStringToDocument(htmlString, options);
153+
```
154+
131155
### Example: Change all "div" elements to "p" elements
132156

133157
```typescript

src/htmlStringToDocument.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const mapHtmlNodeToRichTextNode = (
5858
marks: Mark[],
5959
options: OptionsWithDefaults,
6060
) => {
61-
const { convertText, convertTag } = options;
61+
const { convertText, convertTag, defaultTagConverter } = options;
6262

6363
const mapChildren: Next = (node, mark) => {
6464
const newMarks = mark ? getAsList(mark) : [];
@@ -76,7 +76,7 @@ const mapHtmlNodeToRichTextNode = (
7676
return convertText(node, marks);
7777
}
7878

79-
const tagConverter = convertTag?.[node.tagName] ?? convertTagToChildren;
79+
const tagConverter = convertTag[node.tagName] ?? defaultTagConverter;
8080
const convertedNode = tagConverter(node, next);
8181
return convertedNode;
8282
};
@@ -90,6 +90,7 @@ export const htmlStringToDocument = (
9090
...DEFAULT_TAG_CONVERTERS,
9191
...options.convertTag,
9292
},
93+
defaultTagConverter: options.defaultTagConverter ?? convertTagToChildren,
9394
convertText: options.convertText ?? convertTextNodeToText,
9495
parserOptions: {
9596
handleWhitespaceNodes:

src/test/index.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,46 @@ describe("Parse with custom converter functions", () => {
8888
});
8989
});
9090

91+
describe("Parse unsupported tags", () => {
92+
it("Skips element by default", () => {
93+
const matchText = "This is text in a custom tag";
94+
const htmlNodes = htmlStringToDocument(
95+
`<p><custom-tag>${matchText}</custom-tag></p>`,
96+
);
97+
98+
const matchNode = createDocumentNode([
99+
helpers.createBlock(BLOCKS.PARAGRAPH, helpers.createText(matchText)),
100+
] as TopLevelBlock[]);
101+
102+
expect(htmlNodes).toMatchObject(matchNode);
103+
expect(validateRichTextDocument(htmlNodes).length).toEqual(0);
104+
});
105+
106+
it("Uses default converter when specified", () => {
107+
const toParagraphConverter: TagConverter<Block> = (node, next) => {
108+
return {
109+
nodeType: BLOCKS.PARAGRAPH,
110+
content: next(node),
111+
data: {},
112+
};
113+
};
114+
const matchText = "This is text in a custom tag";
115+
const htmlNodes = htmlStringToDocument(
116+
`<custom-tag>${matchText}</custom-tag>`,
117+
{
118+
defaultTagConverter: toParagraphConverter,
119+
},
120+
);
121+
122+
const matchNode = createDocumentNode([
123+
helpers.createBlock(BLOCKS.PARAGRAPH, helpers.createText(matchText)),
124+
] as TopLevelBlock[]);
125+
126+
expect(htmlNodes).toMatchObject(matchNode);
127+
expect(validateRichTextDocument(htmlNodes).length).toEqual(0);
128+
});
129+
});
130+
91131
describe("Processing top level inline nodes to valid formats", () => {
92132
it("Keeps an invalid top level inline node by default", () => {
93133
const htmlNodes = htmlStringToDocument(

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export interface PostProcessingOptions {
7070

7171
export interface OptionsWithDefaults {
7272
convertTag: ConvertTagOptions;
73+
defaultTagConverter: TagConverter;
7374
convertText: TextConverter;
7475
parserOptions: ParserOptions;
7576
postProcessing: PostProcessingOptions;

0 commit comments

Comments
 (0)