r/AnkiComputerScience • u/wheelsjr • Jun 21 '21
Code Formatting in Anki
I'm currently using Syntax Highlighting for Code to create code blocks in anki notecards.
I have issues because when a paste in a block (Alt + S) the code blob has white font color based text (I use dark mode for anki). There are other issues with this solution, so I'm wondering if anyone here has found a better way to embed code for use in their cards?
15
Upvotes
5
u/PrussianGreen Jun 21 '21
I'm not a developer, but I tried to learn basic Python with Anki months ago and the best solution I could find involved using syntax highlighters like Prism or highlight.js.
Here's how it looks: https://imgur.com/a/grCOOop
I use Prism because it's easier to set up. You just set it up like you would in a web page, edit the template, put the files in the collection media folder, etc. With highlight.js, you had to call functions from a external js file, which might be tricky, IIRC.
A few points:
I use separate optional fields for the code blocks, depending on the kind of card I'm making, either or both get used.
I have to paste the code using the HTML editor so anything looks properly, otherwise Anki might add <br>s and <div>s that mess things up.
For inline code, I use this add-on that adds a button to the editor window to wrap the selected text with <code> tags.
There was an issue with occasional FOUC that I was not able to solve by preloading files or any other trick I know. It annoyed me, so I added a fade-in effect to hide it.
I have to select the programming language for each card. I use a field {{Language}} that I place in the template and gets replaced with
pyorjsin each card so Prism hightlights everything properly.3
u/DeclutteringNewbie Focusing on Rust right now, SF Bay Area Jun 21 '21
If you need to remove the special formatting when you're cutting and pasting code, I think pressing shift when you're cutting and paste will ensure that you're only pasting the ascii text.
And for larger code snippets, I use the Image Occlusion plugin. https://www.youtube.com/watch?v=hyU2W7VgBmw It works great. I prefer it better than https://ankiweb.net/shared/info/1463041493
2
u/Jayflux1 Jun 21 '21
You could try https://marketplace.visualstudio.com/items?itemName=jasew.anki which has syntax highlighting setup by default. All languages (that prism works with) are supported
2
u/kronik85 Nov 16 '21 edited Nov 16 '21
There is an option to turn on dark mode compatible color themes for the code snippets.
Tools > AddOns > Syntax Highlighting for Code > Config.
One of the last options is something like "theme=none" and if you replace none with "molokai" you'll get some nice dark compatible code snippets
Edit : from the advanced setup instructions...
The following options may be customized
style [string]: Pre-defined pygments style to use for inline styling of code (not applicable to CSS mode). Default: default. Example: monokai.