MCPcopy Index your code
hub / github.com/writefreely/writefreely / postDescription

Function postDescription

postrender.go:295–325  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

293// desc = shortPostDescription(content)
294// }
295func 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
327func shortPostDescription(content string) string {
328 maxLen := 140

Callers 2

SummaryMethod · 0.85
handleViewPostFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected