tiptap-markdown
Version:
Edit markdown content in tiptap editor.
40 lines (29 loc) • 1.01 kB
JavaScript
export function elementFromString(value) {
// add a wrapper to preserve leading and trailing whitespace
const wrappedValue = `<body>${value}</body>`
return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body
}
export function escapeHTML(value) {
return value
?.replace(/</g, '<')
.replace(/>/g, '>');
}
export function extractElement(node) {
const parent = node.parentElement;
const prepend = parent.cloneNode();
while(parent.firstChild && parent.firstChild !== node) {
prepend.appendChild(parent.firstChild);
}
if(prepend.childNodes.length > 0) {
parent.parentElement.insertBefore(prepend, parent);
}
parent.parentElement.insertBefore(node, parent);
if(parent.childNodes.length === 0) {
parent.remove();
}
}
export function unwrapElement(node) {
const parent = node.parentNode;
while (node.firstChild) parent.insertBefore(node.firstChild, node);
parent.removeChild(node);
}