@@ -19,7 +19,7 @@ const lunr = require('lunr')
19
19
const marked = require ( 'marked' )
20
20
21
21
const PAT_HEADMATTER = / ^ \+ \+ \+ \n ( [ ^ ] + ) \n \+ \+ \+ /
22
- const SNIPPET_LENGTH = 175
22
+ const SNIPPET_LENGTH = 220
23
23
24
24
function escape ( html , encode ) {
25
25
return html
@@ -70,6 +70,21 @@ function makeRenderer() {
70
70
return renderer
71
71
}
72
72
73
+ // Creates a renderer to render HTML without headings for snippets and
74
+ // make the removed headings available (for the searchDoc)
75
+ function makeHeadingRemover ( ) {
76
+ const renderer = new marked . Renderer ( )
77
+ let headings = [ ]
78
+
79
+ renderer . heading = function ( text , level , raw ) {
80
+ headings . push ( text )
81
+ return ''
82
+ }
83
+
84
+ renderer . headings = headings
85
+ return renderer
86
+ }
87
+
73
88
// Recursively step through an object and replace any numbers with a number
74
89
// representable in a short ASCII string.
75
90
function truncateNumbers ( r ) {
@@ -133,19 +148,27 @@ function processFile(path) {
133
148
headmatter . slug = '/' + pathModule . parse ( path ) . name
134
149
}
135
150
151
+ const renderer = makeRenderer ( )
152
+ const html = marked ( rawdata . slice ( match [ 0 ] . length ) , { renderer : renderer } ) + renderer . flush ( )
153
+
154
+ const headingRemover = makeHeadingRemover ( )
155
+ const htmlNoHeadings = marked ( rawdata . slice ( match [ 0 ] . length ) , { renderer : headingRemover } )
156
+ // Remove HTML tags, quotation mark entities, and single quote entities
157
+ const paragraphText = htmlNoHeadings . replace ( / < (?: .| \n ) * ?> / gm, '' )
158
+ . replace ( / & q u o t ; / g, '\"' )
159
+ . replace ( / & # 3 9 ; / g, '\'' )
160
+
136
161
const searchDoc = {
137
162
id : searchIndex . docId ,
138
163
title : headmatter . title ,
139
- tags : Object . keys ( headmatter . tags ) ,
140
- minorTitles : [ ] ,
141
- body : [ ]
164
+ tags : headmatter . tags ,
165
+ minorTitles : headingRemover . headings ,
166
+ body : paragraphText
142
167
}
168
+
143
169
searchIndex . docId += 1
144
170
searchIndex . slugs . push ( headmatter . slug )
145
171
146
- const renderer = makeRenderer ( )
147
- const html = marked ( rawdata . slice ( match [ 0 ] . length ) , { renderer : renderer } ) + renderer . flush ( )
148
- searchDoc . body = searchDoc . body . join ( ' ' )
149
172
searchIndex . idx . add ( searchDoc )
150
173
151
174
return {
@@ -154,7 +177,7 @@ function processFile(path) {
154
177
headmatter : {
155
178
url : headmatter . slug ,
156
179
title : headmatter . title ,
157
- snippet : searchDoc . body . substring ( 0 , SNIPPET_LENGTH ) ,
180
+ snippet : paragraphText . substring ( 0 , SNIPPET_LENGTH ) + '...' ,
158
181
options : headmatter . tags ,
159
182
}
160
183
}
0 commit comments