remark-slate-transformer
v0.12.2
Published
remark plugin to transform remark syntax tree (mdast) to Slate document tree, and vice versa. Made for WYSIWYG markdown editor.
Readme
remark-slate-transformer
remark plugin to transform remark syntax tree (mdast) to Slate document tree, and vice versa. Made for WYSIWYG markdown editor.
remark is popular markdown parser/serializer which data structure can be converted to what used in rehype, retext and so on. Slate is fully customizable rich text editor built on React. Connect both 2 worlds should be great...
[!NOTE] For Plate users, try https://github.com/inokawa/remark-slate-transformer/issues/31#issuecomment-2786317057
Support
This plugin supports slate 0.50+. The data structure is described here. slate ~0.47.9 was supported until 0.8.1.
All nodes in mdast syntax tree are supported, including nodes created with...
- remark-gfm
- remark-frontmatter
mathandinlineMathfrom remark-math.
And also have experimental support for custom AST.
Demo
https://inokawa.github.io/remark-slate-transformer/
Install
npm install remark-slate-transformerSupported unified versions
| remark-slate-transformer | unified | | ------------------------ | ---------------- | | >=0.9.0 | >=11.0.0 | | >=0.7.0 <0.9.0 | >=10.1.0 <11.0.0 | | >=0.5.0 <0.7.0 | >=10.0.0 | | <0.5.0 | <10.0.0 |
Usage
Transform remark to slate
import { unified } from "unified";
import markdown from "remark-parse";
import { remarkToSlate } from "remark-slate-transformer";
const processor = unified().use(markdown).use(remarkToSlate);
const text = "# hello world";
const value = processor.processSync(text).result;
console.log(value);Transform slate to remark
import { unified } from "unified";
import stringify from "remark-stringify";
import { slateToRemark } from "remark-slate-transformer";
const processor = unified().use(stringify);
const value = ...; // value passed to slate editor
const ast = processor.runSync(slateToRemark(value));
const text = processor.stringify(ast);
console.log(text);Support custom AST
import { unified } from "unified";
import markdown from "remark-parse";
import stringify from "remark-stringify";
import { remarkToSlate, slateToRemark } from "remark-slate-transformer";
const text = "# hello world";
const r2s = unified()
.use(markdown)
.use(remarkToSlate, {
// If you use TypeScript, install `@types/mdast` for autocomplete.
overrides: {
// This overrides `type: "heading"` builder of remarkToSlate
heading: (node, next) => ({
type: "head",
dep: node.depth,
// You have to call next if the node have children
children: next(node.children),
}),
// Unknown type from community plugins can be handled
foo: (node, next) => ({ type: "foo", value: node.bar }),
},
});
const value = r2s.processSync(text).result;
console.log(value);
const s2r = unified().use(stringify);
const ast = s2r.runSync(
slateToRemark(value, {
overrides: {
head: (node, next) => ({
type: "heading",
depth: node.dep,
children: next(node.children),
}),
foo: (node, next) => ({ type: "foo", bar: node.value }),
},
})
);
const text = s2r.stringify(ast);
console.log(text);remarkToSlate builders also get the marks around the node, like { strong: true }, as a third argument. Spread them into a text you return so it keeps them:
inlineCode: (node, next, marks) => ({ ...marks, mono: true, text: node.value }),Mark names
Texts get marks named after the mdast types: strong, emphasis, delete and inlineCode. Pass marks to both sides to use your editor's names instead:
const marks = {
strong: "bold",
emphasis: "italic",
delete: "strikethrough",
inlineCode: "code",
};
unified().use(markdown).use(remarkToSlate, { marks });
slateToRemark(value, { marks });Once a mark is renamed, slateToRemark reads only the new name.
HTML marks
Markdown has no underline, subscript or superscript, so they're usually written as inline HTML like <u>a</u>. Pass htmlMarks to both sides to turn paired tags into marks and back:
const htmlMarks = { u: "underline", sub: "subscript", sup: "superscript" };
unified().use(markdown).use(remarkToSlate, { htmlMarks });
slateToRemark(value, { htmlMarks });remarkToSlatefollows how browsers render the tags: an opening tag marks the text after it until its closing tag or the end of the paragraph, heading, table cell or link, and a stray closing tag is dropped. Tag names match case-insensitively, and other tags stayhtml.slateToRemarkwraps each marked text in the tags, skipping text that is only whitespace.
Utilities
Transformer utilities mdastToSlate and slateToMdast are also exported for more fine-tuned control.
Inline HTML
mdast uses type: "html" for both block and inline HTML. When html appears inside phrasing content (a paragraph, heading, table cell or link), the Slate element gets inline: true. Use it in isInline so Slate doesn't treat inline HTML as a block:
const { isInline } = editor;
editor.isInline = (element) =>
element.type === "html" ? !!element.inline : isInline(element);Editor setup
Slate needs to know which elements are inline and which are void, or normalization removes them from paragraphs. withMdast sets isInline and isVoid for the elements this package creates, wraps stray list children in listItem, and makes Enter inside a list item start a new item:
import { createEditor } from "slate";
import { withMdast } from "remark-slate-transformer";
const editor = withMdast(createEditor());isMdastInline and isMdastVoid are exported too, for editors that set up isInline and isVoid themselves.
TypeScript
MdastElement and MdastText describe the nodes remarkToSlate creates, with the default mark names. Use them as Slate's custom types to get element types you can switch on:
import type { BaseEditor } from "slate";
import type { MdastElement, MdastText } from "remark-slate-transformer";
declare module "slate" {
interface CustomTypes {
Editor: BaseEditor;
Element: MdastElement;
Text: MdastText;
}
}slateToRemark overrides for these element types get the matching node type.
Contribute
All contributions are welcome. If you find a problem, feel free to create an issue or a PR.
Making a Pull Request
- Fork this repo.
- Run
npm install. - Commit your fix.
- Add tests to cover the fix.
- Make a PR and confirm all the CI checks passed.
