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 // validate form values
if post_type == "" { 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 return
} }
if post_type == "article" && title == "" { if (post_type == "article" || post_type == "page") && title == "" {
userEditorGetHandler(repo)(w, r, ps) html, _ := owl.RenderUserError(user, owl.ErrorMessage{
Error: "Missing Title",
Message: "Articles and Pages must have a title",
})
w.Write([]byte(html))
return return
} }
if post_type == "reply" && reply_url == "" { if post_type == "reply" && reply_url == "" {

View File

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

View File

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

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

View File

@ -436,3 +436,15 @@ func TestRenderHeaderMenuUrlItem(t *testing.T) {
assertions.AssertContains(t, result, "Test Entry") assertions.AssertContains(t, result, "Test Entry")
assertions.AssertContains(t, result, "https://example.com") 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"` Title string `yaml:"title"`
List string `yaml:"list"` List string `yaml:"list"`
Url string `yaml:"url"` Url string `yaml:"url"`
Post string `yaml:"post"`
} }
func (l *PostList) ContainsType(t string) bool { func (l *PostList) ContainsType(t string) bool {