slate-serializers
Advanced tools
Comparing version 0.0.3 to 0.0.5
@@ -37,33 +37,33 @@ "use strict"; | ||
}; | ||
const deserialize = (el) => { | ||
if (el.type === htmlparser2_1.ElementType.Text) { | ||
const text = (0, domutils_1.textContent)(el); | ||
return text.trim() ? text : null; | ||
} | ||
if (el.type !== htmlparser2_1.ElementType.Tag) { | ||
const deserialize = (el, index) => { | ||
if (el.type !== htmlparser2_1.ElementType.Tag && el.type !== htmlparser2_1.ElementType.Text) { | ||
return null; | ||
} | ||
if ((0, domutils_1.getName)(el) === 'br') { | ||
const parent = el; | ||
if ((0, domutils_1.getName)(parent) === 'br') { | ||
return '\n'; | ||
} | ||
const nodeName = (0, domutils_1.getName)(el); | ||
let parent = el; | ||
if (nodeName === 'pre' && el.childNodes[0] && (0, domutils_1.getName)(el.childNodes[0]) === 'code') { | ||
; | ||
[parent] = el.childNodes; | ||
const nodeName = (0, domutils_1.getName)(parent); | ||
const nodeFirstChildName = parent.childNodes && parent.childNodes[0] && (0, domutils_1.getName)(parent.childNodes[0]); | ||
const nodeFirstParentName = parent.parent && (0, domutils_1.getName)(parent.parent); | ||
const children = parent.childNodes ? Array.from(parent.childNodes).map(deserialize).flat() : []; | ||
if ((0, domutils_1.getName)(parent) === 'body') { | ||
return (0, slate_hyperscript_1.jsx)('fragment', {}, children); | ||
} | ||
let children = Array.from(parent.childNodes).map(deserialize).flat(); | ||
if (children.length === 0) { | ||
children = [{ text: '' }]; | ||
if ((nodeName === 'pre' && nodeFirstChildName === 'code') || | ||
(nodeName === 'code' && nodeFirstChildName === 'pre')) { | ||
const attrs = TEXT_TAGS[`code`](parent); | ||
return [(0, slate_hyperscript_1.jsx)('text', attrs, (0, domutils_1.textContent)((0, domutils_1.getChildren)(parent.childNodes[0])))]; | ||
} | ||
if ((0, domutils_1.getName)(el) === 'body') { | ||
return (0, slate_hyperscript_1.jsx)('fragment', {}, children); | ||
} | ||
if (ELEMENT_TAGS[nodeName]) { | ||
const attrs = ELEMENT_TAGS[nodeName](el); | ||
const attrs = ELEMENT_TAGS[nodeName](parent); | ||
return (0, slate_hyperscript_1.jsx)('element', attrs, children); | ||
} | ||
if (TEXT_TAGS[nodeName]) { | ||
const attrs = TEXT_TAGS[nodeName](el); | ||
return children.map((child) => (0, slate_hyperscript_1.jsx)('text', attrs, child)); | ||
if (el.type === htmlparser2_1.ElementType.Text) { | ||
if ((0, domutils_1.textContent)(el).trim() === '') { | ||
return null; | ||
} | ||
const name = nodeName || nodeFirstParentName || ''; | ||
const attrs = TEXT_TAGS[name] ? TEXT_TAGS[name](parent) : {}; | ||
return [(0, slate_hyperscript_1.jsx)('text', { ...attrs, text: (0, domutils_1.textContent)(el) }, children)]; | ||
} | ||
@@ -80,3 +80,13 @@ return children; | ||
// Parsing completed, do something | ||
slateContent = dom.map((node) => deserialize(node)).filter((element) => element); | ||
slateContent = dom | ||
.map((node) => deserialize(node)) // run the deserializer | ||
.filter((element) => element) // filter out null elements | ||
.map((element) => { | ||
if (!element.children) { | ||
return { | ||
children: element | ||
}; | ||
} | ||
return element; | ||
}); | ||
} | ||
@@ -83,0 +93,0 @@ }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _1 = require("."); | ||
const elementTags_1 = require("../../__tests__/fixtures/elementTags"); | ||
const textTags_1 = require("../../__tests__/fixtures/textTags"); | ||
describe('Housekeeping', () => { | ||
@@ -46,67 +48,18 @@ it('ignores non-HTML line breaks and extra spaces', () => { | ||
describe('HTML to Slate JSON transforms', () => { | ||
it('converts a sequence of basic text elements', () => { | ||
const fixture = `<h1>Heading 1</h1><p>Paragraph 1</p>`; | ||
const expected = [ | ||
{ | ||
children: [ | ||
{ | ||
text: 'Heading 1', | ||
}, | ||
], | ||
type: 'h1', | ||
}, | ||
{ | ||
children: [ | ||
{ | ||
text: 'Paragraph 1', | ||
}, | ||
], | ||
type: 'p', | ||
}, | ||
]; | ||
expect((0, _1.htmlToSlate)(fixture)).toEqual(expected); | ||
describe('Element tags', () => { | ||
const fixtures = elementTags_1.fixtures; | ||
for (const fixture of fixtures) { | ||
it(`${fixture.name}`, () => { | ||
expect((0, _1.htmlToSlate)(fixture.html)).toEqual(fixture.slate); | ||
}); | ||
} | ||
}); | ||
it('handles links', () => { | ||
const fixture = `<a href="https://github.com/thompsonsj/slate-serializers">Slate Serializers | GitHub</a>`; | ||
const expected = [ | ||
{ | ||
children: [ | ||
{ | ||
text: 'Slate Serializers | GitHub', | ||
}, | ||
], | ||
newTab: false, | ||
type: 'link', | ||
url: 'https://github.com/thompsonsj/slate-serializers', | ||
}, | ||
]; | ||
expect((0, _1.htmlToSlate)(fixture)).toEqual(expected); | ||
describe('Text tags', () => { | ||
const fixtures = textTags_1.fixtures; | ||
for (const fixture of fixtures) { | ||
it(`${fixture.name}`, () => { | ||
expect((0, _1.htmlToSlate)(fixture.html)).toEqual(fixture.slate); | ||
}); | ||
} | ||
}); | ||
it('handles unordered lists', () => { | ||
const fixture = `<ul><li>Item 1</li><li>Item 2</li></ul>`; | ||
const expected = [ | ||
{ | ||
children: [ | ||
{ | ||
children: [ | ||
{ | ||
text: 'Item 1', | ||
}, | ||
], | ||
type: 'li', | ||
}, | ||
{ | ||
children: [ | ||
{ | ||
text: 'Item 2', | ||
}, | ||
], | ||
type: 'li', | ||
}, | ||
], | ||
type: 'ul', | ||
}, | ||
]; | ||
expect((0, _1.htmlToSlate)(fixture)).toEqual(expected); | ||
}); | ||
}); |
@@ -1,1 +0,1 @@ | ||
export declare const slateToHtml: (node: any[]) => any; | ||
export declare const slateToHtml: (node: any[]) => string | any[]; |
@@ -17,5 +17,8 @@ "use strict"; | ||
} | ||
if (node.code) { | ||
str = `<pre><code>${str}</code></pre>`; | ||
} | ||
return str; | ||
} | ||
const children = node.children && node.children.map((n) => slateNodeToHtml(n)).join(''); | ||
const children = node.children ? node.children.map((n) => slateNodeToHtml(n)).join('') : []; | ||
switch (node.type) { | ||
@@ -28,2 +31,10 @@ case 'p': | ||
return `<h2>${children}</h2>`; | ||
case 'h3': | ||
return `<h3>${children}</h3>`; | ||
case 'h4': | ||
return `<h4>${children}</h4>`; | ||
case 'h5': | ||
return `<h5>${children}</h5>`; | ||
case 'h6': | ||
return `<h6>${children}</h6>`; | ||
case 'quote': | ||
@@ -35,2 +46,4 @@ return `<blockquote><p>${children}</p></blockquote>`; | ||
return `<ul>${children}</ul>`; | ||
case 'ol': | ||
return `<ol>${children}</ol>`; | ||
case 'li': | ||
@@ -40,2 +53,4 @@ return `<li>${children}</li>`; | ||
return `<a href="${(0, html_escaper_1.escape)(node.url)}">${children}</a>`; | ||
case 'blockquote': | ||
return `<blockquote>${children}</blockquote>`; | ||
default: | ||
@@ -42,0 +57,0 @@ return children; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _1 = require("."); | ||
const elementTags_1 = require("../../__tests__/fixtures/elementTags"); | ||
const textTags_1 = require("../../__tests__/fixtures/textTags"); | ||
describe('Slate JSON to HTML transforms', () => { | ||
it('converts a sequence of basic text elements', () => { | ||
const expected = `<h1>Heading 1</h1><p>Paragraph 1</p>`; | ||
const fixture = [ | ||
{ | ||
children: [ | ||
{ | ||
text: 'Heading 1', | ||
}, | ||
], | ||
type: 'h1', | ||
}, | ||
{ | ||
children: [ | ||
{ | ||
text: 'Paragraph 1', | ||
}, | ||
], | ||
type: 'p', | ||
}, | ||
]; | ||
expect((0, _1.slateToHtml)(fixture)).toEqual(expected); | ||
describe('Element tags', () => { | ||
const fixtures = elementTags_1.fixtures; | ||
for (const fixture of fixtures) { | ||
it(`${fixture.name}`, () => { | ||
expect((0, _1.slateToHtml)(fixture.slate)).toEqual(fixture.html); | ||
}); | ||
} | ||
}); | ||
it('handles links', () => { | ||
const expected = `<a href="https://github.com/thompsonsj/slate-serializers">Slate Serializers | GitHub</a>`; | ||
const fixture = [ | ||
{ | ||
children: [ | ||
{ | ||
text: 'Slate Serializers | GitHub', | ||
}, | ||
], | ||
newTab: false, | ||
type: 'link', | ||
url: 'https://github.com/thompsonsj/slate-serializers', | ||
}, | ||
]; | ||
expect((0, _1.slateToHtml)(fixture)).toEqual(expected); | ||
describe('Text tags', () => { | ||
const fixtures = textTags_1.fixtures; | ||
for (const fixture of fixtures) { | ||
it(`${fixture.name}`, () => { | ||
expect((0, _1.slateToHtml)(fixture.slate)).toEqual(fixture.html); | ||
}); | ||
} | ||
}); | ||
it('handles unordered lists', () => { | ||
const expected = `<ul><li>Item 1</li><li>Item 2</li></ul>`; | ||
const fixture = [ | ||
{ | ||
children: [ | ||
{ | ||
children: [ | ||
{ | ||
text: 'Item 1', | ||
}, | ||
], | ||
type: 'li', | ||
}, | ||
{ | ||
children: [ | ||
{ | ||
text: 'Item 2', | ||
}, | ||
], | ||
type: 'li', | ||
}, | ||
], | ||
type: 'ul', | ||
}, | ||
]; | ||
expect((0, _1.slateToHtml)(fixture)).toEqual(expected); | ||
}); | ||
}); |
{ | ||
"name": "slate-serializers", | ||
"version": "0.0.3", | ||
"version": "0.0.5", | ||
"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", |
# slate-serializers | ||
A collection of serializers to convert Slate JSON objects to various formats and vice versa. | ||
A collection of serializers to convert [Slate](https://www.npmjs.com/package/slate) JSON objects to various formats and vice versa. Designed to work in both Node.js and browser environments. | ||
@@ -50,2 +50,18 @@ Serializers included so far: | ||
## Details | ||
### slateToHtml | ||
Based on logic in [Deserializing | Serializing | Slate](https://docs.slatejs.org/concepts/10-serializing#deserializing). | ||
[htmlparser2](https://www.npmjs.com/package/htmlparser2) is used to parse HTML instead of the `DOMHandler` object. Rationale: | ||
- Works in all environments, including Node.js. | ||
- Speed - `htmlparser2` is the fastest HTML parser. | ||
- Forgiving regarding HTML spec compliance. | ||
### htmlToSlate | ||
Based on logic in [HTML | Serializing | Slate](https://docs.slatejs.org/concepts/10-serializing#html). | ||
## Development | ||
@@ -52,0 +68,0 @@ |
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
21320
18
566
80
1