How to run milkdown parser and schema in Nodejs #898
-
I am using the next version of milkdown the new api and trying to convert markdown file in nodejs to yDoc. Is this possible? |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 5 replies
-
I think you can give it a try. IMO the @milkdown/transformer doesn't rely on any browser API so it should be possible. If you meet any problem please come back and share. |
Beta Was this translation helpful? Give feedback.
-
Hi, @Saul-Mirone I looked into import { ParserState, SerializerState } from '@milkdown/transformer';
import { schema } from '@milkdown/preset-gfm';
import remarkGFM from 'remark-gfm';
const remark = remarkGFM();
if (remark) {
const parser = ParserState.create(schema, remark);
const serializer = SerializerState.create(schema, remark);
} But typescript throughs error that Also I checked the Schema type is quite complex while MilkdownPlugin are functions. Can you guide me on how I can use a function probably to generate schema that is same as the one that is generated by the combination of plugins in the browser. |
Beta Was this translation helpful? Give feedback.
-
Hi @Saul-Mirone, I was working on running the milkdown-gfm parser and schema in nodejs. I was able to come till here. import remarkGFM from 'remark-gfm';
import { ParserState, SerializerState } from '@milkdown/transformer';
import { schema, gfm } from '@milkdown/preset-gfm';
import * as Y from 'yjs';
import { prosemirrorToYDoc, yDocToProsemirror } from 'y-prosemirror';
import { Schema } from '@milkdown/prose/model';
import {} from '@milkdown/transformer/src/serializer/state.spec';
/** Build Schema?? */
const S = new Schema({
nodes: {},
marks: {}
});
const remark = remarkGFM() as import('unified').Transformer<
import('mdast').Root,
import('mdast').Root
>;
/** Build a processor?? */
const parser = ParserState.create(S, remark);
const serializer = SerializerState.create(S, remark);
export function CommonMarkToString(doc: Y.Doc) {
const str = serializer(yDocToProsemirror(S, doc));
return str;
}
export function CommonMarkToDoc(value: string) {
return prosemirrorToYDoc(parser(value));
} I still haven't figured out as to how do it provide it with the schema and remark. |
Beta Was this translation helpful? Give feedback.
-
hi @Saul-Mirone I updated the above code to create a parser from the milkdowns code import {
emphasisSchema,
inlineCodeSchema,
linkSchema,
strongSchema
} from '@milkdown/preset-commonmark/src/mark';
import {
blockquoteSchema,
bulletListSchema,
codeBlockSchema,
docSchema,
hardbreakSchema,
headingSchema,
hrSchema,
imageSchema,
orderedListSchema,
paragraphSchema,
textSchema,
listItemSchema
} from '@milkdown/preset-commonmark/src/node';
import {
extendListItemSchemaForTask,
tableCellSchema,
tableHeaderSchema,
tableRowSchema,
tableSchema
} from '@milkdown/preset-gfm';
import { Schema } from '@milkdown/prose/model';
import { ParserState, SerializerState } from '@milkdown/transformer';
import {} from '@milkdown/transformer/src/serializer/state.spec';
import { remark } from 'remark';
import { prosemirrorToYDoc, yDocToProsemirror } from 'y-prosemirror';
import * as Y from 'yjs';
/** Build Schema?? */
const S = new Schema({
nodes: {
listItem: extendListItemSchemaForTask,
// listItem: listItemSchema,
table: tableSchema,
table_row: tableRowSchema,
table_cell: tableCellSchema,
table_header: tableHeaderSchema,
hr: hrSchema,
doc: docSchema,
text: textSchema,
image: imageSchema,
heading: headingSchema,
code_block: codeBlockSchema
},
marks: {
emphasis: emphasisSchema,
link: linkSchema,
strong: strongSchema,
inline: inlineCodeSchema,
hardbreak: hardbreakSchema,
paragraph: paragraphSchema,
blockquote: blockquoteSchema,
bullet_list: bulletListSchema,
ordered_list: orderedListSchema
}
});
const R = remark();
/** Build a processor?? */
const parser = ParserState.create(S, R);
const serializer = SerializerState.create(S, R);
export function CommonMarkToString(doc: Y.Doc) {
const str = serializer(yDocToProsemirror(S, doc));
return str;
}
export function CommonMarkToDoc(value: string) {
return prosemirrorToYDoc(parser(value));
} But it is not able to parse the string. |
Beta Was this translation helpful? Give feedback.
-
If all you want is just run milkdown in nodejs, have you considered to use Puppeteer? Anyway, the code of creating the schema of milkdown is in https://github.com/Milkdown/milkdown/blob/9978182c9828d557c92bb387a5ab3745ed39b642/packages/core/src/internal-plugin/schema.ts, you can copy it try it out. But you need to understand how ctx working. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi @Saul-Mirone, I manage to get till here, I think this is what you meant import { remarkCtx, schema, schemaCtx } from '@milkdown/core';
import { Clock, Container, Ctx } from '@milkdown/ctx';
import { gfm } from '@milkdown/preset-gfm';
import { ParserState, SerializerState } from '@milkdown/transformer';
import {} from '@milkdown/transformer/src/serializer/state.spec';
import { prosemirrorToYDoc, yDocToProsemirror } from 'y-prosemirror';
import * as Y from 'yjs';
const clock = new Clock();
const container = new Container();
const ctx = new Ctx(container, clock);
// ctx.use(commonmark);
gfm.forEach((x) => x(ctx));
schema(ctx);
const S = ctx.get(schemaCtx);
const R = ctx.get(remarkCtx);
/** Build a processor?? */
const parser = ParserState.create(S, R);
const serializer = SerializerState.create(S, R);
export function CommonMarkToString(doc: Y.Doc) {
const str = serializer(yDocToProsemirror(S, doc));
return str;
}
export function CommonMarkToDoc(value: string) {
return prosemirrorToYDoc(parser(value));
} I am getting the error |
Beta Was this translation helpful? Give feedback.
-
Hi @Saul-Mirone, what do you think of this code import {
parser,
prosePluginsCtx,
remarkCtx,
remarkPluginsCtx,
remarkStringifyOptionsCtx,
schema,
schemaCtx
} from '@milkdown/core';
import { Clock, Container, Ctx } from '@milkdown/ctx';
import { commonmark } from '@milkdown/preset-commonmark';
import { gfm } from '@milkdown/preset-gfm';
import { ParserState, SerializerState } from '@milkdown/transformer';
import {} from '@milkdown/transformer/src/serializer/state.spec';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { unified } from 'unified';
import { prosemirrorToYDoc, yDocToProsemirror } from 'y-prosemirror';
import * as Y from 'yjs';
const clock = new Clock();
const container = new Container();
const ctx = new Ctx(container, clock);
ctx
.inject(prosePluginsCtx)
.inject(remarkPluginsCtx)
.inject(remarkStringifyOptionsCtx)
.inject(remarkCtx, unified().use(remarkParse).use(remarkStringify));
commonmark.forEach((x) => x(ctx));
gfm.forEach((x) => x(ctx));
schema(ctx);
parser(ctx);
const S = ctx.get(schemaCtx);
const R = ctx.get(remarkCtx);
/** Build a processor?? */
const PR = ParserState.create(S, R);
const SR = SerializerState.create(S, R);
export function CommonMarkToString(doc: Y.Doc) {
const str = SR(yDocToProsemirror(S, doc));
return str;
}
export function CommonMarkToDoc(value: string) {
return prosemirrorToYDoc(PR(value));
} |
Beta Was this translation helpful? Give feedback.
https://stackblitz.com/edit/node-ltifxb?file=index.js