most primitive rendering
This commit is contained in:
parent
407b4ce7a2
commit
4a0b7d8db6
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module h4kor/kiss-social
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/yuin/goldmark v1.4.13 // indirect
|
||||
|
|
|
@ -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=
|
|
@ -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
|
||||
}
|
|
@ -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()
|
||||
}
|
|
@ -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, "<h1>testpost</h1>") {
|
||||
t.Error("Post title not rendered as h1. Got: " + result)
|
||||
}
|
||||
|
||||
}
|
17
user.go
17
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
|
||||
}
|
||||
|
|
14
user_test.go
14
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")
|
||||
|
|
Loading…
Reference in New Issue