Skip to main content

Convert Markdown files into Anki cards via AnkiConnect

Project description

Markdown to Anki

markdown-to-anki is a lightweight Python CLI that automatically converts raw Markdown files into Anki cards using AnkiConnect. It's designed for learners and note-takers who prefer writing in Markdown and want to seamlessly sync their content into Anki.

Installation

pip install markdown-to-anki

Requires Python 3.10+ and AnkiConnect running in your Anki app.

Getting Started

Open Anki, then run:

m2a --folder ~/notes/anki init all

This creates the note models and imports your media and notes. After that, sync anytime with:

m2a --folder ~/notes/anki anki sync

Commands

Command Description
m2a init all Create/update Anki note models, then import media and notes.
m2a anki check Verify the AnkiConnect server is reachable.
m2a anki init Create/update Anki note models only.
m2a anki sync Import media and notes, then trigger an AnkiWeb sync.
m2a anki sync_web Trigger an AnkiWeb sync only (no re-import). Useful for cron.

Only files modified within the last TIME_RANGE seconds (default 2 hours) are processed.

Configuration

Settings are resolved in this order — highest priority wins:

CLI flag  >  environment variable / .env  >  config file  >  default

CLI flags

m2a --folder ~/notes/anki --url http://localhost:8765 anki sync
m2a --resources ~/my-templates init all
Flag Description Default
--folder Path to your Markdown notes dir see Config file
--url AnkiConnect URL http://localhost:8765
--resources Path to custom card templates/styles directory built-in resources

Config file

Create ~/.config/markdown-to-anki/config.yaml (respects $XDG_CONFIG_HOME):

md_folder: ~/notes/anki
time_range: 7200
anki_url: http://localhost:8765
resources_dir: ~/notes/anki-templates

Environment variables / .env

Place a .env file in the directory where you run m2a, or set the variables in your shell:

MD_FOLDER=~/notes/anki
TIME_RANGE=7200
ANKI_URL=http://localhost:8765
M2A_RESOURCES_DIR=~/notes/anki-templates

Custom Resources

Point --resources (or resources_dir / M2A_RESOURCES_DIR) at your own directory to override built-in templates/styles and define new note types. Only the files you provide are overridden — everything else falls back to the built-in defaults.

Override built-in templates or styles

Mirror the built-in layout under your resources directory:

my-resources/
└── anki/
    ├── styles/
    │   └── basic.css          # overrides the built-in basic.css
    └── templates/
        └── basic/
            └── front.html     # overrides the built-in front template

Define custom note types

Place a YAML file per model inside a models/ subdirectory:

my-resources/
├── models/
│   └── my-vocab.yaml
├── styles/
│   └── my-vocab.css
└── templates/
    └── my-vocab/
        ├── front.html
        └── back.html

models/my-vocab.yaml:

name: my-vocab
fields:
  - Word
  - Definition
  - Example
is_cloze: false
css_file: styles/my-vocab.css      # relative to my-resources/
templates:
  - name: word-to-definition
    front_file: templates/my-vocab/front.html
    back_file: templates/my-vocab/back.html

Inline CSS and HTML are also supported instead of file references:

name: my-simple
fields:
  - Question
  - Hint
  - Answer
css: ".card { font-family: sans-serif; }"
templates:
  - name: card
    front: "<div>{{Question}}</div><div class='hint'>{{Hint}}</div>"
    back: "<div>{{Answer}}</div>"

Fields map to <!--FIELD--> sections in order. A card with three fields uses two separators:

---
model: my-vocab
deck: Languages::English
---

<!--CARD-->

hello

<!--FIELD-->

A common English greeting.

<!--FIELD-->

"Hello, how are you?" — used when meeting someone.

Run m2a --resources my-resources/ init all to register your models in Anki before syncing notes.

Markdown Style

Each Markdown file may contain one or more cards. Use YAML frontmatter for per-file metadata and the comment separators below to split cards and fields:

---
deck: M2A::Example::Math
tags: math, theorem
model: m2a-basic
# skip: 1   # uncomment to skip this file
---

<!--CARD-->

### Front of card 1

<!--FIELD-->

Back of card 1

<!--CARD-->
<!--TAGS: hard, exam-->

### Front of card 2

<!--FIELD-->

Back of card 2

Supported metadata keys: deck, tags, model (one of m2a-basic, m2a-basic-reverse, m2a-english), skip.

Per-card tags via <!--TAGS: a, b--> (anywhere inside a card section) are merged with the file-level tags from frontmatter.

See the example notes for more details.

License

This project is open-sourced and licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_to_anki-0.1.0.tar.gz (84.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_to_anki-0.1.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file markdown_to_anki-0.1.0.tar.gz.

File metadata

  • Download URL: markdown_to_anki-0.1.0.tar.gz
  • Upload date:
  • Size: 84.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for markdown_to_anki-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a2aca74ec0abcd0f94612416d5b1d77ef22ec862bd714a83b31bba54a3f9389a
MD5 b94ac9149962fb42c5e8e7697d1beb59
BLAKE2b-256 19c8b44f1df731ba14d5fa1c027a5eaae8b53be514ad15dba6c50d699be2326b

See more details on using hashes here.

File details

Details for the file markdown_to_anki-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: markdown_to_anki-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for markdown_to_anki-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 592076ca729fcc45e8db60f29ff84ec497b34b7352ea7eb0adc612e6b6a91893
MD5 01cef6f53cf8976528e224564fdce410
BLAKE2b-256 24d837a7d5a63167f40c4c946950090f43a6ae05f8b2219115c6fbdce2de94f4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page