@openstage/glyph-core
v0.2.2
Published
Engine-agnostic document model, smart constructors, validation and migration for the Glyph editor.
Maintainers
Readme
@openstage/glyph-core
Engine-agnostic core for Glyph: the serializable document model, smart constructors, validation, migration, branded value types, and the shared config/command types. No DOM and no editor-engine dependency — safe to use on a server or in any framework.
npm i @openstage/glyph-coreBuilding documents
Documents are a serializable tree. Build them with the smart constructors rather than object literals:
import {
document,
heading,
paragraph,
plain,
text,
bold,
link,
bulletList,
listItem,
codeBlock,
} from '@openstage/glyph-core';
const doc = document(
heading(1)(plain('Welcome')),
paragraph(plain('Hello '), text(bold)('world'), plain('.')),
paragraph(text(link('https://example.com'))('a link')),
bulletList(listItem(paragraph(plain('item one')))),
codeBlock('typescript')('const x = 1;'),
);The result is plain JSON — safe to JSON.stringify and store verbatim.
Validation & migration
For untrusted input (backend, localStorage):
import { isEditorDocument, migrate } from '@openstage/glyph-core';
const raw: unknown = JSON.parse(localStorage.getItem('doc') ?? 'null');
if (isEditorDocument(raw)) {
const doc = migrate(raw); // upgrades older schema versions
}assertEditorDocument throws a TypeError with a path on invalid input.
What's inside
- Document model & schema (
EditorDocument,SCHEMA_VERSION) - Curried smart constructors (
document,heading,paragraph,text,link,codeBlock, list/quote helpers, …) - Branded value types (
Href,Language,CommandId) with.of()validators - Structural validation (
isEditorDocument/assertEditorDocument) - Forward migration (
migrate) and the supported-language list - Shared types (
EditorConfig,EditorApi,SlashCommand,EditorTheme,ToolbarItemId,ToolbarIcon) andDEFAULT_TOOLBAR
The public API exposes no third-party types.
Documentation
Full guide, configuration reference, and backend-integration notes: https://codeberg.org/open-stage/glyph#readme
License
MIT © Gabriel Bornea
