slate-serializers
Advanced tools
Comparing version 0.1.0 to 0.2.0
import { ChildNode, Element } from 'domhandler'; | ||
interface MarkTagTransform { | ||
[key: string]: ({ node, attribs, }: { | ||
[key: string]: ({ node, attribs }: { | ||
node?: any; | ||
@@ -5,0 +5,0 @@ attribs?: { |
@@ -37,13 +37,12 @@ "use strict"; | ||
const markElements = []; | ||
const markTransformKeys = (0, utilities_1.intersection)(config.markTransforms || {}, node); | ||
markTransformKeys.map((key) => { | ||
var _a; | ||
if ((_a = config.markTransforms) === null || _a === void 0 ? void 0 : _a[key]) { | ||
markElements.push(config.markTransforms[key]({ node, attribs: {} })); | ||
} | ||
}); | ||
Object.keys(config.markMap).forEach((key) => { | ||
if (node[key]) { | ||
const elements = config.markMap[key] | ||
.map((tagName) => { | ||
var _a; | ||
// more complex transforms | ||
if ((_a = config.markTransforms) === null || _a === void 0 ? void 0 : _a[tagName]) { | ||
return config.markTransforms[tagName]({ node, attribs: {} }); | ||
} | ||
return new domhandler_1.Element(tagName, {}, []); | ||
}); | ||
const elements = config.markMap[key].map((tagName) => new domhandler_1.Element(tagName, {}, [])); | ||
markElements.push(...elements); | ||
@@ -50,0 +49,0 @@ } |
@@ -1,7 +0,7 @@ | ||
import { AnyNode } from 'domhandler'; | ||
import { SlateToDomConfig } from '../..'; | ||
type SlateToHtml = (node: any[], config?: SlateToDomConfig) => string; | ||
type SlateToDom = (node: any[], config?: SlateToDomConfig) => AnyNode | ArrayLike<AnyNode>; | ||
export declare const slateToHtml: SlateToHtml; | ||
export declare const slateToDom: SlateToDom; | ||
import { Config } from '../../config/slateToDom/types'; | ||
interface ISlateToReact { | ||
node: any[]; | ||
config?: Config; | ||
} | ||
export declare const SlateToReact: ({ node, config }: ISlateToReact) => any; | ||
export {}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.slateToDom = exports.slateToHtml = void 0; | ||
exports.SlateToReact = void 0; | ||
const react_1 = __importDefault(require("react")); | ||
const domhandler_1 = require("domhandler"); | ||
const dom_serializer_1 = require("dom-serializer"); | ||
const domutils_1 = require("domutils"); | ||
const html_entities_1 = require("html-entities"); | ||
const slate_1 = require("slate"); | ||
const default_1 = require("../../config/slateToDom/default"); | ||
const domhandler_2 = require("../../utilities/domhandler"); | ||
const utilities_1 = require("../../utilities"); | ||
const slateToHtml = (node, config = default_1.config) => { | ||
const document = (0, exports.slateToDom)(node, config); | ||
return (0, dom_serializer_1.default)(document, { | ||
encodeEntities: 'encodeEntities' in config ? config.encodeEntities : false, | ||
}); | ||
}; | ||
exports.slateToHtml = slateToHtml; | ||
const slateToDom = (node, config = default_1.config) => { | ||
const convert_slate_1 = require("../../utilities/convert-slate"); | ||
const SlateToReact = ({ node, config = default_1.config }) => { | ||
if (!Array.isArray(node)) { | ||
return new domhandler_1.Document([]); | ||
return react_1.default.createElement(react_1.default.Fragment, null); | ||
} | ||
const document = node.map((n, index) => slateNodeToHtml(n, config, index === node.length - 1)); | ||
const document = node.map((n, index) => (0, convert_slate_1.convertSlate)({ | ||
node: n, | ||
config, | ||
isLastNodeInDocument: index === node.length - 1, | ||
transformText: (text) => { | ||
if ((0, domhandler_1.isTag)(text)) { | ||
return react_1.default.createElement((0, domutils_1.getName)(text), {}, (0, domutils_1.textContent)(text)); | ||
} | ||
return react_1.default.createElement(react_1.default.Fragment, null, (0, domutils_1.textContent)(text)); | ||
}, | ||
transformElement: (element) => domElementToReactElement(element), | ||
wrapChildren: (children) => children | ||
})); | ||
return document; | ||
}; | ||
exports.slateToDom = slateToDom; | ||
const slateNodeToHtml = (node, config = default_1.config, isLastNodeInDocument = false) => { | ||
if (slate_1.Text.isText(node)) { | ||
const str = node.text; | ||
// convert line breaks to br tags | ||
const strLines = config.convertLineBreakToBr ? str.split('\n') : [str]; | ||
const textChildren = []; | ||
strLines.forEach((line, index) => { | ||
const markElements = []; | ||
Object.keys(config.markMap).forEach((key) => { | ||
if (node[key]) { | ||
markElements.push(...config.markMap[key]); | ||
} | ||
}); | ||
// clone markElements (it gets modified) | ||
const markElementsClone = [...markElements]; | ||
const textElement = (0, domhandler_2.nestedMarkElements)(markElements, new domhandler_1.Text(line)); | ||
if (config.alwaysEncodeCodeEntities && | ||
config.encodeEntities === false && | ||
(0, domhandler_1.isTag)(textElement) && | ||
(0, domutils_1.getName)(textElement) === 'pre') { | ||
textChildren.push((0, domhandler_2.nestedMarkElements)(markElementsClone, new domhandler_1.Text((0, html_entities_1.encode)(line)))); | ||
} | ||
else { | ||
textChildren.push(textElement); | ||
} | ||
if (index < strLines.length - 1) { | ||
textChildren.push(new domhandler_1.Element('br', {})); | ||
} | ||
}); | ||
return new domhandler_1.Document(textChildren); | ||
} | ||
const children = node.children ? node.children.map((n) => slateNodeToHtml(n, config)) : []; | ||
let attribs = {}; | ||
const styleAttrs = {}; | ||
const style = (0, utilities_1.getNested)(config, 'elementStyleMap'); | ||
if (style) { | ||
Object.keys(style).forEach((slateKey) => { | ||
const cssProperty = style[slateKey]; | ||
const cssValue = node[slateKey]; | ||
if (cssValue) { | ||
styleAttrs[cssProperty] = cssValue; | ||
} | ||
}); | ||
if (!(0, utilities_1.isEmptyObject)(styleAttrs)) { | ||
attribs = { | ||
...attribs, | ||
style: (0, utilities_1.styleToString)(styleAttrs), | ||
}; | ||
} | ||
} | ||
let element = null; | ||
// more complex transforms | ||
if (config.elementTransforms[node.type]) { | ||
element = config.elementTransforms[node.type]({ node, attribs, children }); | ||
} | ||
// straightforward node to element | ||
if (!element && config.elementMap[node.type]) { | ||
element = new domhandler_1.Element(config.elementMap[node.type], attribs, children); | ||
} | ||
// default tag | ||
if (!element && config.defaultTag && !node.type) { | ||
element = new domhandler_1.Element(config.defaultTag, {}, children); | ||
} | ||
if (element) { | ||
return element; | ||
} | ||
// add line break between inline nodes | ||
if (config.convertLineBreakToBr && !isLastNodeInDocument) { | ||
children.push(new domhandler_1.Element('br', {})); | ||
} | ||
return new domhandler_1.Document(children); | ||
exports.SlateToReact = SlateToReact; | ||
const domElementToReactElement = (element) => { | ||
return react_1.default.createElement((0, domutils_1.getName)(element), { className: 'greeting' }, element.children); | ||
}; |
@@ -19,1 +19,6 @@ export declare const hasLineBreak: (str: string) => boolean; | ||
export declare const encodeBreakingEntities: (str: string) => string; | ||
export declare const intersection: (o1: { | ||
[key: string]: any; | ||
}, o2: { | ||
[key: string]: any; | ||
}) => string[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.encodeBreakingEntities = exports.getNested = exports.removeEmpty = exports.isEmptyObject = exports.styleToString = exports.parseStyleCssText = exports.prependSpace = exports.hasLineBreak = void 0; | ||
exports.intersection = exports.encodeBreakingEntities = exports.getNested = exports.removeEmpty = exports.isEmptyObject = exports.styleToString = exports.parseStyleCssText = exports.prependSpace = exports.hasLineBreak = void 0; | ||
const hasLineBreak = (str) => str.match(/[\r\n]+/) !== null; | ||
@@ -78,1 +78,13 @@ exports.hasLineBreak = hasLineBreak; | ||
exports.encodeBreakingEntities = encodeBreakingEntities; | ||
const intersection = (o1, o2) => { | ||
return Object.keys(o1) | ||
.concat(Object.keys(o2)) | ||
.sort() | ||
.reduce((r, a, i, aa) => { | ||
if (i && aa[i - 1] === a) { | ||
r.push(a); | ||
} | ||
return r; | ||
}, []); | ||
}; | ||
exports.intersection = intersection; |
{ | ||
"name": "slate-serializers", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Serialize Slate JSON objects to HTML and vice versa. Define rules to modify the end result.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
47838
40
1143