Logo with initials
Click me x5 ↓

Export Anki to Markdown & Obsidian

Last updated on December 3, 2020

Anki is a fantastic flashcard software. Its export, unfortunately, isn’t so fantastic. It’s quite inconsistent and the output is hard to use.

So, as developers do, I coded my way around it in JavaScript/Node.js.

Steps:

  1. Export Anki decks to JSON with this addon.
  2. Convert the cards from HTML to Markdown with Turndown.
  3. Create and save a Markdown file for each card.
/*
 * How to use?
 * First, install the Anki addon from 'https://ankiweb.net/shared/info/1788670778'
 * then export your deck to JSON. (deck.json)
 */

import fs from 'fs';
import TurndownService from 'turndown';
import data from './deck.json'; // Change it to your JSON file

const turndownService = new TurndownService();

const saveCard = (question, answer) => {
  let md = '';

  const questionMD = turndownService.turndown(question);

  md += '# ' + questionMD + `\n`;

  // sometimes Anki concatenates the question and the answer, easy enough to clean up afterwards
  if (answer) {
    md += turndownService.turndown(answer) + `\n`;
  }

  let fileName = questionMD + '.md';

  /*
   * 👮‍♂️ Warning: Sometimes the filename will be too long, and it will crash
   * If that happens, you can limit its length like this
   */ 👇
  // fileName = questionMD.slice(0, 10) + '.md';

  const filePath = 'export/' + fileName;

  fs.writeFile(filePath, md, (err) => {
    if (err) throw err;
    console.log('The file has been saved!');
  });
};

// run for each card in the deck (In the "notes" array)
data.notes.map((card) => saveCard(card.fields[0], card.fields[1]));

More Obsidian stuff:

Good luck, Alvin