Created
September 6, 2021 23:00
-
-
Save maybemkl/d9be15bcabadaa19d2ca50c87b59a92e to your computer and use it in GitHub Desktop.
Remove markdown wiki-link brackets during pandoc exports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pandocfilters import toJSONFilter, Str | |
import re | |
def replace(key, value, format, meta): | |
if key == 'Str': | |
if '[[' in value: | |
new_value = value.replace('[[', '') | |
return Str(new_value) | |
if ']]' in value: | |
new_value = value.replace(']]', '') | |
return Str(new_value) | |
if __name__ == '__main__': | |
toJSONFilter(replace) |
Thanks for the original filter code @maybemkl! I was hitting the same problem as @aravindk100 in that the filter would not find the closing ]]
characters, so I modified the script to take advantage of some newer Python 3.8 features which also greatly simplifies the code. Here's my version:
#!/usr/bin/env python3
from pandocfilters import toJSONFilter, Str
import re
def replace(key, value, format, meta):
if key == 'Str':
if match := re.search('\[\[(.+)\]\]',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
Thanks for this great insight @racng . I was able to make this change and get it work except I noticed that the back end of the link did not get filtered correctly.
It went from [[name]] to name] . I found it odd that it was able to replace [[ but only one of the ]
This is the modified code I am using,
#!/usr/bin/env python3
from pandocfilters import toJSONFilter, Str
import re
def replace(key, value, format, meta):
if key == 'Str':
if '[' in value:
new_value = value.replace('[', '')
return Str(new_value)
if ']' in value:
new_value = value.replace(']', '')
return Str(new_value)
if name == 'main':
toJSONFilter(replace)