Skip to content

Commit

Permalink
remove element enum for absolute url function
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Nov 4, 2024
1 parent 847f2d7 commit 395858e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
12 changes: 6 additions & 6 deletions converter/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
10 changes: 1 addition & 9 deletions converter/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 == "#" {
Expand Down
40 changes: 20 additions & 20 deletions converter/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
runs := []struct {
desc string

element Element
tagName string
input string
domain string

Expand All @@ -24,7 +24,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "empty fragment",

element: ElementLink,
tagName: "a",
input: "#",
domain: "",

Expand All @@ -33,7 +33,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "fragment",

element: ElementLink,
tagName: "a",
input: "#heading",
domain: "",

Expand All @@ -42,7 +42,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "fragment with space",

element: ElementLink,
tagName: "a",
input: "#my heading",
domain: "",

Expand All @@ -51,7 +51,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "no domain",

element: ElementLink,
tagName: "a",
input: "/page.html?key=val#hash",
domain: "",

Expand All @@ -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",

Expand All @@ -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",

Expand All @@ -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",

Expand All @@ -87,7 +87,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "URI scheme",

element: ElementLink,
tagName: "a",
input: "slack://open?team=abc",
domain: "test.com",

Expand All @@ -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",

Expand All @@ -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",

Expand Down Expand Up @@ -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",

Expand All @@ -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",

Expand All @@ -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",

Expand All @@ -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",

Expand All @@ -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",

Expand All @@ -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",

Expand All @@ -197,7 +197,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "brackets inside link #1",

element: ElementLink,
tagName: "a",
input: "foo(and(bar)",
domain: "",

Expand All @@ -206,7 +206,7 @@ func TestDefaultAssembleAbsoluteURL(t *testing.T) {
{
desc: "brackets inside link #2",

element: ElementLink,
tagName: "a",
input: "[foo](uri)",
domain: "",

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/commonmark/render_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", " ")
Expand Down
2 changes: 1 addition & 1 deletion plugin/commonmark/render_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", " ")
Expand Down

0 comments on commit 395858e

Please sign in to comment.