diff --git a/internal/api/audit.go b/internal/api/audit.go index 18b8e09..9baa49f 100644 --- a/internal/api/audit.go +++ b/internal/api/audit.go @@ -27,6 +27,7 @@ import ( // routes missing here, so new endpoints cannot slip past the log. var auditActions = map[string]string{ "POST /api/v1/locale": "", + "POST /api/v1/markdown/preview": "", "POST /api/v1/auth/signup": "account.signup", "POST /api/v1/auth/login": "auth.login", "POST /api/v1/auth/login/totp": "auth.login", diff --git a/internal/api/legal_test.go b/internal/api/legal_test.go index 1a37075..96cef8b 100644 --- a/internal/api/legal_test.go +++ b/internal/api/legal_test.go @@ -126,3 +126,17 @@ func TestLegalChangeReachesOtherReplicas(t *testing.T) { t.Error("the other replica still serves the old footer") } } + +func TestMarkdownPreview(t *testing.T) { + e := newEnv(t) + if st, _ := e.browser(t).do("POST", "/api/v1/markdown/preview", map[string]any{"markdown": "x"}); st != http.StatusUnauthorized { + t.Errorf("anonymous preview = %d, want 401", st) + } + b := e.browser(t) + b.signup("p@"+uniq("md")+".test", uniq("md-")) + st, body := b.do("POST", "/api/v1/markdown/preview", map[string]any{"markdown": "**hi** ![t](https://e.example/p.png)"}) + html, _ := body["html"].(string) + if st != http.StatusOK || !strings.Contains(html, "hi") || strings.Contains(html, " { + // insertText keeps the browser's undo history, setRangeText is the + // fallback where it is unsupported. + const replace = (ta, start, end, text, selStart, selEnd) => { + ta.focus(); + ta.setSelectionRange(start, end); + if (!document.execCommand("insertText", false, text)) { + ta.setRangeText(text, start, end, "end"); + } + ta.setSelectionRange(start + selStart, start + selEnd); + ta.dispatchEvent(new Event("input", { bubbles: true })); + }; + + const wrap = (ta, mark, placeholder) => { + const { selectionStart: s, selectionEnd: e, value } = ta; + const sel = value.slice(s, e); + if (value.slice(s - mark.length, s) === mark && value.slice(e, e + mark.length) === mark) { + replace(ta, s - mark.length, e + mark.length, sel, 0, sel.length); + return; + } + const inner = sel || placeholder; + replace(ta, s, e, mark + inner + mark, mark.length, mark.length + inner.length); + }; + + // Prefixes every selected line, or removes the prefix when all have it. + const prefixLines = (ta, prefixFor, pattern) => { + const { selectionStart: s, selectionEnd: e, value } = ta; + const start = value.lastIndexOf("\n", s - 1) + 1; + const nl = value.indexOf("\n", e); + const end = nl === -1 ? value.length : nl; + const lines = value.slice(start, end).split("\n"); + const all = lines.every((l) => pattern.test(l)); + const out = lines.map((l, i) => (all ? l.replace(pattern, "") : prefixFor(i) + l)).join("\n"); + replace(ta, start, end, out, 0, out.length); + }; + + const block = (ta, text, selFrom, selTo) => { + const { selectionStart: s, selectionEnd: e, value } = ta; + const before = s > 0 && value[s - 1] !== "\n" ? "\n\n" : ""; + replace(ta, s, e, before + text, before.length + selFrom, before.length + selTo); + }; + + const actions = { + bold: (ta) => wrap(ta, "**", "text"), + italic: (ta) => wrap(ta, "_", "text"), + strike: (ta) => wrap(ta, "~~", "text"), + code: (ta) => wrap(ta, "`", "code"), + heading: (ta) => prefixLines(ta, () => "## ", /^#{1,6} /), + ul: (ta) => prefixLines(ta, () => "- ", /^[-*+] /), + ol: (ta) => prefixLines(ta, (i) => `${i + 1}. `, /^\d+\. /), + quote: (ta) => prefixLines(ta, () => "> ", /^> ?/), + link: (ta) => { + const { selectionStart: s, selectionEnd: e, value } = ta; + const text = value.slice(s, e) || "text"; + const url = "https://"; + replace(ta, s, e, `[${text}](${url})`, text.length + 3, text.length + 3 + url.length); + }, + codeblock: (ta) => { + const sel = ta.value.slice(ta.selectionStart, ta.selectionEnd) || "code"; + block(ta, "```\n" + sel + "\n```\n", 4, 4 + sel.length); + }, + table: (ta) => block(ta, "| Column | Column |\n| --- | --- |\n| Cell | Cell |\n", 2, 8), + hr: (ta) => block(ta, "---\n", 4, 4), + }; + const shortcuts = { b: "bold", i: "italic", k: "link" }; + + // Enter on a list item continues the list; on an empty item it ends it. + const continueList = (ta, ev) => { + const { selectionStart: s, selectionEnd: e, value } = ta; + if (s !== e) return; + const start = value.lastIndexOf("\n", s - 1) + 1; + const line = value.slice(start, s); + const m = line.match(/^(\s*)([-*+]|(\d+)\.|>)( +)(.*)$/); + if (!m || value.slice(s, value.indexOf("\n", s) === -1 ? value.length : value.indexOf("\n", s)).trim()) return; + ev.preventDefault(); + if (!m[5].trim()) { + replace(ta, start, s, "", 0, 0); + return; + } + const marker = m[3] ? `${Number(m[3]) + 1}.` : m[2]; + const next = "\n" + m[1] + marker + m[4]; + replace(ta, s, s, next, next.length, next.length); + }; + + for (const ed of document.querySelectorAll("[data-md-editor]")) { + const ta = ed.querySelector("textarea"); + const preview = ed.querySelector("[data-md-preview]"); + const count = ed.querySelector("[data-md-count]"); + const available = new Set([...ed.querySelectorAll("[data-md-action]")].map((b) => b.dataset.mdAction)); + + const updateCount = () => { + count.textContent = `${ta.value.length.toLocaleString()} / ${Number(ta.maxLength).toLocaleString()}`; + }; + updateCount(); + ta.addEventListener("input", updateCount); + + ed.querySelector("[data-md-toolbar]").addEventListener("click", (ev) => { + const b = ev.target.closest("[data-md-action]"); + if (b && !ta.hidden) actions[b.dataset.mdAction]?.(ta); + }); + ta.addEventListener("keydown", (ev) => { + if ((ev.ctrlKey || ev.metaKey) && !ev.altKey && shortcuts[ev.key.toLowerCase()]) { + const name = shortcuts[ev.key.toLowerCase()]; + if (!available.has(name)) return; + ev.preventDefault(); + actions[name](ta); + } else if (ev.key === "Enter" && !ev.shiftKey && !ev.isComposing) { + continueList(ta, ev); + } + }); + + const show = async (tab) => { + for (const t of ed.querySelectorAll("[data-md-tab]")) t.setAttribute("aria-selected", String(t.dataset.mdTab === tab)); + const previewing = tab === "preview"; + ta.hidden = previewing; + preview.hidden = !previewing; + for (const b of ed.querySelectorAll("[data-md-action]")) b.disabled = previewing; + if (!previewing) { + ta.focus(); + return; + } + if (!ta.value.trim()) { + preview.textContent = preview.dataset.empty; + return; + } + try { + const res = await window.api.post("/markdown/preview", { markdown: ta.value }); + // The server renders without raw HTML or unsafe links. + preview.innerHTML = res.data.html; + } catch (err) { + preview.textContent = err.response?.data?.message ?? document.body.dataset.msgNetwork; + } + }; + for (const t of ed.querySelectorAll("[data-md-tab]")) t.addEventListener("click", () => show(t.dataset.mdTab)); + } +})(); diff --git a/internal/web/editor_test.go b/internal/web/editor_test.go new file mode 100644 index 0000000..9eedd4b --- /dev/null +++ b/internal/web/editor_test.go @@ -0,0 +1,42 @@ +package web + +import ( + "os/exec" + "path/filepath" + "regexp" + "strings" + "testing" +) + +// TestMarkdownEditor runs testdata/editor.html, which drives the editor in +// assets/app.js and reports PASS or FAIL lines, in a headless browser. +func TestMarkdownEditor(t *testing.T) { + var browser string + for _, name := range []string{"chromium", "chromium-browser", "google-chrome"} { + if p, err := exec.LookPath(name); err == nil { + browser = p + break + } + } + if browser == "" { + t.Skip("no Chromium to run the editor in") + } + page, err := filepath.Abs("testdata/editor.html") + if err != nil { + t.Fatal(err) + } + out, err := exec.Command(browser, "--headless", "--disable-gpu", "--allow-file-access-from-files", + "--virtual-time-budget=3000", "--dump-dom", "file://"+page).Output() + if err != nil { + t.Fatalf("running %s: %v", browser, err) + } + m := regexp.MustCompile(`(?s)
(.*?)
`).FindSubmatch(out) + if m == nil || len(m[1]) == 0 { + t.Fatal("the editor test page produced no results") + } + for _, line := range strings.Split(string(m[1]), "\n") { + if !strings.HasPrefix(line, "PASS ") { + t.Error(line) + } + } +} diff --git a/internal/web/legal.go b/internal/web/legal.go index af09305..c33d4aa 100644 --- a/internal/web/legal.go +++ b/internal/web/legal.go @@ -4,7 +4,10 @@ import ( "bytes" "github.com/yuin/goldmark" + "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/extension" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/util" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" ) @@ -23,8 +26,23 @@ type LegalLink struct { } // markdown renders untrusted input: goldmark drops raw HTML and links with -// dangerous schemes such as javascript: unless told otherwise. -var markdown = goldmark.New(goldmark.WithExtensions(extension.Table, extension.Linkify, extension.Strikethrough)) +// dangerous schemes such as javascript: unless told otherwise. Images are +// shown as their alt text: on a status page an external image would hand +// every visitor's address to its host. +var markdown = goldmark.New( + goldmark.WithExtensions(extension.Table, extension.Linkify, extension.Strikethrough), + goldmark.WithRendererOptions(renderer.WithNodeRenderers(util.Prioritized(altTextImages{}, 100))), +) + +type altTextImages struct{} + +// RegisterFuncs implements renderer.NodeRenderer. +func (altTextImages) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) { + r.Register(ast.KindImage, func(w util.BufWriter, src []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { + // The children are the alt text and render as usual. + return ast.WalkContinue, nil + }) +} // RenderMarkdown converts src to HTML that is safe to embed. func RenderMarkdown(src string) (string, error) { diff --git a/internal/web/legal.templ b/internal/web/legal.templ index e865cd9..0a1538a 100644 --- a/internal/web/legal.templ +++ b/internal/web/legal.templ @@ -79,10 +79,10 @@ templ LegalSettingsPage(p Page, me Me, titleID, introID, apiBase, next string, d { i18n.T(ctx, "legal.url") } -
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -456,29 +456,24 @@ func LegalSettingsPage(p Page, me Me, titleID, introID, apiBase, next string, do if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 28, "

") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var25 string - templ_7745c5c3_Var25, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "legal.hint")) + templ_7745c5c3_Var24, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "legal.hint")) if templ_7745c5c3_Err != nil { return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/legal.templ`, Line: 86, Col: 67} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var25)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var24)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/web/maintenance.templ b/internal/web/maintenance.templ index ea04b94..8071e15 100644 --- a/internal/web/maintenance.templ +++ b/internal/web/maintenance.templ @@ -133,10 +133,10 @@ templ maintenanceForm(v MaintenanceView) { @labeled("maintenance.field_title") { } - @labeled("maintenance.field_description") { - - { i18n.T(ctx, "common.markdown_hint") } - } +

+ { i18n.T(ctx, "maintenance.field_description") } + @MarkdownEditor("description", v.Form.Description, 5, 20000, MarkdownInline) +
@labeled("maintenance.field_start") { diff --git a/internal/web/maintenance_templ.go b/internal/web/maintenance_templ.go index ec3bd30..e2e7cc7 100644 --- a/internal/web/maintenance_templ.go +++ b/internal/web/maintenance_templ.go @@ -576,7 +576,32 @@ func maintenanceForm(v MaintenanceView) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Var25 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 46, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var25 string + templ_7745c5c3_Var25, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_description")) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 137, Col: 131} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var25)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 47, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = MarkdownEditor("description", v.Form.Description, 5, 20000, MarkdownInline).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 48, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var26 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) if !templ_7745c5c3_IsBuffer { @@ -592,25 +617,38 @@ func maintenanceForm(v MaintenanceView) templ.Component { templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer templ_7745c5c3_Buffer := templ.GetBuffer() var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 46, " ") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var29 string - templ_7745c5c3_Var29, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "common.markdown_hint")) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 138, Col: 85} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var29)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 50, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 52, ">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) - templ_7745c5c3_Err = labeled("maintenance.field_description").Render(templ.WithChildren(ctx, templ_7745c5c3_Var25), templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 51, "
") + templ_7745c5c3_Err = labeled("maintenance.field_start").Render(templ.WithChildren(ctx, templ_7745c5c3_Var26), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -680,20 +688,20 @@ func maintenanceForm(v MaintenanceView) templ.Component { templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer templ_7745c5c3_Buffer := templ.GetBuffer() var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 52, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 56, ">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) - templ_7745c5c3_Err = labeled("maintenance.field_start").Render(templ.WithChildren(ctx, templ_7745c5c3_Var30), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = labeled("maintenance.field_duration").Render(templ.WithChildren(ctx, templ_7745c5c3_Var30), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -751,20 +759,20 @@ func maintenanceForm(v MaintenanceView) templ.Component { templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer templ_7745c5c3_Buffer := templ.GetBuffer() var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 56, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 60, "> ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, tz := range commonTimeZones { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 61, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 63, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) - templ_7745c5c3_Err = labeled("maintenance.field_duration").Render(templ.WithChildren(ctx, templ_7745c5c3_Var34), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = labeled("maintenance.field_time_zone").Render(templ.WithChildren(ctx, templ_7745c5c3_Var34), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Var38 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 64, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var39 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) if !templ_7745c5c3_IsBuffer { @@ -822,20 +857,7 @@ func maintenanceForm(v MaintenanceView) templ.Component { templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer templ_7745c5c3_Buffer := templ.GetBuffer() var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 60, " ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 67, ">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - for _, tz := range commonTimeZones { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 64, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 69, "\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if v.Form.Recurrence == r { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 70, " selected") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 71, ">") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var43 string + templ_7745c5c3_Var43, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.recurrence_"+r)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 160, Col: 108} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var43)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 72, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 66, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 73, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) - templ_7745c5c3_Err = labeled("maintenance.field_time_zone").Render(templ.WithChildren(ctx, templ_7745c5c3_Var38), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = labeled("maintenance.field_recurrence").Render(templ.WithChildren(ctx, templ_7745c5c3_Var39), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 67, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Var43 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_Var44 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) if !templ_7745c5c3_IsBuffer { @@ -920,25 +961,38 @@ func maintenanceForm(v MaintenanceView) templ.Component { templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer templ_7745c5c3_Buffer := templ.GetBuffer() var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 68, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 77, ">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) - templ_7745c5c3_Err = labeled("maintenance.field_recurrence").Render(templ.WithChildren(ctx, templ_7745c5c3_Var43), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = labeled("maintenance.field_every").Render(templ.WithChildren(ctx, templ_7745c5c3_Var44), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -1024,20 +1032,20 @@ func maintenanceForm(v MaintenanceView) templ.Component { templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer templ_7745c5c3_Buffer := templ.GetBuffer() var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 77, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 81, ">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return nil }) - templ_7745c5c3_Err = labeled("maintenance.field_every").Render(templ.WithChildren(ctx, templ_7745c5c3_Var48), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = labeled("maintenance.field_until").Render(templ.WithChildren(ctx, templ_7745c5c3_Var48), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Var52 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { - templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) - if !templ_7745c5c3_IsBuffer { - defer func() { - templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) - if templ_7745c5c3_Err == nil { - templ_7745c5c3_Err = templ_7745c5c3_BufErr - } - }() - } - ctx = templ.InitializeContext(ctx) - { - templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer - templ_7745c5c3_Buffer := templ.GetBuffer() - var templ_7745c5c3_ElementScripts []templ.ComponentScript - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 81, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 82, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var52 string + templ_7745c5c3_Var52, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_weekdays")) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 172, Col: 124} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var52)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 83, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, d := range []int{1, 2, 3, 4, 5, 6, 0} { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 84, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 89, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 85, "
") + var templ_7745c5c3_Var55 string + templ_7745c5c3_Var55, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.weekdays_hint")) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 181, Col: 90} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var55)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 90, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var56 string - templ_7745c5c3_Var56, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_weekdays")) + templ_7745c5c3_Var56, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_groups")) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 172, Col: 124} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 185, Col: 123} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var56)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 86, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 91, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - for _, d := range []int{1, 2, 3, 4, 5, 6, 0} { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 87, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 92, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var59 string - templ_7745c5c3_Var59, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.weekdays_hint")) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 181, Col: 90} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var59)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 93, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 98, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var60 string - templ_7745c5c3_Var60, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_groups")) + templ_7745c5c3_Var60, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_monitors")) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 185, Col: 123} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 196, Col: 125} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var60)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 94, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - for _, g := range v.Groups { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 95, "") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 101, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var64 string - templ_7745c5c3_Var64, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "maintenance.field_monitors")) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/maintenance.templ`, Line: 196, Col: 125} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var64)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 102, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 99, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for _, m := range v.Monitors { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 103, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 104, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 108, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 105, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -1361,7 +1298,7 @@ func maintenanceForm(v MaintenanceView) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 109, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 106, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -1385,30 +1322,30 @@ func labeled(labelID string) templ.Component { }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var67 := templ.GetChildren(ctx) + templ_7745c5c3_Var63 := templ.GetChildren(ctx) ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 110, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 109, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/web/markdown.templ b/internal/web/markdown.templ new file mode 100644 index 0000000..604a256 --- /dev/null +++ b/internal/web/markdown.templ @@ -0,0 +1,59 @@ +package web + +import ( + "strconv" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n" +) + +// Markdown editor feature sets. Every entry is something RenderMarkdown +// shows; images and raw HTML are not, so the editor offers neither. +var ( + // MarkdownDocument suits standalone pages such as a legal notice. + MarkdownDocument = []string{"heading", "bold", "italic", "strike", "link", "ul", "ol", "quote", "code", "codeblock", "table", "hr"} + // MarkdownInline suits descriptions shown inside a page, where headings + // and tables would compete with the page's own structure. + MarkdownInline = []string{"bold", "italic", "strike", "link", "ul", "ol", "quote", "code"} +) + +var markdownGlyphs = map[string]string{ + "heading": "H", "bold": "B", "italic": "I", "strike": "S", "link": "↗", "ul": "•", "ol": "1.", + "quote": "❝", "code": "`", "codeblock": "{ }", "table": "▦", "hr": "—", +} + +var markdownGlyphClass = map[string]string{"bold": "font-bold", "italic": "italic", "strike": "line-through"} + +// MarkdownEditor is a textarea with a toolbar of features, a preview +// rendered by the server, list continuation and a character count. +templ MarkdownEditor(name, value string, rows, maxLength int, features []string) { +
+
+ +
+ + +
+
+ + +
+ { i18n.T(ctx, "md.hint") } + +
+
+} diff --git a/internal/web/markdown_templ.go b/internal/web/markdown_templ.go new file mode 100644 index 0000000..8fb5b27 --- /dev/null +++ b/internal/web/markdown_templ.go @@ -0,0 +1,277 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.3.1118 + +package web + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +import ( + "strconv" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n" +) + +// Markdown editor feature sets. Every entry is something RenderMarkdown +// shows; images and raw HTML are not, so the editor offers neither. +var ( + // MarkdownDocument suits standalone pages such as a legal notice. + MarkdownDocument = []string{"heading", "bold", "italic", "strike", "link", "ul", "ol", "quote", "code", "codeblock", "table", "hr"} + // MarkdownInline suits descriptions shown inside a page, where headings + // and tables would compete with the page's own structure. + MarkdownInline = []string{"bold", "italic", "strike", "link", "ul", "ol", "quote", "code"} +) + +var markdownGlyphs = map[string]string{ + "heading": "H", "bold": "B", "italic": "I", "strike": "S", "link": "↗", "ul": "•", "ol": "1.", + "quote": "❝", "code": "`", "codeblock": "{ }", "table": "▦", "hr": "—", +} + +var markdownGlyphClass = map[string]string{"bold": "font-bold", "italic": "italic", "strike": "line-through"} + +// MarkdownEditor is a textarea with a toolbar of features, a preview +// rendered by the server, list continuation and a character count. +func MarkdownEditor(name, value string, rows, maxLength int, features []string) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, f := range features { + { + templ_7745c5c3_ElementWriter := templ_7745c5c3_Buffer + templ_7745c5c3_Buffer := templ.GetBuffer() + var templ_7745c5c3_ElementScripts []templ.ComponentScript + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var16 string + templ_7745c5c3_Var16, templ_7745c5c3_Err = templ.JoinStringErrs(i18n.T(ctx, "md.hint")) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/markdown.templ`, Line: 55, Col: 33} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var16)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return nil + }) +} + +var _ = templruntime.GeneratedTemplate diff --git a/internal/web/markdown_test.go b/internal/web/markdown_test.go new file mode 100644 index 0000000..3cd285f --- /dev/null +++ b/internal/web/markdown_test.go @@ -0,0 +1,27 @@ +package web + +import ( + "strings" + "testing" +) + +func TestRenderMarkdown(t *testing.T) { + for _, tc := range []struct { + in, want, notWant string + }{ + {"**bold** _it_ ~~gone~~", "bold it gone", ""}, + {"![tracker](https://evil.example/pixel.png)", "tracker", "alert(1)", "", "", ""}, + {"see https://example.com", ``, ""}, + } { + got, err := RenderMarkdown(tc.in) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(got, tc.want) || (tc.notWant != "" && strings.Contains(got, tc.notWant)) { + t.Errorf("RenderMarkdown(%q) = %q, want %q without %q", tc.in, got, tc.want, tc.notWant) + } + } +} diff --git a/internal/web/statuspages.templ b/internal/web/statuspages.templ index 83911f8..4791c36 100644 --- a/internal/web/statuspages.templ +++ b/internal/web/statuspages.templ @@ -104,11 +104,10 @@ templ StatusPageEditor(p Page, me Me, e PageEditor) { @formError() @valueField(i18n.T(ctx, "pages.page_title"), "title", e.Title, "", true) @valueField(i18n.T(ctx, "pages.slug"), "slug", e.Slug, "", true) -