mirror of
				https://github.com/usememos/memos.git
				synced 2025-06-05 22:09:59 +02:00 
			
		
		
		
	chore: update frontend metadata
This commit is contained in:
		| @@ -3,7 +3,6 @@ package frontend | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"html/template" |  | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| @@ -80,7 +79,7 @@ func (s *FrontendService) registerRoutes(e *echo.Echo) { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Inject memo metadata into `index.html`. | 		// Inject memo metadata into `index.html`. | ||||||
| 		indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- memos.metadata -->", generateMemoMetadata(memo, creator)) | 		indexHTML := strings.ReplaceAll(rawIndexHTML, "<!-- memos.metadata -->", generateMemoMetadata(memo, creator).String()) | ||||||
| 		return c.HTML(http.StatusOK, indexHTML) | 		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 { | func generateMemoMetadata(memo *store.Memo, creator *store.User) *Metadata { | ||||||
| 	description := "" | 	metadata := getDefaultMetadata() | ||||||
| 	if memo.Visibility == store.Private { | 	metadata.Title = fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username) | ||||||
| 		description = "This memo is private." | 	if memo.Visibility == store.Public { | ||||||
| 	} else if memo.Visibility == store.Protected { |  | ||||||
| 		description = "This memo is protected." |  | ||||||
| 	} else { |  | ||||||
| 		tokens := tokenizer.Tokenize(memo.Content) | 		tokens := tokenizer.Tokenize(memo.Content) | ||||||
| 		nodes, _ := parser.Parse(tokens) | 		nodes, _ := parser.Parse(tokens) | ||||||
| 		description = renderer.NewStringRenderer().Render(nodes) | 		description := renderer.NewStringRenderer().Render(nodes) | ||||||
| 		if len(description) == 0 { | 		if len(description) == 0 { | ||||||
| 			description = memo.Content | 			description = memo.Content | ||||||
| 		} | 		} | ||||||
| 		if len(description) > maxMetadataDescriptionLength { | 		if len(description) > maxMetadataDescriptionLength { | ||||||
| 			description = description[:maxMetadataDescriptionLength] + "..." | 			description = description[:maxMetadataDescriptionLength] + "..." | ||||||
| 		} | 		} | ||||||
|  | 		metadata.Description = description | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	metadataList := []string{ | 	return metadata | ||||||
| 		fmt.Sprintf(`<meta name="description" content="%s" />`, template.HTMLEscapeString(description)), |  | ||||||
| 		fmt.Sprintf(`<meta property="og:title" content="%s" />`, template.HTMLEscapeString(fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username))), |  | ||||||
| 		fmt.Sprintf(`<meta property="og:description" content="%s" />`, template.HTMLEscapeString(description)), |  | ||||||
| 		fmt.Sprintf(`<meta property="og:image" content="%s" />`, "https://www.usememos.com/logo.png"), |  | ||||||
| 		`<meta property="og:type" content="website" />`, |  | ||||||
| 		// Twitter related metadata. |  | ||||||
| 		fmt.Sprintf(`<meta name="twitter:title" content="%s" />`, template.HTMLEscapeString(fmt.Sprintf("%s(@%s) on Memos", creator.Nickname, creator.Username))), |  | ||||||
| 		fmt.Sprintf(`<meta name="twitter:description" content="%s" />`, template.HTMLEscapeString(description)), |  | ||||||
| 		fmt.Sprintf(`<meta name="twitter:image" content="%s" />`, "https://www.usememos.com/logo.png"), |  | ||||||
| 		`<meta name="twitter:card" content="summary" />`, |  | ||||||
| 	} |  | ||||||
| 	return strings.Join(metadataList, "\n") |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func getRawIndexHTML() string { | func getRawIndexHTML() string { | ||||||
| 	bytes, _ := os.ReadFile("dist/index.html") | 	bytes, _ := os.ReadFile("dist/index.html") | ||||||
| 	return string(bytes) | 	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(`<meta name="description" content="%s" />`, m.Description), | ||||||
|  | 		fmt.Sprintf(`<meta property="og:title" content="%s" />`, m.Title), | ||||||
|  | 		fmt.Sprintf(`<meta property="og:description" content="%s" />`, m.Description), | ||||||
|  | 		fmt.Sprintf(`<meta property="og:image" content="%s" />`, m.ImageURL), | ||||||
|  | 		`<meta property="og:type" content="website" />`, | ||||||
|  | 		// Twitter related fields. | ||||||
|  | 		fmt.Sprintf(`<meta property="twitter:title" content="%s" />`, m.Title), | ||||||
|  | 		fmt.Sprintf(`<meta property="twitter:description" content="%s" />`, m.Description), | ||||||
|  | 		fmt.Sprintf(`<meta property="twitter:image" content="%s" />`, m.ImageURL), | ||||||
|  | 		`<meta name="twitter:card" content="summary" />`, | ||||||
|  | 		`<meta name="twitter:creator" content="memos" />`, | ||||||
|  | 	} | ||||||
|  | 	return strings.Join(metadataList, "\n") | ||||||
|  | } | ||||||
|   | |||||||
| @@ -6,8 +6,8 @@ | |||||||
|     <meta name="theme-color" media="(prefers-color-scheme: light)" content="#f4f4f5" /> |     <meta name="theme-color" media="(prefers-color-scheme: light)" content="#f4f4f5" /> | ||||||
|     <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#27272a" /> |     <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#27272a" /> | ||||||
|     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> |     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> | ||||||
|     <title>Memos</title> |  | ||||||
|     <!-- memos.metadata --> |     <!-- memos.metadata --> | ||||||
|  |     <title>Memos</title> | ||||||
|   </head> |   </head> | ||||||
|   <body> |   <body> | ||||||
|     <div id="root"></div> |     <div id="root"></div> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user