Skip to content

Commit

Permalink
handing bullet points in the page text
Browse files Browse the repository at this point in the history
  • Loading branch information
viktomas committed Sep 24, 2022
1 parent 1fc9c5f commit 48ab64c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
42 changes: 39 additions & 3 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"regexp"
"strings"
)

Expand All @@ -13,18 +14,53 @@ func generateFileName(originalName string, attributes map[string]string) string
if _, ok := attributes["slug"]; !ok {
return sanitizeName(originalName)
}

var date string
if _, ok := attributes["date"]; ok {
return fmt.Sprintf("%s-%s", attributes["date"], attributes["slug"])
date = fmt.Sprintf("%s-", attributes["date"])
}

return attributes["slug"]
return fmt.Sprintf("%s%s.md", date, attributes["slug"])
}

func removeEmptyBulletPoints(from string) string {
return regexp.MustCompile(`(?m:^\s*-\s*$)`).ReplaceAllString(from, "")
}

func firstBulletPointsToParagraphs(from string) string {
return regexp.MustCompile(`(?m:^- )`).ReplaceAllString(from, "\n")
}

func secondToFirstBulletPoints(from string) string {
return regexp.MustCompile(`(?m:^\t-)`).ReplaceAllString(from, "\n-")
}

func removeTabFromMultiLevelBulletPoints(from string) string {
return regexp.MustCompile(`(?m:^\t{2,}-)`).ReplaceAllStringFunc(from, func(s string) string {
return s[1:]
}) //TODO make general (it can be merged with previous rule)
}

func applyAll(from string, transformers ...func(string) string) string {
result := from
for _, t := range transformers {
result = t(result)
}
return result
}

func transformPage(p page) page {
filename := generateFileName(p.filename, p.attributes)
text := applyAll(
p.text,
removeEmptyBulletPoints,
firstBulletPointsToParagraphs,
secondToFirstBulletPoints,
removeTabFromMultiLevelBulletPoints,
)
return page{
filename: filename,
attributes: p.attributes,
text: p.text,
text: text,
}
}
37 changes: 33 additions & 4 deletions transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,30 @@ func GenerateFileName(t *testing.T) {
"slug": "this-is-a-slug",
"date": "2022-09-24",
})
require.Equal(t, "2022-09-24-this-is-a-slug", result)
require.Equal(t, "2022-09-24-this-is-a-slug.md", result)
})

t.Run("combines slug and date into a filename", func(t *testing.T) {
result := generateFileName("name with space.md", map[string]string{
"slug": "this-is-a-slug",
"date": "2022-09-24",
})
require.Equal(t, "2022-09-24-this-is-a-slug", result)
require.Equal(t, "2022-09-24-this-is-a-slug.md", result)
})

}

func TestTransformPage(t *testing.T) {
func transformText(from string) string {
testPage := page{
filename: "",
attributes: map[string]string{},
text: from,
}
result := transformPage(testPage)
return result.text
}

func TestTransformPage(t *testing.T) {
t.Run("generates filename", func(t *testing.T) {
testPage := page{
filename: "name with space.md",
Expand All @@ -48,6 +57,26 @@ func TestTransformPage(t *testing.T) {
text: "",
}
result := transformPage(testPage)
require.Equal(t, "2022-09-24-this-is-a-slug", result.filename)
require.Equal(t, "2022-09-24-this-is-a-slug.md", result.filename)
})

t.Run("removes dashes with no text after them", func(t *testing.T) {
result := transformText("-\n\t- \n\t\t-")
require.Equal(t, "\n\n", result)
})

t.Run("removes dashes from the text", func(t *testing.T) {
result := transformText("-\n- hello")
require.Equal(t, "\n\nhello", result)
})

t.Run("turns second level bullet points into first level", func(t *testing.T) {
result := transformText("\t- hello\n\t- world")
require.Equal(t, "\n- hello\n\n- world", result) // TODO: maybe remove the duplicated new line
})

t.Run("removes one tab from multi-level bullet points", func(t *testing.T) {
result := transformText("\t\t- hello\n\t\t\t- world")
require.Equal(t, "\t- hello\n\t\t- world", result)
})
}

0 comments on commit 48ab64c

Please sign in to comment.