diff --git a/lib/models/text.js b/lib/models/text.js index 5895ad23c..78ad32ec4 100644 --- a/lib/models/text.js +++ b/lib/models/text.js @@ -37,6 +37,18 @@ class Text extends new Record(DEFAULTS) { return new Text(properties) } + /** + * Create a list of `Texts` from an array. + * + * @param {Array} elements + * @return {List} map + */ + + static createList(elements = []) { + if (List.isList(elements)) return elements + return new List(elements.map(Text.create)) + } + /** * Get the node's kind. * diff --git a/test/serializers/fixtures/raw/deserialize/block-nested/input.yaml b/test/serializers/fixtures/raw/deserialize/block-nested/input.yaml new file mode 100644 index 000000000..aba50d05d --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block-nested/input.yaml @@ -0,0 +1,11 @@ + +nodes: + - kind: block + type: quote + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/block-nested/output.yaml b/test/serializers/fixtures/raw/deserialize/block-nested/output.yaml new file mode 100644 index 000000000..f46e65acb --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block-nested/output.yaml @@ -0,0 +1,17 @@ + +nodes: + - type: quote + isVoid: false + data: {} + nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: [] + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/deserialize/block-with-data/input.yaml b/test/serializers/fixtures/raw/deserialize/block-with-data/input.yaml new file mode 100644 index 000000000..1100f59c1 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block-with-data/input.yaml @@ -0,0 +1,10 @@ + +nodes: + - kind: block + type: paragraph + data: + key: value + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/block-with-data/output.yaml b/test/serializers/fixtures/raw/deserialize/block-with-data/output.yaml new file mode 100644 index 000000000..183a31ee8 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block-with-data/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - type: paragraph + isVoid: false + data: + key: value + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: [] + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/deserialize/block-with-is-void/input.yaml b/test/serializers/fixtures/raw/deserialize/block-with-is-void/input.yaml new file mode 100644 index 000000000..009766839 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block-with-is-void/input.yaml @@ -0,0 +1,9 @@ + +nodes: + - kind: block + type: paragraph + isVoid: true + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/block-with-is-void/output.yaml b/test/serializers/fixtures/raw/deserialize/block-with-is-void/output.yaml new file mode 100644 index 000000000..c18f3f47e --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block-with-is-void/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - type: paragraph + isVoid: true + data: {} + nodes: + - characters: [] diff --git a/test/serializers/fixtures/raw/deserialize/block/input.yaml b/test/serializers/fixtures/raw/deserialize/block/input.yaml new file mode 100644 index 000000000..a9bf9ed14 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block/input.yaml @@ -0,0 +1,8 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/block/output.yaml b/test/serializers/fixtures/raw/deserialize/block/output.yaml new file mode 100644 index 000000000..4c208207b --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/block/output.yaml @@ -0,0 +1,13 @@ + +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: [] + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/deserialize/inline-nested/input.yaml b/test/serializers/fixtures/raw/deserialize/inline-nested/input.yaml new file mode 100644 index 000000000..edab6c050 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline-nested/input.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + nodes: + - kind: inline + type: hashtag + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/inline-nested/output.yaml b/test/serializers/fixtures/raw/deserialize/inline-nested/output.yaml new file mode 100644 index 000000000..6cc09ef75 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline-nested/output.yaml @@ -0,0 +1,21 @@ + +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - type: link + isVoid: false + data: {} + nodes: + - type: hashtag + isVoid: false + data: {} + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: [] + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/deserialize/inline-with-data/input.yaml b/test/serializers/fixtures/raw/deserialize/inline-with-data/input.yaml new file mode 100644 index 000000000..92f4c8e21 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline-with-data/input.yaml @@ -0,0 +1,13 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + data: + key: value + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/inline-with-data/output.yaml b/test/serializers/fixtures/raw/deserialize/inline-with-data/output.yaml new file mode 100644 index 000000000..518f9fa8c --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline-with-data/output.yaml @@ -0,0 +1,18 @@ + +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - type: link + isVoid: false + data: + key: value + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: [] + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/deserialize/inline-with-is-void/input.yaml b/test/serializers/fixtures/raw/deserialize/inline-with-is-void/input.yaml new file mode 100644 index 000000000..13ee4a612 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline-with-is-void/input.yaml @@ -0,0 +1,12 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + isVoid: true + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/inline-with-is-void/output.yaml b/test/serializers/fixtures/raw/deserialize/inline-with-is-void/output.yaml new file mode 100644 index 000000000..d43f53514 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline-with-is-void/output.yaml @@ -0,0 +1,11 @@ + +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - type: link + isVoid: true + data: {} + nodes: + - characters: [] diff --git a/test/serializers/fixtures/raw/deserialize/inline/input.yaml b/test/serializers/fixtures/raw/deserialize/inline/input.yaml new file mode 100644 index 000000000..875b0c4b4 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline/input.yaml @@ -0,0 +1,11 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/deserialize/inline/output.yaml b/test/serializers/fixtures/raw/deserialize/inline/output.yaml new file mode 100644 index 000000000..963558dc1 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/inline/output.yaml @@ -0,0 +1,17 @@ + +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - type: link + isVoid: false + data: {} + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: [] + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/deserialize/range-with-mark/input.yaml b/test/serializers/fixtures/raw/deserialize/range-with-mark/input.yaml new file mode 100644 index 000000000..c852eea9d --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/range-with-mark/input.yaml @@ -0,0 +1,12 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + ranges: + - text: o + - text: n + marks: + - type: bold + - text: e diff --git a/test/serializers/fixtures/raw/deserialize/range-with-mark/output.yaml b/test/serializers/fixtures/raw/deserialize/range-with-mark/output.yaml new file mode 100644 index 000000000..83b8211b1 --- /dev/null +++ b/test/serializers/fixtures/raw/deserialize/range-with-mark/output.yaml @@ -0,0 +1,15 @@ + +nodes: + - type: paragraph + isVoid: false + data: {} + nodes: + - characters: + - text: o + marks: [] + - text: n + marks: + - type: bold + data: {} + - text: e + marks: [] diff --git a/test/serializers/fixtures/raw/serialize/block-nested/input.js b/test/serializers/fixtures/raw/serialize/block-nested/input.js new file mode 100644 index 000000000..3e88fa872 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block-nested/input.js @@ -0,0 +1,26 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'quote', + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/block-nested/output.yaml b/test/serializers/fixtures/raw/serialize/block-nested/output.yaml new file mode 100644 index 000000000..aba50d05d --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block-nested/output.yaml @@ -0,0 +1,11 @@ + +nodes: + - kind: block + type: quote + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/serialize/block-with-data/input.js b/test/serializers/fixtures/raw/serialize/block-with-data/input.js new file mode 100644 index 000000000..837db3ff5 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block-with-data/input.js @@ -0,0 +1,22 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + data: { key: 'value' }, + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/block-with-data/output.yaml b/test/serializers/fixtures/raw/serialize/block-with-data/output.yaml new file mode 100644 index 000000000..1100f59c1 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block-with-data/output.yaml @@ -0,0 +1,10 @@ + +nodes: + - kind: block + type: paragraph + data: + key: value + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/serialize/block-with-is-void/input.js b/test/serializers/fixtures/raw/serialize/block-with-is-void/input.js new file mode 100644 index 000000000..c2ca20cba --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block-with-is-void/input.js @@ -0,0 +1,22 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + isVoid: true, + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/block-with-is-void/output.yaml b/test/serializers/fixtures/raw/serialize/block-with-is-void/output.yaml new file mode 100644 index 000000000..52255964e --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block-with-is-void/output.yaml @@ -0,0 +1,9 @@ + +nodes: + - kind: block + type: paragraph + isVoid: true + nodes: + - kind: text + ranges: + - text: "" diff --git a/test/serializers/fixtures/raw/serialize/block/input.js b/test/serializers/fixtures/raw/serialize/block/input.js new file mode 100644 index 000000000..f4264b3b0 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block/input.js @@ -0,0 +1,21 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/block/output.yaml b/test/serializers/fixtures/raw/serialize/block/output.yaml new file mode 100644 index 000000000..a9bf9ed14 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/block/output.yaml @@ -0,0 +1,8 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/serialize/character-with-mark/input.js b/test/serializers/fixtures/raw/serialize/character-with-mark/input.js new file mode 100644 index 000000000..36cfdff18 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/character-with-mark/input.js @@ -0,0 +1,28 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { + text: 'e', + marks: Mark.createSet([ + { + type: 'bold' + } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/character-with-mark/output.yaml b/test/serializers/fixtures/raw/serialize/character-with-mark/output.yaml new file mode 100644 index 000000000..0f7aeffbd --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/character-with-mark/output.yaml @@ -0,0 +1,11 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + ranges: + - text: "on" + - text: e + marks: + - type: bold diff --git a/test/serializers/fixtures/raw/serialize/inline-nested/input.js b/test/serializers/fixtures/raw/serialize/inline-nested/input.js new file mode 100644 index 000000000..b0b7477bb --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline-nested/input.js @@ -0,0 +1,31 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Inline.createList([ + { + type: 'link', + nodes: Inline.createList([ + { + type: 'hashtag', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/inline-nested/output.yaml b/test/serializers/fixtures/raw/serialize/inline-nested/output.yaml new file mode 100644 index 000000000..edab6c050 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline-nested/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + nodes: + - kind: inline + type: hashtag + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/serialize/inline-with-data/input.js b/test/serializers/fixtures/raw/serialize/inline-with-data/input.js new file mode 100644 index 000000000..f820cc7eb --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline-with-data/input.js @@ -0,0 +1,27 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Inline.createList([ + { + type: 'link', + data: { key: 'value' }, + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/inline-with-data/output.yaml b/test/serializers/fixtures/raw/serialize/inline-with-data/output.yaml new file mode 100644 index 000000000..92f4c8e21 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline-with-data/output.yaml @@ -0,0 +1,13 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + data: + key: value + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/fixtures/raw/serialize/inline-with-is-void/input.js b/test/serializers/fixtures/raw/serialize/inline-with-is-void/input.js new file mode 100644 index 000000000..883cbd4e1 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline-with-is-void/input.js @@ -0,0 +1,27 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Inline.createList([ + { + type: 'link', + isVoid: true, + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/inline-with-is-void/output.yaml b/test/serializers/fixtures/raw/serialize/inline-with-is-void/output.yaml new file mode 100644 index 000000000..9234feb75 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline-with-is-void/output.yaml @@ -0,0 +1,12 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + isVoid: true + nodes: + - kind: text + ranges: + - text: "" diff --git a/test/serializers/fixtures/raw/serialize/inline/input.js b/test/serializers/fixtures/raw/serialize/inline/input.js new file mode 100644 index 000000000..ed3cf91e2 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline/input.js @@ -0,0 +1,26 @@ + +import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Inline.createList([ + { + type: 'link', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/raw/serialize/inline/output.yaml b/test/serializers/fixtures/raw/serialize/inline/output.yaml new file mode 100644 index 000000000..875b0c4b4 --- /dev/null +++ b/test/serializers/fixtures/raw/serialize/inline/output.yaml @@ -0,0 +1,11 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + nodes: + - kind: text + ranges: + - text: one diff --git a/test/serializers/index.js b/test/serializers/index.js new file mode 100644 index 000000000..e358873ad --- /dev/null +++ b/test/serializers/index.js @@ -0,0 +1,79 @@ + +import assert from 'assert' +import fs from 'fs' +import readMetadata from 'read-metadata' +import { Html, Plain, Raw } from '../..' +import { equal, strictEqual } from '../helpers/assert-json' +import { resolve } from 'path' + +/** + * Serializers. + */ + +const SERIALIZERS = { + html: Html, + plain: Plain, + raw: Raw +} + +/** + * Tests. + */ + +describe('serializers', () => { + const serializers = fs.readdirSync(resolve(__dirname, './fixtures')) + + for (const serializer of serializers) { + describe(serializer, () => { + describe('deserialize()', () => { + const dir = resolve(__dirname, './fixtures', serializer, 'deserialize') + const tests = fs.readdirSync(dir) + + for (const test of tests) { + it(test, () => { + const innerDir = resolve(dir, test) + const input = readMetadata.sync(resolve(innerDir, 'input.yaml')) + const expected = readMetadata.sync(resolve(innerDir, 'output.yaml')) + const Serializer = SERIALIZERS[serializer] + const state = Serializer.deserialize(input) + const json = state.document.toJS() + strictEqual(clean(json), expected) + }) + } + }) + + describe('serialize()', () => { + const dir = resolve(__dirname, './fixtures', serializer, 'serialize') + const tests = fs.readdirSync(dir) + + for (const test of tests) { + it(test, () => { + const innerDir = resolve(dir, test) + const input = require(resolve(innerDir, 'input.js')).default + const expected = readMetadata.sync(resolve(innerDir, 'output.yaml')) + const Serializer = SERIALIZERS[serializer] + const serialized = Serializer.serialize(input) + strictEqual(serialized, expected) + }) + } + }) + }) + } +}) + +/** + * Clean a `json` object of dynamic `key` properties. + * + * @param {Object} json + * @return {Object} + */ + +function clean(json) { + const { key, cache, decorations, ...props } = json + + if (props.nodes) { + props.nodes = props.nodes.map(clean) + } + + return props +} diff --git a/test/server.js b/test/server.js index 6072ef27d..2e0a0061f 100644 --- a/test/server.js +++ b/test/server.js @@ -1,3 +1,4 @@ +import './serializers' import './rendering' import './transforms'