Have Pandoc Recognize Your Literature Links as Citations!

I use the Citations plugin to manage citations.

Within Obsidian, I really would prefer to have links to the literature note rather than citations:

(1) [[@darwin-1856-origin]]

But, of course, for Pandoc to render this citation properly this should be:

(2) [@darwin-1856-origin]

Pandoc recognizes the second as a citation, but wraps the finalrendering with the extra brackets .

The following filters the token stream to strip the extra brackets from the citation.

#! /usr/bin/env python
# -*- coding: utf-8 -*-

from pandocfilters import toJSONFilter, Str, Plain
import sys
import re

def replace_citation_links_with_citation(key, value, format, meta):
    # sys.stderr.write(f"=== {key} ===>\n")
    # sys.stderr.write(f"{value}\n\n")
    elements = []
    if key == "Plain" or key == "Para":
        for item_idx, item in enumerate(value):
            if item["t"] == "Str" and item["c"] == "[":
                if item_idx < len(value) - 1:
                    if value[item_idx + 1]["t"] == "Cite":
                        continue
            elif item["t"] == "Str" and item["c"] == "]":
                if item_idx > 0:
                    if value[item_idx - 1]["t"] == "Cite":
                        continue
            elements.append(item)
        if key == "Plain":
            return Plain(elements)
        elif key == "Para":
            return Para(elements)


if __name__ == "__main__":
    toJSONFilter(replace_citation_links_with_citation)

If saved to a file, e.g. ~/.local/share/pandoc-filters/obisidian-citation-links.py, and invoked when running pandoc by --filter ~/.local/share/pandoc-filters/obisidian-citation-links.py, then something like this:

The best cookies in the world have cardamom in them [[@darwin-1856-origin]] .

will look like the following to Pandoc.

The best cookies in the world have cardamom in them [@darwin-1856-origin] .

So you can now have your citations and link them too.