aboutsummaryrefslogtreecommitdiffstats
path: root/internal/web/search.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/web/search.go')
-rw-r--r--internal/web/search.go21
1 files changed, 19 insertions, 2 deletions
diff --git a/internal/web/search.go b/internal/web/search.go
index ef554ae..9af980d 100644
--- a/internal/web/search.go
+++ b/internal/web/search.go
@@ -1,11 +1,14 @@
package web
import (
+ "html"
"html/template"
"regexp"
"strings"
"unicode/utf8"
+ "github.com/microcosm-cc/bluemonday"
+
"github.com/gramanas/blogspace/internal/store"
)
@@ -32,8 +35,22 @@ type searchHit struct {
Snippet template.HTML
}
-// searchSnippet is a short piece of the Markdown body around the first match,
-// with the match marked; when only the title matched it is the body's start.
+var stripTags = bluemonday.StrictPolicy()
+
+// snippetSource is the text a post's snippet is cut from: the Markdown as
+// written, or an HTML post with its tags stripped so the excerpt reads as
+// prose. (Postgres still matches against the source, so a query can hit a
+// tag or attribute name in an HTML post; the snippet then shows the text
+// nearest to it.)
+func snippetSource(p *store.Post) string {
+ if p.Format == store.FormatHTML {
+ return html.UnescapeString(stripTags.Sanitize(p.BodyMD))
+ }
+ return p.BodyMD
+}
+
+// searchSnippet is a short piece of the body around the first match, with the
+// match marked; when only the title matched it is the body's start.
func searchSnippet(body string, re *regexp.Regexp) template.HTML {
text := strings.Join(strings.Fields(body), " ")
loc := re.FindStringIndex(text)