2022-02-28 06:16:06 +00:00
|
|
|
import { DOMParser as ProseMirrorDOMParser } from 'prosemirror-model';
|
|
|
|
|
|
|
|
export default ({ render }) => {
|
|
|
|
/**
|
|
|
|
* Converts a Markdown string into a ProseMirror JSONDocument based
|
|
|
|
* on a ProseMirror schema.
|
2022-03-14 18:07:46 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options — The schema and content for deserialization
|
2022-02-28 06:16:06 +00:00
|
|
|
* @param {ProseMirror.Schema} params.schema A ProseMirror schema that defines
|
|
|
|
* the types of content supported in the document
|
|
|
|
* @param {String} params.content An arbitrary markdown string
|
2022-03-14 18:07:46 +00:00
|
|
|
*
|
|
|
|
* @returns An object with the following properties:
|
|
|
|
* - document: A ProseMirror document object generated from the deserialized Markdown
|
|
|
|
* - dom: The Markdown Deserializer renders Markdown as HTML to generate the ProseMirror
|
|
|
|
* document. The dom property contains the HTML generated from the Markdown Source.
|
2022-02-28 06:16:06 +00:00
|
|
|
*/
|
|
|
|
return {
|
2022-07-14 12:08:33 +00:00
|
|
|
deserialize: async ({ schema, markdown }) => {
|
|
|
|
const html = await render(markdown);
|
2022-02-28 06:16:06 +00:00
|
|
|
|
2022-03-14 18:07:46 +00:00
|
|
|
if (!html) return {};
|
2022-02-28 06:16:06 +00:00
|
|
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
const { body } = parser.parseFromString(html, 'text/html');
|
|
|
|
|
|
|
|
// append original source as a comment that nodes can access
|
2022-07-14 12:08:33 +00:00
|
|
|
body.append(document.createComment(markdown));
|
2022-02-28 06:16:06 +00:00
|
|
|
|
2022-05-05 18:08:40 +00:00
|
|
|
return { document: ProseMirrorDOMParser.fromSchema(schema).parse(body) };
|
2022-02-28 06:16:06 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|