From 32d76eeb9e4d0608624ed0d4acc5559c6dfaa2c8 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 4 Sep 2022 17:07:26 +0200 Subject: [PATCH] docs --- server/models/Document.ts | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/server/models/Document.ts b/server/models/Document.ts index 7df7966966..5a804d6c24 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -759,21 +759,28 @@ class Document extends ParanoidModel { }; /** - * Returns the document as plain HTML. This is a lossy conversion and should - * only be used for export. This method uses the collaborative state if - * available, otherwise it falls back to Markdown -> HTML. + * Returns the document as a Prosemirror Node. This method uses the + * collaborative state if available, otherwise it falls back to Markdown->HTML. * - * @returns The document content as a HTML string + * @returns The document content as a Prosemirror Node */ - toHTML = () => { - let node; + toProsemirror = () => { if (this.state) { const ydoc = new Y.Doc(); Y.applyUpdate(ydoc, this.state); - node = Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default")); - } else { - node = parser.parse(this.text); + return Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default")); } + return parser.parse(this.text); + }; + + /** + * Returns the document as plain HTML. This is a lossy conversion and should + * only be used for export. + * + * @returns The document title and content as a HTML string + */ + toHTML = () => { + const node = this.toProsemirror(); const dom = new JSDOM( `

${escape( @@ -795,6 +802,12 @@ class Document extends ParanoidModel { return doc.getElementsByTagName("body")[0]?.innerHTML; }; + /** + * Returns the document as Markdown. This is a lossy conversion and should + * only be used for export. + * + * @returns The document title and content as a Markdown string + */ toMarkdown = () => { const text = unescape(this.text);