commit06ed2c3cbeAuthor: adminoo <git@kadath.corp> Date: Tue Feb 3 11:34:24 2026 +0100 fix: changed detected by scanner but no updated by render layer commit01dcaf882aAuthor: adminoo <git@kadath.corp> Date: Tue Feb 3 10:19:05 2026 +0100 feat: VERSION bumb commit229223f77aAuthor: adminoo <git@kadath.corp> Date: Tue Feb 3 09:53:08 2026 +0100 feat: filter and search by tag commitcb11e34798Author: adminoo <git@kadath.corp> Date: Tue Feb 3 09:41:03 2026 +0100 feat: tag system commit3f5cf0d673Author: adminoo <git@kadath.corp> Date: Tue Feb 3 09:15:29 2026 +0100 feat: sqlite storage draft commitd6617cec02Author: adminoo <git@kadath.corp> Date: Tue Feb 3 09:04:11 2026 +0100 feat: metadata draft commit7238d02a13Author: adminoo <git@kadath.corp> Date: Mon Feb 2 10:18:42 2026 +0100 fix: body overflowing commit16ff836274Author: adminoo <git@kadath.corp> Date: Mon Feb 2 10:09:01 2026 +0100 feat: tests for http handlers and render package commit36ac3f03aaAuthor: adminoo <git@kadath.corp> Date: Mon Feb 2 09:45:29 2026 +0100 feat: Dark theme, placeholder metadata panel commite6923fa4f5Author: adminoo <git@kadath.corp> Date: Sun Feb 1 18:26:59 2026 +0100 fix: uneeded func + uneeded bogus note creation logic commit4458ba2d15Author: adminoo <git@kadath.corp> Date: Sun Feb 1 18:26:21 2026 +0100 feat: log when changing note states commit92a6f84540Author: adminoo <git@kadath.corp> Date: Sun Feb 1 16:55:40 2026 +0100 possibly first working draft commite27aadc603Author: adminoo <git@kadath.corp> Date: Sun Feb 1 11:55:16 2026 +0100 draft shits
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package render
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"github.com/russross/blackfriday/v2"
|
|
)
|
|
|
|
type TemplateData struct {
|
|
Name string
|
|
FileNameSet []string
|
|
}
|
|
|
|
type TemplateManager struct {
|
|
templates map[string]*template.Template
|
|
mu sync.RWMutex
|
|
basePath string
|
|
devMode bool
|
|
}
|
|
|
|
func NewTemplateManager(basePath string) *TemplateManager {
|
|
return &TemplateManager{
|
|
templates: make(map[string]*template.Template),
|
|
basePath: basePath,
|
|
devMode: false,
|
|
}
|
|
}
|
|
|
|
func (tm *TemplateManager) buildTemplatePath(name string) string {
|
|
return filepath.Join(tm.basePath, name+".tmpl")
|
|
}
|
|
|
|
func (tm *TemplateManager) GetTemplate(td *TemplateData) (*template.Template, error) {
|
|
// Skip cache in dev mode
|
|
if !tm.devMode {
|
|
tm.mu.RLock()
|
|
if tmpl, exists := tm.templates[td.Name]; exists {
|
|
tm.mu.RUnlock()
|
|
return tmpl, nil
|
|
}
|
|
tm.mu.RUnlock()
|
|
}
|
|
|
|
// Build file paths
|
|
var files []string
|
|
for _, file := range td.FileNameSet {
|
|
files = append(files, tm.buildTemplatePath(file))
|
|
}
|
|
|
|
// Parse template
|
|
tmpl, err := template.ParseFiles(files...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse template %s: %w", td.Name, err)
|
|
}
|
|
|
|
// Cache it (unless in dev mode)
|
|
if !tm.devMode {
|
|
tm.mu.Lock()
|
|
tm.templates[td.Name] = tmpl
|
|
tm.mu.Unlock()
|
|
}
|
|
|
|
return tmpl, nil
|
|
}
|
|
|
|
func (tm *TemplateManager) Render(w http.ResponseWriter, name string, data any) error {
|
|
// Build the template files - include all necessary templates
|
|
var files []string
|
|
|
|
// Always include base template
|
|
files = append(files, tm.buildTemplatePath("base"))
|
|
|
|
// Include noteList template (used by index)
|
|
files = append(files, tm.buildTemplatePath("noteList"))
|
|
|
|
// Include metadata template (used by index)
|
|
files = append(files, tm.buildTemplatePath("metadata"))
|
|
|
|
// Include metadata template
|
|
files = append(files, tm.buildTemplatePath("metadata"))
|
|
|
|
// Add the specific template
|
|
files = append(files, tm.buildTemplatePath(name))
|
|
|
|
// Parse templates
|
|
tmpl, err := template.ParseFiles(files...)
|
|
if err != nil {
|
|
return fmt.Errorf("parse template %s: %w", name, err)
|
|
}
|
|
|
|
// Set content type
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
err = tmpl.ExecuteTemplate(w, "base", data)
|
|
if err != nil {
|
|
return fmt.Errorf("execute template %s: %w", name, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Render markdown to HTML with target="_blank" on links
|
|
func RenderMarkdown(content []byte) (template.HTML, error) {
|
|
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
|
Flags: blackfriday.CommonHTMLFlags | blackfriday.HrefTargetBlank,
|
|
})
|
|
|
|
html := blackfriday.Run(content, blackfriday.WithRenderer(renderer))
|
|
return template.HTML(html), nil
|
|
}
|