Skip to content

Commit

Permalink
Merge pull request #241 from goatslacker/lunr-search
Browse files Browse the repository at this point in the history
Adding a script that indexes our docs
  • Loading branch information
goatslacker committed May 15, 2015
2 parents aa3eff9 + 1d0d093 commit ba1dda9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions web/assets/search.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions web/scripts/createIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require('fs')
const path = require('path')
const lunr = require('lunr')

const index = lunr(function () {
this.field('title', { boost: 10 })
this.field('description', { boost: 5 })
this.field('body')
this.field('layout')
this.field('permalink')
})

function addDocument(name) {
const markdown = String(fs.readFileSync('../docs/' + name)).split('---')

const topdoc = markdown[1]

const body = markdown.slice(2, markdown.length).join('')

const doc = topdoc
.split('\n')
.filter(Boolean)
.reduce(function (obj, prop) {
const keyVal = prop.split(':')
obj[keyVal[0]] = keyVal[1].trim()
return obj
}, { id: name, body: body })

index.add(doc)
}

const docsdir = fs.readdirSync('../docs')
docsdir.forEach(function (file) {
if (/.md$/.test(file)) {
addDocument(file)
}
})

const search_json = JSON.stringify(index.toJSON())

fs.writeFileSync('./assets/search.json', search_json)

0 comments on commit ba1dda9

Please sign in to comment.