1 import {MarkdownSerializer, defaultMarkdownSerializer, MarkdownSerializerState} from "prosemirror-markdown";
2 import {docToHtml} from "./util";
4 const nodes = defaultMarkdownSerializer.nodes;
5 const marks = defaultMarkdownSerializer.marks;
8 nodes.callout = function (state, node) {
9 writeNodeAsHtml(state, node);
12 nodes.table = function (state, node) {
13 writeNodeAsHtml(state, node);
16 function isPlainURL(link, parent, index, side) {
17 if (link.attrs.title || !/^\w+:/.test(link.attrs.href)) {
20 const content = parent.child(index + (side < 0 ? -1 : 0));
21 if (!content.isText || content.text != link.attrs.href || content.marks[content.marks.length - 1] != link) {
24 if (index == (side < 0 ? 1 : parent.childCount - 1)) {
27 const next = parent.child(index + (side < 0 ? -2 : 1));
28 return !link.isInSet(next.marks)
32 open(state, mark, parent, index) {
33 const attrs = mark.attrs;
35 return `<a href="${attrs.target}" ${attrs.title ? `title="${attrs.title}"` : ''} target="${attrs.target}">`
37 return isPlainURL(mark, parent, index, 1) ? "<" : "["
39 close(state, mark, parent, index) {
40 if (mark.attrs.target) {
43 return isPlainURL(mark, parent, index, -1) ? ">"
44 : "](" + state.esc(mark.attrs.href) + (mark.attrs.title ? " " + state.quote(mark.attrs.title) : "") + ")"
49 open: '<span style="text-decoration: underline;">',
54 open: '<span style="text-decoration: line-through;">',
69 open(state, mark, parent, index) {
70 return `<span style="color: ${mark.attrs.color};">`
75 marks.background_color = {
76 open(state, mark, parent, index) {
77 return `<span style="background-color: ${mark.attrs.color};">`
83 * @param {MarkdownSerializerState} state
86 function writeNodeAsHtml(state, node) {
87 const html = docToHtml({content: [node]});
89 state.ensureNewLine();
94 // Update serializers to just write out as HTML if we have an attribute
95 // or element that cannot be represented in commonmark without losing
96 // formatting or content.
97 for (const [nodeType, serializerFunction] of Object.entries(nodes)) {
98 nodes[nodeType] = function (state, node, parent, index) {
99 if (node.attrs.align || node.attrs.height || node.attrs.width) {
100 writeNodeAsHtml(state, node);
102 serializerFunction(state, node, parent, index);
108 const serializer = new MarkdownSerializer(nodes, marks);
110 export default serializer;