Skip to content

Commit

Permalink
chore: remove redundant last category if it matches the document title (
Browse files Browse the repository at this point in the history
  • Loading branch information
medcl authored Jan 21, 2025
1 parent 8ac6e91 commit cdeb07c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions modules/common/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package common

import (
"strings"
"time"
)

Expand Down Expand Up @@ -56,6 +57,19 @@ type Document struct {

}

func (document *Document) Cleanup() {
document.TrimLastDuplicatedCategory()
}

func (document *Document) TrimLastDuplicatedCategory() {
// Ensure RichCategories is not empty before accessing the last element
if len(document.RichCategories) > 0 &&
strings.TrimSpace(document.RichCategories[len(document.RichCategories)-1].Label) == strings.TrimSpace(document.Title) {
// Remove the last category if it matches the book's title (it's redundant)
document.RichCategories = document.RichCategories[:len(document.RichCategories)-1]
}
}

type EditorInfo struct {
UserInfo UserInfo `json:"user,omitempty" elastic_mapping:"user:{type:object}"` // Information about the last user who updated the document
UpdatedAt *time.Time `json:"timestamp,omitempty"` // Timestamp of the last update
Expand Down
7 changes: 7 additions & 0 deletions plugins/connectors/yuque/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,12 @@ func (this *Plugin) collectBooks(connector *common.Connector, datasource *common
}

if !cfg.SkipIndexingBookToc {
// Check if the book's Table of Contents (ToC) exists in the map
if v, ok := bookTocMap[bookDetail.Book.Slug]; ok {
// Assign the ToC to the document's RichCategories
document.RichCategories = v
} else {
// Log a warning if the ToC information is missing
log.Debug("missing toc info:", bookDetail.Book.Slug, ",", bookDetail.Book.Name)
}
}
Expand All @@ -268,6 +271,8 @@ func (this *Plugin) collectBooks(connector *common.Connector, datasource *common
document.Created = &bookDetail.Book.CreatedAt
document.Updated = &bookDetail.Book.UpdatedAt

document.Cleanup()

this.save(document)
} else {
log.Info("skip book:", bookDetail.Book.Name, ",", bookDetail.Book.Public)
Expand Down Expand Up @@ -415,6 +420,8 @@ func (this *Plugin) collectDocDetails(connector *common.Connector, datasource *c
document.Created = &doc.Doc.CreatedAt
document.Updated = &doc.Doc.UpdatedAt

document.Cleanup()

this.save(document)
}
}
Expand Down

0 comments on commit cdeb07c

Please sign in to comment.