mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import ExtensionManager from "@shared/editor/lib/ExtensionManager";
|
|
import { richExtensions, withComments } from "@shared/editor/nodes";
|
|
import type { ProsemirrorData } from "@shared/types";
|
|
import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
|
import { Schema } from "prosemirror-model";
|
|
import { Node } from "prosemirror-model";
|
|
|
|
interface HasData {
|
|
data: ProsemirrorData;
|
|
}
|
|
|
|
const extensionManager = new ExtensionManager(withComments(richExtensions));
|
|
const schema = new Schema({
|
|
nodes: extensionManager.nodes,
|
|
marks: extensionManager.marks,
|
|
});
|
|
const serializer = extensionManager.serializer();
|
|
|
|
export class ProsemirrorHelper {
|
|
/**
|
|
* Returns the markdown representation of the document derived from the ProseMirror data.
|
|
*
|
|
* @returns The markdown representation of the document as a string.
|
|
*/
|
|
static toMarkdown = (document: HasData) => {
|
|
const doc = Node.fromJSON(
|
|
schema,
|
|
SharedProsemirrorHelper.attachmentsToAbsoluteUrls(document.data)
|
|
);
|
|
|
|
const markdown = serializer.serialize(doc, {
|
|
softBreak: true,
|
|
});
|
|
return markdown;
|
|
};
|
|
|
|
/**
|
|
* Returns the plain text representation of the document derived from the ProseMirror data.
|
|
*
|
|
* @returns The plain text representation of the document as a string.
|
|
*/
|
|
static toPlainText = (document: HasData) => {
|
|
const text = SharedProsemirrorHelper.toPlainText(
|
|
Node.fromJSON(schema, document.data)
|
|
);
|
|
return text;
|
|
};
|
|
}
|