8

What is the best way to clear the formatting of text pasted in from a different source (like Microsoft Word) into a rich text field (RTF) field in SDL Tridion?

The text pasted should be plain text so that it has no formatting, and the text should be pasted directly into the rich text field (i.e. not via Notepad).

11

Without knowing your full requirements, it is difficult to propose a specific solution. However, some options that you may want to consider are:

  1. Make the Schema field plain text. Obvioulsy this isn't an option if the text needs to be formatted after it has been pasted (as plain text).

  2. Use the browser's "Paste as plain text" functionality (CTRL + SHIFT + V in Chrome)

enter image description here

  1. Write a GUI extension to strip out the HTML/Word formatting when pasting

  2. Use the Paste Special Cross Browser Extension GUI extension from SDL Tridion World (a nod to Indra & Raj here).

enter image description here

  1. Configure the XSLT filtering (Set on the rich text field within the Schema) to strip out the Word formatting when you save the Component. This question by Will (and answer by Bart) may help here: Word/HTML cleanup XSLT for Rich Text Field example

  2. Use a tool such as PureText to strip out formatting when pasting

enter image description here

  1. Write some Event System code to clean up the pasted content on Component Save, using a library such as HTML Tidy

  2. Paste into Notepad (or a similar plain text editor) first, and then from here into the rich text field.

  3. Use an online tool (such as Clean my text) to clean up the content before pasting

Options 8 & 9 obviously go against the "the text should be pasted directly not via notepad" requirement that you have, but I've included them here for completeness.

  • @ Jonathan - PasteSpecial for RTF is a great GUI extension. I have downloaded the PasteSpecial for RTF from appstore.sdl.com. But the solution is not compatible with SDL web 8.5. Would you be able to help with the source code for this solution. I can work on the fix for SDL Web 8.5 Please let me know. – Mohammed Ali Sep 7 '18 at 20:40
  • I’m afraid not. You’re probably better off speaking to the original developers. – Jonathan Williams Sep 7 '18 at 20:46
1

Old post...but to go along with Johnathan's answer, I made an extension that just used RegEx to clean up the paste:

                // Clear the Word header
                wordGarbage = wordGarbage.replace(/<html[\S\s]*<!--StartFragment-->[\S\s]*?</gim,"<");

                // Fix the lists
                wordGarbage = wordGarbage.replace(/<p class=MsoListParagraphCxSpFirst[\S\s]*?mso-list:l1[\S\s]*?<\!\[endif\]>([\S\s]*?)<\/p>/gim,"<ul><li>$1</li>");
                wordGarbage = wordGarbage.replace(/<p class=MsoListParagraphCxSpFirst[\S\s]*?mso-list:l0[\S\s]*?<\!\[endif\]>([\S\s]*?)<\/p>/gim,"<ol><li>$1</li>");
                wordGarbage = wordGarbage.replace(/<p class=MsoListParagraphCxSpMiddle[\S\s]*?<!\[endif\]>([\S\s]*?)<\/p>/gim,"<li>$1</li> ");
                wordGarbage = wordGarbage.replace(/<p class=MsoListParagraphCxSpLast[\S\s]*?mso-list:l1[\S\s]*?<\!\[endif\]>([\S\s]*?)<\/p>/gim,"<li>$1</li></ul>");
                wordGarbage = wordGarbage.replace(/<p class=MsoListParagraphCxSpLast[\S\s]*?mso-list:l0[\S\s]*?<\!\[endif\]>([\S\s]*?)<\/p>/gim,"<li>$1</li></ol>");

                // Remvoe all the classes
                wordGarbage = wordGarbage.replace(/[\s]?class=[\S\s]*?>/gim,">");

                // Get the radio button that was selected
                var whatToDo = document.querySelector('input[name = "action"]:checked').value;

                if (whatToDo === "keepStyles") {
                    // Just remove the mso entries
                    wordGarbage = wordGarbage.replace(/mso[\S\s]*?([;'])/gim,"$1");
                }
                else if (whatToDo === "removeStyles")
                {
                    // Remove all styles
                    wordGarbage = wordGarbage.replace(/[\s]?style=[\S\s]*?>/gim,">");
                }

                // Remove empty paragraphs
                wordGarbage = wordGarbage.replace(/<o:p><\/o:p>/gim,"");

                // Clean up breaks
                wordGarbage = wordGarbage.replace(/<p><o:p>&nbsp;<\/o:p><\/p>/gim,"<p>&nbsp;</p>");

                // Remove all the styles in the table datas
                //wordGarbage = wordGarbage.replace(/<td[\S\s]*?>/gim,"<td>");
                //wordGarbage = wordGarbage.replace(/(<td[\s\S]*?)style=[\s\S]*?>/gim,"$1>");

                // Clear the footer
                wordGarbage = wordGarbage.replace(/[\s]*<!--EndFragment-->[\S\s]*<\/html>/gim,"");

I used the extension example to pop a window, get the paste from the users clipboard with the help of this, and then return it cleaned up. Only catch is that this does not work for nested lists.

Hope this helps someone. Enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.