Skip to content

Commit

Permalink
chore: use gomark in rss api
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Dec 14, 2023
1 parent 242f64f commit e0290b9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
24 changes: 15 additions & 9 deletions api/v1/rss.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -12,9 +11,11 @@ import (

"github.com/gorilla/feeds"
"github.com/labstack/echo/v4"
"github.com/yuin/goldmark"

"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/plugin/gomark/parser"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/render/html"
"github.com/usememos/memos/store"
)

Expand Down Expand Up @@ -117,10 +118,14 @@ func (s *APIV1Service) generateRSSFromMemoList(ctx context.Context, memoList []*
if err != nil {
return "", err
}
description, err := getRSSItemDescription(memoMessage.Content)
if err != nil {
return "", err
}
feed.Items[i] = &feeds.Item{
Title: getRSSItemTitle(memoMessage.Content),
Link: &feeds.Link{Href: baseURL + "/m/" + fmt.Sprintf("%d", memoMessage.ID)},
Description: getRSSItemDescription(memoMessage.Content),
Description: description,
Created: time.Unix(memoMessage.CreatedTs, 0),
Enclosure: &feeds.Enclosure{Url: baseURL + "/m/" + fmt.Sprintf("%d", memoMessage.ID) + "/image"},
}
Expand Down Expand Up @@ -182,7 +187,7 @@ func getRSSItemTitle(content string) string {
return title
}

func getRSSItemDescription(content string) string {
func getRSSItemDescription(content string) (string, error) {
var description string
if isTitleDefined(content) {
var firstLineEnd = strings.Index(content, "\n")
Expand All @@ -191,12 +196,13 @@ func getRSSItemDescription(content string) string {
description = content
}

// TODO: use our `./plugin/gomark` parser to handle markdown-like content.
var buf bytes.Buffer
if err := goldmark.Convert([]byte(description), &buf); err != nil {
panic(err)
tokens := tokenizer.Tokenize(description)
nodes, err := parser.Parse(tokens)
if err != nil {
return "", err
}
return buf.String()
result := html.NewHTMLRender().Render(nodes)
return result, nil
}

func isTitleDefined(content string) bool {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/stretchr/testify v1.8.4
github.com/swaggo/echo-swagger v1.4.1
github.com/swaggo/swag v1.16.2
github.com/yuin/goldmark v1.6.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.16.0
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,6 @@ github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQ
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
Expand Down

0 comments on commit e0290b9

Please sign in to comment.