mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func trim(source, prefix, suffix string) string {
|
|
return strings.TrimPrefix(strings.TrimSuffix(source, suffix), prefix)
|
|
}
|
|
|
|
func hugoPathTrim(source string) string {
|
|
return strings.TrimSuffix(strings.TrimSuffix(source, "/index"), "_index")
|
|
}
|
|
|
|
func processTarget(source string) string {
|
|
if !isInternal(source) {
|
|
return source
|
|
}
|
|
if strings.HasPrefix(source, "/") {
|
|
return strings.TrimSuffix(source, ".md")
|
|
}
|
|
return "/" + strings.TrimSuffix(strings.TrimSuffix(source, ".html"), ".md")
|
|
}
|
|
|
|
func isInternal(link string) bool {
|
|
return !strings.HasPrefix(link, "http")
|
|
}
|
|
|
|
// filter out certain links (e.g. to media)
|
|
func filter(links []Link) (res []Link) {
|
|
for _, l := range links {
|
|
// filter external and non-md
|
|
isMarkdown := filepath.Ext(l.Target) == "" || filepath.Ext(l.Target) == ".md"
|
|
if isInternal(l.Target) && isMarkdown {
|
|
res = append(res, l)
|
|
}
|
|
}
|
|
fmt.Printf("Removed %d external and non-markdown links\n", len(links)-len(res))
|
|
return res
|
|
}
|
|
|