[Guide] How to convert markdown/mathjax into Anki (HTML) using Sublime Text

How to Convert markdown text/notes into Anki cards (and optional CSS/JS setup)

(NOTE: skip to “How to use” to see examples)

(Big shoutout to the devs for making things like this possible!)

Requirements:

  • Sublime Text 4
    • Packages/Add-ons:
      • MarkdownEditing
      • Markdown Preview
  • Anki Version 2.1.50+
    • (any version that supports new HTML editor)

Setting up:

Step 1: Install Sublime Text

  • (this exercise is left to the reader)

Step 2: Install Markdown Preview

  • Open command palette
    • cmd-shift-p or ctrl-shift-p depending on system
  • Type install and you should see a command called “Package Control: Install Package”; click on it or hit enter
  • Type markdown preview and hit enter to install

Step 3: Install MarkdownEditing

  • Similar to above
  • (this just allows you to write in markdown syntax to work with Markdown Preview)

Step 4: Configure Markdown Preview

  • Sublime Text menus, do Preferences > Package Settings > Settings
  • You should now see two panels side by side. The right side is the one that you can change (it’s the user configurable one)
  • It should be a file that looks like this at the highest (outermost level):
{
     (lines of code ...)
     (lines of code ...)
}
  • If you look at one level deeper in the structure, you should see this:
{
    (lines of code ...)

    "markdown_extensions":  [

        (lines of code ...)
    ]
}
  • Within this “markdown_extensions” structure, you will want to include certain lines of code which enable helpful parts of the conversion from markdown into HTML.
  • Without changing what’s inside too much, you will want to make sure you have these lines of code:
"markdown_extensions": [
    (lines of code ...)
    {
        "pymdownx.highlight": {
            "use_pygments": false
        }
    },
    {
        "pymdownx.arithmatex": {
            "generic": true
        }
    },
    (lines of code ...)
]
  • You will also see a line of code with "markdown.extensions.meta", in here. You will want to disable it by adding two // in front of it, so it will look something like this:
"markdown_extensions": [
    (lines of code ...)
    // "markdown.extensions.meta",
    (lines of code ...)
]
  • Finally, going back to the top level structure, you will want to have these lines (ignore // ... ):
{
    "enabled_parsers": ["markdown"],
    // ...
    "include_head": ["build", "browser", "sublime", "save"],
    "build_action": "clipboard",
    "markdown_extensions": [
        // ...
    ]
}

Optional Configuration in Anki

CSS

  • Markdown Preview converts *foo* and **bar** into <em>foo</em> and <strong>bar</strong>, so you could add this to the “styling” portion of an Anki template:
b, strong {
    /* what you want for style */
}
i, em {
    /* what you want for style */
}
  • Markdown Preview automatically wraps everything in HTML <p> which can create extra space; and with Mathjax, block equations are wrapped in <div> elements. So I put this in my CSS:
p {
	display: contents;
}

div.arithmatex {
	display: contents;
}
  • This next set of CSS code below is more useful if writing code in Anki and for the Javascript portion and really not important at all:
code {
	font-size: 24px;
}

:not(pre) > code {
	color: #e95d00;
	background-color: #e4e4e4;
	font-size: 24px;
}

.nightMode :not(pre) > code {
	color: #d58000;
	background-color: #444444;
}

pre {
text-align: left;
}

Javascript

  • If you write in a code, Anki allows you to use a code highlighter to make it look pretty and help with syntax. ex:

  • If you do use a highlighter, it can work really well with this Markdown Preview setup

How I set up my highlighter

(I got this from another website post for Anki; not my idea, just my steps)

  1. Go to highlightjs.org and download the basic package (which includes a lot of languages).
  2. Go to your collection.media folder for Anki (which for Mac is found ~/Library/Application Support/Anki2) and add these 2 files found in your downloaded highlight.js package:
    • highlight.min.js file
    • your selected theme file, which for me is atom-one-dark-reasonable.min.css
  3. Create a new file called my_highlight.js and paste this inside of it:
document.querySelectorAll('pre code').forEach((block) => {
    hljs.highlightBlock(block);
});
  1. Put this 3rd file in your collections.media folder in Anki
  2. Add this javascript to the Front part of the template:
<link rel="stylesheet" href="atom-one-dark-reasonable.min.css">


<script>
    if (typeof hljs === "undefined") {
        var script = document.createElement('script');
        script.src = "highlight.min.js";
        script.async = false;
        document.head.appendChild(script);
    }

    var script = document.createElement('script');
    script.src = 'my_highlight.js';
    script.async = false;
    document.head.appendChild(script);
    document.head.removeChild(script);
</script>

<script>hljs.highlightAll();</script>

How to use

Basic

  • In Sublime Text, open up the command palette and type: ssmd and hit enter on the Set Syntax: Markdown command to set the syntax

  • Write any markdown you want, ex:

  • In command palette, you should type and see a command called “Build” or “Build With: Markdown”; (I believe this should also be cmd-b or ctrl-b in Sublime). Use that and the markdown will convert to HTML and be in your clipboard, ready to paste into Anki

  • In Anki, open the HTML editor and paste into there, done!

Advanced

Mathjax

  • With this setup, you can now convert $ and $$ mathjax delimiters into version Anki will understand
  • Important: With $$ (block mathjax), there needs to be an empty line above and below the block, like so:
foo

$$this is okay$$

bar

$$
this is
also okay!
$$

baz

Example Use:
Markdown

Converted into Anki HTML:

Code highlighting

  • If you set up the javascript as described in the earlier section, you can get syntax highlighting by writing the language in fenced codeblocks:

Example Markdown:

Then after pasting the conversion into Anki HTML:

This will be the result when actually previewing the card:

3 Likes