Skip to content

Commit

Permalink
nested lists #26
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed May 18, 2022
1 parent d9c5abb commit b216265
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
10 changes: 7 additions & 3 deletions html/list.partial.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{define "list"}}
<ul style="list-style-type: {{.ListType}};">
<ul style="list-style-type: {{.MetaData.ListType}};">
{{range .Items}}
{{if .IsText}}
{{if .Value}}
Expand All @@ -20,16 +20,20 @@
{{end}}

{{if .IsHeaderOne}}
</ul><h2 class="text-xl font-bold">{{.Value}}</h2><ul style="list-style-type: {{$.ListType}};">
</ul><h2 class="text-xl font-bold">{{.Value}}</h2><ul style="list-style-type: {{$.MetaData.ListType}};">
{{end}}

{{if .IsHeaderTwo}}
</ul><h3 class="text-lg font-bold">{{.Value}}</h3><ul style="list-style-type: {{$.ListType}};">
</ul><h3 class="text-lg font-bold">{{.Value}}</h3><ul style="list-style-type: {{$.MetaData.ListType}};">
{{end}}

{{if .IsPre}}
<li><pre>{{.Value}}</pre></li>
{{end}}

{{if .IsChild}}
<li style="list-style: none;">{{template "list" .Children}}</li>
{{end}}
{{end}}
</ul>
{{end}}
8 changes: 4 additions & 4 deletions internal/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type PostPageData struct {
Description string
Username string
BlogName string
ListType string
MetaData *pkg.MetaData
Items []*pkg.ListItem
PublishAtISO string
PublishAt string
Expand Down Expand Up @@ -281,13 +281,13 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
URL: template.URL(PostURL(post)),
BlogURL: template.URL(internal.BlogURL(username)),
Description: post.Description,
ListType: parsedText.MetaData.ListType,
Title: internal.FilenameToTitle(post.Filename, post.Title),
PublishAt: post.PublishAt.Format("02 Jan, 2006"),
PublishAtISO: post.PublishAt.Format(time.RFC3339),
Username: username,
BlogName: blogName,
Items: parsedText.Items,
MetaData: parsedText.MetaData,
}

ts, err := renderTemplate([]string{
Expand Down Expand Up @@ -451,7 +451,7 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
parsed := pkg.ParseText(post.Text)
var tpl bytes.Buffer
data := &PostPageData{
ListType: parsed.MetaData.ListType,
MetaData: parsed.MetaData,
Items: parsed.Items,
}
if err := ts.Execute(&tpl, data); err != nil {
Expand Down Expand Up @@ -509,7 +509,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
parsed := pkg.ParseText(post.Text)
var tpl bytes.Buffer
data := &PostPageData{
ListType: parsed.MetaData.ListType,
MetaData: parsed.MetaData,
Items: parsed.Items,
}
if err := ts.Execute(&tpl, data); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type ListItem struct {
IsHeaderTwo bool
IsImg bool
IsPre bool
IsChild bool
Children *ParsedText
}

type MetaData struct {
Expand All @@ -39,6 +41,8 @@ var imgToken = "=<"
var headerOneToken = "#"
var headerTwoToken = "##"
var preToken = "```"
var nestedSpaceToken = " "
var nestedTabToken = '\t'

type SplitToken struct {
Key string
Expand Down Expand Up @@ -132,6 +136,19 @@ func ParseText(text string) *ParsedText {
nextValue := strings.Replace(li.Value, preToken, "", 1)
prevItem.Value = fmt.Sprintf("%s\n%s", prevItem.Value, nextValue)
skip = true
} else if strings.HasPrefix(t, nestedSpaceToken) {
nextItem := ParseText(li.Value)

if prevItem.IsChild {
prevItem.Children.Items = append(prevItem.Children.Items, nextItem.Items...)
skip = true
} else {
li.IsChild = true
li.Children = &ParsedText{
MetaData: nextItem.MetaData,
Items: nextItem.Items,
}
}
} else if strings.HasPrefix(li.Value, urlToken) {
li.IsURL = true
split := TextToSplitToken(strings.Replace(li.Value, urlToken, "", 1))
Expand Down

0 comments on commit b216265

Please sign in to comment.