Browse Source

Add test for markdown images

master
Jan-Lukas Else 3 months ago
parent
commit
116b9eb8cc
  1. 2
      go.mod
  2. 4
      go.sum
  3. 2
      markdown.go
  4. 67
      markdown_test.go

2
go.mod

@ -74,7 +74,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tdewolff/parse/v2 v2.5.19 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

4
go.sum

@ -529,8 +529,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e h1:XMgFehsDnnLGtjvjOfqWSUzt0alpTR1RSEuznObga2c=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

2
markdown.go

@ -164,7 +164,7 @@ func (c *customRenderer) renderImage(w util.BufWriter, source []byte, node ast.N
n := node.(*ast.Image)
// Make URL absolute if it's relative
destination := util.URLEscape(n.Destination, true)
if c.publicAddress != "" && bytes.HasPrefix(destination, []byte("/")) {
if c.absoluteLinks && c.publicAddress != "" && bytes.HasPrefix(destination, []byte("/")) {
destination = util.EscapeHTML(append([]byte(c.publicAddress), destination...))
} else {
destination = util.EscapeHTML(destination)

67
markdown_test.go

@ -2,10 +2,10 @@ package main
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_markdown(t *testing.T) {
@ -23,50 +23,55 @@ func Test_markdown(t *testing.T) {
// Relative / absolute links
rendered, err := app.renderMarkdown("[Relative](/relative)", false)
if err != nil {
t.Fatalf("Error: %v", err)
}
if !strings.Contains(string(rendered), `href="/relative"`) {
t.Errorf("Wrong result, got %v", string(rendered))
}
require.NoError(t, err)
assert.Contains(t, string(rendered), `href="/relative"`)
rendered, err = app.renderMarkdown("[Relative](/relative)", true)
if err != nil {
t.Fatalf("Error: %v", err)
}
if !strings.Contains(string(rendered), `href="https://example.com/relative"`) {
t.Errorf("Wrong result, got %v", string(rendered))
}
if strings.Contains(string(rendered), `target="_blank"`) {
t.Errorf("Wrong result, got %v", string(rendered))
}
require.NoError(t, err)
assert.Contains(t, string(rendered), `href="https://example.com/relative"`)
assert.NotContains(t, string(rendered), `target="_blank"`)
// Images
rendered, err = app.renderMarkdown("![](/relative)", false)
require.NoError(t, err)
assert.Contains(t, string(rendered), `src="/relative"`)
assert.Contains(t, string(rendered), `href="/relative"`)
rendered, err = app.renderMarkdown("![](/relative)", true)
require.NoError(t, err)
assert.Contains(t, string(rendered), `src="https://example.com/relative"`)
assert.Contains(t, string(rendered), `href="https://example.com/relative"`)
// Image title
rendered, err = app.renderMarkdown(`![](/test "Test-Title")`, false)
require.NoError(t, err)
assert.Contains(t, string(rendered), `title="Test-Title"`)
// External links
rendered, err = app.renderMarkdown("[External](https://example.com)", true)
if err != nil {
t.Fatalf("Error: %v", err)
}
if !strings.Contains(string(rendered), `target="_blank"`) {
t.Errorf("Wrong result, got %v", string(rendered))
}
require.NoError(t, err)
assert.Contains(t, string(rendered), `target="_blank"`)
// Link title
rendered, err = app.renderMarkdown(`[With title](https://example.com "Test-Title")`, true)
if err != nil {
t.Fatalf("Error: %v", err)
}
if !strings.Contains(string(rendered), `title="Test-Title"`) {
t.Errorf("Wrong result, got %v", string(rendered))
}
require.NoError(t, err)
assert.Contains(t, string(rendered), `title="Test-Title"`)
// Text
renderedText := app.renderText("**This** *is* [text](/)")
if renderedText != "This is text" {
t.Errorf("Wrong result, got \"%v\"", renderedText)
}
assert.Equal(t, "This is text", renderedText)
// Title

Loading…
Cancel
Save