sharkdp/bat

Format hyperlinks in markdown files

Closed this issue · 2 comments

It would be great to have a "style" that allows for rendering

I like [squirrels](https://duckduckgo.com/?q=i%20like%20squirrels&ia=images&iax=images)

as

I like squirrels

with the word "squirrels" underlined and a clickable link. This can be achieved using ascii escape sequences that most terminals understand and allowing the default handlers in the terminal process what to do when clicking on the link.

Example code in python (sorry):

# taken from comment in https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
def terminal_link(url, text):
    return "\033]8;;" + url + "\033\\" + text + "\033]8;;\033\\"


def format_markdown_link(line):
    # TODO check for plain text flag

    # split of line number from the rest of the text
    lineno, *myline = line.split()
    myline = " ".join(myline)

    # find all of the md-formatted links
    match_link = re.findall(r"\[.+\]\(.+\)", myline)  # \(.+\)", line)
    for item in match_link:
        text, url = re.findall(r"\[(.+)\]\((.+)\)", item)[0]
        myline = myline.replace(item, terminal_link(url, text))
        myline.replace(item, terminal_link(url, text))

    return lineno + " " + myline

A very detailed description is given in this reference.

This could be done in rst files, but the regular expression for detecting links would be different.

Thanks for your request. We've received similar questions before (see also #1908 (comment)), and we believe that it is out of scope for bat to transform/replace/remove text. Generally, bat syntax highlights text files/stdin and adds decorations like line numbers but doesn't change the actual content in any way.

To expand on this, indeed, currently bat uses regular expressions defined in a "grammar" to determine what a piece of text means semantically and then uses the theme to color it. But it doesn't "remember" the "meaning", the current implementation using the syntect highlighting library only uses that information to decide how to color it, and bat isn't actually aware of it. So theoretically we don't need to worry about identifying regular expressions to find links in various supported languages, that is already handled. But the codebase would have to change significantly to make it a 3 step process (parse, process text, color text) instead of a one step process (parse and highlight text in one go).

Thanks for the clear explanation. I was hoping it was a small-ish change that would make my files render a lot more like what github does.