From f78ebe7d5d3dce48e05355c505444767b2ce1671 Mon Sep 17 00:00:00 2001 From: luxick Date: Wed, 5 Aug 2026 18:26:09 +0200 Subject: [PATCH] Remove auto new tab for external links --- extlinks.go | 57 ----------------------------------------------------- render.go | 2 +- 2 files changed, 1 insertion(+), 58 deletions(-) delete mode 100644 extlinks.go diff --git a/extlinks.go b/extlinks.go deleted file mode 100644 index e99a1f4..0000000 --- a/extlinks.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "strings" - - "github.com/yuin/goldmark" - "github.com/yuin/goldmark/ast" - "github.com/yuin/goldmark/parser" - "github.com/yuin/goldmark/text" - "github.com/yuin/goldmark/util" -) - -type extLinksTransformer struct{} - -func (extLinksTransformer) Transform(doc *ast.Document, reader text.Reader, pc parser.Context) { - ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { - if !entering { - return ast.WalkContinue, nil - } - link, ok := n.(*ast.Link) - if !ok { - return ast.WalkContinue, nil - } - if isExternalURL(string(link.Destination)) { - link.SetAttribute([]byte("target"), []byte("_blank")) - link.SetAttribute([]byte("rel"), []byte("noopener noreferrer")) - } - return ast.WalkContinue, nil - }) -} - -func isExternalURL(dest string) bool { - if strings.HasPrefix(dest, "//") { - return true - } - i := strings.Index(dest, ":") - if i <= 0 { - return false - } - for _, c := range dest[:i] { - if !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && - !(c >= '0' && c <= '9') && c != '+' && c != '-' && c != '.' { - return false - } - } - return true -} - -type extLinksExt struct{} - -func newExtLinksExt() goldmark.Extender { return &extLinksExt{} } - -func (e *extLinksExt) Extend(m goldmark.Markdown) { - m.Parser().AddOptions(parser.WithASTTransformers( - util.Prioritized(extLinksTransformer{}, 999), - )) -} diff --git a/render.go b/render.go index 6c423ba..4ffe155 100644 --- a/render.go +++ b/render.go @@ -24,7 +24,7 @@ var md goldmark.Markdown // targets against the filesystem. func initMarkdown(root string) { md = goldmark.New( - goldmark.WithExtensions(extension.GFM, extension.Table, newWikiLinkExt(root), newWikiEmbedExt(root), newExtLinksExt()), + goldmark.WithExtensions(extension.GFM, extension.Table, newWikiLinkExt(root), newWikiEmbedExt(root)), goldmark.WithParserOptions(parser.WithAutoHeadingID()), goldmark.WithRendererOptions(html.WithUnsafe(), html.WithHardWraps()), )