-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(global-normalize): add and apply global-normalize
- Loading branch information
1 parent
91bfb9a
commit c947c64
Showing
8 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { Element } from 'slate'; | ||
import { check, normalize } from './global-normalize'; | ||
|
||
describe('global-normalize', () => { | ||
const invalidData3: any[] = [ | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ text: '' } | ||
] | ||
}, | ||
{ | ||
type: 'numbered-list', | ||
children: [ | ||
{ | ||
type: 'list-item', | ||
children: [ | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
text: '' | ||
} | ||
] | ||
}, | ||
{ | ||
type: 'paragraph', | ||
children: [] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
]; | ||
|
||
it('should return true', () => { | ||
const validData: Element[] = [ | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ text: 'This is editable ' }, | ||
{ text: 'rich', bold: true }, | ||
{ text: ' text, ' }, | ||
{ text: 'much', bold: true, italic: true }, | ||
{ text: ' better than a ' }, | ||
{ text: '<textarea>', ['code-line']: true }, | ||
{ text: '!' } | ||
] | ||
}, | ||
{ | ||
type: 'heading-one', | ||
children: [{ text: 'This is h1 ' }] | ||
}, | ||
{ | ||
type: 'heading-three', | ||
children: [{ text: 'This is h3 ' }] | ||
}, | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
text: `Since it's rich text, you can do things like turn a selection of text ` | ||
}, | ||
{ text: 'bold', bold: true }, | ||
{ | ||
text: ', or add a semantically rendered block quote in the middle of the page, like this:' | ||
} | ||
] | ||
}, | ||
{ | ||
type: 'block-quote', | ||
children: [{ text: 'A wise quote.' }] | ||
}, | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: 'Try it out for yourself!' }] | ||
}, | ||
{ | ||
type: 'paragraph', | ||
children: [{ text: '' }] | ||
} | ||
]; | ||
const result = check(validData); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false', () => { | ||
const invalidData1: any[] = [ | ||
{ text: '' } | ||
]; | ||
|
||
const result1 = check(invalidData1); | ||
expect(result1).toBeFalse(); | ||
|
||
const invalidData2: any[] = [ | ||
{ | ||
type: 'paragraph', | ||
children: [] | ||
} | ||
]; | ||
|
||
const result2 = check(invalidData2); | ||
expect(result2).toBeFalse(); | ||
|
||
const result3 = check(invalidData3); | ||
expect(result3).toBeFalse(); | ||
}); | ||
|
||
it('should return valid data', () => { | ||
const result3 = normalize(invalidData3); | ||
expect(result3.length).toEqual(1); | ||
expect(result3[0]).toEqual(invalidData3[0]); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Descendant, Element, Text } from "slate"; | ||
|
||
const isValid = (value: Descendant) => ( | ||
Element.isElement(value) && | ||
value.children.length > 0 && | ||
(value.children as Descendant[]).every((child) => isValid(child))) || | ||
Text.isText(value); | ||
|
||
const check = (document: Element[]) => { | ||
return document.every((value) => Element.isElement(value) && isValid(value));; | ||
} | ||
|
||
function normalize(document: Element[]) { | ||
return document.filter((value) => Element.isElement(value) && isValid(value)); | ||
} | ||
|
||
export { normalize, check, isValid }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters