links to posts and creating pages

This commit is contained in:
Niko Abeler 2022-12-04 19:15:50 +01:00
parent 51fd8cefe2
commit 668eb658b2
6 changed files with 39 additions and 7 deletions

View File

@ -209,11 +209,19 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http
// validate form values
if post_type == "" {
userEditorGetHandler(repo)(w, r, ps)
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
Error: "Missing post type",
Message: "Post type is required",
})
w.Write([]byte(html))
return
}
if post_type == "article" && title == "" {
userEditorGetHandler(repo)(w, r, ps)
if (post_type == "article" || post_type == "page") && title == "" {
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
Error: "Missing Title",
Message: "Articles and Pages must have a title",
})
w.Write([]byte(html))
return
}
if post_type == "reply" && reply_url == "" {

View File

@ -1,9 +1,12 @@
<details>
<summary>Write Article</summary>
<summary>Write Article/Page</summary>
<form action="" method="post">
<h2>Create New Article</h2>
<input type="hidden" name="csrf_token" value="{{.CsrfToken}}">
<input type="hidden" name="type" value="article">
<select name="type">
<option value="article">Article</option>
<option value="page">Page</option>
</select>
<label for="title">Title</label>
<input type="text" name="title" placeholder="Title" />
<label for="description">Description</label>

View File

@ -95,6 +95,8 @@
{{ range $link := .User.Config.HeaderMenu }}
{{ if $link.List }}
<li><a href="{{ listUrl $.User $link.List }}">{{ $link.Title }}</a></li>
{{ else if $link.Post }}
<li><a href="{{ postUrl $.User $link.Post }}">{{ $link.Title }}</a></li>
{{ else }}
<li><a href="{{ $link.Url }}">{{ $link.Title }}</a></li>
{{ end }}
@ -103,7 +105,6 @@
</nav>
</div>
</header>
<main class="container">
{{ .Content }}
</main>
<footer class="container">

View File

@ -55,6 +55,11 @@ func listUrl(user User, id string) string {
})
}
func postUrl(user User, id string) string {
post, _ := user.GetPost(id)
return post.UrlPath()
}
func renderEmbedTemplate(templateFile string, data interface{}) (string, error) {
templateStr, err := embed_files.ReadFile(templateFile)
if err != nil {
@ -67,6 +72,7 @@ func renderTemplateStr(templateStr []byte, data interface{}) (string, error) {
t, err := template.New("_").Funcs(template.FuncMap{
"noescape": noescape,
"listUrl": listUrl,
"postUrl": postUrl,
}).Parse(string(templateStr))
if err != nil {
return "", err
@ -84,6 +90,7 @@ func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
t, err := template.New("index").Funcs(template.FuncMap{
"noescape": noescape,
"listUrl": listUrl,
"postUrl": postUrl,
}).Parse(baseTemplate)
if err != nil {
return "", err

View File

@ -436,3 +436,15 @@ func TestRenderHeaderMenuUrlItem(t *testing.T) {
assertions.AssertContains(t, result, "Test Entry")
assertions.AssertContains(t, result, "https://example.com")
}
func TestRenderHeaderMenuPost(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
user.AddHeaderMenuItem(owl.MenuItem{
Title: "Test Entry",
Post: post.Id(),
})
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "Test Entry")
assertions.AssertContains(t, result, post.UrlPath())
}

View File

@ -41,6 +41,7 @@ type MenuItem struct {
Title string `yaml:"title"`
List string `yaml:"list"`
Url string `yaml:"url"`
Post string `yaml:"post"`
}
func (l *PostList) ContainsType(t string) bool {