Skip to content

Commit 329ce3f

Browse files
committed
rename JSON transformer to HTML, get rid of JSON references
1 parent 784a650 commit 329ce3f

File tree

14 files changed

+629
-248
lines changed

14 files changed

+629
-248
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Install the package via npm
2525

2626
![Module API](media/resolver-api-overview.png)
2727

28-
### Parsing rich text HTML to a JSON tree
28+
### Parsing rich text HTML to an array of simplified nodes
2929

30-
The tool provides environment-aware (browser or Node.js) `parseHtml` function to transform HTML into an array of simplified JSON trees. Any valid HTML is parsed, including all attributes. Together with built-in transformation methods, this tool is a suitable option for processing HTML and rich text from external sources, to make it compatible with Kontent.ai rich text format. See dedicated [JSON transformer docs](docs/index.md) for further information.
30+
The tool provides environment-aware (browser or Node.js) `parseHTML` function to transform HTML into an array of `DomNode` trees. Any valid HTML is parsed, including all attributes. Together with built-in transformation methods, this tool is a suitable option for processing HTML and rich text from external sources, to make it compatible with Kontent.ai rich text format. See dedicated [HTML transformer docs](docs/index.md) for further information.
3131

3232
### Portable text resolution
3333

@@ -291,8 +291,7 @@ Package exports a `traversePortableText` method, which accepts an array of `Port
291291
const richTextContent =
292292
`<p>Here is an <a data-item-id="12345"><strong>internal link</strong></a> in some text.</p>`;
293293

294-
const tree = parseHtml(richTextContent);
295-
const portableText = transformToPortableText(tree);
294+
const portableText = transformToPortableText(richTextContent);
296295

297296
// your logic to modify the portable text
298297

@@ -302,7 +301,7 @@ Package exports a `traversePortableText` method, which accepts an array of `Port
302301
> [!IMPORTANT]
303302
> MAPI transformation logic expects Portable Text that had been previously created from management API rich text and performs only minimal validation. It doesn't provide implicit transformation capabilities from other formats (such as delivery API).
304303
>
305-
> If you're interested in transforming external HTML or rich text to a MAPI compatible format, see [JSON transformer docs](docs/index.md) instead.
304+
> If you're interested in transforming external HTML or rich text to a MAPI compatible format, see [HTML transformer docs](docs/index.md) instead.
306305
307306
[last-commit]: https://img.shields.io/github/last-commit/kontent-ai/rich-text-resolver-js?style=for-the-badge
308307
[contributors-shield]: https://img.shields.io/github/contributors/kontent-ai/rich-text-resolver-js?style=for-the-badge

docs/index.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# JSON Transformers
1+
# HTML Transformers
22

3-
This module provides an environment-aware (browser or Node.js) `parseHtml` function to convert an HTML string into an array of nodes. The resulting array can subsequently be modified by one of the provided functions and transformed back to HTML.
3+
This module provides an environment-aware (browser or Node.js) `parseHTML` function to convert an HTML string into an array of nodes. The resulting array can subsequently be modified by one of the provided functions and transformed back to HTML.
44

55
This toolset can be particularly useful for transforming rich text or HTML content from external sources into a valid Kontent.ai rich text format in migration scenarios.
66

77
## Usage
88

9-
Pass stringified HTML to `parseHtml` function to get an array of `DomNode` objects:
9+
Pass stringified HTML to `parseHTML` function to get an array of `DomNode` objects:
1010

1111
```ts
12-
import { parseHtml } from '@kontent-ai/rich-text-resolver';
12+
import { parseHTML } from '@kontent-ai/rich-text-resolver';
1313

1414
const rawHtml = `<p>Hello <strong>World!</strong></p>`;
1515

16-
const parsedNodes = parseHtml(rawHtml);
16+
const parsedNodes = parseHTML(rawHtml);
1717
```
1818

1919
`DomNode` is a union of `DomHtmlNode` and `DomTextNode`, defined as follows:
@@ -36,15 +36,15 @@ export interface DomHtmlNode<TAttributes = Record<string, string | undefined>> {
3636

3737
### HTML Transformation
3838

39-
To transform the `DomNode` array back to HTML, you can use `nodesToHtml` function or its async variant `nodesToHtmlAsync`. The function accepts the parsed array and a `transformers` object, which defines custom transformation for each HTML node. Text nodes are transformed automatically. A wildcard `*` can be used to define fallback transformation for remaining tags. If no explicit or wildcard transformation is provided, default resolution is used.
39+
To transform the `DomNode` array back to HTML, you can use `nodesToHTML` function or its async variant `nodesToHTMLAsync`. The function accepts the parsed array and a `transformers` object, which defines custom transformation for each HTML node. Text nodes are transformed automatically. A wildcard `*` can be used to define fallback transformation for remaining tags. If no explicit or wildcard transformation is provided, default resolution is used.
4040

4141
#### Basic
4242
Basic example of HTML transformation, removing HTML attribute `style` and transforming `b` tag to `strong`:
4343
```ts
44-
import { nodesToHtml, NodeToHtmlMap, parseHtml } from '@kontent-ai/rich-text-resolver';
44+
import { nodesToHTML, NodeToHtmlMap, parseHTML } from '@kontent-ai/rich-text-resolver';
4545

4646
const rawHtml = `<p style="color:red">Hello <b>World!</b></p>`;
47-
const parsedNodes = parseHtml(rawHtml);
47+
const parsedNodes = parseHTML(rawHtml);
4848

4949
const transformers: NodeToHtmlMap = {
5050
// children contains already transformed child nodes
@@ -55,17 +55,17 @@ const transformers: NodeToHtmlMap = {
5555
};
5656

5757
// restores original HTML with attributes
58-
const defaultOutput = nodesToHtml(parsedNodes, {});
58+
const defaultOutput = nodesToHTML(parsedNodes, {});
5959
console.log(defaultOutput); // <p style="color:red">Hello <b>World!</b></p>
6060

6161
// b is converted to strong, wildcard transformation is used for remaining nodes
62-
const customOutput = nodesToHtml(parsedNodes, transformers);
62+
const customOutput = nodesToHTML(parsedNodes, transformers);
6363
console.log(customOutput); // <p>Hello <strong>World!</strong></p>
6464
```
6565

6666
#### Advanced
6767

68-
For more complex scenarios, optional context and its handler can be passed to `nodesToHtml` as third and fourth parameters respectively.
68+
For more complex scenarios, optional context and its handler can be passed to `nodesToHTML` as third and fourth parameters respectively.
6969

7070
The context can then be accessed in individual transformations, defined in the `transformers` object. If you need to dynamically update the context, you may optionally provide a context handler, which accepts current node and context as parameters and passes a cloned, modified context for child node processing, ensuring each node gets valid contextual data.
7171

@@ -76,18 +76,18 @@ In Kontent.ai rich text, images are represented by a `<figure>` tag, with `data-
7676
1. Load the binaries from `src` attribute and create an asset in Kontent.ai asset library
7777
2. Use the asset ID from previous step to reference the asset in the transformed `<figure>` tag.
7878

79-
For that matter, we will use `nodesToHtmlAsync` method and pass an instance of JS SDK `ManagementClient` as context, to perform the asset creation. Since we don't need to modify the client in any way, we can omit the context handler for this example.
79+
For that matter, we will use `nodesToHTMLAsync` method and pass an instance of JS SDK `ManagementClient` as context, to perform the asset creation. Since we don't need to modify the client in any way, we can omit the context handler for this example.
8080

8181
```ts
8282
import { ManagementClient } from "@kontent-ai/management-sdk";
8383
import {
84-
parseHtml,
84+
parseHTML,
8585
AsyncNodeToHtmlMap,
86-
nodesToHtmlAsync,
86+
nodesToHTMLAsync,
8787
} from "@kontent-ai/rich-text-resolver";
8888

8989
const input = `<img src="https://website.com/image.jpg" alt="some image">`;
90-
const nodes = parseHtml(input);
90+
const nodes = parseHTML(input);
9191

9292
// type parameter specifies context type, in this case ManagementClient
9393
const transformers: AsyncNodeToHtmlMap<ManagementClient> = {
@@ -129,7 +129,7 @@ const transformers: AsyncNodeToHtmlMap<ManagementClient> = {
129129
}),
130130
};
131131

132-
const richText = nodesToHtmlAsync(
132+
const richText = nodesToHTMLAsync(
133133
nodes,
134134
transformers,
135135
new ManagementClient({
@@ -153,10 +153,10 @@ In this case, we can store depth as a context and increment it via handler anyti
153153
154154
```ts
155155
import {
156-
nodesToHtml,
156+
nodesToHTML,
157157
DomNode,
158158
NodeToHtmlMap,
159-
parseHtml,
159+
parseHTML,
160160
} from "@kontent-ai/rich-text-resolver";
161161

162162
type DepthContext = {
@@ -170,7 +170,7 @@ const input = `
170170
<div>Another top-level div <span>with text</span></div>
171171
`;
172172

173-
const parsedNodes = parseHtml(input);
173+
const parsedNodes = parseHTML(input);
174174

175175
// handler increments depth whenever we encounter a div or span tag node.
176176
const depthHandler = (node: DomNode, context: DepthContext): DepthContext =>
@@ -189,7 +189,7 @@ const transformers: NodeToHtmlMap<DepthContext> = {
189189
context?.divSpanDepth === 1 ? `<p>${children}</p>` : children,
190190
};
191191

192-
const output = nodesToHtml(
192+
const output = nodesToHTML(
193193
parsedNodes,
194194
transformers,
195195
{ divSpanDepth: 0 }, // initial context

index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export { nodesToHtml, nodesToHtmlAsync, NodeToHtml, NodeToHtmlAsync, NodeToHtmlMap, AsyncNodeToHtmlMap } from './src/index.js';
1+
export { nodesToHTML, nodesToHTMLAsync, NodeToHtml, NodeToHtmlAsync, NodeToHtmlMap, AsyncNodeToHtmlMap } from './src/index.js';
22
export { traversePortableText } from './src/utils/transformer-utils.js';
33
export { transformToPortableText } from "./src/transformers/portable-text-transformer/portable-text-transformer.js";
4-
export { parseHtml } from "./src/parser/index.js";
4+
export { parseHTML } from "./src/parser/index.js";
55
export { resolveImage, resolveTable, toHTMLImageDefault, PortableTextHtmlResolvers, toHTML } from "./src/utils/resolution/html.js";
66
export { resolveImage as resolveImageVue, resolveTable as resolveTableVue, toVueImageDefault } from "./src/utils/resolution/vue.js";
77
export { toManagementApiFormat } from './src/utils/resolution/mapi.js';

0 commit comments

Comments
 (0)