editorjs_mdast
v0.1.3
Published
Two-way conversion between Editor.js JSON blocks and MDAST - Markdown Abstract Syntax Tree
Maintainers
Readme
editorjs_mdast
Two-way conversion between Editor.js JSON blocks and MDAST (Markdown Abstract Syntax Tree).
Installation
npm install editorjs_mdast
# or
yarn add editorjs_mdast
# or
pnpm add editorjs_mdastUsage
Converting Editor.js to MDAST
import { editorJsToMdast } from 'editorjs_mdast';
import { toMarkdown } from 'mdast-util-to-markdown';
// Your Editor.js data
const editorJsData = {
time: 1625756954764,
blocks: [
{
id: '1',
type: 'paragraph',
data: {
text: 'Hello, world!',
},
},
{
id: '2',
type: 'header',
data: {
text: 'This is a heading',
level: 2,
},
},
],
version: '2.22.2',
};
// Convert to MDAST
const mdast = editorJsToMdast(editorJsData);
// Convert MDAST to Markdown (optional)
const markdown = toMarkdown(mdast);
console.log(markdown);
// Output:
// Hello, world!
//
// ## This is a headingConverting MDAST to Editor.js
import { mdastToEditorJs } from 'editorjs_mdast';
import { fromMarkdown } from 'mdast-util-from-markdown';
// Create or parse MDAST
const mdast = fromMarkdown(`
# Heading
This is a paragraph with some text.
- List item 1
- List item 2
`);
// Convert to Editor.js data
const editorJsData = mdastToEditorJs(mdast);
console.log(JSON.stringify(editorJsData, null, 2));
// Output: Editor.js compatible JSON structureLibrary Structure
The library is organized into two main modules:
- editorjs-to-mdast: Converts Editor.js JSON blocks to MDAST structure
- mdast-to-editorjs: Converts MDAST structure to Editor.js JSON blocks
Supported Conversions
| Editor.js Block | MDAST Node | Notes | |-----------------|---------------|----------------------------| | paragraph | paragraph | Text content is preserved | | header | heading | Level is preserved | | list | list | Ordered/unordered preserved | | image | image | URL and caption preserved | | code | code | Language is preserved | | quote | blockquote | Caption becomes attribution | | delimiter | thematicBreak | Horizontal rule |
Development
# Install dependencies
pnpm install
# Build the project
pnpm build
# Run tests
pnpm test
# Lint code
pnpm lint
# Format code
pnpm formatLicense
ISC
