Skip to content

Commit

Permalink
editor: fix code blocks pasting in a single line without formatting (s…
Browse files Browse the repository at this point in the history
…treetwriters#3910)

* editor: added code support for w3school

* editor: fixed newline issue in per tag code
  • Loading branch information
alihamuh authored Dec 19, 2023
1 parent 8be4fea commit 88fc24c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/editor/src/extensions/clipboard/clipboard-dom-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ClipboardDOMParser extends ProsemirrorDOMParser {
}

parseSlice(dom: Node, options?: ParseOptions | undefined): Slice {
if (dom instanceof HTMLElement) {
if (dom instanceof HTMLElement || dom instanceof Document) {
formatCodeblocks(dom);
convertBrToSingleSpacedParagraphs(dom);
}
Expand All @@ -47,6 +47,7 @@ export class ClipboardDOMParser extends ProsemirrorDOMParser {

export function formatCodeblocks(dom: HTMLElement | Document) {
for (const pre of dom.querySelectorAll("pre")) {
pre.innerHTML = pre.innerHTML?.replaceAll(/<br.*?>/g, "\n");
const codeAsText = pre.textContent;
const languageElement = pre.querySelector(
'[class*="language-"],[class*="lang-"]'
Expand All @@ -58,6 +59,16 @@ export function formatCodeblocks(dom: HTMLElement | Document) {
code.innerHTML = encodeNonAsciiHTML(codeAsText || "");
pre.replaceChildren(code);
}

for (const div of dom.querySelectorAll(".w3-code")) {
div.innerHTML = div.innerHTML?.replaceAll(/<br.*?>/g, "\n");
const codeAsText = div.textContent;
const pre = document.createElement("pre");
const code = document.createElement("code");
code.innerHTML = encodeNonAsciiHTML(codeAsText || "");
pre.replaceChildren(code);
div.replaceChildren(pre);
}
}

export function convertBrToSingleSpacedParagraphs(dom: HTMLElement | Document) {
Expand Down

0 comments on commit 88fc24c

Please sign in to comment.