This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.go
68 lines (55 loc) · 1.95 KB
/
posts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package models
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/wilhelm-murdoch/go-stash/config"
)
const imageRegexPattern = `<img[^>]+\bsrc=["']([^"'?]+).*["']`
type Post struct {
Title string `json:"title"`
Slug string `json:"slug"`
DateAdded string `json:"dateAdded"`
DateUpdated string `json:"dateUpdated"`
CUID string `json:"cuid"`
Type string `json:"type"`
Content string `json:"content"`
Brief string `json:"brief"`
CoverImage string `json:"coverImage"`
Tags []Tag `json:"tags,omitempty"`
Author *Author
}
func (p Post) GetUrl(cfg *config.Configuration) string {
return fmt.Sprintf("%s/%s/%s", cfg.Url, cfg.Paths.Posts, p.GetSlug())
}
func (p Post) GetDateUpdated() string {
if p.DateUpdated == "" {
return p.DateAdded
}
return p.DateUpdated
}
func (p Post) GetSlug() string {
return p.Slug
}
func (p Post) GetImages(cfg *config.Configuration) []Image {
var images []Image
destination := fmt.Sprintf("%s/%s/images", cfg.Paths.Root, cfg.Paths.Files)
images = append(images, Image{p.CoverImage, fmt.Sprintf("%s/cover-%s-%s", destination, p.GetSlug(), filepath.Base(p.CoverImage))})
pattern := regexp.MustCompile(imageRegexPattern)
matches := pattern.FindAllStringSubmatch(p.Content, -1)
for _, match := range matches {
fmt.Println(match[1])
images = append(images, Image{match[1], fmt.Sprintf("%s/post-%s-%s", destination, p.GetSlug(), filepath.Base(match[1]))})
}
return images
}
func (p *Post) ReplaceImagePaths(cfg *config.Configuration) {
p.CoverImage = fmt.Sprintf("/%s/images/cover-%s-%s", cfg.Paths.Files, p.GetSlug(), filepath.Base(p.CoverImage))
pattern := regexp.MustCompile(imageRegexPattern)
matches := pattern.FindAllStringSubmatch(p.Content, -1)
for _, match := range matches {
p.Content = strings.Replace(p.Content, match[1], fmt.Sprintf("/%s/images/post-%s-%s", cfg.Paths.Files, p.GetSlug(), filepath.Base(match[1])), -1)
}
p.Author.ReplaceImagePaths(cfg)
}