datocms-html-to-structured-text
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -53,3 +53,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resolveUrl = exports.wrapText = exports.wrapListItems = exports.handlers = exports.withMark = exports.noop = exports.extractInlineStyles = exports.base = exports.head = exports.highlight = exports.strikethrough = exports.underline = exports.italic = exports.strong = exports.inlineCode = exports.span = exports.link = exports.listItem = exports.list = exports.blockquote = exports.code = exports.heading = exports.paragraph = exports.root = void 0; | ||
exports.resolveUrl = exports.wrapText = exports.wrapListItems = exports.handlers = exports.withMark = exports.noop = exports.extractInlineStyles = exports.base = exports.head = exports.highlight = exports.strikethrough = exports.underline = exports.italic = exports.strong = exports.inlineCode = exports.span = exports.link = exports.listItem = exports.list = exports.blockquote = exports.code = exports.heading = exports.thematicBreak = exports.paragraph = exports.root = void 0; | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
@@ -105,2 +105,12 @@ // @ts-nocheck | ||
exports.paragraph = paragraph; | ||
var thematicBreak = function thematicBreak(createNode, node, context) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var isAllowedChild; | ||
return __generator(this, function (_a) { | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('thematicBreak'); | ||
return [2 /*return*/, isAllowedChild ? createNode('thematicBreak', {}) : undefined]; | ||
}); | ||
}); | ||
}; | ||
exports.thematicBreak = thematicBreak; | ||
var heading = function heading(createNode, node, context) { | ||
@@ -254,3 +264,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var isAllowedChild, allowedChildrenWrapped, wrapsHeadings, i_1, splitChildren_1, children; | ||
var isAllowedChild, allowedChildrenWrapped, wrapsHeadings, i_1, splitChildren_1, children, props, meta_1; | ||
return __generator(this, function (_a) { | ||
@@ -301,8 +311,24 @@ switch (_a.label) { | ||
if (Array.isArray(children) && children.length) { | ||
return [2 /*return*/, isAllowedChild | ||
? createNode('link', { | ||
url: resolveUrl(context, node.properties.href), | ||
children: children, | ||
}) | ||
: children]; | ||
if (!isAllowedChild) { | ||
return [2 /*return*/, children]; | ||
} | ||
props = { | ||
url: resolveUrl(context, node.properties.href), | ||
children: children, | ||
}; | ||
meta_1 = {}; | ||
if (node.properties) { | ||
if (node.properties.target === '_blank') { | ||
meta_1.openInNewWindow = true; | ||
} | ||
['rel', 'title'].forEach(function (attr) { | ||
if (node.properties[attr]) { | ||
meta_1[attr] = node.properties[attr]; | ||
} | ||
}); | ||
} | ||
if (Object.keys(meta_1).length > 0) { | ||
props.meta = meta_1; | ||
} | ||
return [2 /*return*/, createNode('link', props)]; | ||
} | ||
@@ -476,2 +502,3 @@ return [2 /*return*/, undefined]; | ||
text: exports.span, | ||
hr: exports.thematicBreak, | ||
head: exports.head, | ||
@@ -478,0 +505,0 @@ comment: noop, |
@@ -26,3 +26,6 @@ "use strict"; | ||
if (isGDocsNode) { | ||
if ('children' in parent && 'children' in node) { | ||
if ('children' in parent && | ||
'children' in node && | ||
parent.children && | ||
node.children) { | ||
// Remove google docs tag. | ||
@@ -29,0 +32,0 @@ (_a = parent.children).splice.apply(_a, __spreadArrays([index, 1], node.children)); |
import { Handler, Mark, Context, HastTextNode, HastElementNode, HastRootNode } from './types'; | ||
export declare const root: Handler<HastRootNode>; | ||
export declare const paragraph: Handler<HastElementNode>; | ||
export declare const thematicBreak: Handler<HastElementNode>; | ||
export declare const heading: Handler<HastElementNode>; | ||
@@ -60,2 +61,3 @@ export declare const code: Handler<HastElementNode>; | ||
text: Handler<HastTextNode>; | ||
hr: Handler<HastElementNode>; | ||
head: Handler<HastElementNode>; | ||
@@ -62,0 +64,0 @@ comment: typeof noop; |
@@ -14,5 +14,5 @@ import { Node, Root, NodeType, Mark } from 'datocms-structured-text-utils'; | ||
export interface Context { | ||
/** The current parent Dast node type. */ | ||
/** The parent `dast` node type. */ | ||
parentNodeType: NodeType; | ||
/** The parent Hast node. */ | ||
/** The parent `hast` node. */ | ||
parentNode: HastNode; | ||
@@ -19,0 +19,0 @@ /** A reference to the current handlers - merged default + user handlers. */ |
{ | ||
"name": "datocms-html-to-structured-text", | ||
"version": "1.0.4", | ||
"description": "Convert HTML (or Hast syntax tree) to a valid DatoCMS Structured Text Dast document", | ||
"version": "1.0.5", | ||
"description": "Convert HTML (or a `hast` syntax tree) to a valid DatoCMS Structured Text `dast` document", | ||
"keywords": [ | ||
@@ -34,3 +34,3 @@ "datocms", | ||
"dependencies": { | ||
"datocms-structured-text-utils": "^1.0.4", | ||
"datocms-structured-text-utils": "^1.0.5", | ||
"extend": "^3.0.2", | ||
@@ -49,3 +49,3 @@ "hast-util-from-dom": "^3.0.0", | ||
}, | ||
"gitHead": "7f39864c4987d37a2676c06def2eaf914dacf84f" | ||
"gitHead": "f03a62f2d53133ebbadd3266c0b9116489688bdf" | ||
} |
# `datocms-html-to-structured-text` | ||
Convert HTML (or [Hast](https://github.com/syntax-tree/hast) syntax tree) to a valid DatoCMS Structured Text Dast document. | ||
This package contains utilities to convert HTML (or a [Hast](https://github.com/syntax-tree/hast) to a DatoCMS Structured Text `dast` (DatoCMS Abstract Syntax Tree) document. | ||
Dast stands for Dato Abstract Syntax Tree. | ||
Please refer to [the `dast` format docs](https://www.datocms.com/docs/structured-text/dast) to learn more about the syntax tree format and the available nodes. | ||
## Usage | ||
The main utility is `htmlToStructuredText` which takes a string of HTML and transforms it into a valid Dast. | ||
The main utility in this package is `htmlToStructuredText` which takes a string of HTML and transforms it into a valid `dast` document. | ||
`htmlToStructuredText` returns a `Promise` that resolves with a `Dast`. | ||
`htmlToStructuredText` returns a `Promise` that resolves with a Structured Text document. | ||
@@ -45,9 +45,9 @@ ```js | ||
Internally, both utilities work on a Hast. Should you have an Hast already you can use a third utility called `hastToDast`. | ||
Internally, both utilities work on a [Hast](https://github.com/syntax-tree/hast). Should you have a `hast` already you can use a third utility called `hastToDast`. | ||
## Valid Dast | ||
## Validate `dast` documents | ||
Dast is a strict format that is compliant with DatoCMS' Structured Text records. As such the resulting document is generally a simplified, content-centric version of the input HTML. | ||
`dast` is a strict format for DatoCMS' Structured Text fields. As such the resulting document is generally a simplified, content-centric version of the input HTML. | ||
When possible, the library relies on semantic HTML to generate a valid Dast document. | ||
When possible, the library relies on semantic HTML to generate a valid `dast` document. | ||
@@ -70,3 +70,3 @@ The `datocms-structured-text-utils` package provides a `validate` utility to validate a value to make sure that the resulting tree is compatible with DatoCMS' Structured Text field. | ||
We recommend to validate every Dast to avoid errors later when creating records. | ||
We recommend to validate every `dast` to avoid errors later when creating records. | ||
@@ -77,3 +77,3 @@ ## Advanced Usage | ||
All the `*ToStructuredText` utils accept an optional `options` object as second argument. | ||
All the `*ToStructuredText` utils accept an optional `options` object as second argument: | ||
@@ -83,5 +83,5 @@ ```js | ||
newlines: boolean, | ||
// Override existing Hast node handlers or add new ones. | ||
// Override existing `hast` node handlers or add new ones. | ||
handlers: Record<string, CreateNodeFunction>, | ||
// Allows to tweak the Hast tree before transforming it to a Dast document. | ||
// Allows to tweak the `hast` tree before transforming it to a `dast` document. | ||
preprocess: (hast: HastRootNode) => HastRootNode, | ||
@@ -99,5 +99,5 @@ // Array of allowed Block nodes. | ||
This library traverses a Hast tree and transforms supported nodes to Dast nodes. The transformation is done by working on a Hast node with a handler (async) function. | ||
The utils in this library traverse a `hast` tree and transform supported nodes to `dast` nodes. The transformation is done by working on a `hast` node with a handler (async) function. | ||
Handlers are associated to Hast nodes by `tagName` or `type` when `node.type !== 'element'` and look as follow: | ||
Handlers are associated to `hast` nodes by `tagName` or `type` when `node.type !== 'element'` and look as follow: | ||
@@ -115,7 +115,7 @@ ```js | ||
Handlers can return either a promise that resolves to a Dast node, an array of Dast Nodes or `undefined` to skip the current node. | ||
Handlers can return either a promise that resolves to a `dast` node, an array of `dast` Nodes or `undefined` to skip the current node. | ||
To ensure that a valid Dast is generated the default handlers also check that the current `hastNode` is a valid Dast node for its parent and, if not, they ignore the current node and continue visiting its children. | ||
To ensure that a valid `dast` is generated the default handlers also check that the current `hastNode` is a valid `dast` node for its parent and, if not, they ignore the current node and continue visiting its children. | ||
Information about the parent Dast node name is available in `context.name`. | ||
Information about the parent `dast` node name is available in `context.parentNodeType`. | ||
@@ -140,5 +140,5 @@ Please take a look at the [default handlers implementation](./handlers.ts) for examples. | ||
export interface Context { | ||
// The current parent Dast node type. | ||
// The current parent `dast` node type. | ||
parentNodeType: NodeType; | ||
// The parent Hast node. | ||
// The parent `hast` node. | ||
parentNode: HastNode; | ||
@@ -185,9 +185,9 @@ // A reference to the current handlers - merged default + user handlers. | ||
It is **highly encouraged** to validate the Dast when using custom handlers as handlers are responsible for dictating valid parent-children relationships and therefore generating a tree that is compliant with DatoCMS' Structured Text. | ||
It is **highly encouraged** to validate the `dast` when using custom handlers because handlers are responsible for dictating valid parent-children relationships and therefore generating a tree that is compliant with DatoCMS' Structured Text. | ||
## preprocessing | ||
Because of the strictness of the Dast spec it is possible that some semantic or elements might be lost during the transformation. | ||
Because of the strictness of the `dast` spec it is possible that some semantic or elements might be lost during the transformation. | ||
To improve the final result, you might want to modify the Hast before it is transformed to Dast with the `preprocess` hook. | ||
To improve the final result, you might want to modify the `hast` before it is transformed to `dast` with the `preprocess` hook. | ||
@@ -219,3 +219,3 @@ ```js | ||
In Dast images can be presented as `Block` nodes but these are not allowed inside of `ListItem` nodes (ul/ol lists). In this example we will split the list in 3 pieces and lift up the image. | ||
In `dast` images can be presented as `Block` nodes but these are not allowed inside of `ListItem` nodes (ul/ol lists). In this example we will split the list in 3 pieces and lift up the image. | ||
@@ -350,3 +350,3 @@ The same approach can be used to split other types of branches and lift up nodes to become root nodes. | ||
To work with Hast and Dast trees we recommend using the [unist-utils-core](https://www.npmjs.com/package/unist-utils-core) library. | ||
To work with `hast` and `dast` trees we recommend using the [unist-utils-core](https://www.npmjs.com/package/unist-utils-core) library. | ||
@@ -353,0 +353,0 @@ ## License |
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
91827
1192