Skip to content

Commit

Permalink
adjust regular expression for .md links
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Oct 31, 2024
1 parent 3569d4d commit 6613b37
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions gm_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func compile(markdown []byte) (html []byte, err error) {

// regexMdLink is used to identify .md links like href="xxxx.md"
// and .md links with tags like href="filename.md#tagname"
var regexMdLink = regexp.MustCompile(`href\s*=\s*"([^"]+)\.md#?[^"]*?"`)
var regexMdLink = regexp.MustCompile(`href\s*=\s*"[^"]+?\.md#?[^"]*?"`)

// replaceLinks replaces all links like href="path/xxxx.md#tag" to href="path/xxxx.html#tag"
// if the file `path/xxxx.md` exists
Expand All @@ -86,12 +86,12 @@ func replaceLinks(html []byte, dir string) []byte {
return regexMdLink.ReplaceAllFunc(html, func(s []byte) []byte {
fullhref := strings.Split(string(s), `"`)[1]
filename := strings.Split(string(fullhref), `#`)[0]
relname := filepath.Join(dir, filename)
relname := filepath.Join(dir, filename)
if _, err := os.Stat(relname); err != nil {
return s
}

if (fullhref == filename) {
if fullhref == filename {
return []byte(fmt.Sprintf(`href="%s.html"`, filename[:len(filename)-3]))
}

Expand Down

0 comments on commit 6613b37

Please sign in to comment.