Skip to content

Instantly share code, notes, and snippets.

@maybemkl
Created September 6, 2021 23:00
Show Gist options
  • Save maybemkl/d9be15bcabadaa19d2ca50c87b59a92e to your computer and use it in GitHub Desktop.
Save maybemkl/d9be15bcabadaa19d2ca50c87b59a92e to your computer and use it in GitHub Desktop.
Remove markdown wiki-link brackets during pandoc exports
@archifont
Copy link

Thank you all, this is really helpful, especially when exporting linked notes from Obsidian through pandoc!

Apparently there seemed to be an invalid escape sequence. The regex pattern '\[\[(.+)\]\]' contains backslashes (\). In Python strings, \[ and \] could be misinterpreted as escape sequences. A raw string (r"") tells Python to ignore escape sequences, so \[ and \] are treated as literal brackets instead of escape sequences.

This is the improved version (with help of ChatGTP):

#!/usr/bin/env python3

from pandocfilters import toJSONFilter, Str
import re

def replace(key, value, format, meta):
    if key == 'Str':
        if match := re.search(r'\[\[(.+)\]\]', value, re.IGNORECASE):
            new_value = match.group(1)
            return Str(new_value)

if __name__ == '__main__':
    toJSONFilter(replace)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment