Skip to content

How to get the html string? #466

Answered by Saul-Mirone
langsz asked this question in Q&A
Apr 19, 2022 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

You can also write a simple action which will be provided officially in v6.0:

/* Copyright 2021, Milkdown by Mirone. */
import { Ctx, editorViewCtx, schemaCtx } from '@milkdown/core';
import { DOMSerializer } from '@milkdown/prose';

export const getHTML = () => (ctx: Ctx) => {
    const div = document.createElement('div');
    const schema = ctx.get(schemaCtx);
    const view = ctx.get(editorViewCtx);
    const fragment = DOMSerializer.fromSchema(schema).serializeFragment(view.state.doc.content);

    div.appendChild(fragment);

    return div.innerHTML;
};

const html = editor.action(getHTML());

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@langsz
Comment options

@seeARMS
Comment options

@langsz
Comment options

@Saul-Mirone
Comment options

@langsz
Comment options

Answer selected by langsz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants