Skip to content

Instantly share code, notes, and snippets.

@TkTech
Created March 21, 2021 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TkTech/29d8fd346c941c981752595b202ac75d to your computer and use it in GitHub Desktop.
Save TkTech/29d8fd346c941c981752595b202ac75d to your computer and use it in GitHub Desktop.
Rewrite links in Markdown
from humanmark import loads, dumps, ast
doc = loads('[a](../b.c)')
links = doc.find(
# Only find link nodes
ast.Link,
# We don't want to rewrite links that use a reference
f=lambda link: link.reference is None,
# A negative value means to search the entire tree.
depth=-1
)
for link in links:
# Just an example, you probably want to properly parse the URLs.
link.url = link.url.replace('..', 'https://hosted.com')
print(dumps(doc))
@TkTech
Copy link
Author

TkTech commented Mar 25, 2021

@NickHeiner, if you're still looking for an option.

@NickHeiner
Copy link

Thanks!

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