1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Added jsdoc types for prosemirror

Also added link markdown handling when target is set.
This commit is contained in:
Dan Brown
2022-01-16 15:21:57 +00:00
parent 89194a3f85
commit 7622106665
9 changed files with 198 additions and 17 deletions

View File

@ -1,14 +1,45 @@
import {MarkdownSerializer, defaultMarkdownSerializer} from "prosemirror-markdown";
import {MarkdownSerializer, defaultMarkdownSerializer, MarkdownSerializerState} from "prosemirror-markdown";
import {docToHtml} from "./util";
const nodes = defaultMarkdownSerializer.nodes;
const marks = defaultMarkdownSerializer.marks;
nodes.callout = function(state, node) {
nodes.callout = function (state, node) {
writeNodeAsHtml(state, node);
};
function isPlainURL(link, parent, index, side) {
if (link.attrs.title || !/^\w+:/.test(link.attrs.href)) {
return false
}
const content = parent.child(index + (side < 0 ? -1 : 0));
if (!content.isText || content.text != link.attrs.href || content.marks[content.marks.length - 1] != link) {
return false
}
if (index == (side < 0 ? 1 : parent.childCount - 1)) {
return true
}
const next = parent.child(index + (side < 0 ? -2 : 1));
return !link.isInSet(next.marks)
}
marks.link = {
open(state, mark, parent, index) {
const attrs = mark.attrs;
if (attrs.target) {
return `<a href="${attrs.target}" ${attrs.title ? `title="${attrs.title}"` : ''} target="${attrs.target}">`
}
return isPlainURL(mark, parent, index, 1) ? "<" : "["
},
close(state, mark, parent, index) {
if (mark.attrs.target) {
return `</a>`;
}
return isPlainURL(mark, parent, index, -1) ? ">"
: "](" + state.esc(mark.attrs.href) + (mark.attrs.title ? " " + state.quote(mark.attrs.title) : "") + ")"
}
};
marks.underline = {
open: '<span style="text-decoration: underline;">',
@ -44,9 +75,12 @@ marks.background_color = {
close: '</span>',
};
/**
* @param {MarkdownSerializerState} state
* @param node
*/
function writeNodeAsHtml(state, node) {
const html = docToHtml({ content: [node] });
const html = docToHtml({content: [node]});
state.write(html);
state.ensureNewLine();
state.write('\n');
@ -57,7 +91,7 @@ function writeNodeAsHtml(state, node) {
// or element that cannot be represented in commonmark without losing
// formatting or content.
for (const [nodeType, serializerFunction] of Object.entries(nodes)) {
nodes[nodeType] = function(state, node, parent, index) {
nodes[nodeType] = function (state, node, parent, index) {
if (node.attrs.align) {
writeNodeAsHtml(state, node);
} else {