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 nodes.iframe = function (state, node) {
17 writeNodeAsHtml(state, node);
20 function isPlainURL(link, parent, index, side) {
21 if (link.attrs.title || !/^\w+:/.test(link.attrs.href)) {
24 const content = parent.child(index + (side < 0 ? -1 : 0));
25 if (!content.isText || content.text != link.attrs.href || content.marks[content.marks.length - 1] != link) {
28 if (index == (side < 0 ? 1 : parent.childCount - 1)) {
31 const next = parent.child(index + (side < 0 ? -2 : 1));
32 return !link.isInSet(next.marks)
36 open(state, mark, parent, index) {
37 const attrs = mark.attrs;
39 return `<a href="${attrs.target}" ${attrs.title ? `title="${attrs.title}"` : ''} target="${attrs.target}">`
41 return isPlainURL(mark, parent, index, 1) ? "<" : "["
43 close(state, mark, parent, index) {
44 if (mark.attrs.target) {
47 return isPlainURL(mark, parent, index, -1) ? ">"
48 : "](" + state.esc(mark.attrs.href) + (mark.attrs.title ? " " + state.quote(mark.attrs.title) : "") + ")"
53 open: '<span style="text-decoration: underline;">',
58 open: '<span style="text-decoration: line-through;">',
73 open(state, mark, parent, index) {
74 return `<span style="color: ${mark.attrs.color};">`
79 marks.background_color = {
80 open(state, mark, parent, index) {
81 return `<span style="background-color: ${mark.attrs.color};">`
87 * @param {MarkdownSerializerState} state
90 function writeNodeAsHtml(state, node) {
91 const html = docToHtml({content: [node]});
93 state.ensureNewLine();
98 // Update serializers to just write out as HTML if we have an attribute
99 // or element that cannot be represented in commonmark without losing
100 // formatting or content.
101 for (const [nodeType, serializerFunction] of Object.entries(nodes)) {
102 nodes[nodeType] = function (state, node, parent, index) {
103 if (node.attrs.align || node.attrs.height || node.attrs.width) {
104 writeNodeAsHtml(state, node);
106 serializerFunction(state, node, parent, index);
112 const serializer = new MarkdownSerializer(nodes, marks);
114 export default serializer;