- commit
- b82b2f7
- parent
- 866976a
- author
- Eric Bower
- date
- 2024-08-01 15:29:31 +0000 UTC
chore: diff in post
5 files changed,
+55,
-4
+1,
-0
1@@ -139,6 +139,7 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
2
3 if post != nil {
4 metadata.Cur = post
5+ metadata.Data = post.Data
6 metadata.Post.PublishAt = post.PublishAt
7 }
8
+11,
-0
1@@ -83,6 +83,7 @@ type PostPageData struct {
2 Footer template.HTML
3 Favicon template.URL
4 Unlisted bool
5+ Diff template.HTML
6 }
7
8 type TransparencyPageData struct {
9@@ -399,6 +400,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
10 favicon = readmeParsed.Favicon
11 }
12
13+ diff := ""
14 post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
15 if err == nil {
16 parsedText, err := shared.ParseText(post.Text)
17@@ -414,6 +416,14 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
18 ogImageCard = parsedText.ImageCard
19 }
20
21+ // if parsedText.Live {
22+ if post.Data.Diff != "" {
23+ str := "```diff\n" + post.Data.Diff + "\n```"
24+ diffParsed, _ := shared.ParseText(str)
25+ diff = diffParsed.Html
26+ }
27+ // }
28+
29 // track visit
30 view, err := shared.AnalyticsVisitFromRequest(r, user.ID, cfg.Secret)
31 if err == nil {
32@@ -451,6 +461,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
33 Favicon: template.URL(favicon),
34 Footer: footerHTML,
35 Unlisted: unlisted,
36+ Diff: template.HTML(diff),
37 }
38 } else {
39 // TODO: HACK to support imgs slugs inside prose
+8,
-1
1@@ -62,9 +62,16 @@
2 </div>
3 </header>
4 <main>
5+ {{if .Diff}}
6+ <details>
7+ <summary>diff</summary>
8+ <div class="md">{{.Diff}}</div>
9+ </details>
10+ {{end}}
11+
12 <article class="md">
13 {{.Contents}}
14- <div id="post-footer">{{.Footer}}</div>
15+ <div id="post-footer" class="box">{{.Footer}}</div>
16 </article>
17 </main>
18 {{template "footer" .}}
+28,
-3
1@@ -1,6 +1,7 @@
2 package prose
3
4 import (
5+ "bytes"
6 "fmt"
7 "strings"
8
9@@ -47,6 +48,26 @@ func (p *MarkdownHooks) FileValidate(s ssh.Session, data *filehandlers.PostMetaD
10 return true, nil
11 }
12
13+func diffText(diffs []diffmatchpatch.Diff) string {
14+ var buff bytes.Buffer
15+ for _, diff := range diffs {
16+ text := diff.Text
17+
18+ switch diff.Type {
19+ case diffmatchpatch.DiffInsert:
20+ _, _ = buff.WriteString("+")
21+ _, _ = buff.WriteString(text)
22+ case diffmatchpatch.DiffDelete:
23+ _, _ = buff.WriteString("-")
24+ _, _ = buff.WriteString(text)
25+ case diffmatchpatch.DiffEqual:
26+ _, _ = buff.WriteString(text)
27+ }
28+ }
29+
30+ return buff.String()
31+}
32+
33 func (p *MarkdownHooks) FileMeta(s ssh.Session, data *filehandlers.PostMetaData) error {
34 parsedText, err := shared.ParseText(data.Text)
35 if err != nil {
36@@ -63,9 +84,13 @@ func (p *MarkdownHooks) FileMeta(s ssh.Session, data *filehandlers.PostMetaData)
37 data.Tags = parsedText.Tags
38 data.Description = parsedText.Description
39
40- dmp := diffmatchpatch.New()
41- diffs := dmp.DiffMain(data.Cur.Text, data.Text, false)
42- data.Data.Diff = dmp.DiffPrettyText(diffs)
43+ if data.Cur.Text == data.Text {
44+ } else {
45+ dmp := diffmatchpatch.New()
46+ diffs := dmp.DiffMain(data.Cur.Text, data.Text, false)
47+ fmt.Println(diffs)
48+ data.Data.Diff = diffText(diffs)
49+ }
50
51 if parsedText.PublishAt != nil && !parsedText.PublishAt.IsZero() {
52 data.PublishAt = parsedText.MetaData.PublishAt
1@@ -40,6 +40,7 @@ type MetaData struct {
2 ImageCard string
3 Favicon string
4 Hidden bool
5+ Live bool
6 }
7
8 type ParsedText struct {
9@@ -303,6 +304,12 @@ func ParseText(text string) (*ParsedText, error) {
10 }
11 parsed.MetaData.Favicon = favicon
12
13+ live, err := toBool(metaData["live"])
14+ if err != nil {
15+ return &parsed, fmt.Errorf("front-matter field (%s): %w", "live", err)
16+ }
17+ parsed.MetaData.Live = live
18+
19 var publishAt *time.Time = nil
20 date, err := toString(metaData["date"])
21 if err != nil {