Skip to content

Commit

Permalink
fix the issue where getting text for meta.description works on the wr…
Browse files Browse the repository at this point in the history
…ong content
  • Loading branch information
emad-elsaid committed Jun 8, 2024
1 parent a0cc50f commit 286adfa
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
6 changes: 4 additions & 2 deletions extensions/autolink_pages/autolink_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func UpdatePagesList(_ Page) (err error) {
}

func countTodos(p Page) (total int, done int) {
tasks := FindAllInAST[*east.TaskCheckBox](p.AST())
_, tree := p.AST()
tasks := FindAllInAST[*east.TaskCheckBox](tree)
for _, v := range tasks {
total++
if v.IsChecked {
Expand All @@ -62,7 +63,8 @@ func backlinksSection(p Page) template.HTML {
}

pages := MapPageCon(context.Background(), func(a Page) *Page {
if a.Name() == p.Name() || !containLinkTo(a.AST(), p) {
_, tree := a.AST()
if a.Name() == p.Name() || !containLinkTo(tree, p) {
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion extensions/date/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func dateHandler(w Response, r Request) Output {
}

pages := MapPageCon(r.Context(), func(p Page) *Page {
allDates := FindAllInAST[*DateNode](p.AST())
_, tree := p.AST()
allDates := FindAllInAST[*DateNode](tree)
for _, d := range allDates {
if d.time.Equal(date) {
return &p
Expand Down
10 changes: 6 additions & 4 deletions extensions/gpg/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ func (p *page) Write(content xlog.Markdown) bool {
return true
}

func (p *page) AST() ast.Node {
func (p *page) AST() ([]byte, ast.Node) {
src := p.Content()
if p.ast == nil {
p.ast = xlog.MarkDownRenderer.Parser().Parse(text.NewReader([]byte(p.Content())))
p.ast = xlog.MarkDownRenderer.Parser().Parse(text.NewReader([]byte(src)))
}

return p.ast
return []byte(src), p.ast
}
func (p *page) Emoji() string {
if e, ok := xlog.FindInAST[*emojiAst.Emoji](p.AST()); ok {
_, tree := p.AST()
if e, ok := xlog.FindInAST[*emojiAst.Emoji](tree); ok {
return string(e.Value.Unicode)
}

Expand Down
15 changes: 10 additions & 5 deletions extensions/hashtags/hashtags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func tagsHandler(_ Response, r Request) Output {
EachPageCon(context.Background(), func(a Page) {

set := map[string]bool{}
hashes := FindAllInAST[*HashTag](a.AST())
_, tree := a.AST()
hashes := FindAllInAST[*HashTag](tree)
for _, v := range hashes {
val := strings.ToLower(string(v.value))

Expand Down Expand Up @@ -154,7 +155,8 @@ func tagPages(ctx context.Context, keyword string) []*Page {
return nil
}

tags := FindAllInAST[*HashTag](p.AST())
_, tree := p.AST()
tags := FindAllInAST[*HashTag](tree)
for _, t := range tags {
if strings.EqualFold(string(t.value), keyword) {
return &p
Expand All @@ -170,7 +172,8 @@ func relatedPages(p Page) template.HTML {
return ""
}

found_hashtags := FindAllInAST[*HashTag](p.AST())
_, tree := p.AST()
found_hashtags := FindAllInAST[*HashTag](tree)
hashtags := map[string]bool{}
for _, v := range found_hashtags {
hashtags[strings.ToLower(string(v.value))] = true
Expand All @@ -181,7 +184,8 @@ func relatedPages(p Page) template.HTML {
return nil
}

page_hashtags := FindAllInAST[*HashTag](rp.AST())
_, tree := rp.AST()
page_hashtags := FindAllInAST[*HashTag](tree)
for _, h := range page_hashtags {
if _, ok := hashtags[strings.ToLower(string(h.value))]; ok {
return &rp
Expand All @@ -208,7 +212,8 @@ func (a autocomplete) Suggestions() []*Suggestion {
set := map[string]bool{}
var lck sync.Mutex
EachPageCon(context.Background(), func(a Page) {
hashes := FindAllInAST[*HashTag](a.AST())
_, tree := a.AST()
hashes := FindAllInAST[*HashTag](tree)
lck.Lock()
defer lck.Unlock()
for _, v := range hashes {
Expand Down
4 changes: 2 additions & 2 deletions extensions/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,5 @@ func (p *page) Write(content xlog.Markdown) bool {
return true
}

func (p *page) AST() ast.Node { return ast.NewDocument() }
func (p *page) Emoji() string { return "" }
func (p *page) AST() ([]byte, ast.Node) { return []byte{}, ast.NewDocument() }
func (p *page) Emoji() string { return "" }
5 changes: 3 additions & 2 deletions extensions/opengraph/opengraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func opengraphTags(p Page) template.HTML {
URL := u.String()

var image string
if imageAST, ok := FindInAST[*ast.Image](p.AST()); ok {
src, tree := p.AST()
if imageAST, ok := FindInAST[*ast.Image](tree); ok {
image = "https://" + domain + string(imageAST.Destination)
}

firstParagraph := rawText([]byte(p.Content()), p.AST(), descriptionLength)
firstParagraph := rawText(src, tree, descriptionLength)

ogTags := fmt.Sprintf(`
<meta property="og:site_name" content="%s" />
Expand Down
4 changes: 2 additions & 2 deletions extensions/pandoc/pandoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ func (p *page) Write(content xlog.Markdown) bool {
return true
}

func (p *page) AST() ast.Node { return ast.NewDocument() }
func (p *page) Emoji() string { return "" }
func (p *page) AST() ([]byte, ast.Node) { return []byte{}, ast.NewDocument() }
func (p *page) Emoji() string { return "" }
14 changes: 7 additions & 7 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Page interface {
// extensions can use it to walk the tree and modify it or collect statistics or
// parts of the page. for example the following "Emoji" function uses it to
// extract the first emoji.
AST() ast.Node
AST() ([]byte, ast.Node)
// Returns the first emoji of the page.
Emoji() string
}
Expand Down Expand Up @@ -71,11 +71,10 @@ func (p *page) Exists() bool {
}

func (p *page) Render() template.HTML {
content := p.preProcessedContent()
ast := p.AST()
src, ast := p.AST()

var buf bytes.Buffer
if err := MarkDownRenderer.Renderer().Render(&buf, []byte(content), ast); err != nil {
if err := MarkDownRenderer.Renderer().Render(&buf, src, ast); err != nil {
return template.HTML(err.Error())
}

Expand Down Expand Up @@ -146,19 +145,20 @@ func (p *page) ModTime() time.Time {
return s.ModTime()
}

func (p *page) AST() ast.Node {
func (p *page) AST() (source []byte, tree ast.Node) {
lastModified := p.lastUpdate
content := p.preProcessedContent()

if p.ast == nil || p.lastUpdate != lastModified {
p.ast = MarkDownRenderer.Parser().Parse(text.NewReader([]byte(content)))
}

return p.ast
return []byte(content), p.ast
}

func (p *page) Emoji() string {
if e, ok := FindInAST[*emojiAst.Emoji](p.AST()); ok {
_, tree := p.AST()
if e, ok := FindInAST[*emojiAst.Emoji](tree); ok {
return string(e.Value.Unicode)
}

Expand Down

0 comments on commit 286adfa

Please sign in to comment.