MCPcopy
hub / github.com/marktext/marktext / normalizePastedHTML

Function normalizePastedHTML

packages/muya/src/utils/paste.ts:37–113  ·  view source on GitHub ↗
(html: string)

Source from the content-addressed store, hash-verified

35}
36
37export async function normalizePastedHTML(html: string) {
38 // Only extract the `body.innerHTML` when the `html` is a full HTML Document.
39 if (/<body>[\s\S]*<\/body>/.test(html)) {
40 const match = /<body>([\s\S]*)<\/body>/.exec(html);
41 if (match && typeof match[1] === 'string')
42 html = match[1];
43 }
44
45 // Prevent XSS and sanitize HTML.
46 const sanitizedHtml = sanitize(
47 html,
48 PREVIEW_DOMPURIFY_CONFIG,
49 false,
50 ) as string;
51 const tempWrapper = document.createElement('div');
52 tempWrapper.innerHTML = sanitizedHtml;
53
54 // Special process for turndown.js, needed for Number app on macOS.
55 const tables = Array.from(tempWrapper.querySelectorAll('table'));
56
57 for (const table of tables) {
58 const row = table.querySelector('tr');
59 if (row && row.firstElementChild?.tagName !== 'TH') {
60 [...row.children].forEach((cell) => {
61 const th = document.createElement('th');
62 th.innerHTML = cell.innerHTML;
63 cell.replaceWith(th);
64 });
65 }
66 const paragraphs = Array.from(table.querySelectorAll('p'));
67
68 for (const p of paragraphs) {
69 const span = document.createElement('span');
70 span.innerHTML = p.innerHTML;
71 p.replaceWith(span);
72 }
73
74 const tds = table.querySelectorAll('td');
75
76 for (const td of tds) {
77 const rawHtml = td.innerHTML;
78 if (/<br>/.test(rawHtml))
79 td.innerHTML = rawHtml.replace(/<br>/g, '&lt;br&gt;');
80 }
81 }
82
83 // Prevent it parse into a link if copy a url.
84 const links: HTMLElement[] = Array.from(
85 tempWrapper.querySelectorAll('a'),
86 );
87
88 for (const link of links) {
89 const href = link.getAttribute('href');
90 const text = link.textContent;
91
92 if (href === text && typeof href === 'string') {
93 // Resolve empty string when `TIMEOUT` passed.
94 const timer = new Promise((resolve) => {

Callers 1

pasteHandlerMethod · 0.90

Calls 5

sanitizeFunction · 0.90
replaceWithMethod · 0.80
getPageTitleFunction · 0.70
forEachMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected