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

Function PostLede

parse/posts.go:28–67  ·  view source on GitHub ↗

PostLede attempts to extract the first thought of the given post, generally contained within the first line or sentence of text.

(t string, includePunc bool)

Source from the content-addressed store, hash-verified

26// PostLede attempts to extract the first thought of the given post, generally
27// contained within the first line or sentence of text.
28func PostLede(t string, includePunc bool) string {
29 // Adjust where we truncate if we want to include punctuation
30 iAdj := 0
31 if includePunc {
32 iAdj = 1
33 }
34
35 // Find lede within first line of text
36 nl := strings.IndexRune(t, '\n')
37 if nl > -1 {
38 t = t[:nl]
39 }
40
41 // Strip certain HTML tags
42 t = titleElementReg.ReplaceAllString(t, "")
43
44 // Strip URL protocols
45 t = urlReg.ReplaceAllString(t, "")
46
47 // Strip image URL, leaving only alt text
48 t = imgReg.ReplaceAllString(t, " $1 ")
49
50 // Find lede within first sentence
51 punc := strings.Index(t, ". ")
52 if punc > -1 {
53 t = t[:punc+iAdj]
54 }
55 punc = stringmanip.IndexRune(t, '。')
56 if punc > -1 {
57 c := []rune(t)
58 t = string(c[:punc+iAdj])
59 }
60 punc = stringmanip.IndexRune(t, '?')
61 if punc > -1 {
62 c := []rune(t)
63 t = string(c[:punc+iAdj])
64 }
65
66 return t
67}
68
69// TruncToWord truncates the given text to the provided limit.
70func TruncToWord(s string, l int) (string, bool) {

Callers 3

getSlugFromPostFunction · 0.92
friendlyPostTitleFunction · 0.92
TestPostLedeFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestPostLedeFunction · 0.68