@@ -19,7 +19,7 @@ const lunr = require('lunr')
1919const marked = require ( 'marked' )
2020
2121const PAT_HEADMATTER = / ^ \+ \+ \+ \n ( [ ^ ] + ) \n \+ \+ \+ /
22- const SNIPPET_LENGTH = 175
22+ const SNIPPET_LENGTH = 220
2323
2424function escape ( html , encode ) {
2525 return html
@@ -70,6 +70,21 @@ function makeRenderer() {
7070 return renderer
7171}
7272
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+
7388// Recursively step through an object and replace any numbers with a number
7489// representable in a short ASCII string.
7590function truncateNumbers ( r ) {
@@ -133,19 +148,27 @@ function processFile(path) {
133148 headmatter . slug = '/' + pathModule . parse ( path ) . name
134149 }
135150
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+
136161 const searchDoc = {
137162 id : searchIndex . docId ,
138163 title : headmatter . title ,
139- tags : Object . keys ( headmatter . tags ) ,
140- minorTitles : [ ] ,
141- body : [ ]
164+ tags : headmatter . tags ,
165+ minorTitles : headingRemover . headings ,
166+ body : paragraphText
142167 }
168+
143169 searchIndex . docId += 1
144170 searchIndex . slugs . push ( headmatter . slug )
145171
146- const renderer = makeRenderer ( )
147- const html = marked ( rawdata . slice ( match [ 0 ] . length ) , { renderer : renderer } ) + renderer . flush ( )
148- searchDoc . body = searchDoc . body . join ( ' ' )
149172 searchIndex . idx . add ( searchDoc )
150173
151174 return {
@@ -154,7 +177,7 @@ function processFile(path) {
154177 headmatter : {
155178 url : headmatter . slug ,
156179 title : headmatter . title ,
157- snippet : searchDoc . body . substring ( 0 , SNIPPET_LENGTH ) ,
180+ snippet : paragraphText . substring ( 0 , SNIPPET_LENGTH ) + '...' ,
158181 options : headmatter . tags ,
159182 }
160183 }
0 commit comments