diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go index da80adb1..e100e269 100644 --- a/server/frontend/frontend.go +++ b/server/frontend/frontend.go @@ -3,7 +3,6 @@ package frontend import ( "context" "fmt" - "html/template" "net/http" "os" "strings" @@ -80,7 +79,7 @@ func (s *FrontendService) registerRoutes(e *echo.Echo) { } // Inject memo metadata into `index.html`. - indexHTML := strings.ReplaceAll(rawIndexHTML, "", generateMemoMetadata(memo, creator)) + indexHTML := strings.ReplaceAll(rawIndexHTML, "", generateMemoMetadata(memo, creator).String()) return c.HTML(http.StatusOK, indexHTML) }) } @@ -123,40 +122,57 @@ Sitemap: %s/sitemap.xml`, instanceURL, instanceURL) }) } -func generateMemoMetadata(memo *store.Memo, creator *store.User) string { - description := "" - if memo.Visibility == store.Private { - description = "This memo is private." - } else if memo.Visibility == store.Protected { - description = "This memo is protected." - } else { +func generateMemoMetadata(memo *store.Memo, creator *store.User) *Metadata { + metadata := getDefaultMetadata() + metadata.Title = fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username) + if memo.Visibility == store.Public { tokens := tokenizer.Tokenize(memo.Content) nodes, _ := parser.Parse(tokens) - description = renderer.NewStringRenderer().Render(nodes) + description := renderer.NewStringRenderer().Render(nodes) if len(description) == 0 { description = memo.Content } if len(description) > maxMetadataDescriptionLength { description = description[:maxMetadataDescriptionLength] + "..." } + metadata.Description = description } - metadataList := []string{ - fmt.Sprintf(``, template.HTMLEscapeString(description)), - fmt.Sprintf(``, template.HTMLEscapeString(fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username))), - fmt.Sprintf(``, template.HTMLEscapeString(description)), - fmt.Sprintf(``, "https://www.usememos.com/logo.png"), - ``, - // Twitter related metadata. - fmt.Sprintf(``, template.HTMLEscapeString(fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username))), - fmt.Sprintf(``, template.HTMLEscapeString(description)), - fmt.Sprintf(``, "https://www.usememos.com/logo.png"), - ``, - } - return strings.Join(metadataList, "\n") + return metadata } func getRawIndexHTML() string { bytes, _ := os.ReadFile("dist/index.html") return string(bytes) } + +type Metadata struct { + Title string + Description string + ImageURL string +} + +func getDefaultMetadata() *Metadata { + return &Metadata{ + Title: "Memos", + Description: "A privacy-first, lightweight note-taking service. Easily capture and share your great thoughts.", + ImageURL: "https://www.usememos.com/logo.png", + } +} + +func (m *Metadata) String() string { + metadataList := []string{ + fmt.Sprintf(``, m.Description), + fmt.Sprintf(``, m.Title), + fmt.Sprintf(``, m.Description), + fmt.Sprintf(``, m.ImageURL), + ``, + // Twitter related fields. + fmt.Sprintf(``, m.Title), + fmt.Sprintf(``, m.Description), + fmt.Sprintf(``, m.ImageURL), + ``, + ``, + } + return strings.Join(metadataList, "\n") +} diff --git a/web/index.html b/web/index.html index 12b6dc93..e657174e 100644 --- a/web/index.html +++ b/web/index.html @@ -6,8 +6,8 @@ - Memos + Memos