A hyperscript helper for creating Slate documents with JSX-like syntax.
90
Create a custom hyperscript helper that integrates with a custom editor factory. The factory should add metadata to editor instances and validate their structure. You'll also define element shortcuts to simplify creating common element types.
Implement a custom editor factory function that:
id property (use a simple counter starting at 1, incrementing with each editor created)createdAt property with the current timestampCreate a custom hyperscript helper that:
<editor> tags<paragraph> creates elements with type: 'paragraph'<heading> creates elements with type: 'heading' and level: 1Write tests in editor-factory.test.js that verify:
id and createdAt propertiesid counter increments correctly across multiple editorsProvides JSX-based syntax for creating Slate data structures.
Core Slate editor framework (peer dependency).
@test
// In: editor-factory.test.js
// Create an editor using the custom hyperscript
const editor = (
<editor>
<paragraph>Hello world</paragraph>
</editor>
)
// Verify custom properties exist
expect(editor.id).toBeDefined()
expect(editor.createdAt).toBeDefined()
expect(editor.children).toHaveLength(1)@test
// In: editor-factory.test.js
// Use custom element shortcuts
const editor = (
<editor>
<heading>Title</heading>
<paragraph>Content</paragraph>
</editor>
)
expect(editor.children[0].type).toBe('heading')
expect(editor.children[0].level).toBe(1)
expect(editor.children[1].type).toBe('paragraph')@test
// In: editor-factory.test.js
// Create multiple editors and verify IDs increment
const editor1 = (
<editor>
<paragraph>First</paragraph>
</editor>
)
const editor2 = (
<editor>
<paragraph>Second</paragraph>
</editor>
)
expect(editor2.id).toBe(editor1.id + 1)@test
// In: editor-factory.test.js
// Creating an editor without children should throw
expect(() => {
const editor = <editor></editor>
}).toThrow('Editor must have at least one child')createHyperscript function from slate-hyperscriptcreators optionelements optionjsx so it can be used with JSX syntaxInstall with Tessl CLI
npx tessl i tessl/npm-slate-hyperscriptdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10