Convert markdown links to relref link?

Hi, everyone.

I have several markdown files, each with some links like this:

[anotherfile](anotherfile.md)

However, I would like to convert all these links to:

[anotherfile]({{< relref "anotherfile.md" >}})

Is there any easy approach to do this? I believe this can be done with regular expression, or there is some way to avoid this conversion?

Thanks!

Hm… Maybe try the replace function. But then again that might be a performance hit.

Take a look at the following comment in this Github issue. Someone was able to add a class to a <table> that was inserted with markdown. https://github.com/spf13/hugo/issues/1585#issuecomment-235548558

Regexp

Find: \]\(([^)]+)\)
Replace: ]({{< relref "$1" >}})

something like this, but not tested.

1 Like

Thanks very much! I slightly modified it so that the following shell command works in my case.

sed 's/\](\([A-Za-z-]*\.md\))/]({{< relref "\1" >}})/g' myfile.md

Best.

1 Like

Thanks for the hit! There are not too many files so performance is not a problem, however I am not familiar with the template language, but I have solved the problem in my case with regular expression.

Thanks again!

1 Like