postDescription generates a description based on the given post content, title, and post ID. This doesn't consider a V2 post field, `title` when choosing what to generate. In case a post has a title, this function will fail, and logic should instead be implemented to skip this when there's no title,
(content, title, friendlyId string)
| 293 | // desc = shortPostDescription(content) |
| 294 | // } |
| 295 | func postDescription(content, title, friendlyId string) string { |
| 296 | maxLen := 140 |
| 297 | |
| 298 | if content == "" { |
| 299 | content = "WriteFreely is a painless, simple, federated blogging platform." |
| 300 | } else { |
| 301 | fmtStr := "%s" |
| 302 | truncation := 0 |
| 303 | if utf8.RuneCountInString(content) > maxLen { |
| 304 | // Post is longer than the max description, so let's show a better description |
| 305 | fmtStr = "%s..." |
| 306 | truncation = 3 |
| 307 | } |
| 308 | |
| 309 | if title == friendlyId { |
| 310 | // No specific title was found; simply truncate the post, starting at the beginning |
| 311 | content = fmt.Sprintf(fmtStr, strings.Replace(stringmanip.Substring(content, 0, maxLen-truncation), "\n", " ", -1)) |
| 312 | } else { |
| 313 | // There was a title, so return a real description |
| 314 | blankLine := strings.Index(content, "\n\n") |
| 315 | if blankLine < 0 { |
| 316 | blankLine = 0 |
| 317 | } |
| 318 | truncd := stringmanip.Substring(content, blankLine, blankLine+maxLen-truncation) |
| 319 | contentNoNL := strings.Replace(truncd, "\n", " ", -1) |
| 320 | content = strings.TrimSpace(fmt.Sprintf(fmtStr, contentNoNL)) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return content |
| 325 | } |
| 326 | |
| 327 | func shortPostDescription(content string) string { |
| 328 | maxLen := 140 |
no outgoing calls
no test coverage detected