Add DocumentHelper.toPlainText

This commit is contained in:
Tom Moor
2022-12-30 14:49:53 -05:00
parent 7c47ab560e
commit 0f489d54c3
2 changed files with 82 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ import { renderToString } from "react-dom/server";
import styled, { ServerStyleSheet, ThemeProvider } from "styled-components";
import * as Y from "yjs";
import EditorContainer from "@shared/editor/components/Styles";
import textBetween from "@shared/editor/lib/textBetween";
import GlobalStyles from "@shared/styles/globals";
import light from "@shared/styles/theme";
import {
@@ -44,7 +45,7 @@ type HTMLOptions = {
export default class DocumentHelper {
/**
* Returns the document as a Prosemirror Node. This method uses the
* collaborative state if available, otherwise it falls back to Markdown->HTML.
* collaborative state if available, otherwise it falls back to Markdown.
*
* @param document The document or revision to convert
* @returns The document content as a Prosemirror Node
@@ -58,6 +59,24 @@ export default class DocumentHelper {
return parser.parse(document.text);
}
/**
* Returns the document as plain text. This method uses the
* collaborative state if available, otherwise it falls back to Markdown.
*
* @param document The document or revision to convert
* @returns The document content as plain text without formatting.
*/
static toPlainText(document: Document | Revision) {
const node = DocumentHelper.toProsemirror(document);
const textSerializers = Object.fromEntries(
Object.entries(schema.nodes)
.filter(([, node]) => node.spec.toPlainText)
.map(([name, node]) => [name, node.spec.toPlainText])
);
return textBetween(node, 0, node.content.size, textSerializers);
}
/**
* Returns the document as Markdown. This is a lossy conversion and should
* only be used for export.