Paste while preserving formatting on Anki 2.1

Florian's Avatar

Florian

08 Oct, 2019 08:07 AM

Hi,
I am still stuck with Anki 2.0 because there is one feature I cannot find in Anki 2.1: Paste while preserving formatting. I created most of my cards by copy & pasting parts of Wikipedia entries. They look nice and I want new cards to look like them. I know about ctrl+shift+v, which preserves links but not the actual formatting.

Is it still possible in Anki 2.1? If not, is there at least some kind of "hacky" way of being able to create cards with the formatting preserved - just in case if, e.g., syncing stops working at some point in time (because Anki 2.0 is discontinued).

Thanks and greetings,

Florian

  1. Support Staff 1 Posted by Damien Elmes on 09 Oct, 2019 03:03 AM

    Damien Elmes's Avatar

    Improving the paste filtering is on the todo list. Please provide a link or two, and indicate the parts of the formatting you're trying to preserve.

  2. 2 Posted by Florian on 16 Oct, 2019 09:12 AM

    Florian's Avatar

    Hi,
    Anki 2.0 simply preserves everything, which is pretty good. I provide an example in the screenshots. The example is from the article on general relativity on Wikipedia.
    I have attached the version as shown in the browser I am copying from (chrome.png), the result in Anki 2.0 (Anki2.0.png), the "normal" (Ctrl+V) pasting in Anki 2.1 (Anki2.1png), and the result obtained when also pressing shift in Anki 2.1 (Anki2.1withShift.png).
    A result without any filters would work fine for me. If you are asking what I am precisely missing (I exclude the base font because I think that can be changed easily in the card properties), then it is that figures cannot be beside the text. And the boxes get lost, making the caption harder to associate with the image. (Pasting formulas with MathJax also does not work anymore, but by switching to png they can at least be copied).

    Anyways, if it is some change in philosophy that pure html should not be pastable anymore, then I accept that. It is not necessary that these specific features are implemented in some filter. I merely wanted to ask if I am missing something because it sounded as it Anki 2.1 should now succeed Anki 2.0, so I thought it may have the feature but may have moved somewhere else.

    If anybody else misses this feature. Installing Anki 2.0 as 32 bit Version and Anki 2.1 in 64 bit simultaneously works fine for me. So one can have Anki 2.1 by default and use 2.0 for adding cards.

  3. Support Staff 3 Posted by Damien Elmes on 16 Oct, 2019 11:21 AM

    Damien Elmes's Avatar

    Thanks for those examples Florian, I'll give this some thought.

  4. 4 Posted by Ben on 29 Nov, 2019 01:53 PM

    Ben's Avatar

    Hi Florian, I too very much miss the option to preserve formatting. I found a "hacky" way to paste with formatting for the time being and thought it might help others who may be in an urgent situation like me. The hack is to locate editor.js under ${Anki_Installation_folder}/web/ then comment out the line "html = filterHTML(html, internal, extendedMode);". I am not sure whether there are other unforeseeable consequences but for now it meets my needs and I will try to survive using it before an official solution is available.

  5. 5 Posted by Florian on 30 Nov, 2019 07:51 AM

    Florian's Avatar

    Hi Ben, works like a charm! Thanks a lot for this solution. (If there are no side effects, a checkbox to enable/disable that line would be great, so it does not have to be redone when updating Anki).
    Now I can finally use Anki 2.1, which will surely be the state of the art for many years to come ;).

    Greetings,
    Florian

  6. Support Staff 6 Posted by Damien Elmes on 30 Nov, 2019 08:06 AM

    Damien Elmes's Avatar

    Turning off filtering is not ideal, because some sources included a huge amount of superfluous formatting, which can easily inflate storage requirements a hundred fold. If you're only pasting from websites with relatively clean HTML you may be ok, but adding a switch is not the best general solution.

  7. 7 Posted by Ben on 30 Nov, 2019 04:10 PM

    Ben's Avatar

    Hi Damien,
    Thanks for the insight! I agree that completing turning off filtering is not an ideal solution to the problem, especially considering the storage space. Luckily the amount of information I paste in my flashcard is relatively small and the html contents are relatively clean. I will try to use it only for the time being and consider other options such as adding a css file for html from some fixed-format sources if the html tags can be somehow preserved after filtering.

    Best wishes,
    Ben

  8. 8 Posted by Elizabeth on 05 Dec, 2019 11:27 PM

    Elizabeth's Avatar

    Hi Ben,

    I'm not very good with computers. Can you explain what this means?:
    The hack is to locate editor.js under ${Anki_Installation_folder}/web/ then comment out the line "html = filterHTML(html, internal, extendedMode);

    I'm also having problems because when I have some highlighted in Word but when I try to copy-paste it into Anki 2.1 all formatting gets stripped.

    Thank you,
    Elizabeth

  9. Support Staff 9 Posted by Damien Elmes on 06 Dec, 2019 05:56 AM

    Damien Elmes's Avatar

    I don't recommend you do that, as Word's unfiltered output is extremely verbose. I suggest you try the beta that has just been released, as it should preserve the text foreground and background color when pasting.

    https://apps.ankiweb.net/docs/beta.html

  10. 10 Posted by Elizabeth on 06 Dec, 2019 06:03 AM

    Elizabeth's Avatar

    I have windows. I think there are 4 versions, is the a noticeable difference between them?
    anki-2.1.16beta1-2eb7560-windows-alternate.exe
    anki-2.1.16beta1-2eb7560-windows.exe
    anki-2.1.16beta2-bceb4feb-windows-alternate.exe
    anki-2.1.16beta2-bceb4feb-windows.exe

  11. Support Staff 11 Posted by Damien Elmes on 06 Dec, 2019 06:08 AM

    Damien Elmes's Avatar

    The one you probably want is anki-2.1.16beta2-bceb4feb-windows.exe

  12. 12 Posted by Elizabeth on 06 Dec, 2019 06:14 AM

    Elizabeth's Avatar

    OMG thank you so much! That worked!!

  13. 13 Posted by Elizabeth on 06 Dec, 2019 03:50 PM

    Elizabeth's Avatar

    Update: I am unable to paste an outline with bullets from Word into Anki 2.1. The text pastes into Anki with all the text at the left (instead of keeping the indents).

    Is there a way to keep the outline format? I've tried changing word doc to PDF and it it still doesn't keep the formatting when pasting into Anki. There was a way I got around this in 2.0 but I forget how I did it.

    Thank you SO much!

  14. Support Staff 14 Posted by Damien Elmes on 06 Dec, 2019 10:26 PM

    Damien Elmes's Avatar

    Word doesn't use a standard way to represent lists, so they come out looking a bit strange. If you open your documents in the free LibreOffice program instead and paste from there, the lists should come out looking better.

  15. 15 Posted by Elizabeth on 08 Dec, 2019 02:11 PM

    Elizabeth's Avatar

    THANK YOU so much!!!!!!!! Yes the lists are coming out great so far!! The only thing it's that it's still stripping the highlights that I had in Word. Is there a way to get around that?

  16. 16 Posted by Elizabeth on 08 Dec, 2019 02:13 PM

    Elizabeth's Avatar

    Also: font color does transfer over successfully into Anki from LibreOffice

  17. 17 Posted by Elizabeth on 08 Dec, 2019 05:15 PM

    Elizabeth's Avatar

    Also: Bold, italics, underline do not transfer (i.e. copy-paste) into Anki from LibreOffice. Sorry for the multiple emails, I'm finding these out as I go

  18. Support Staff 18 Posted by Damien Elmes on 08 Dec, 2019 11:45 PM

    Damien Elmes's Avatar

    The foreground/background color in Word seems to copy across for me - perhaps the text you're copying has been styled some other way?

    Bold/italics/underline not working is a bug in the beta - I'll fix it in the next beta update and let you know when it's ready.

  19. Support Staff 19 Posted by Damien Elmes on 09 Dec, 2019 04:53 AM

    Damien Elmes's Avatar

    Beta 3 is now available, and should fix bold/italics/underline issue.

  20. 20 Posted by Elizabeth on 09 Dec, 2019 12:57 PM

    Elizabeth's Avatar

    Thank you Damien! Yes the italics/bold/underline is copying over now!! I really appreciate it! Text with colored highlighted background color still isn't transferring over from LibreOffice for me..? :-(

  21. Support Staff 21 Posted by Damien Elmes on 09 Dec, 2019 09:24 PM

    Damien Elmes's Avatar

    Is it LibreOffice or Word you're having trouble with? They're both working for me - could you please attach the document you're pasting from as a private ticket, and let me know which part I should copy from to reproduce the problem?

  22. 22 Posted by Elizabeth on 09 Dec, 2019 09:34 PM

    Elizabeth's Avatar

    It's Word that is not transferring the outline. Amazingly, LibreOffice did not transfer the highlighted background color initially, but once I saved the document as I was preparing to send it to you, I thought I would check it one last time and it worked. So it needs to be a saved LibreOffice to transfer both the highlight and outline bullets? (I will attached the Word document in a new thread like you are requesting)

  23. Support Staff 23 Posted by Damien Elmes on 09 Dec, 2019 11:04 PM

    Damien Elmes's Avatar

    Thanks for the test file. It's tricky to properly support the lists coming from Word I'm afraid, so LibreOffice is your best bet. Sadly it seems to ignore the color formatting until you save the document as an .odf file, close and then reopen it, but after doing so everything seems to paste correctly.

    If you're creating these documents yourself, you may want to have a look at the Anki addons available, as I believe there is one that gives you access to list buttons, and there is one that lets you assign different colours to different shortcut keys.

  24. 24 Posted by Arash on 03 Jan, 2020 10:53 PM

    Arash's Avatar

    There are also some online "Rich Text to HTML" convertors that you can use them to preserve the formatting of copied texts (wordhtml.com, for example).

    At those websites, you can paste your copied rich-formated text in the "word" section, then select all characters from the "html" section and copy it.

    Then, in an Anki text field, press "Ctrl+Shift+X" and paste (Cntl+V).

    It takes a little time, but works well as a temporary option.

  25. 25 Posted by Walter on 04 Jan, 2020 06:33 PM

    Walter's Avatar

    Hi Damien,

    I've read the thread thus far and so far I've triend everything and nothing seems to have helped.
    I like to use Anki to paste all my notes and then form questions based on the headings that I have on my notes (Image 1). I make notes on Onenote and then copy and paste them into Anki to make flashcards.

    With the Anki 2.1 update I am no longer to use option of retaining this formating by using the tickbox of "stripping HTML". As a result I have had to result to screenshotting pictures of my notes and using that instead which is really not ideal as it doesn't scale very well and I am not able to edit my flashcards as its a picture and not text.

    I have tried to use AnkiWeb to by pass this and while Ankiweb does preserve the formatting, it does not allow me to copy and paste pictures in.

    If you guys could add in the "HTML Stripping" option to Anki 2.1 like there was for Anki 2.0 it would really be a life saver!

    Thanks and Regards,
    Walter

  26. Support Staff 26 Posted by Damien Elmes on 05 Jan, 2020 03:33 AM

    Damien Elmes's Avatar

    No images were attached. Please attach a small file with instructions on a private ticket, so I can try to reproduce the issue.

  27. 27 Posted by Walter on 05 Jan, 2020 07:35 AM

    Walter's Avatar

    Hi Damien,

    Thank you for your response. I'm not entirely sure on how to create a private ticket. But I have attached the file to this post.

    Thank you very much for your help & regards,
    Walter

  28. Support Staff 28 Posted by Damien Elmes on 06 Jan, 2020 10:48 AM

    Damien Elmes's Avatar

    Thanks, I've made a note to see if there's anything I can do.

  29. 29 Posted by ilya_nice on 07 Jan, 2020 11:39 AM

    ilya_nice's Avatar

    For anyone struggling with Anki not preserving whitespace, I figured out a temp solution that's not bloated in terms of space.

    Find the folder called web in anki installation and find editor.js file inside.

    My problem was that Anki was not preserving indentation and ctrl+shift+v did not help at some times, e.x. when pasting code from editors on web pages it just turned the code into a one-line mess. I didn't want to preserve colors, I just wanted to preserve indentation, so my solution strips colors away.

    1) in editor.js file, find the line beginning with var filterHTML = function (html, internal, extendedMode) ... and inside that function comment out these lines:

    outHtml = outHtml.replace(/[\n\t ]+/g, " "); and
    outHtml = outHtml.trim();

    2) Find a function in the same file that begins with var filterNode = function (node, extendedMode) and inside this function find the following lines:

    if (node.tagName === "ANKITOP") {
            return;
        }
    

    Right below this if statement, paste in this code:

        if (node.style) {
            node.style.removeProperty("background-color");
            node.style.removeProperty("font-size");
            node.style.removeProperty("font-family");
            node.style.removeProperty("color");
        }
        node.removeAttribute("class");
        return;
    

    What it will do is will remove classes and some styles from the pasted text, but at the same time it will preserve text indentation. Basically it will give you the effect of Anki <2.1.

    If you do want to preserve colors, you can paste only the word return from step 2. That will preserve all colors but on the white background I guess it will look a bit ugly :)

  30. Support Staff 30 Posted by Damien Elmes on 16 Jan, 2020 01:55 AM

    Damien Elmes's Avatar

    Walter: I should be able to address the table background issue in a future update. Could you elaborate on what you mean about the bullet points resetting back to zero?

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac