datocms-html-to-structured-text
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -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.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.paragraph = exports.root = void 0; | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
@@ -366,2 +366,47 @@ // @ts-nocheck | ||
exports.base = base; | ||
var extractInlineStyles = function extractInlineStyles(createNode, node, context) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var marks, newMarks_1; | ||
return __generator(this, function (_a) { | ||
marks = { marks: Array.isArray(context.marks) ? context.marks : [] }; | ||
if (node.properties && typeof node.properties.style === 'string') { | ||
newMarks_1 = []; | ||
node.properties.style.split(';').forEach(function (declaration) { | ||
var _a = declaration.split(':'), firstChunk = _a[0], otherChunks = _a.slice(1); | ||
var prop = firstChunk.trim(); | ||
var value = otherChunks.join(':').trim(); | ||
switch (prop) { | ||
case 'font-weight': | ||
if (value === 'bold' || Number(value) > 400) { | ||
newMarks_1.push('strong'); | ||
} | ||
break; | ||
case 'font-style': | ||
if (value === 'italic') { | ||
newMarks_1.push('emphasis'); | ||
} | ||
break; | ||
case 'text-decoration': | ||
if (value === 'underline') { | ||
newMarks_1.push('underline'); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
if (newMarks_1.length > 0) { | ||
marks.marks = marks.marks.concat(newMarks_1.filter(function (mark) { | ||
return !marks.marks.includes(mark) && context.allowedMarks.includes(mark); | ||
})); | ||
} | ||
} | ||
if (marks.marks.length === 0) { | ||
marks = {}; | ||
} | ||
return [2 /*return*/, visit_children_1.default(createNode, node, __assign(__assign({}, context), marks))]; | ||
}); | ||
}); | ||
}; | ||
exports.extractInlineStyles = extractInlineStyles; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/explicit-module-boundary-types | ||
@@ -427,2 +472,3 @@ function noop() { | ||
base: exports.base, | ||
span: exports.extractInlineStyles, | ||
text: exports.span, | ||
@@ -429,0 +475,0 @@ head: exports.head, |
@@ -13,47 +13,5 @@ "use strict"; | ||
function preprocessGoogleDocs(tree) { | ||
var gdocsBranches = unist_utils_core_1.findAll(tree, isGoogleDocsNode); | ||
gdocsBranches.forEach(function (branch) { | ||
unist_utils_core_1.visit(branch, (function (node) { | ||
if (node.type !== 'element' || | ||
node.tagName !== 'span' || | ||
typeof node.properties !== 'object' || | ||
typeof node.properties.style !== 'string') { | ||
return; | ||
} | ||
var markTags = []; | ||
node.properties.style.split(';').forEach(function (declaration) { | ||
var _a = declaration.split(':'), firstChunk = _a[0], otherChunks = _a.slice(1); | ||
var prop = firstChunk.trim(); | ||
var gdocsMark = otherChunks.join(':').trim(); | ||
switch (prop) { | ||
case 'font-weight': | ||
if (gdocsMark === 'bold' || Number(gdocsMark) > 400) { | ||
markTags.push('strong'); | ||
} | ||
break; | ||
case 'font-style': | ||
if (gdocsMark === 'italic') { | ||
markTags.push('em'); | ||
} | ||
break; | ||
case 'text-decoration': | ||
if (gdocsMark === 'underline') { | ||
markTags.push('u'); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
markTags.reverse().forEach(function (markTagName) { | ||
node.children = node.children.map(function (child) { | ||
return { | ||
type: 'element', | ||
tagName: markTagName, | ||
children: [child], | ||
}; | ||
}); | ||
}); | ||
})); | ||
}); | ||
// Remove Google docs <b> tags. | ||
// Inline styles are already handled by the extractInlineStyles handler in handlers.ts | ||
unist_utils_core_1.findAll(tree, isGoogleDocsNode); | ||
} | ||
@@ -64,2 +22,3 @@ exports.default = preprocessGoogleDocs; | ||
var isGDocsNode = node.type === 'element' && | ||
node.tagName === 'b' && | ||
typeof node.properties === 'object' && | ||
@@ -66,0 +25,0 @@ typeof node.properties.id === 'string' && |
@@ -19,2 +19,3 @@ import { Handler, Mark, Context, HastTextNode, HastElementNode, HastRootNode } from './types'; | ||
export declare const base: Handler<HastElementNode>; | ||
export declare const extractInlineStyles: Handler<HastElementNode>; | ||
export declare function noop(): Promise<void>; | ||
@@ -58,2 +59,3 @@ export declare function withMark(type: Mark): Handler<HastElementNode>; | ||
base: Handler<HastElementNode>; | ||
span: Handler<HastElementNode>; | ||
text: Handler<HastTextNode>; | ||
@@ -60,0 +62,0 @@ head: Handler<HastElementNode>; |
@@ -5,13 +5,33 @@ import { Node, Root, NodeType, Mark } from 'datocms-structured-text-utils'; | ||
export interface GlobalContext { | ||
/** | ||
* Whether the library has found a <base> tag or should not look further. | ||
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base | ||
*/ | ||
baseUrlFound?: boolean; | ||
/** <base> tag url. This is used for resolving relative URLs. */ | ||
baseUrl?: string; | ||
} | ||
export interface Context { | ||
/** The current parent Dast node type. */ | ||
parentNodeType: NodeType; | ||
/** The parent Hast node. */ | ||
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>>; | ||
/** true if the content can include newlines, and false if not (such as in headings). */ | ||
wrapText: boolean; | ||
/** Marks for span nodes. */ | ||
marks?: Mark[]; | ||
/** | ||
* Prefix for language detection in code blocks. | ||
* | ||
* Detection is done on a class name eg class="language-html". | ||
* Default is `language-`. | ||
*/ | ||
codePrefix?: string; | ||
/** Properties in this object are avaliable to every handler as Context | ||
* is not deeply cloned. | ||
*/ | ||
global: GlobalContext; | ||
@@ -18,0 +38,0 @@ } |
{ | ||
"name": "datocms-html-to-structured-text", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"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": "^1.0.1", | ||
"datocms-structured-text-utils": "^1.0.3", | ||
"extend": "^3.0.2", | ||
@@ -49,3 +49,3 @@ "hast-util-from-dom": "^3.0.0", | ||
}, | ||
"gitHead": "2c34fbd150cd515645bcac40cdb2ccebb8ae51de" | ||
"gitHead": "a1f48c8cc4e30209eccb138d5c60eea8941eaddc" | ||
} |
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
89203
24
1160