Skip to content
Snippets Groups Projects
Commit 21b4c031 authored by Travis Ralston's avatar Travis Ralston
Browse files

Limit the URL preview title length by words

Fixes #62
parent f285b08f
No related branches found
No related tags found
No related merge requests found
...@@ -91,6 +91,7 @@ urlPreviews: ...@@ -91,6 +91,7 @@ urlPreviews:
enabled: true # If enabled, the preview_url routes will be accessible enabled: true # If enabled, the preview_url routes will be accessible
maxPageSizeBytes: 10485760 # 10MB default, 0 to disable maxPageSizeBytes: 10485760 # 10MB default, 0 to disable
numWords: 30 # The number of words to include in a preview (maximum) numWords: 30 # The number of words to include in a preview (maximum)
numTitleWords: 30 # The maximum number of words to include in a preview's title
# The number of workers to use when generating url previews. Raise this number if url # The number of workers to use when generating url previews. Raise this number if url
# previews are slow or timing out. # previews are slow or timing out.
......
...@@ -62,6 +62,7 @@ type ThumbnailSize struct { ...@@ -62,6 +62,7 @@ type ThumbnailSize struct {
type UrlPreviewsConfig struct { type UrlPreviewsConfig struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
NumWords int `yaml:"numWords"` NumWords int `yaml:"numWords"`
NumTitleWords int `yaml:"numTitleWords"`
MaxPageSizeBytes int64 `yaml:"maxPageSizeBytes"` MaxPageSizeBytes int64 `yaml:"maxPageSizeBytes"`
NumWorkers int `yaml:"numWorkers"` NumWorkers int `yaml:"numWorkers"`
DisallowedNetworks []string `yaml:"disallowedNetworks,flow"` DisallowedNetworks []string `yaml:"disallowedNetworks,flow"`
...@@ -204,6 +205,7 @@ func NewDefaultConfig() *MediaRepoConfig { ...@@ -204,6 +205,7 @@ func NewDefaultConfig() *MediaRepoConfig {
UrlPreviews: &UrlPreviewsConfig{ UrlPreviews: &UrlPreviewsConfig{
Enabled: true, Enabled: true,
NumWords: 30, NumWords: 30,
NumTitleWords: 30,
MaxPageSizeBytes: 10485760, // 10mb MaxPageSizeBytes: 10485760, // 10mb
NumWorkers: 10, NumWorkers: 10,
DisallowedNetworks: []string{ DisallowedNetworks: []string{
......
...@@ -71,11 +71,15 @@ func (p *openGraphUrlPreviewer) GeneratePreview(urlStr string) (openGraphResult, ...@@ -71,11 +71,15 @@ func (p *openGraphUrlPreviewer) GeneratePreview(urlStr string) (openGraphResult,
og.Images = calcImages(html) og.Images = calcImages(html)
} }
// Be sure to trim the title and description
og.Title = summarize(og.Title, config.Get().UrlPreviews.NumTitleWords)
og.Description = summarize(og.Description, config.Get().UrlPreviews.NumWords)
graph := &openGraphResult{ graph := &openGraphResult{
Type: og.Type, Type: og.Type,
Url: og.URL, Url: og.URL,
Title: og.Title, Title: og.Title,
Description: summarize(og.Description), Description: og.Description,
SiteName: og.SiteName, SiteName: og.SiteName,
} }
...@@ -260,7 +264,7 @@ func calcImages(html string) []*opengraph.Image { ...@@ -260,7 +264,7 @@ func calcImages(html string) []*opengraph.Image {
return []*opengraph.Image{&img} return []*opengraph.Image{&img}
} }
func summarize(text string) (string) { func summarize(text string, maxWords int) (string) {
// Normalize the whitespace to be something useful (crush it to one giant line) // Normalize the whitespace to be something useful (crush it to one giant line)
surroundingWhitespace := regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`) surroundingWhitespace := regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
interiorWhitespace := regexp.MustCompile(`[\s\p{Zs}]{2,}`) interiorWhitespace := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
...@@ -269,7 +273,6 @@ func summarize(text string) (string) { ...@@ -269,7 +273,6 @@ func summarize(text string) (string) {
text = interiorWhitespace.ReplaceAllString(text, " ") text = interiorWhitespace.ReplaceAllString(text, " ")
text = newlines.ReplaceAllString(text, " ") text = newlines.ReplaceAllString(text, " ")
maxWords := config.Get().UrlPreviews.NumWords
words := strings.Split(text, " ") words := strings.Split(text, " ")
if len(words) < maxWords { if len(words) < maxWords {
return text return text
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment