Skip to content

Commit 9798444

Browse files
committed
Add docs-search service
1 parent b9615f1 commit 9798444

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

addon/components/docs-viewer/x-nav/component.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default Component.extend({
77
layout,
88

99
store: service(),
10+
docsSearch: service(),
1011

1112
tagName: 'nav',
1213

@@ -16,6 +17,11 @@ export default Component.extend({
1617
return this.get('store').peekAll('project-version').get('firstObject');
1718
}),
1819

20+
init() {
21+
this._super();
22+
this.get('docsSearch').loadSearchIndex();
23+
}
24+
1925
// didInsertElement() {
2026
// this._super(...arguments);
2127
//

addon/services/docs-search.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Service from '@ember/service';
2+
import { getOwner } from '@ember/application';
3+
import { computed } from '@ember/object';
4+
import $ from 'jquery';
5+
import lunr from 'lunr';
6+
7+
export default Service.extend({
8+
search(phrase, { exact = false } = {}) {
9+
if (!exact) {
10+
phrase = `*${phrase}*`;
11+
}
12+
13+
return this.loadSearchIndex()
14+
.then(({ index, documents }) => {
15+
return index.search(phrase).map(resultInfo => {
16+
let document = documents[resultInfo.ref];
17+
return { resultInfo, document };
18+
});
19+
});
20+
},
21+
22+
loadSearchIndex() {
23+
if (!this._searchIndex) {
24+
this._searchIndex = $.get(this.get('_indexURL'))
25+
.then(json => {
26+
return {
27+
index: lunr.Index.load(json.index),
28+
documents: json.documents
29+
};
30+
});
31+
}
32+
33+
return this._searchIndex;
34+
},
35+
36+
_indexURL: computed(function() {
37+
let config = getOwner(this).resolveRegistration('config:environment');
38+
return `${config.rootURL}ember-cli-addon-docs/search-index.json`;
39+
})
40+
});

app/services/docs-search.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-cli-addon-docs/services/docs-search';

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ module.exports = {
6464
let importer = findImporter(this);
6565

6666
importer.import(`${this._hasEmberSource() ? 'vendor' : 'bower_components'}/ember/ember-template-compiler.js`);
67+
importer.import('vendor/lunr/lunr.js', {
68+
using: [{ transformation: 'amd', as: 'lunr' }]
69+
});
70+
6771
// importer.import('vendor/highlightjs-styles/default.css');
6872
// importer.import('vendor/styles/highlightjs-styles/default.css');
6973
// importer.import('vendor/highlight.js/styles/monokai.css');
@@ -91,6 +95,7 @@ module.exports = {
9195
return new MergeTrees([
9296
vendor,
9397
this._highlightJSTree(),
98+
this._lunrTree(),
9499
this._templateCompilerTree()
95100
].filter(Boolean));
96101
},
@@ -118,6 +123,10 @@ module.exports = {
118123
return new MergeTrees([ defaultTree, docsTree, searchIndexTree ]);
119124
},
120125

126+
_lunrTree() {
127+
return new Funnel(path.dirname(require.resolve('lunr/package.json')), { destDir: 'lunr' });
128+
},
129+
121130
_highlightJSTree() {
122131
return new Funnel(path.dirname(require.resolve('highlightjs/package.json')), {
123132
srcDir: 'styles',

0 commit comments

Comments
 (0)