2024-02-26 16:16:06 +01:00
|
|
|
package markup
|
2024-02-11 17:16:10 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseTemplate(t *testing.T) {
|
|
|
|
input := `---
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
tags: ["software", "web"]
|
|
|
|
---
|
|
|
|
<p>Hello World!</p>
|
|
|
|
`
|
|
|
|
|
|
|
|
file := newFile("test*.html", input)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 22:39:21 +01:00
|
|
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 17:16:10 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
assertEqual(t, templ.Metadata["title"], "my new post")
|
|
|
|
assertEqual(t, templ.Metadata["subtitle"], "a blog post")
|
|
|
|
assertEqual(t, templ.Metadata["tags"].([]interface{})[0], "software")
|
|
|
|
assertEqual(t, templ.Metadata["tags"].([]interface{})[1], "web")
|
|
|
|
|
2024-02-27 16:40:40 +01:00
|
|
|
content, err := templ.Render()
|
2024-02-11 17:16:10 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-13 17:43:20 +01:00
|
|
|
assertEqual(t, string(content), "<p>Hello World!</p>")
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonTemplate(t *testing.T) {
|
|
|
|
// not identified as front matter, leaving file as is
|
|
|
|
input := `+++
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
+++
|
|
|
|
<p>Hello World!</p>`
|
|
|
|
|
|
|
|
file := newFile("test*.html", input)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 22:39:21 +01:00
|
|
|
_, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 17:16:10 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
|
|
|
// not first thing in file, leaving as is
|
|
|
|
input = `#+OPTIONS: toc:nil num:nil
|
|
|
|
---
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
tags: ["software", "web"]
|
|
|
|
---
|
|
|
|
<p>Hello World!</p>`
|
|
|
|
|
|
|
|
file = newFile("test*.html", input)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 22:39:21 +01:00
|
|
|
_, err = Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 17:16:10 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidFrontMatter(t *testing.T) {
|
|
|
|
input := `---
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
tags: ["software", "web"]
|
|
|
|
`
|
|
|
|
file := newFile("test*.html", input)
|
|
|
|
defer os.Remove(file.Name())
|
2024-02-16 22:39:21 +01:00
|
|
|
_, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 17:16:10 +01:00
|
|
|
|
|
|
|
assertEqual(t, err.Error(), "front matter not closed")
|
|
|
|
|
|
|
|
input = `---
|
|
|
|
title
|
|
|
|
tags: ["software", "web"]
|
|
|
|
---
|
|
|
|
<p>Hello World!</p>`
|
|
|
|
|
|
|
|
file = newFile("test*.html", input)
|
|
|
|
defer os.Remove(file.Name())
|
2024-02-16 22:39:21 +01:00
|
|
|
_, err = Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 17:16:10 +01:00
|
|
|
assert(t, strings.Contains(err.Error(), "invalid yaml"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderLiquid(t *testing.T) {
|
2024-02-11 17:30:10 +01:00
|
|
|
input := `---
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
tags: ["software", "web"]
|
|
|
|
---
|
2024-02-12 19:16:56 +01:00
|
|
|
<h1>{{ page.title }}</h1>
|
|
|
|
<h2>{{ page.subtitle }}</h2>
|
|
|
|
<ul>{% for tag in page.tags %}
|
2024-02-11 17:30:10 +01:00
|
|
|
<li>{{tag}}</li>{% endfor %}
|
2024-02-13 17:43:20 +01:00
|
|
|
</ul>`
|
2024-02-11 17:30:10 +01:00
|
|
|
|
|
|
|
file := newFile("test*.html", input)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 22:39:21 +01:00
|
|
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 17:30:10 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-27 16:40:40 +01:00
|
|
|
content, err := templ.Render()
|
2024-02-11 17:30:10 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
expected := `<h1>my new post</h1>
|
|
|
|
<h2>a blog post</h2>
|
|
|
|
<ul>
|
|
|
|
<li>software</li>
|
|
|
|
<li>web</li>
|
2024-02-13 17:43:20 +01:00
|
|
|
</ul>`
|
2024-02-11 17:30:10 +01:00
|
|
|
assertEqual(t, string(content), expected)
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderOrg(t *testing.T) {
|
2024-02-11 18:03:28 +01:00
|
|
|
input := `---
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
tags: ["software", "web"]
|
|
|
|
---
|
|
|
|
#+OPTIONS: toc:nil num:nil
|
|
|
|
* My title
|
|
|
|
** my Subtitle
|
|
|
|
- list 1
|
|
|
|
- list 2
|
|
|
|
`
|
|
|
|
|
|
|
|
file := newFile("test*.org", input)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 22:39:21 +01:00
|
|
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-11 18:03:28 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
2024-02-27 16:40:40 +01:00
|
|
|
content, err := templ.Render()
|
2024-02-11 18:03:28 +01:00
|
|
|
assertEqual(t, err, nil)
|
2024-02-16 15:55:53 +01:00
|
|
|
expected := `<div id="outline-container-headline-1" class="outline-1">
|
|
|
|
<h1 id="headline-1">
|
2024-02-11 18:03:28 +01:00
|
|
|
My title
|
2024-02-16 15:55:53 +01:00
|
|
|
</h1>
|
|
|
|
<div id="outline-text-headline-1" class="outline-text-1">
|
|
|
|
<div id="outline-container-headline-2" class="outline-2">
|
|
|
|
<h2 id="headline-2">
|
2024-02-11 18:03:28 +01:00
|
|
|
my Subtitle
|
2024-02-16 15:55:53 +01:00
|
|
|
</h2>
|
|
|
|
<div id="outline-text-headline-2" class="outline-text-2">
|
2024-02-11 18:03:28 +01:00
|
|
|
<ul>
|
|
|
|
<li>list 1</li>
|
|
|
|
<li>list 2</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
assertEqual(t, string(content), expected)
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
func TestRenderMarkdown(t *testing.T) {
|
|
|
|
input := `---
|
|
|
|
title: my new post
|
|
|
|
subtitle: a blog post
|
|
|
|
tags: ["software", "web"]
|
|
|
|
---
|
|
|
|
# My title
|
|
|
|
## my Subtitle
|
|
|
|
- list 1
|
|
|
|
- list 2
|
|
|
|
`
|
|
|
|
|
|
|
|
file := newFile("test*.md", input)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2024-02-16 22:39:21 +01:00
|
|
|
templ, err := Parse(NewEngine("https://olano.dev", "includes"), file.Name())
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
|
2024-02-27 16:40:40 +01:00
|
|
|
content, err := templ.Render()
|
2024-02-15 03:54:46 +01:00
|
|
|
assertEqual(t, err, nil)
|
|
|
|
expected := `<h1>My title</h1>
|
|
|
|
<h2>my Subtitle</h2>
|
|
|
|
<ul>
|
|
|
|
<li>list 1</li>
|
|
|
|
<li>list 2</li>
|
|
|
|
</ul>
|
|
|
|
`
|
|
|
|
assertEqual(t, string(content), expected)
|
|
|
|
}
|
|
|
|
|
2024-02-11 17:16:10 +01:00
|
|
|
// ------ HELPERS --------
|
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
func newFile(path string, contents string) *os.File {
|
2024-02-12 20:40:12 +01:00
|
|
|
file, _ := os.CreateTemp("", path)
|
2024-02-11 17:16:10 +01:00
|
|
|
file.WriteString(contents)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO move to assert package
|
|
|
|
func assert(t *testing.T, cond bool) {
|
|
|
|
t.Helper()
|
|
|
|
if !cond {
|
|
|
|
t.Fatalf("%v is false", cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
if a != b {
|
|
|
|
t.Fatalf("%v != %v", a, b)
|
|
|
|
}
|
|
|
|
}
|