1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
package export
import (
"os"
"path/filepath"
"strings"
"testing"
)
func testOpts() Options {
return Options{
Title: "Doc",
Theme: "flexoki",
FontDisplay: "Georgia, serif",
FontBody: "system-ui, sans-serif",
FontMono: "ui-monospace, monospace",
Cover: true,
}
}
func TestDocumentEmbedsHouseStyle(t *testing.T) {
html, err := Document("Hello world.", testOpts())
if err != nil {
t.Fatal(err)
}
// The vendored doc.css must be baked into the output (self-contained).
for _, want := range []string{`<article class="doc`, ".doc {", "@media print", "@page"} {
if !strings.Contains(html, want) {
t.Errorf("output missing %q", want)
}
}
}
func TestDocumentSetsTheme(t *testing.T) {
opts := testOpts()
opts.Theme = "humdrum-dark"
html, err := Document("x", opts)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, `data-theme="humdrum-dark"`) {
t.Errorf("theme not applied to <html>:\n%s", firstLines(html, 3))
}
}
func TestDocumentRendersMarkdown(t *testing.T) {
html, err := Document("# Title\n\nA **bold** word.", testOpts())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, "<strong>bold</strong>") {
t.Errorf("markdown not rendered to HTML")
}
}
func TestDocumentWrapsLeadingH1InCover(t *testing.T) {
html, err := Document("# Title\n\nSubtitle line.\n\nBody.", testOpts())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, `<div class="cover">`) {
t.Fatalf("no cover wrapper:\n%s", html)
}
// The h1 and the immediately following subtitle paragraph live inside cover.
cover := between(html, `<div class="cover">`, `</div>`)
if !strings.Contains(cover, "<h1") || !strings.Contains(cover, "Subtitle line.") {
t.Errorf("cover should hold h1 + subtitle, got:\n%s", cover)
}
}
func TestDocumentNoCoverWhenDisabled(t *testing.T) {
opts := testOpts()
opts.Cover = false
html, err := Document("# Title\n\nBody.", opts)
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, `<div class="cover">`) {
t.Errorf("cover wrapper present though disabled")
}
}
func TestDocumentPageBreakHeadingSuffix(t *testing.T) {
html, err := Document("# T\n\n## Section {.page-break}\n\nx", testOpts())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, `class="page-break"`) {
t.Errorf("{.page-break} suffix not turned into a class:\n%s", html)
}
if strings.Contains(html, "{.page-break}") {
t.Errorf("literal {.page-break} suffix left in heading text")
}
}
func TestPageBreakSuffixDoesNotLeakToEarlierHeading(t *testing.T) {
// A leading H1 with no suffix, followed by an H2 carrying {.page-break}:
// the suffix must attach to the H2 only, never span to the H1.
html, err := Document("# Title\n\nbody\n\n## Section {.page-break}\n\nmore", testOpts())
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, `<h1 class="page-break"`) {
t.Errorf("page-break leaked onto the leading H1:\n%s", html)
}
if strings.Contains(html, "</h1>\n<p") && strings.Contains(html, "<h2>Section</h1>") {
t.Errorf("mismatched heading close tags produced")
}
if !strings.Contains(html, `<h2 class="page-break">Section</h2>`) {
t.Errorf("H2 did not get a clean page-break class:\n%s", html)
}
}
func TestDocumentTaskListItem(t *testing.T) {
html, err := Document("- [ ] todo\n- [x] done", testOpts())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, "task-list-item") {
t.Errorf("checkbox list item missing task-list-item class:\n%s", html)
}
}
func TestDocumentInjectsConfiguredFonts(t *testing.T) {
opts := testOpts()
opts.FontDisplay = "Charter, serif"
opts.FontBody = "Inter, sans-serif"
opts.FontMono = "Fira Code, monospace"
html, err := Document("x", opts)
if err != nil {
t.Fatal(err)
}
// Multi-word names are quoted (Fira Code → "Fira Code"); single-word names
// and generics stay bare.
for _, want := range []string{"Charter, serif", "Inter, sans-serif", `"Fira Code", monospace`} {
if !strings.Contains(html, want) {
t.Errorf("configured font %q not injected", want)
}
}
// No licensed face should leak into the output.
for _, bad := range []string{"Awke", "Untitled Sans", "Name Mono"} {
if strings.Contains(html, bad) {
t.Errorf("licensed font %q leaked into export", bad)
}
}
}
func TestDocumentStripsFrontmatter(t *testing.T) {
md := "---\ntitle: Hi\ntags: [a, b]\n---\n\n# Real Title\n\nBody."
html, err := Document(md, testOpts())
if err != nil {
t.Fatal(err)
}
if strings.Contains(html, "tags: [a, b]") {
t.Errorf("YAML frontmatter rendered into body:\n%s", html)
}
if !strings.Contains(html, "Real Title") {
t.Errorf("content after frontmatter lost")
}
}
func TestCSSFontStackQuotesMultiWordNames(t *testing.T) {
cases := map[string]string{
"Awke": "Awke", // single ident — fine bare
"Untitled Sans": `"Untitled Sans"`, // space → must quote
"Maple Mono": `"Maple Mono"`, // space → must quote
"Inter, system-ui, sans-serif": "Inter, system-ui, sans-serif",
"Maple Mono, ui-monospace, monospace": `"Maple Mono", ui-monospace, monospace`,
`"Untitled Sans", sans-serif`: `"Untitled Sans", sans-serif`, // already quoted — leave
"'Already Quoted'": "'Already Quoted'", // single-quoted — leave
"": "",
}
for in, want := range cases {
if got := cssFontStack(in); got != want {
t.Errorf("cssFontStack(%q) = %q, want %q", in, got, want)
}
}
}
func TestDocumentQuotesMultiWordFonts(t *testing.T) {
opts := testOpts()
opts.FontBody = "Untitled Sans"
opts.FontMono = "Maple Mono"
html, err := Document("x", opts)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(html, `--font-body:"Untitled Sans"`) {
t.Errorf("multi-word body font not quoted in override:\n%s", firstLines(between(html, "--font-display", "</style>"), 1))
}
if !strings.Contains(html, `--font-mono:"Maple Mono"`) {
t.Errorf("multi-word mono font not quoted in override")
}
}
func TestMapTheme(t *testing.T) {
cases := map[string]string{
"flexoki-light": "flexoki",
"flexoki-dark": "flexoki-dark",
"charm": "flexoki-dark",
"": "flexoki",
"nonexistent": "flexoki",
}
for in, want := range cases {
if got := MapTheme(in); got != want {
t.Errorf("MapTheme(%q) = %q, want %q", in, got, want)
}
}
}
func TestOutputPathFromSource(t *testing.T) {
// Named files export to the temp dir (basename, .html), never beside the
// source — so a vault never accumulates stray HTML next to its markdown.
tmp := strings.TrimRight(os.TempDir(), string(os.PathSeparator))
if got := OutputPath("/x/y/note.md", "Title"); got != filepath.Join(tmp, "note.html") {
t.Errorf("OutputPath = %q, want %q", got, filepath.Join(tmp, "note.html"))
}
if got := OutputPath("/x/y/note.markdown", "Title"); got != filepath.Join(tmp, "note.html") {
t.Errorf("OutputPath = %q, want %q", got, filepath.Join(tmp, "note.html"))
}
}
func TestOutputPathUnnamedUsesTempSlug(t *testing.T) {
got := OutputPath("", "My Great Note!")
if filepath.Dir(got) != strings.TrimRight(os.TempDir(), string(os.PathSeparator)) {
t.Errorf("unnamed export should land in temp dir, got %q", got)
}
base := filepath.Base(got)
if base != "my-great-note.html" {
t.Errorf("slug base = %q, want my-great-note.html", base)
}
}
func TestWriteCreatesSelfContainedFile(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "doc.md")
if err := os.WriteFile(src, []byte("# Hi\n\nbody"), 0o644); err != nil {
t.Fatal(err)
}
out, err := Write(src, "# Hi\n\nbody", testOpts())
if err != nil {
t.Fatal(err)
}
wantOut := filepath.Join(strings.TrimRight(os.TempDir(), string(os.PathSeparator)), "doc.html")
if out != wantOut {
t.Errorf("out = %q, want %q (temp dir, not beside source)", out, wantOut)
}
if filepath.Dir(out) == dir {
t.Errorf("export landed beside source %q; should go to temp", dir)
}
data, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), `<article class="doc`) {
t.Errorf("written file is not a rendered document")
}
}
func TestTitle(t *testing.T) {
cases := []struct {
src, md, want string
}{
{"/x/note.md", "# Real Heading\n\nbody", "Real Heading"},
{"/x/note.md", "no heading here", "note"},
{"", "no heading", "Untitled"},
{"/x/draft.md", "---\ntitle: y\n---\n# After Frontmatter\n", "After Frontmatter"},
}
for _, c := range cases {
if got := Title(c.src, c.md); got != c.want {
t.Errorf("Title(%q, %q) = %q, want %q", c.src, c.md, got, c.want)
}
}
}
func TestBrowserCommandNonEmpty(t *testing.T) {
name, args := browserCommand("/tmp/x.html")
if name == "" {
t.Fatal("browserCommand returned empty program")
}
joined := strings.Join(append([]string{name}, args...), " ")
if !strings.Contains(joined, "/tmp/x.html") {
t.Errorf("browser command %q does not reference the file", joined)
}
}
// --- helpers ---
func firstLines(s string, n int) string {
lines := strings.SplitN(s, "\n", n+1)
if len(lines) > n {
lines = lines[:n]
}
return strings.Join(lines, "\n")
}
func between(s, start, end string) string {
i := strings.Index(s, start)
if i < 0 {
return ""
}
i += len(start)
j := strings.Index(s[i:], end)
if j < 0 {
return s[i:]
}
return s[i : i+j]
}
|