Skip to content

Commit

Permalink
Make indexing work
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Oct 11, 2015
1 parent bdb8392 commit 5ae0dd6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 11 deletions.
5 changes: 3 additions & 2 deletions fixture/onmount/metalsmith.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* istanbul ignore next */
(function() {
var docpress = require('../../ms')
var docpress = require('docpress-core/ms')

var app = docpress(__dirname)
.use(require('../../')())
.use(require('docpress-core')())
.use(require('../../index')())

if (module.parent) {
module.exports = app
Expand Down
8 changes: 0 additions & 8 deletions fixture/with-config/docs/docpress.json

This file was deleted.

58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

module.exports = function search () {
return function search (files, ms, done) {
let idx = {}

Object.keys(files).forEach((fname) => {
const file = files[fname]
if (file.$) index(fname, file, idx)
})

extendIndex(files['docpress.json'], (file) => {
file.searchIndex = idx
})

files['search.js'] = { contents: '' }
done()
}
}

/**
* Internal: extends `docpress.json` with more metadata. It will rewrite
* `contents` afterwards.
*/

function extendIndex (file, block) {
delete file.contents
block(file)
file.contents = JSON.stringify(file, null, 2)
}

/**
* Internal: adds new indexes to `idx` from the file `file`/`fname`.
*/

function index (fname, file, idx) {
let slug = fname
let title = file.title

file.$(':root').each(function () {
const $this = file.$(this)
if ($this.is('h2, h3, h4, h5, h6')) {
slug = fname + '#' + $this.attr('id')
title = $this.text()
} else {
if (!idx[slug]) {
idx[slug] = {
title: title,
pagetitle: file.title,
slug: slug,
text: ''
}
} else {
idx[slug].text += ' ' + $this.text()
}
}
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"author": "Rico Sta. Cruz <[email protected]>",
"license": "MIT",
"dependencies": {
"lunr": "0.5.12"
"lunr": "0.5.12",
"ware": "1.3.0"
},
"devDependencies": {
"expect": "1.12.1",
Expand Down
28 changes: 28 additions & 0 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'
const fixture = require('./support/fixture')

describe('fixture', function () {
let fx = fixture('onmount')
let app

before(function (done) {
app = require(fx.path('metalsmith.js'))
app.build((err, files) => {
if (err) return done(err)
this.files = files
done()
})
})

it('appends searchIndex metadata to docpress.json', function () {
expect(this.files['docpress.json'].searchIndex).toExist()
})

it('writes searchIndex into docpress.json as well', function () {
expect(JSON.parse(fx.read('_docpress/docpress.json')).searchIndex).toExist()
})

it('creates search.js', function () {
expect(this.files['search.js']).toExist()
})
})
28 changes: 28 additions & 0 deletions test/support/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

var join = require('path').join
var fs = require('fs')

function Fixture (path) {
if (!(this instanceof Fixture)) return new Fixture(path)
this.root = path
}

Fixture.root = join(__dirname, '../../fixture')

Fixture.prototype.path = function (file) {
return join(Fixture.root, this.root, file)
}

Fixture.prototype.exists = function (file) {
try {
fs.statSync(this.path(file))
return true
} catch (e) { return false }
}

Fixture.prototype.read = function (file) {
return fs.readFileSync(this.path(file), 'utf-8')
}

module.exports = Fixture

0 comments on commit 5ae0dd6

Please sign in to comment.