Files
outline/app/models/helpers/ProsemirrorHelper.ts
T
Tom Moor f329b56d0e fix: Hard break serialization for commonMark (#12603)
* fix: Hard break serialization for commonMark

* tests
2026-06-06 07:24:16 -04:00

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, {
commonMark: 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;
};
}