-
Notifications
You must be signed in to change notification settings - Fork 256
Improve support for copy-paste from Microsoft Word #5595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,67 @@ export const paramsToMathMd = ({ latex }) => { | |
| return `$$${latex || ''}$$`; | ||
| }; | ||
|
|
||
| export function sanitizePastedHTML(html) { | ||
| if (!html) return ''; | ||
| // This code ine 55 to 66 is geneted with the help of LLM with the prompt | ||
| // "Create a function that sanitizes HTML pasted from Microsoft | ||
| // Word by removing Word-specific tags, styles, and classes while preserving other formatting." | ||
| let cleaned = html; | ||
| cleaned = cleaned.replace(/<!--\[if.*?endif\]-->/gis, ''); | ||
| cleaned = cleaned.replace(/<\/?(w|m|o|v):[^>]*>/gis, ''); | ||
| const parser = new DOMParser(); | ||
| const doc = parser.parseFromString(cleaned, 'text/html'); | ||
| doc.querySelectorAll('*').forEach(el => { | ||
| if (el.hasAttribute('style')) { | ||
| const style = el.getAttribute('style') || ''; | ||
| const filtered = style | ||
| .split(';') | ||
| .map(s => s.trim()) | ||
| .filter(s => s && !s.toLowerCase().startsWith('mso-')) | ||
| .join('; '); | ||
| if (filtered) { | ||
| el.setAttribute('style', filtered); | ||
| } else { | ||
| el.removeAttribute('style'); | ||
| } | ||
| } | ||
| if (el.hasAttribute('class')) { | ||
| const cls = el | ||
| .getAttribute('class') | ||
| .split(/\s+/) | ||
| .filter(c => c && !/^Mso/i.test(c)) | ||
| .join(' '); | ||
| if (cls) { | ||
| el.setAttribute('class', cls); | ||
| } else { | ||
| el.removeAttribute('class'); | ||
| } | ||
| } | ||
| }); | ||
| const strikeElements = doc.querySelectorAll('s, strike, del'); | ||
| strikeElements.forEach(el => { | ||
| const nestedLists = el.querySelectorAll('ul, ol'); | ||
| if (nestedLists.length > 0) { | ||
| nestedLists.forEach(list => { | ||
| el.parentNode.insertBefore(list, el.nextSibling); | ||
| }); | ||
| } | ||
| }); | ||
| const lists = doc.querySelectorAll('ul, ol'); | ||
| lists.forEach(list => { | ||
| const items = list.querySelectorAll(':scope > li'); | ||
| items.forEach(item => { | ||
| const nestedLists = Array.from(item.children).filter( | ||
| child => child.tagName === 'UL' || child.tagName === 'OL', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these uppercase? Is that coming from the pasted text?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The uppercase comes from the DOM API itself. Browsers normalize element.tagName to uppercase for all HTML elements, regardless of how they appear in the pasted HTML. https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName.
So I think checking against 'UL' and 'OL' is the correct and standard way to identify list elements in sanitized HTML |
||
| ); | ||
| nestedLists.forEach(nestedList => { | ||
| item.appendChild(nestedList); | ||
| }); | ||
| }); | ||
| }); | ||
| return doc.body.innerHTML; | ||
| } | ||
|
|
||
| /** | ||
| * Pre-processes a raw Markdown string to convert custom syntax into HTML tags | ||
| * that Tiptap's extensions can understand. This is our custom "loader". | ||
|
|
@@ -87,6 +148,5 @@ export function preprocessMarkdown(markdown) { | |
| return `<span data-latex="${params.latex}"></span>`; | ||
| }); | ||
|
|
||
| // Use marked.js to parse the rest of the markdown | ||
| return marked(processedMarkdown); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is specific to MS word then maybe putting that in the name is a good idea a la
sanitizeMSWordHTMLor something?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the sanitizer initially targeted MS Word, but we’ve expanded it to handle copy-paste issues from Google Docs and LibreOffice as well (e.g., strike-through bleed, nested list normalization). And since it now applies to all external HTML paste sources, renaming it to sanitizeMSWordHTML would be misleading. Keeping the more general name sanitizePastedHTML better reflects its broader use and sounds good to me.