-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |