diff --git a/go.mod b/go.mod
index b2c512b..7981876 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module h4kor/kiss-social
go 1.18
+
+require github.com/yuin/goldmark v1.4.13 // indirect
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..29914fb
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
diff --git a/post.go b/post.go
new file mode 100644
index 0000000..ae0beb8
--- /dev/null
+++ b/post.go
@@ -0,0 +1,25 @@
+package kiss
+
+import (
+ "io/ioutil"
+ "path"
+)
+
+type Post struct {
+ user User
+ id string
+}
+
+func (post Post) Dir() string {
+ return path.Join(post.user.Dir(), "public", post.id)
+}
+
+func (post Post) ContentFile() string {
+ return path.Join(post.Dir(), "index.md")
+}
+
+func (post Post) Content() []byte {
+ // read file
+ data, _ := ioutil.ReadFile(post.ContentFile())
+ return data
+}
diff --git a/renderer.go b/renderer.go
new file mode 100644
index 0000000..227f37d
--- /dev/null
+++ b/renderer.go
@@ -0,0 +1,16 @@
+package kiss
+
+import (
+ "bytes"
+
+ "github.com/yuin/goldmark"
+)
+
+func RenderPost(post Post) string {
+
+ var buf bytes.Buffer
+ if err := goldmark.Convert(post.Content(), &buf); err != nil {
+ panic(err)
+ }
+ return buf.String()
+}
diff --git a/renderer_test.go b/renderer_test.go
new file mode 100644
index 0000000..652dfbd
--- /dev/null
+++ b/renderer_test.go
@@ -0,0 +1,23 @@
+package kiss_test
+
+import (
+ "h4kor/kiss-social"
+ "strings"
+ "testing"
+)
+
+func getTestUser() kiss.User {
+ repo, _ := kiss.CreateRepository(testRepoName())
+ user, _ := repo.CreateUser(randomUserName())
+ return user
+}
+
+func TestCanRenderPost(t *testing.T) {
+ user := getTestUser()
+ post, _ := user.CreateNewPost("testpost")
+ result := kiss.RenderPost(post)
+ if !strings.Contains(result, "
testpost
") {
+ t.Error("Post title not rendered as h1. Got: " + result)
+ }
+
+}
diff --git a/user.go b/user.go
index f20b843..3bc4b10 100644
--- a/user.go
+++ b/user.go
@@ -12,11 +12,6 @@ type User struct {
name string
}
-type Post struct {
- user User
- title string
-}
-
func (user User) Dir() string {
return path.Join(user.repo.Dir(), "users", user.name)
}
@@ -29,12 +24,12 @@ func (user User) Posts() ([]Post, error) {
postNames := listDir(path.Join(user.Dir(), "public"))
posts := make([]Post, len(postNames))
for i, name := range postNames {
- posts[i] = Post{user: user, title: name}
+ posts[i] = Post{user: user, id: name}
}
return posts, nil
}
-func CreateNewPost(user User, title string) {
+func (user User) CreateNewPost(title string) (Post, error) {
timestamp := time.Now().UTC().Unix()
folder_name := fmt.Sprintf("%d-%s", timestamp, title)
post_dir := path.Join(user.Dir(), "public", folder_name)
@@ -50,13 +45,11 @@ func CreateNewPost(user User, title string) {
break
}
}
+ post := Post{user: user, id: folder_name}
initial_content := "# " + title
// create post file
os.Mkdir(post_dir, 0755)
- os.WriteFile(path.Join(post_dir, "index.md"), []byte(initial_content), 0644)
-}
-
-func PostDir(post Post) string {
- return path.Join(post.user.Dir(), "public", post.title)
+ os.WriteFile(post.ContentFile(), []byte(initial_content), 0644)
+ return post, nil
}
diff --git a/user_test.go b/user_test.go
index 3ea8027..7b86ddc 100644
--- a/user_test.go
+++ b/user_test.go
@@ -13,7 +13,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
- kiss.CreateNewPost(user, "testpost")
+ user.CreateNewPost("testpost")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
@@ -28,9 +28,9 @@ func TestCreateNewPostMultipleCalls(t *testing.T) {
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
- kiss.CreateNewPost(user, "testpost")
- kiss.CreateNewPost(user, "testpost")
- kiss.CreateNewPost(user, "testpost")
+ user.CreateNewPost("testpost")
+ user.CreateNewPost("testpost")
+ user.CreateNewPost("testpost")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
@@ -45,9 +45,9 @@ func TestCanListUserPosts(t *testing.T) {
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
- kiss.CreateNewPost(user, "testpost")
- kiss.CreateNewPost(user, "testpost")
- kiss.CreateNewPost(user, "testpost")
+ user.CreateNewPost("testpost")
+ user.CreateNewPost("testpost")
+ user.CreateNewPost("testpost")
posts, err := user.Posts()
if err != nil {
t.Error("Error reading posts")