You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-51Lines changed: 48 additions & 51 deletions
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,10 @@ Install the package via npm
21
21
22
22
## Features
23
23
24
+
### API Overview
25
+
26
+

27
+
24
28
### Parsing rich text HTML to a JSON tree
25
29
26
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.
@@ -35,6 +39,11 @@ The tool provides environment-aware (browser or Node.js) `parseHtml` function to
> This module re-exports modified `toHTML` function and `<PortableText>` component from `to-html` and `react-portabletext` packages respectively. These modified helpers provide default resolution for tags which are either unsupported or only partially supported in the original packages (`sub` and `sup` tags, images, tables and links).
44
+
>
45
+
> Make sure to use these re-exports if you want to take advantage of the default resolution.
46
+
38
47
The tool provides `transformToPortableText` function to convert rich text content into an array of Portable Text blocks, with custom blocks defined for Kontent.ai-specific objects.
39
48
40
49
Combined with a suitable package for the framework of your choice, this makes for an optimal solution for resolving rich text.
> * React: `TableComponent` component, accepting `PortableTextTable` as a prop.
76
-
> * HTML: `resolveTable` function, accepting `PortableTextTable` and an optional custom resolver.
77
-
> * Vue: `resolveTableVue` function, accepting `PortableTextTable`, Vue render function and an optional custom resolver.
78
-
79
76
## Examples
80
77
81
-
### Modifying portable text nodes
82
-
83
-
Package exports a `traversePortableText` method, which accepts an array of `PortableTextObject` and a callback function. The method recursively traverses all nodes and their subnodes, optionally modifying them with the provided callback:
Package exports a `traversePortableText` method, which accepts an array of `PortableTextObject` and a callback function. The method recursively traverses all nodes and their subnodes, optionally modifying them with the provided callback:
`toManagementApiFormat` is a custom transformation method built upon `toHTML` package, allowing you to restore portable text previously created from management API rich text back into MAPI supported format.
> MAPI transformation logic expects Portable Text that had been previously created from management API rich text and performs only minimal validation.
303
+
> 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).
307
304
>
308
-
> Transformation from other formats (such as delivery API) is not supported unless the blocks are manually adjusted to be MAPI compatible prior to running the method.
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.
Copy file name to clipboardExpand all lines: docs/index.md
+11-63Lines changed: 11 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# JSON Transformers
2
2
3
-
This module provides an environment-aware (browser or Node.js) `parseHtml` function to convert an HTML string into an array of nodes. The JSON structure can subsequently be transformed using one of the provided transformation methods, either to a modified HTML string or a completely different structure, both in synchronous and asynchronous manner.
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.
4
4
5
5
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.
The resulting array can be transformed using one of the functions included in the module.
38
-
39
37
### HTML Transformation
40
38
41
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.
42
40
43
41
#### Basic
44
42
Basic example of HTML transformation, removing HTML attribute `style` and transforming `b` tag to `strong`:
For more complex scenarios, optional context and its handler can be passed to the top level transformation function (`nodesToHtml` or its async variant) 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.
69
69
70
70
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.
71
71
@@ -82,15 +82,15 @@ For that matter, we will use `nodesToHtmlAsync` method and pass an instance of J
// we'll only define transformations for 'div' and 'span'. Default resolution will transform remaining tags.
183
183
div: (_, children, context) =>
184
184
// topmost div is at depth=1, as context is updated before processing.
@@ -201,56 +201,4 @@ console.log(output);
201
201
202
202
```
203
203
204
-
### Generic transformation
205
-
206
-
Should you need to transform the nodes to a different structure, rather than HTML (string), you can use `transformNodes` (`transformNodesAsync`) function. Its usage is similar but revolves around generics and provides further control over the output, such as specifying custom transformation for text nodes.
207
-
208
-
Snippet showcasing use of `transformNodes` to convert the `DomNode` array into Portable Text, as used internally in this module. Full source code in [the corresponding TS file](../src/transformers/portable-text-transformer/portable-text-transformer.ts).
209
-
210
-
```ts
211
-
// context stores current list type and list item depth
0 commit comments