From 395858e7716bb7866d32b593f1b83ac47a570e16 Mon Sep 17 00:00:00 2001 From: Johannes Kaufmann Date: Mon, 4 Nov 2024 21:36:18 +0100 Subject: [PATCH] remove element enum for absolute url function --- converter/ctx.go | 12 +++++----- converter/url.go | 10 +------- converter/url_test.go | 40 +++++++++++++++---------------- plugin/commonmark/render_image.go | 2 +- plugin/commonmark/render_link.go | 2 +- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/converter/ctx.go b/converter/ctx.go index 65451aa..01d456c 100644 --- a/converter/ctx.go +++ b/converter/ctx.go @@ -42,9 +42,9 @@ func GetDomain(ctx context.Context) string { // - - - - - - - - - - - - - - - - - - - - - // -type AssembleAbsoluteURLFunc func(elem Element, rawURL string, domain string) string +type AssembleAbsoluteURLFunc func(tagName string, rawURL string, domain string) string -func assembleAbsoluteURL(ctx context.Context, elem Element, rawURL string) string { +func assembleAbsoluteURL(ctx context.Context, tagName string, rawURL string) string { domain := GetDomain(ctx) // TODO: since this gets passed down from the converter, it doesn't have to provided from the ctx anymore @@ -54,7 +54,7 @@ func assembleAbsoluteURL(ctx context.Context, elem Element, rawURL string) strin return "" } - return fn(elem, rawURL, domain) + return fn(tagName, rawURL, domain) } func provideAssembleAbsoluteURL(ctx context.Context, fn AssembleAbsoluteURLFunc) context.Context { @@ -136,7 +136,7 @@ func UpdateState[V any](ctx context.Context, key string, fn func(V) V) { type Context interface { context.Context - AssembleAbsoluteURL(ctx Context, elem Element, rawURL string) string + AssembleAbsoluteURL(ctx Context, tagName string, rawURL string) string GetTagType(tagName string) (tagType, bool) @@ -161,8 +161,8 @@ func newConverterContext(ctx context.Context, conv *Converter) Context { } } -func (c *converterContext) AssembleAbsoluteURL(ctx Context, elem Element, rawURL string) string { - return assembleAbsoluteURL(ctx, elem, rawURL) +func (c *converterContext) AssembleAbsoluteURL(ctx Context, tagName string, rawURL string) string { + return assembleAbsoluteURL(ctx, tagName, rawURL) } func (c *converterContext) RenderNodes(ctx Context, w Writer, nodes ...*html.Node) { diff --git a/converter/url.go b/converter/url.go index 4f86969..ede0361 100644 --- a/converter/url.go +++ b/converter/url.go @@ -6,14 +6,6 @@ import ( "strings" ) -type Element string - -const ( - // TODO: shorter value? Or maybe atom? Or nodeName "img" / "a"? - ElementLink Element = "ElementLink" - ElementImage Element = "ElementImage" -) - var percentEncodingReplacer = strings.NewReplacer( " ", "%20", "[", "%5B", @@ -24,7 +16,7 @@ var percentEncodingReplacer = strings.NewReplacer( ">", "%3E", ) -func defaultAssembleAbsoluteURL(elem Element, rawURL string, domain string) string { +func defaultAssembleAbsoluteURL(tagName string, rawURL string, domain string) string { rawURL = strings.TrimSpace(rawURL) if rawURL == "#" { diff --git a/converter/url_test.go b/converter/url_test.go index 989aed5..5f4375c 100644 --- a/converter/url_test.go +++ b/converter/url_test.go @@ -8,7 +8,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { runs := []struct { desc string - element Element + tagName string input string domain string @@ -24,7 +24,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "empty fragment", - element: ElementLink, + tagName: "a", input: "#", domain: "", @@ -33,7 +33,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "fragment", - element: ElementLink, + tagName: "a", input: "#heading", domain: "", @@ -42,7 +42,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "fragment with space", - element: ElementLink, + tagName: "a", input: "#my heading", domain: "", @@ -51,7 +51,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "no domain", - element: ElementLink, + tagName: "a", input: "/page.html?key=val#hash", domain: "", @@ -60,7 +60,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "with domain", - element: ElementLink, + tagName: "a", input: "/page.html?key=val#hash", domain: "test.com", @@ -69,7 +69,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "data uri", - element: ElementLink, + tagName: "a", input: "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7", domain: "test.com", @@ -78,7 +78,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "data uri (with spaces)", - element: ElementLink, + tagName: "a", input: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 56 56' width='56' height='56' %3E%3C/svg%3E", domain: "test.com", @@ -87,7 +87,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "URI scheme", - element: ElementLink, + tagName: "a", input: "slack://open?team=abc", domain: "test.com", @@ -97,7 +97,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "already with http", - element: ElementLink, + tagName: "a", input: "http://www.example.com", domain: "test.com", @@ -106,7 +106,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "already with https", - element: ElementLink, + tagName: "a", input: "https://www.example.com", domain: "test.com", @@ -141,7 +141,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "mailto", - element: ElementLink, + tagName: "a", input: "mailto:hi@example.com?subject=Mail&cc=someoneelse@example.com", domain: "test.com", @@ -150,7 +150,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "invalid url with newline in mailto", - element: ElementLink, + tagName: "a", input: "mailto:hi@example.com?body=Hello\nJohannes", domain: "test.com", @@ -159,7 +159,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "mailto with already encoded space", - element: ElementLink, + tagName: "a", input: "mailto:hi@example.com?subject=Hello%20Johannes", domain: "test.com", @@ -168,7 +168,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "mailto with raw space", - element: ElementLink, + tagName: "a", input: "mailto:hi@example.com?subject=Greetings to Johannes", domain: "test.com", @@ -177,7 +177,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "mailto with german 'ä' character", - element: ElementLink, + tagName: "a", input: "mailto:hi@example.com?subject=Sie können gern einen Screenshot anhängen", domain: "test.com", @@ -188,7 +188,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "mailto with link", - element: ElementLink, + tagName: "a", input: "mailto:hi@example.com?body=Article: www.website.com/page.html", domain: "test.com", @@ -197,7 +197,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "brackets inside link #1", - element: ElementLink, + tagName: "a", input: "foo(and(bar)", domain: "", @@ -206,7 +206,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { { desc: "brackets inside link #2", - element: ElementLink, + tagName: "a", input: "[foo](uri)", domain: "", @@ -215,7 +215,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) { } for _, run := range runs { t.Run(run.desc, func(t *testing.T) { - res := defaultAssembleAbsoluteURL(run.element, run.input, run.domain) + res := defaultAssembleAbsoluteURL(run.tagName, run.input, run.domain) if res != run.expected { t.Errorf("expected '%s' but got '%s'", run.expected, res) } diff --git a/plugin/commonmark/render_image.go b/plugin/commonmark/render_image.go index 481db95..81f761e 100644 --- a/plugin/commonmark/render_image.go +++ b/plugin/commonmark/render_image.go @@ -34,7 +34,7 @@ func (c *commonmark) renderImage(ctx converter.Context, w converter.Writer, n *h return converter.RenderTryNext } - src = ctx.AssembleAbsoluteURL(ctx, converter.ElementImage, src) + src = ctx.AssembleAbsoluteURL(ctx, "img", src) title := dom.GetAttributeOr(n, "title", "") title = strings.ReplaceAll(title, "\n", " ") diff --git a/plugin/commonmark/render_link.go b/plugin/commonmark/render_link.go index 5dac9b6..b3f0689 100644 --- a/plugin/commonmark/render_link.go +++ b/plugin/commonmark/render_link.go @@ -51,7 +51,7 @@ func (c *commonmark) renderLink(ctx converter.Context, w converter.Writer, n *ht href := dom.GetAttributeOr(n, "href", "") href = strings.TrimSpace(href) - href = ctx.AssembleAbsoluteURL(ctx, converter.ElementLink, href) + href = ctx.AssembleAbsoluteURL(ctx, "a", href) title := dom.GetAttributeOr(n, "title", "") title = strings.ReplaceAll(title, "\n", " ")