-
Notifications
You must be signed in to change notification settings - Fork 7
Fix post previews rendering raw markdown #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ package blog | |
|
|
||
| import ( | ||
| "bytes" | ||
| "html/template" | ||
| "regexp" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
@@ -36,6 +38,93 @@ func (p Post) PreviewContent(length int) string { | |
| return string(runes) | ||
| } | ||
|
|
||
| var ( | ||
| reFencedCode = regexp.MustCompile("(?s)```[^\n]*\n(.*?)```") | ||
| reInlineCode = regexp.MustCompile("`([^`]+)`") | ||
| reImages = regexp.MustCompile(`!\[([^\]]*)\]\([^)]+\)`) | ||
| reLinks = regexp.MustCompile(`\[([^\]]+)\]\([^)]+\)`) | ||
| reLinksWithURL = regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`) | ||
| reHeadings = regexp.MustCompile(`(?m)^#{1,6}\s+`) | ||
| reBold = regexp.MustCompile(`\*\*(.+?)\*\*`) | ||
| reItalic = regexp.MustCompile(`\*(.+?)\*`) | ||
| reUBold = regexp.MustCompile(`__(.+?)__`) | ||
| reUItalic = regexp.MustCompile(`_(.+?)_`) | ||
| reHR = regexp.MustCompile(`(?m)^[-*_]{3,}\s*$`) | ||
| reWhitespace = regexp.MustCompile(`\s+`) | ||
| ) | ||
|
|
||
| // PlainTextPreview strips markdown syntax and returns clean plain text for previews | ||
| func (p Post) PlainTextPreview(length int) string { | ||
| s := p.Content | ||
| s = reFencedCode.ReplaceAllString(s, "$1") | ||
| s = reInlineCode.ReplaceAllString(s, "$1") | ||
| s = reImages.ReplaceAllString(s, "$1") | ||
| s = reLinks.ReplaceAllString(s, "$1") | ||
| s = reHeadings.ReplaceAllString(s, "") | ||
| s = reBold.ReplaceAllString(s, "$1") | ||
| s = reItalic.ReplaceAllString(s, "$1") | ||
| s = reUBold.ReplaceAllString(s, "$1") | ||
| s = reUItalic.ReplaceAllString(s, "$1") | ||
| s = reHR.ReplaceAllString(s, "") | ||
| s = reWhitespace.ReplaceAllString(s, " ") | ||
| s = strings.TrimSpace(s) | ||
|
|
||
| runes := bytes.Runes([]byte(s)) | ||
| if len(runes) > length { | ||
| return string(runes[:length]) + "..." | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // HTMLPreview strips markdown syntax but preserves links as HTML hyperlinks | ||
| func (p Post) HTMLPreview(length int) template.HTML { | ||
| s := p.Content | ||
| s = reFencedCode.ReplaceAllString(s, "<code>$1</code>") | ||
| s = reInlineCode.ReplaceAllString(s, "<code>$1</code>") | ||
| s = reImages.ReplaceAllString(s, "$1") | ||
| s = reLinksWithURL.ReplaceAllString(s, `<a href="$2">$1</a>`) | ||
| s = reHeadings.ReplaceAllString(s, "") | ||
|
Comment on lines
+80
to
+86
|
||
| s = reBold.ReplaceAllString(s, "$1") | ||
| s = reItalic.ReplaceAllString(s, "$1") | ||
| s = reUBold.ReplaceAllString(s, "$1") | ||
| s = reUItalic.ReplaceAllString(s, "$1") | ||
| s = reHR.ReplaceAllString(s, "") | ||
|
Comment on lines
+82
to
+91
|
||
| s = reWhitespace.ReplaceAllString(s, " ") | ||
| s = strings.TrimSpace(s) | ||
|
|
||
| // Truncate by visible character count, skipping HTML tags | ||
| runes := []rune(s) | ||
| visible := 0 | ||
| cutoff := len(runes) | ||
| inTag := false | ||
| for i, r := range runes { | ||
| if r == '<' { | ||
| inTag = true | ||
| } else if r == '>' { | ||
| inTag = false | ||
| } else if !inTag { | ||
| visible++ | ||
| if visible > length { | ||
| cutoff = i | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if cutoff < len(runes) { | ||
| result := string(runes[:cutoff]) | ||
| if strings.Count(result, "<a ") > strings.Count(result, "</a>") { | ||
| result += "</a>" | ||
| } | ||
| if strings.Count(result, "<code>") > strings.Count(result, "</code>") { | ||
| result += "</code>" | ||
| } | ||
| result += "..." | ||
| return template.HTML(result) | ||
| } | ||
| return template.HTML(s) | ||
| } | ||
|
Comment on lines
+56
to
+126
|
||
|
|
||
| // Permalink returns the link to the post relative to root | ||
| func (p Post) Permalink() string { | ||
| return p.CreatedAt.Format("/posts/2006/01/02/") + p.Slug | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PlainTextPreviewappends "..." after truncating tolengthrunes, so the returned string can exceed the requested length (e.g.,length+3). For meta description usage, consider truncating tolength-3when adding an ellipsis (or make ellipsis optional) so callers can enforce a strict maximum length.