Fix page creation

This commit is contained in:
2026-04-13 13:07:20 +02:00
parent 95ca30509c
commit 19017bf136
4 changed files with 44 additions and 3 deletions

View File

@@ -1,3 +1,10 @@
function newPage() {
const name = prompt('New page name:');
if (!name || !name.trim()) return;
const slug = name.trim().replace(/\s+/g, '-');
window.location.href = window.location.pathname + slug + '/?edit';
}
(function () { (function () {
document.addEventListener('keydown', function (e) { document.addEventListener('keydown', function (e) {
if (!e.altKey || !e.shiftKey) return; if (!e.altKey || !e.shiftKey) return;
@@ -5,7 +12,11 @@
case 'E': case 'E':
e.preventDefault(); e.preventDefault();
window.location.href = window.location.pathname + '?edit'; window.location.href = window.location.pathname + '?edit';
break; break;
case 'N':
e.preventDefault();
newPage();
break;
} }
}); });
})(); })();

View File

@@ -19,7 +19,8 @@
<a class="btn-cancel" href="{{.PostURL}}">CANCEL</a> <a class="btn-cancel" href="{{.PostURL}}">CANCEL</a>
<button class="btn-save" type="submit" form="edit-form" data-action="save" data-key="S" title="Save (S)">SAVE</button> <button class="btn-save" type="submit" form="edit-form" data-action="save" data-key="S" title="Save (S)">SAVE</button>
{{else if .CanEdit}} {{else if .CanEdit}}
<a class="edit-btn" href="?edit" title="Edit (Alt+Shift+E)">EDIT</a> <button class="new-btn" onclick="newPage()" title="New page (N)">NEW</button>
<a class="edit-btn" href="?edit" title="Edit page (E)">EDIT</a>
{{end}} {{end}}
</header> </header>
<main> <main>

View File

@@ -102,6 +102,25 @@ header {
color: #ffd54f; color: #ffd54f;
} }
.new-btn {
background: none;
border: none;
color: #ffb300;
font: inherit;
cursor: pointer;
padding: 0;
white-space: nowrap;
}
.new-btn::before {
content: "[";
}
.new-btn::after {
content: "]";
}
.new-btn:hover {
color: #ffd54f;
}
/* === Main === */ /* === Main === */
main { main {
max-width: 860px; max-width: 860px;
@@ -313,6 +332,7 @@ textarea:focus {
color: #ffb300; color: #ffb300;
font: inherit; font: inherit;
cursor: pointer; cursor: pointer;
text-shadow: inherit;
padding: 0; padding: 0;
} }
.btn-save::before { .btn-save::before {
@@ -330,6 +350,7 @@ textarea:focus {
border: none; border: none;
color: #ffb300; color: #ffb300;
font: inherit; font: inherit;
text-shadow: inherit;
cursor: pointer; cursor: pointer;
padding: 0; padding: 0;
text-decoration: none; text-decoration: none;

10
main.go
View File

@@ -86,7 +86,15 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
info, err := os.Stat(fsPath) info, err := os.Stat(fsPath)
if err != nil { if err != nil {
if os.IsNotExist(err) && strings.HasSuffix(r.URL.Path, "/") { if os.IsNotExist(err) {
// Non-existent path: redirect GETs to the canonical slash form so
// the browser URL is consistent, then serve an empty folder page.
// POSTs must not be redirected — the form action has no trailing
// slash (path.Clean strips it) and the content would be lost.
if !strings.HasSuffix(r.URL.Path, "/") && r.Method != http.MethodPost {
http.Redirect(w, r, r.URL.Path+"/", http.StatusMovedPermanently)
return
}
h.serveDir(w, r, urlPath, fsPath) h.serveDir(w, r, urlPath, fsPath)
return return
} }