diff options
Diffstat (limited to 'app/assets/javascripts/content_editor/services/markdown_deserializer.js')
-rw-r--r-- | app/assets/javascripts/content_editor/services/markdown_deserializer.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/app/assets/javascripts/content_editor/services/markdown_deserializer.js b/app/assets/javascripts/content_editor/services/markdown_deserializer.js new file mode 100644 index 00000000000..cd4863d8eac --- /dev/null +++ b/app/assets/javascripts/content_editor/services/markdown_deserializer.js @@ -0,0 +1,33 @@ +import { DOMParser as ProseMirrorDOMParser } from 'prosemirror-model'; + +export default ({ render }) => { + /** + * Converts a Markdown string into a ProseMirror JSONDocument based + * on a ProseMirror schema. + * + * @param {Object} options — The schema and content for deserialization + * @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 + * + * @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. + */ + return { + deserialize: async ({ schema, content }) => { + const html = await render(content); + + if (!html) return {}; + + const parser = new DOMParser(); + const { body } = parser.parseFromString(html, 'text/html'); + + // append original source as a comment that nodes can access + body.append(document.createComment(content)); + + return { document: ProseMirrorDOMParser.fromSchema(schema).parse(body), dom: body }; + }, + }; +}; |