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 function isPlainURL(link, parent, index, side) {
13 if (link.attrs.title || !/^\w+:/.test(link.attrs.href)) {
16 const content = parent.child(index + (side < 0 ? -1 : 0));
17 if (!content.isText || content.text != link.attrs.href || content.marks[content.marks.length - 1] != link) {
20 if (index == (side < 0 ? 1 : parent.childCount - 1)) {
23 const next = parent.child(index + (side < 0 ? -2 : 1));
24 return !link.isInSet(next.marks)
28 open(state, mark, parent, index) {
29 const attrs = mark.attrs;
31 return `<a href="${attrs.target}" ${attrs.title ? `title="${attrs.title}"` : ''} target="${attrs.target}">`
33 return isPlainURL(mark, parent, index, 1) ? "<" : "["
35 close(state, mark, parent, index) {
36 if (mark.attrs.target) {
39 return isPlainURL(mark, parent, index, -1) ? ">"
40 : "](" + state.esc(mark.attrs.href) + (mark.attrs.title ? " " + state.quote(mark.attrs.title) : "") + ")"
45 open: '<span style="text-decoration: underline;">',
50 open: '<span style="text-decoration: line-through;">',
65 open(state, mark, parent, index) {
66 return `<span style="color: ${mark.attrs.color};">`
71 marks.background_color = {
72 open(state, mark, parent, index) {
73 return `<span style="background-color: ${mark.attrs.color};">`
79 * @param {MarkdownSerializerState} state
82 function writeNodeAsHtml(state, node) {
83 const html = docToHtml({content: [node]});
85 state.ensureNewLine();
90 // Update serializers to just write out as HTML if we have an attribute
91 // or element that cannot be represented in commonmark without losing
92 // formatting or content.
93 for (const [nodeType, serializerFunction] of Object.entries(nodes)) {
94 nodes[nodeType] = function (state, node, parent, index) {
95 if (node.attrs.align || node.attrs.height || node.attrs.width) {
96 writeNodeAsHtml(state, node);
98 serializerFunction(state, node, parent, index);
104 const serializer = new MarkdownSerializer(nodes, marks);
106 export default serializer;