@@ -27,7 +27,17 @@ var dateFormats = []string{
27
27
"January 2, 2006" ,
28
28
}
29
29
30
- func cleanHTML (input string ) string {
30
+ // We're rendering to markdown so to preserve formatting we need to strip out any markdown characters
31
+ func stripMarkdown (input string ) string {
32
+ invalidChars := []string {"*" , "_" , "#" , "`" , ">" , "<" , "[" , "]" , "(" , ")" , "!" , "~" , "|" , "{" , "}" , "+" }
33
+ for _ , char := range invalidChars {
34
+ input = strings .ReplaceAll (input , char , "" )
35
+ }
36
+
37
+ return input
38
+ }
39
+
40
+ func cleanHTML (input string , maxLength int ) string {
31
41
// first, remove HTML tags
32
42
tagRegex := regexp .MustCompile ("<[^>]*>" )
33
43
cleaned := tagRegex .ReplaceAllString (input , "" )
@@ -43,6 +53,17 @@ func cleanHTML(input string) string {
43
53
wsRegex := regexp .MustCompile (`\s+` )
44
54
cleaned = wsRegex .ReplaceAllString (cleaned , " " )
45
55
56
+ // & truncate to maxLength
57
+ if len (cleaned ) > maxLength {
58
+ cleaned = cleaned [:maxLength ]
59
+
60
+ if cleaned [len (cleaned )- 1 ] == ' ' || cleaned [len (cleaned )- 1 ] == '.' {
61
+ cleaned = cleaned [:len (cleaned )- 1 ]
62
+ }
63
+
64
+ cleaned += "..."
65
+ }
66
+
46
67
return strings .TrimSpace (cleaned )
47
68
}
48
69
@@ -73,13 +94,14 @@ func parseDate(item Item) time.Time {
73
94
func getDescription (item Item ) string {
74
95
candidates := []string {
75
96
item .Description ,
97
+ item .Summary ,
76
98
item .Content ,
77
99
item .Encoded ,
78
100
}
79
101
80
102
for _ , candidate := range candidates {
81
103
if candidate != "" {
82
- return cleanHTML (candidate )
104
+ return stripMarkdown ( cleanHTML (candidate , 200 ) )
83
105
}
84
106
}
85
107
0 commit comments