datocms-html-to-structured-text
Advanced tools
Comparing version 0.1.0-alpha.27 to 1.0.1
@@ -49,9 +49,2 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -118,3 +111,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
case 0: | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('heading'); | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('heading') && | ||
context.allowedBlocks.includes('heading'); | ||
return [4 /*yield*/, visit_children_1.default(createNode, node, __assign(__assign({}, context), { parentNodeType: isAllowedChild ? 'heading' : context.parentNodeType, wrapText: isAllowedChild ? false : context.wrapText }))]; | ||
@@ -145,2 +139,5 @@ case 1: | ||
} | ||
if (!context.allowedBlocks.includes('code')) { | ||
return [2 /*return*/, visit_children_1.default(createNode, node, context)]; | ||
} | ||
prefix = typeof context.codePrefix === 'string' ? context.codePrefix : 'language-'; | ||
@@ -190,3 +187,4 @@ isPre = convert_1.default('pre'); | ||
case 0: | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('blockquote'); | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('blockquote') && | ||
context.allowedBlocks.includes('blockquote'); | ||
return [4 /*yield*/, visit_children_1.default(createNode, node, __assign(__assign({}, context), { parentNodeType: isAllowedChild ? 'blockquote' : context.parentNodeType }))]; | ||
@@ -212,3 +210,4 @@ case 1: | ||
case 0: | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('list'); | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('list') && | ||
context.allowedBlocks.includes('list'); | ||
if (!!isAllowedChild) return [3 /*break*/, 2]; | ||
@@ -235,3 +234,4 @@ return [4 /*yield*/, visit_children_1.default(createNode, node, context)]; | ||
case 0: | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('listItem'); | ||
isAllowedChild = datocms_structured_text_utils_1.allowedChildren[context.parentNodeType].includes('listItem') && | ||
context.allowedBlocks.includes('list'); | ||
return [4 /*yield*/, visit_children_1.default(createNode, node, __assign(__assign({}, context), { parentNodeType: isAllowedChild ? 'listItem' : context.parentNodeType }))]; | ||
@@ -259,2 +259,5 @@ case 1: | ||
case 0: | ||
if (!context.allowedBlocks.includes('link')) { | ||
return [2 /*return*/, visit_children_1.default(createNode, node, context)]; | ||
} | ||
isAllowedChild = false; | ||
@@ -314,7 +317,13 @@ if (datocms_structured_text_utils_1.allowedChildren[context.parentNodeType] === 'inlineNodes') { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var marks; | ||
var marks, allowedMarks; | ||
return __generator(this, function (_a) { | ||
marks = Array.isArray(context.marks) | ||
? { marks: __spreadArrays(context.marks) } | ||
: {}; | ||
marks = {}; | ||
if (Array.isArray(context.marks)) { | ||
allowedMarks = context.marks.filter(function (mark) { | ||
return context.allowedMarks.includes(mark); | ||
}); | ||
if (allowedMarks.length > 0) { | ||
marks.marks = allowedMarks; | ||
} | ||
} | ||
return [2 /*return*/, createNode('span', __assign({ value: wrapText(context, node.value) }, marks))]; | ||
@@ -350,7 +359,7 @@ }); | ||
return __generator(this, function (_a) { | ||
if (!context.shared.baseUrlFound && | ||
if (!context.global.baseUrlFound && | ||
typeof node.properties === 'object' && | ||
node.properties.href) { | ||
context.shared.baseUrl = node.properties.href.replace(/\/$/, ''); | ||
context.shared.baseUrlFound = true; | ||
context.global.baseUrl = node.properties.href.replace(/\/$/, ''); | ||
context.global.baseUrlFound = true; | ||
} | ||
@@ -371,2 +380,5 @@ return [2 /*return*/]; | ||
return function markHandler(createNode, node, context) { | ||
if (!context.allowedMarks.includes(type)) { | ||
return visit_children_1.default(createNode, node, context); | ||
} | ||
var marks = { marks: [type] }; | ||
@@ -470,7 +482,7 @@ if (Array.isArray(context.marks)) { | ||
} | ||
if (context.shared.baseUrl && typeof URL !== 'undefined') { | ||
if (context.global.baseUrl && typeof URL !== 'undefined') { | ||
var isRelative = /^\.?\//.test(url); | ||
var parsed = new URL(url, context.shared.baseUrl); | ||
var parsed = new URL(url, context.global.baseUrl); | ||
if (isRelative) { | ||
var parsedBase = new URL(context.shared.baseUrl); | ||
var parsedBase = new URL(context.global.baseUrl); | ||
if (!parsed.pathname.startsWith(parsedBase.pathname)) { | ||
@@ -477,0 +489,0 @@ parsed.pathname = "" + parsedBase.pathname + parsed.pathname; |
@@ -65,4 +65,5 @@ "use strict"; | ||
var hast_util_from_dom_1 = __importDefault(require("hast-util-from-dom")); | ||
function htmlToStructuredText(html, settings) { | ||
if (settings === void 0) { settings = {}; } | ||
var datocms_structured_text_utils_1 = require("datocms-structured-text-utils"); | ||
function htmlToStructuredText(html, options) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -76,3 +77,3 @@ var document, tree; | ||
tree = hast_util_from_dom_1.default(document); | ||
return [2 /*return*/, hastToStructuredText(tree, settings)]; | ||
return [2 /*return*/, hastToStructuredText(tree, options)]; | ||
}); | ||
@@ -82,4 +83,4 @@ }); | ||
exports.htmlToStructuredText = htmlToStructuredText; | ||
function parse5ToStructuredText(document, settings) { | ||
if (settings === void 0) { settings = {}; } | ||
function parse5ToStructuredText(document, options) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -89,3 +90,3 @@ var tree; | ||
tree = hast_util_from_parse5_1.default(document); | ||
return [2 /*return*/, hastToStructuredText(tree, settings)]; | ||
return [2 /*return*/, hastToStructuredText(tree, options)]; | ||
}); | ||
@@ -95,4 +96,4 @@ }); | ||
exports.parse5ToStructuredText = parse5ToStructuredText; | ||
function hastToStructuredText(tree, settings) { | ||
if (settings === void 0) { settings = {}; } | ||
function hastToStructuredText(tree, options) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -103,3 +104,3 @@ var createNode, rootNode; | ||
case 0: | ||
rehype_minify_whitespace_1.default({ newlines: settings.newlines === true })(tree); | ||
rehype_minify_whitespace_1.default({ newlines: options.newlines === true })(tree); | ||
createNode = function (type, props) { | ||
@@ -109,4 +110,4 @@ props.type = type; | ||
}; | ||
if (typeof settings.preprocess === 'function') { | ||
settings.preprocess(tree); | ||
if (typeof options.preprocess === 'function') { | ||
options.preprocess(tree); | ||
} | ||
@@ -117,5 +118,11 @@ return [4 /*yield*/, visit_node_1.default(createNode, tree, { | ||
defaultHandlers: handlers_1.handlers, | ||
handlers: Object.assign({}, handlers_1.handlers, settings.handlers || {}), | ||
handlers: Object.assign({}, handlers_1.handlers, options.handlers || {}), | ||
wrapText: true, | ||
shared: __assign({ baseUrl: null, baseUrlFound: false }, (settings.shared || {})), | ||
allowedBlocks: Array.isArray(options.allowedBlocks) | ||
? options.allowedBlocks | ||
: ['blockquote', 'code', 'heading', 'link', 'list'], | ||
allowedMarks: Array.isArray(options.allowedMarks) | ||
? options.allowedMarks | ||
: datocms_structured_text_utils_1.allowedMarks, | ||
global: __assign({ baseUrl: null, baseUrlFound: false }, (options.shared || {})), | ||
})]; | ||
@@ -122,0 +129,0 @@ case 1: |
@@ -1,14 +0,16 @@ | ||
import { Root, CreateNodeFunction, HastRootNode } from './types'; | ||
import { CreateNodeFunction, HastRootNode } from './types'; | ||
import visitNode from './visit-node'; | ||
import visitChildren from './visit-children'; | ||
import parse5 from 'parse5'; | ||
import { Document } from 'datocms-structured-text-utils'; | ||
export declare type Settings = Partial<{ | ||
import { Document, Mark, BlockquoteType, CodeType, HeadingType, LinkType, ListType } from 'datocms-structured-text-utils'; | ||
export declare type Options = Partial<{ | ||
newlines: boolean; | ||
handlers: Record<string, CreateNodeFunction>; | ||
preprocess: (hast: HastRootNode) => HastRootNode; | ||
allowedBlocks: Array<BlockquoteType | CodeType | HeadingType | LinkType | ListType>; | ||
allowedMarks: Mark[]; | ||
}>; | ||
export declare function htmlToStructuredText(html: string, settings?: Settings): Promise<Root | null>; | ||
export declare function parse5ToStructuredText(document: parse5.Document, settings?: Settings): Promise<Root | null>; | ||
export declare function hastToStructuredText(tree: HastRootNode, settings?: Settings): Promise<Document | null>; | ||
export declare function htmlToStructuredText(html: string, options?: Options): Promise<Document | null>; | ||
export declare function parse5ToStructuredText(document: parse5.Document, options?: Options): Promise<Document | null>; | ||
export declare function hastToStructuredText(tree: HastRootNode, options?: Options): Promise<Document | null>; | ||
export { visitNode, visitChildren }; |
@@ -11,4 +11,4 @@ import { Node, Root, NodeType, Mark } from 'datocms-structured-text-utils'; | ||
parentNode: HastNode; | ||
handlers: Record<string, Handler<unknown>>; | ||
defaultHandlers: Record<string, Handler<unknown>>; | ||
handlers: Record<string, Handler<unknown>>; | ||
wrapText: boolean; | ||
@@ -15,0 +15,0 @@ marks?: Mark[]; |
{ | ||
"name": "datocms-html-to-structured-text", | ||
"version": "0.1.0-alpha.27", | ||
"version": "1.0.1", | ||
"description": "Convert HTML (or Hast syntax tree) to a valid DatoCMS Structured Text Dast document", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
"dependencies": { | ||
"datocms-structured-text-utils": "^0.1.0-alpha.27", | ||
"datocms-structured-text-utils": "^1.0.1", | ||
"extend": "^3.0.2", | ||
@@ -49,3 +49,3 @@ "hast-util-from-dom": "^3.0.0", | ||
}, | ||
"gitHead": "9138f478aedb5acdfe26f9575009250b313f4389" | ||
"gitHead": "6381b05331181aecc82b7f080c6993fc803a7327" | ||
} |
@@ -1,2 +0,2 @@ | ||
# `html-to-structured-text` | ||
# `datocms-html-to-structured-text` | ||
@@ -14,3 +14,3 @@ Convert HTML (or [Hast](https://github.com/syntax-tree/hast) syntax tree) to a valid DatoCMS Structured Text Dast document. | ||
```js | ||
import { htmlToStructuredText } from 'html-to-structured-text'; | ||
import { htmlToStructuredText } from 'datocms-html-to-structured-text'; | ||
@@ -35,3 +35,3 @@ const html = ` | ||
import parse5 from 'parse5'; | ||
import { parse5ToStructuredText } from 'html-to-structured-text'; | ||
import { parse5ToStructuredText } from 'datocms-html-to-structured-text'; | ||
@@ -75,2 +75,22 @@ parse5ToStructuredText( | ||
### Options | ||
All the `*ToStructuredText` utils accept an optional `options` object as second argument. | ||
```js | ||
type Options = Partial<{ | ||
newlines: boolean, | ||
// 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. | ||
preprocess: (hast: HastRootNode) => HastRootNode, | ||
// Array of allowed Block nodes. | ||
allowedBlocks: Array< | ||
BlockquoteType | CodeType | HeadingType | LinkType | ListType, | ||
>, | ||
// Array of allowed marks. | ||
allowedMarks: Mark[], | ||
}>; | ||
``` | ||
### Transforming Nodes | ||
@@ -83,3 +103,3 @@ | ||
```js | ||
import { visitChildren } from 'html-to-structured-text'; | ||
import { visitChildren } from 'datocms-html-to-structured-text'; | ||
@@ -122,6 +142,7 @@ // Handler for the <p> tag. | ||
parentNode: HastNode; | ||
// A reference to the current handlers - merged default + user handlers. | ||
handlers: Record<string, Handler<unknown>>; | ||
// A reference to the default handlers record (map). | ||
defaultHandlers: Record<string, Handler<unknown>>; | ||
// A reference to the current handlers - merged default + user handlers. | ||
handlers: Record<string, Handler<unknown>>; | ||
// true if the content can include newlines, and false if not (such as in headings). | ||
wrapText: boolean; | ||
@@ -134,2 +155,8 @@ // Marks for span nodes. | ||
codePrefix?: string; | ||
// Array of allowed Block types. | ||
allowedBlocks: Array< | ||
BlockquoteType | CodeType | HeadingType | LinkType | ListType, | ||
>; | ||
// Array of allowed marks. | ||
allowedMarks: Mark[]; | ||
// Properties in this object are avaliable to every handler as Context | ||
@@ -143,3 +170,3 @@ // is not deeply cloned. | ||
It is possible to register custom handlers and override the default behavior via settings: | ||
It is possible to register custom handlers and override the default behavior via options: | ||
@@ -146,0 +173,0 @@ ```js |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
80957
1052
0
346
0
+ Addedarray-flatten@3.0.0(transitive)
+ Addeddatocms-structured-text-utils@1.2.0(transitive)
- Removeddatocms-structured-text-utils@0.1.0-alpha.27(transitive)
- Removedlodash.flatten@4.4.0(transitive)