检测 python 中 markdown 文件中的所有链接并将其替换为字符串函数的输出

Detecting all links in markdown files in python and replace them with outputs of string function

我有一个 python 函数 f(foo: string) -> string。我不写函数的细节,因为它可能会改变。

我需要从 markdown 文件中获取 所有 链接,并将它们替换为该函数的结果。

示例:此链接

This is  a text and this [is first link](http://example.com "Example Title") and
 this [is a second](#example) link.

将替换为

This is  a text and this [is first link](result1 "Example Title") and
 this [is a second](result2) link.

其中 f(http://example.com)=result1f(#example)=result2。即 result1f(http://example.com) 的输出,result2f(#example) 的输出。

我们可以在 python 正则表达式中或使用一些具有 markdown 文件特征的特定包吗?

修改对这个问题的回答,可以这样做:

def find_md_links(md):
    """Returns dict of links in markdown:
    'regular': [foo](some.url)
    'footnotes': [foo][3]
    
    [3]: some.url
    """
    # 

    INLINE_LINK_RE = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
    FOOTNOTE_LINK_TEXT_RE = re.compile(r'\[([^\]]+)\]\[(\d+)\]')
    FOOTNOTE_LINK_URL_RE = re.compile(r'\[(\d+)\]:\s+(\S+)')

    links = list(INLINE_LINK_RE.findall(md))
    footnote_links = dict(FOOTNOTE_LINK_TEXT_RE.findall(md))
    footnote_urls = dict(FOOTNOTE_LINK_URL_RE.findall(md))

    footnotes_linking = []
        
    for key in footnote_links.keys():
        footnotes_linking.append((footnote_links[key], footnote_urls[footnote_links[key]]))

    return {'regular': links, 'footnotes': footnotes_linking}


def replace_md_links(md, f):
    """Replace links url to f(url)"""
    
    links = find_md_links(md)
    newmd = md

    for r in links['regular']:
        newmd = newmd.replace(r[1], f(r[1]))

    for r in links['footnotes']:
        newmd = newmd.replace(r[1], f(r[1]))
    
    return newmd

f 是一个函数。例如,我使用这个函数,它只更改属于 # in replace_md_links

的链接
def mychange(s, prefix="/static/entrades/", suffix=".md.html"):
    """Change links from tiddlywiki syntax [foo](#something) to [foo](prefix + something + suffix)"""
    
    if s.startswith('#'):
        return prefix + slugify.slugify(urllib.parse.unquote( s.replace('#', '', 1) )) + suffix
    else:
        return s