impl. Kroki, compile path, and code-editor feature
This commit is contained in:
1 parent
c1bc8f0c03
commit
94d56f9a8f
49 files changed
+40652
-389
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleBrowser(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
currentPath :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/v/browser",
|
||||
)
|
||||
|
||||
if currentPath == "" || currentPath == "/" {
|
||||
currentPath = "/"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(currentPath, "/") {
|
||||
currentPath = "/" + currentPath
|
||||
}
|
||||
|
||||
entries, err :=
|
||||
s.store.ListBrowserEntries(
|
||||
currentPath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"browser.gohtml",
|
||||
map[string]any{
|
||||
"NavItems": resolveNavItems(
|
||||
s.getSystemNavItems(),
|
||||
NavContext{
|
||||
Path: currentPath,
|
||||
},
|
||||
),
|
||||
"Path": currentPath,
|
||||
"Breadcrumbs": buildBreadcrumbs(currentPath),
|
||||
"Entries": entries,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/compiler"
|
||||
)
|
||||
|
||||
func (s *Server) compileMarkdownFromPath(
|
||||
path string,
|
||||
) (string, error) {
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
html :=
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
path,
|
||||
)
|
||||
|
||||
return compiler.HTMLToMarkdown(
|
||||
html,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handleCompileMarkdown(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/:/compile/markdown",
|
||||
)
|
||||
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
out, err :=
|
||||
s.compileMarkdownFromPath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"text/markdown; charset=utf-8",
|
||||
)
|
||||
|
||||
_, _ = w.Write(
|
||||
[]byte(out),
|
||||
)
|
||||
}
|
||||
@@ -166,10 +166,9 @@ func (s *Server) handleConfig(
|
||||
s.getTheme()
|
||||
|
||||
if v := strings.TrimSpace(r.FormValue("theme")); v != "" {
|
||||
switch v {
|
||||
case "dark", "light", "monokai", "solarized-dark", "solarized-light", "terminal-green", "amber-crt", "lain-dark", "nanoha-dark":
|
||||
if isAllowedTheme(v) {
|
||||
theme = v
|
||||
default:
|
||||
} else {
|
||||
http.Error(w, "invalid theme", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -239,6 +238,51 @@ func (s *Server) handleConfig(
|
||||
return
|
||||
}
|
||||
|
||||
case "editor":
|
||||
|
||||
keymap :=
|
||||
strings.TrimSpace(
|
||||
r.FormValue("editor_keymap"),
|
||||
)
|
||||
|
||||
if keymap != "vim" {
|
||||
keymap = "default"
|
||||
}
|
||||
|
||||
err = s.store.SetConfig(
|
||||
"editor_keymap",
|
||||
keymap,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.SetConfig(
|
||||
"editor_syntax_highlight",
|
||||
boolFormValue(
|
||||
r.FormValue("editor_syntax_highlight"),
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.SetConfig(
|
||||
"editor_autocomplete",
|
||||
boolFormValue(
|
||||
r.FormValue("editor_autocomplete"),
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
case "zombie-purge":
|
||||
|
||||
_, err = s.store.PurgeZombieObjects(
|
||||
@@ -338,6 +382,10 @@ func (s *Server) handleConfig(
|
||||
"Locale": s.getLocale(),
|
||||
"Timezone": s.getTimezoneName(),
|
||||
"Theme": s.getTheme(),
|
||||
"ThemeOptions": themeOptions(),
|
||||
"EditorKeymap": s.getEditorKeymap(),
|
||||
"EditorSyntax": s.getEditorSyntaxHighlight(),
|
||||
"EditorAutocomplete": s.getEditorAutocomplete(),
|
||||
"ZombieGraceHours": zombieGraceHours,
|
||||
"Objects": recent,
|
||||
"ZombieObjects": zombies,
|
||||
@@ -346,3 +394,16 @@ func (s *Server) handleConfig(
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func boolFormValue(
|
||||
v string,
|
||||
) string {
|
||||
|
||||
if v == "on" ||
|
||||
v == "true" ||
|
||||
v == "1" {
|
||||
return "true"
|
||||
}
|
||||
|
||||
return "false"
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var diagramSourceCache = map[string]diagramCacheEntry{}
|
||||
|
||||
type diagramCacheEntry struct {
|
||||
Type string
|
||||
Source string
|
||||
At time.Time
|
||||
}
|
||||
|
||||
func diagramHash(
|
||||
diagramType string,
|
||||
source string,
|
||||
format string,
|
||||
) string {
|
||||
|
||||
sum :=
|
||||
sha256.Sum256(
|
||||
[]byte(
|
||||
diagramType + "\x00" + format + "\x00" + source,
|
||||
),
|
||||
)
|
||||
|
||||
return hex.EncodeToString(
|
||||
sum[:],
|
||||
)
|
||||
}
|
||||
|
||||
func allowedDiagramType(
|
||||
t string,
|
||||
) bool {
|
||||
|
||||
switch t {
|
||||
case "mermaid", "plantuml", "graphviz", "dot", "d2":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeDiagramType(
|
||||
t string,
|
||||
) string {
|
||||
|
||||
switch t {
|
||||
case "dot":
|
||||
return "graphviz"
|
||||
default:
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
func krokiURL() string {
|
||||
|
||||
v :=
|
||||
strings.TrimSpace(
|
||||
os.Getenv("KROKI_URL"),
|
||||
)
|
||||
|
||||
if v == "" {
|
||||
return "http://kroki:8000"
|
||||
}
|
||||
|
||||
return strings.TrimRight(
|
||||
v,
|
||||
"/",
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) renderDiagramsInHTML(
|
||||
html string,
|
||||
) string {
|
||||
|
||||
re :=
|
||||
regexp.MustCompile(`(?s)<pre><code class="language-(mermaid|plantuml|graphviz|dot|d2)">(.+?)</code></pre>`)
|
||||
|
||||
return re.ReplaceAllStringFunc(
|
||||
html,
|
||||
func(block string) string {
|
||||
|
||||
m :=
|
||||
re.FindStringSubmatch(
|
||||
block,
|
||||
)
|
||||
|
||||
if len(m) != 3 {
|
||||
return block
|
||||
}
|
||||
|
||||
diagramType :=
|
||||
normalizeDiagramType(
|
||||
m[1],
|
||||
)
|
||||
|
||||
source :=
|
||||
htmlUnescapeString(
|
||||
m[2],
|
||||
)
|
||||
|
||||
hash :=
|
||||
diagramHash(
|
||||
diagramType,
|
||||
source,
|
||||
"svg",
|
||||
)
|
||||
|
||||
diagramSourceCache[hash] =
|
||||
diagramCacheEntry{
|
||||
Type: diagramType,
|
||||
Source: source,
|
||||
At: time.Now(),
|
||||
}
|
||||
|
||||
return `<figure class="diagram diagram-` + diagramType + `">` +
|
||||
`<img src="/:/compile/diagram/` + diagramType + `/` + hash + `.svg" loading="lazy" alt="` + diagramType + ` diagram">` +
|
||||
`</figure>`
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func htmlUnescapeString(
|
||||
s string,
|
||||
) string {
|
||||
|
||||
r :=
|
||||
strings.NewReplacer(
|
||||
"<", "<",
|
||||
">", ">",
|
||||
"&", "&",
|
||||
""", `"`,
|
||||
"'", "'",
|
||||
)
|
||||
|
||||
return r.Replace(
|
||||
s,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handleCompileDiagram(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
s.handleCompileDiagramPost(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
rest :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/:/compile/diagram/",
|
||||
)
|
||||
|
||||
parts :=
|
||||
strings.Split(
|
||||
rest,
|
||||
"/",
|
||||
)
|
||||
|
||||
if len(parts) != 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
diagramType :=
|
||||
normalizeDiagramType(
|
||||
parts[0],
|
||||
)
|
||||
|
||||
if !allowedDiagramType(diagramType) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
hashFile :=
|
||||
parts[1]
|
||||
|
||||
if !strings.HasSuffix(hashFile, ".svg") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
hash :=
|
||||
strings.TrimSuffix(
|
||||
hashFile,
|
||||
".svg",
|
||||
)
|
||||
|
||||
if !regexp.MustCompile(`^[a-f0-9]{64}$`).MatchString(hash) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
cachePath :=
|
||||
filepath.Join(
|
||||
"/data/cache/diagram",
|
||||
diagramType,
|
||||
hash+".svg",
|
||||
)
|
||||
|
||||
if b, err := os.ReadFile(cachePath); err == nil {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"image/svg+xml; charset=utf-8",
|
||||
)
|
||||
|
||||
_, _ = w.Write(b)
|
||||
return
|
||||
}
|
||||
|
||||
entry, ok :=
|
||||
diagramSourceCache[hash]
|
||||
|
||||
if !ok || entry.Type != diagramType {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
s.renderDiagramSVG(
|
||||
w,
|
||||
diagramType,
|
||||
entry.Source,
|
||||
cachePath,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handleCompileDiagramPost(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
rest :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/:/compile/diagram/",
|
||||
)
|
||||
|
||||
diagramType :=
|
||||
normalizeDiagramType(
|
||||
strings.Trim(rest, "/"),
|
||||
)
|
||||
|
||||
if !allowedDiagramType(diagramType) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
sourceBytes, err :=
|
||||
io.ReadAll(
|
||||
io.LimitReader(
|
||||
r.Body,
|
||||
1024*1024,
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
source :=
|
||||
string(sourceBytes)
|
||||
|
||||
hash :=
|
||||
diagramHash(
|
||||
diagramType,
|
||||
source,
|
||||
"svg",
|
||||
)
|
||||
|
||||
cachePath :=
|
||||
filepath.Join(
|
||||
"/data/cache/diagram",
|
||||
diagramType,
|
||||
hash+".svg",
|
||||
)
|
||||
|
||||
if b, err := os.ReadFile(cachePath); err == nil {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"image/svg+xml; charset=utf-8",
|
||||
)
|
||||
|
||||
_, _ = w.Write(b)
|
||||
return
|
||||
}
|
||||
|
||||
s.renderDiagramSVG(
|
||||
w,
|
||||
diagramType,
|
||||
source,
|
||||
cachePath,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) renderDiagramSVG(
|
||||
w http.ResponseWriter,
|
||||
diagramType string,
|
||||
source string,
|
||||
cachePath string,
|
||||
) {
|
||||
|
||||
req, err :=
|
||||
http.NewRequest(
|
||||
http.MethodPost,
|
||||
krokiURL()+"/"+diagramType+"/svg",
|
||||
bytes.NewBufferString(source),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set(
|
||||
"Content-Type",
|
||||
"text/plain; charset=utf-8",
|
||||
)
|
||||
|
||||
client :=
|
||||
http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
}
|
||||
|
||||
resp, err :=
|
||||
client.Do(
|
||||
req,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 502)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 ||
|
||||
resp.StatusCode >= 300 {
|
||||
|
||||
http.Error(w, "diagram renderer failed", 502)
|
||||
return
|
||||
}
|
||||
|
||||
b, err :=
|
||||
io.ReadAll(
|
||||
resp.Body,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
_ =
|
||||
os.MkdirAll(
|
||||
filepath.Dir(cachePath),
|
||||
0755,
|
||||
)
|
||||
|
||||
_ =
|
||||
os.WriteFile(
|
||||
cachePath,
|
||||
b,
|
||||
0644,
|
||||
)
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"image/svg+xml; charset=utf-8",
|
||||
)
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Security-Policy",
|
||||
"default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
||||
)
|
||||
|
||||
_, _ =
|
||||
w.Write(
|
||||
b,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package server
|
||||
|
||||
func (s *Server) getEditorKeymap() string {
|
||||
|
||||
v, err :=
|
||||
s.store.GetConfig(
|
||||
"editor_keymap",
|
||||
)
|
||||
|
||||
if err != nil || v == "" {
|
||||
return "default"
|
||||
}
|
||||
|
||||
switch v {
|
||||
case "default", "vim":
|
||||
return v
|
||||
default:
|
||||
return "default"
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getEditorSyntaxHighlight() bool {
|
||||
|
||||
v, err :=
|
||||
s.store.GetConfig(
|
||||
"editor_syntax_highlight",
|
||||
)
|
||||
|
||||
if err != nil || v == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return v != "false"
|
||||
}
|
||||
|
||||
func (s *Server) getEditorAutocomplete() bool {
|
||||
|
||||
v, err :=
|
||||
s.store.GetConfig(
|
||||
"editor_autocomplete",
|
||||
)
|
||||
|
||||
if err != nil || v == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return v != "false"
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleEditorRoute(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/e/-/") {
|
||||
s.handleSemanticEditor(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Known future editor adapters.
|
||||
for _, prefix := range []string{
|
||||
"/e/plain/",
|
||||
"/e/hex/",
|
||||
"/e/pdf/",
|
||||
"/e/office/",
|
||||
} {
|
||||
if strings.HasPrefix(r.URL.Path, prefix) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Backward compatibility:
|
||||
// /e/foo/bar -> /e/-/foo/bar
|
||||
oldPath :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/e",
|
||||
)
|
||||
|
||||
if oldPath == "" {
|
||||
oldPath = "/"
|
||||
}
|
||||
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
"/e/-"+oldPath,
|
||||
http.StatusFound,
|
||||
)
|
||||
}
|
||||
@@ -11,6 +11,11 @@ func (s *Server) handleHome(
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
if r.URL.Path != "/" {
|
||||
s.handleSemanticRoute(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
homePath, err :=
|
||||
s.store.GetConfig(
|
||||
"home_path",
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MarkdownLine struct {
|
||||
Number int
|
||||
Text string
|
||||
}
|
||||
|
||||
func splitMarkdownLines(
|
||||
body string,
|
||||
) []MarkdownLine {
|
||||
|
||||
body =
|
||||
strings.ReplaceAll(
|
||||
body,
|
||||
"\r\n",
|
||||
"\n",
|
||||
)
|
||||
|
||||
lines :=
|
||||
strings.Split(
|
||||
body,
|
||||
"\n",
|
||||
)
|
||||
|
||||
out :=
|
||||
make(
|
||||
[]MarkdownLine,
|
||||
0,
|
||||
len(lines),
|
||||
)
|
||||
|
||||
for i, line := range lines {
|
||||
|
||||
out = append(
|
||||
out,
|
||||
MarkdownLine{
|
||||
Number: i + 1,
|
||||
Text: line,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) handleMarkdownView(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/v/markdown",
|
||||
)
|
||||
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
markdown, err :=
|
||||
s.compileMarkdownFromPath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"markdown_view.gohtml",
|
||||
map[string]any{
|
||||
"NavItems": resolveNavItems(
|
||||
s.getSystemNavItems(),
|
||||
NavContext{
|
||||
Path: path,
|
||||
ObjectUUID: objectUUID,
|
||||
ObjectURI: "uuid:" + objectUUID,
|
||||
},
|
||||
),
|
||||
"Path": path,
|
||||
"ObjectUUID": objectUUID,
|
||||
"ObjectURI": "uuid:" + objectUUID,
|
||||
"Lines": splitMarkdownLines(markdown),
|
||||
"MarkdownRaw": markdown,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handlePlainView(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/v/plain",
|
||||
)
|
||||
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"text/plain; charset=utf-8",
|
||||
)
|
||||
|
||||
_, _ = w.Write(
|
||||
[]byte(latest.Body),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handlePreview(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/v/preview",
|
||||
)
|
||||
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
if err == nil &&
|
||||
strings.TrimSpace(latest.Body) != "" {
|
||||
|
||||
rendered :=
|
||||
s.renderDiagramsInHTML(
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
path,
|
||||
),
|
||||
)
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"preview.gohtml",
|
||||
map[string]any{
|
||||
"Path": path,
|
||||
"RenderedHTML": template.HTML(
|
||||
rendered,
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
entries, err :=
|
||||
s.store.ListBrowserEntries(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"preview.gohtml",
|
||||
map[string]any{
|
||||
"Path": path,
|
||||
"IsListing": true,
|
||||
"Entries": entries,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -8,11 +8,13 @@ func mergeTemplateData(
|
||||
data any,
|
||||
wikiName string,
|
||||
theme string,
|
||||
sThemeCSS string,
|
||||
) map[string]any {
|
||||
|
||||
out := map[string]any{
|
||||
"WikiName": wikiName,
|
||||
"Theme": theme,
|
||||
"ThemeCSS": sThemeCSS,
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
@@ -46,6 +48,7 @@ func (s *Server) render(
|
||||
data,
|
||||
s.getWikiName(),
|
||||
s.getTheme(),
|
||||
s.getThemeCSS(),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
+47
-102
@@ -3,15 +3,31 @@ package routes
|
||||
import "net/http"
|
||||
|
||||
type UIHandlers struct {
|
||||
Home http.HandlerFunc
|
||||
NewPage http.HandlerFunc
|
||||
SemanticEditor http.HandlerFunc
|
||||
SemanticPage http.HandlerFunc
|
||||
RawView http.HandlerFunc
|
||||
LinkManager http.HandlerFunc
|
||||
Health http.HandlerFunc
|
||||
Tree http.HandlerFunc
|
||||
Config http.HandlerFunc
|
||||
Home http.HandlerFunc
|
||||
NewPage http.HandlerFunc
|
||||
SemanticEditor http.HandlerFunc
|
||||
SemanticPage http.HandlerFunc
|
||||
RawView http.HandlerFunc
|
||||
LinkManager http.HandlerFunc
|
||||
Health http.HandlerFunc
|
||||
Tree http.HandlerFunc
|
||||
Browser http.HandlerFunc
|
||||
Preview http.HandlerFunc
|
||||
PlainView http.HandlerFunc
|
||||
MarkdownView http.HandlerFunc
|
||||
CompileMarkdown http.HandlerFunc
|
||||
CompileDiagram http.HandlerFunc
|
||||
Config http.HandlerFunc
|
||||
}
|
||||
|
||||
func handleIfNotNil(
|
||||
mux *http.ServeMux,
|
||||
pattern string,
|
||||
h http.HandlerFunc,
|
||||
) {
|
||||
if h != nil {
|
||||
mux.HandleFunc(pattern, h)
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterUIRoutes(
|
||||
@@ -19,103 +35,32 @@ func RegisterUIRoutes(
|
||||
h UIHandlers,
|
||||
) {
|
||||
|
||||
mux.HandleFunc(
|
||||
"/healthz",
|
||||
h.Health,
|
||||
)
|
||||
handleIfNotNil(mux, "/", h.Home)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/v/tree",
|
||||
h.Tree,
|
||||
)
|
||||
handleIfNotNil(mux, "/:/new", h.NewPage)
|
||||
handleIfNotNil(mux, "/new", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/:/new", http.StatusFound)
|
||||
})
|
||||
handleIfNotNil(mux, "/e/", h.SemanticEditor)
|
||||
handleIfNotNil(mux, "/r/", h.RawView)
|
||||
handleIfNotNil(mux, "/l/", h.LinkManager)
|
||||
handleIfNotNil(mux, "/health", h.Health)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/-/sys/config",
|
||||
h.Config,
|
||||
)
|
||||
handleIfNotNil(mux, "/v/tree", h.Tree)
|
||||
handleIfNotNil(mux, "/v/browser", h.Browser)
|
||||
handleIfNotNil(mux, "/v/browser/", h.Browser)
|
||||
handleIfNotNil(mux, "/v/preview", h.Preview)
|
||||
handleIfNotNil(mux, "/v/preview/", h.Preview)
|
||||
handleIfNotNil(mux, "/v/plain", h.PlainView)
|
||||
handleIfNotNil(mux, "/v/plain/", h.PlainView)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/new",
|
||||
h.NewPage,
|
||||
)
|
||||
handleIfNotNil(mux, "/v/markdown", h.MarkdownView)
|
||||
handleIfNotNil(mux, "/v/markdown/", h.MarkdownView)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/e",
|
||||
h.SemanticEditor,
|
||||
)
|
||||
handleIfNotNil(mux, "/:/compile/markdown", h.CompileMarkdown)
|
||||
handleIfNotNil(mux, "/:/compile/markdown/", h.CompileMarkdown)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/e/",
|
||||
h.SemanticEditor,
|
||||
)
|
||||
handleIfNotNil(mux, "/:/compile/diagram/", h.CompileDiagram)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/raw",
|
||||
h.RawView,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/raw/",
|
||||
h.RawView,
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"/l/",
|
||||
h.LinkManager,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/",
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
|
||||
h.Home(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
switch r.URL.Path {
|
||||
|
||||
case "/v/tree":
|
||||
|
||||
h.Tree(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
case "/new":
|
||||
|
||||
h.NewPage(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/e" ||
|
||||
r.URL.Path == "/raw" {
|
||||
|
||||
http.NotFound(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
h.SemanticPage(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
},
|
||||
)
|
||||
handleIfNotNil(mux, "/-/sys/config", h.Config)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@@ -41,12 +40,13 @@ func (s *Server) handleSemanticRoute(
|
||||
}
|
||||
|
||||
bs := buildBreadcrumbs(path)
|
||||
log.Printf("breadcrumb debug path=%s bs=%#v", path, bs)
|
||||
|
||||
rendered :=
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
path,
|
||||
s.renderDiagramsInHTML(
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
path,
|
||||
),
|
||||
)
|
||||
|
||||
s.render(
|
||||
@@ -55,7 +55,7 @@ func (s *Server) handleSemanticRoute(
|
||||
map[string]any{
|
||||
"Title": path,
|
||||
"Path": path,
|
||||
"Breadcrumbs": buildBreadcrumbs(path),
|
||||
"Breadcrumbs": bs,
|
||||
"ObjectUUID": objectUUID,
|
||||
"NavItems": resolveNavItems(
|
||||
s.getSystemNavItems(),
|
||||
@@ -79,7 +79,7 @@ func (s *Server) handleSemanticEditor(
|
||||
|
||||
path := strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/e",
|
||||
"/e/-",
|
||||
)
|
||||
|
||||
if path == "" {
|
||||
@@ -128,12 +128,26 @@ func (s *Server) handleSemanticEditor(
|
||||
|
||||
if editType == "markdown" {
|
||||
|
||||
body =
|
||||
htmlToMarkdown(
|
||||
body,
|
||||
compiled, err :=
|
||||
s.compileMarkdownFromPath(
|
||||
path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
body = compiled
|
||||
}
|
||||
|
||||
renderedBody :=
|
||||
body
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"editor.gohtml",
|
||||
@@ -144,56 +158,14 @@ func (s *Server) handleSemanticEditor(
|
||||
"Body": body,
|
||||
"RenderedHTML": template.HTML(
|
||||
s.expandMacros(
|
||||
body,
|
||||
renderedBody,
|
||||
path,
|
||||
),
|
||||
),
|
||||
"EditType": editType,
|
||||
"EditType": editType,
|
||||
"EditorKeymap": s.getEditorKeymap(),
|
||||
"EditorSyntax": s.getEditorSyntaxHighlight(),
|
||||
"EditorAutocomplete": s.getEditorAutocomplete(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func htmlToMarkdown(
|
||||
in string,
|
||||
) string {
|
||||
|
||||
out := in
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"<h1>",
|
||||
"# ",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"</h1>",
|
||||
"",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"<ul>",
|
||||
"",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"</ul>",
|
||||
"",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"<li>",
|
||||
"- ",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"</li>",
|
||||
"",
|
||||
)
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -91,6 +91,18 @@ func (s *Server) registerStaticRoutes() {
|
||||
staticFS,
|
||||
),
|
||||
)
|
||||
|
||||
themeFS := http.FileServer(
|
||||
http.Dir("/data/themes"),
|
||||
)
|
||||
|
||||
s.mux.Handle(
|
||||
"/-/theme/",
|
||||
http.StripPrefix(
|
||||
"/-/theme/",
|
||||
themeFS,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) mustLoadTemplates() {
|
||||
@@ -164,15 +176,21 @@ func (s *Server) registerRoutes() {
|
||||
routespkg.RegisterUIRoutes(
|
||||
s.mux,
|
||||
routespkg.UIHandlers{
|
||||
Home: s.handleHome,
|
||||
NewPage: s.handleNewPage,
|
||||
SemanticEditor: s.handleSemanticEditor,
|
||||
SemanticPage: s.handleSemanticRoute,
|
||||
RawView: s.handleRawView,
|
||||
LinkManager: s.handleLinkManager,
|
||||
Health: s.handleHealth,
|
||||
Tree: s.handleTree,
|
||||
Config: s.handleConfig,
|
||||
Home: s.handleHome,
|
||||
NewPage: s.handleNewPage,
|
||||
SemanticEditor: s.handleEditorRoute,
|
||||
SemanticPage: s.handleSemanticRoute,
|
||||
RawView: s.handleRawView,
|
||||
LinkManager: s.handleLinkManager,
|
||||
Health: s.handleHealth,
|
||||
Tree: s.handleTree,
|
||||
Browser: s.handleBrowser,
|
||||
Preview: s.handlePreview,
|
||||
PlainView: s.handlePlainView,
|
||||
MarkdownView: s.handleMarkdownView,
|
||||
CompileMarkdown: s.handleCompileMarkdown,
|
||||
CompileDiagram: s.handleCompileDiagram,
|
||||
Config: s.handleConfig,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
+98
-10
@@ -1,20 +1,108 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ThemeOption struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
CSS string `json:"css"`
|
||||
}
|
||||
|
||||
func builtinThemeOptions() []ThemeOption {
|
||||
return []ThemeOption{
|
||||
{ID: "dark", Label: "Dark", CSS: "/static/css/theme/dark.css"},
|
||||
{ID: "light", Label: "Light", CSS: "/static/css/theme/light.css"},
|
||||
{ID: "monokai", Label: "Monokai", CSS: "/static/css/theme/monokai.css"},
|
||||
{ID: "solarized-dark", Label: "Solarized Dark", CSS: "/static/css/theme/solarized-dark.css"},
|
||||
{ID: "solarized-light", Label: "Solarized Light", CSS: "/static/css/theme/solarized-light.css"},
|
||||
{ID: "terminal-green", Label: "Terminal Green", CSS: "/static/css/theme/terminal-green.css"},
|
||||
{ID: "amber-crt", Label: "Amber CRT", CSS: "/static/css/theme/amber-crt.css"},
|
||||
{ID: "lain-dark", Label: "Lain Dark", CSS: "/static/css/theme/lain-dark.css"},
|
||||
{ID: "nanoha-dark", Label: "Nanoha Dark", CSS: "/static/css/theme/nanoha-dark.css"},
|
||||
}
|
||||
}
|
||||
|
||||
func externalThemeOptions() []ThemeOption {
|
||||
path := "/data/themes/themes.json"
|
||||
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var items []ThemeOption
|
||||
if err := json.Unmarshal(b, &items); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var out []ThemeOption
|
||||
|
||||
for _, item := range items {
|
||||
item.ID = strings.TrimSpace(item.ID)
|
||||
item.Label = strings.TrimSpace(item.Label)
|
||||
item.CSS = strings.TrimSpace(item.CSS)
|
||||
|
||||
if item.ID == "" || item.Label == "" || item.CSS == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(item.ID, "/") || strings.Contains(item.ID, "..") {
|
||||
continue
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(item.CSS, "/") {
|
||||
item.CSS = "/-/theme/" + filepath.Base(item.CSS)
|
||||
}
|
||||
|
||||
out = append(out, item)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func themeOptions() []ThemeOption {
|
||||
items := builtinThemeOptions()
|
||||
items = append(items, externalThemeOptions()...)
|
||||
return items
|
||||
}
|
||||
|
||||
func findTheme(id string) (ThemeOption, bool) {
|
||||
for _, opt := range themeOptions() {
|
||||
if opt.ID == id {
|
||||
return opt, true
|
||||
}
|
||||
}
|
||||
return ThemeOption{}, false
|
||||
}
|
||||
|
||||
func isAllowedTheme(theme string) bool {
|
||||
_, ok := findTheme(theme)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s *Server) getTheme() string {
|
||||
|
||||
theme, err :=
|
||||
s.store.GetConfig(
|
||||
"theme",
|
||||
)
|
||||
|
||||
theme, err := s.store.GetConfig("theme")
|
||||
if err != nil || theme == "" {
|
||||
return "dark"
|
||||
}
|
||||
|
||||
switch theme {
|
||||
case "dark", "light", "monokai", "solarized-dark", "solarized-light", "terminal-green", "amber-crt", "lain-dark", "nanoha-dark":
|
||||
return theme
|
||||
default:
|
||||
if !isAllowedTheme(theme) {
|
||||
return "dark"
|
||||
}
|
||||
|
||||
return theme
|
||||
}
|
||||
|
||||
func (s *Server) getThemeCSS() string {
|
||||
opt, ok := findTheme(s.getTheme())
|
||||
if !ok {
|
||||
return "/static/css/theme/dark.css"
|
||||
}
|
||||
|
||||
return opt.CSS
|
||||
}
|
||||
Reference in new issue
Block a user