@haprompt/headless
v0.13.3
Published
This package contains Headless helpers and functionality for Haprompt.
Downloads
2
Readme
@haprompt/headless
This package allows you to interact with Haprompt in a headless environment (one that does not rely on DOM, e.g. for Node.js environment), and use its main features like editor.update(), editor.registerNodeTransform(), editor.registerUpdateListener() to create, update or traverse state.
Install @haprompt/headless
:
npm install --save @haprompt/headless
const { createHeadlessEditor } = require('@haprompt/headless');
const editor = createHeadlessEditor({
nodes: [],
onError: () => {},
});
editor.update(() => {
$getRoot().append(
$createParagraphNode().append(
$createTextNode('Hello world')
)
)
});
Any plugins that do not rely on DOM could also be used. Here's an example of how you can convert haprompt editor state to markdown on server:
const { createHeadlessEditor } = require('@haprompt/headless');
const { $convertToMarkdownString, TRANSFORMERS } = require('@haprompt/markdown');
app.get('article/:id/markdown', await (req, res) => {
const editor = createHeadlessEditor({
nodes: [],
onError: () => {},
});
const articleEditorStateJSON = await loadArticleBody(req.query.id);
editor.setEditorState(editor.parseEditorState(articleEditorStateJSON));
editor.update(() => {
const markdown = $convertToMarkdownString(TRANSFORMERS);
res.send(markdown);
});
});