diff --git a/dev/app.js b/dev/app.js index 2aa16150..473a3a8a 100644 --- a/dev/app.js +++ b/dev/app.js @@ -1,9 +1,12 @@ import docsearch from '../index.js'; docsearch({ - apiKey: '52a6d7ab710fcef44537c3cff5290e55', - indexName: 'tim_stripe', - inputSelector: '#search-input' + apiKey: 'e3d767b736584dbe6d4c35f7cf7d4633', + indexName: 'react-native', + inputSelector: '#search-input', + autocompleteOptions: { + debug: true + } }); document.getElementById('search-input').focus(); diff --git a/package.json b/package.json index b0225174..009add1a 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "dev": "./scripts/dev", "dev:docs": "./scripts/dev-docs", + "serve": "./scripts/serve", "shrinkwrap": "npm-shrinkwrap --dev", "doctoc": "doctoc --maxlevel 3 README.md CONTRIBUTING.md", "build": "./scripts/build", diff --git a/scripts/serve b/scripts/serve new file mode 100755 index 00000000..afa01ba8 --- /dev/null +++ b/scripts/serve @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Will expose the build version on: +# - http://0.0.0.0:8080/docsearch.css +# - http://0.0.0.0:8080/docsearch.js + +npm run build:css + +# /bundle.js in memory +webpack-dev-server \ + --config webpack.serve.config.babel.js \ + --hot \ + --inline \ + --no-info & \ +# rebuild docsearch.css and docsearch.min.css +onchange './src/styles/*.scss' -- npm run build:css & \ + +wait diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js index 82c47bad..11523e68 100644 --- a/src/lib/DocSearch.js +++ b/src/lib/DocSearch.js @@ -34,12 +34,11 @@ class DocSearch { indexName, inputSelector, appId = 'BH4D9OD16A', - algoliaOptions = { - hitsPerPage: 5 - }, + algoliaOptions = {}, autocompleteOptions = { debug: false, - hint: false + hint: false, + autoselect: true } }) { DocSearch.checkArguments({apiKey, indexName, inputSelector, algoliaOptions, autocompleteOptions}); @@ -48,7 +47,7 @@ class DocSearch { this.appId = appId; this.indexName = indexName; this.input = DocSearch.getInputFromSelector(inputSelector); - this.algoliaOptions = algoliaOptions; + this.algoliaOptions = {hitsPerPage: 5, ...algoliaOptions}; this.autocompleteOptions = autocompleteOptions; this.client = algoliasearch(this.appId, this.apiKey); diff --git a/test/DocSearch-test.js b/test/DocSearch-test.js index 3e05c94f..6b0ff292 100644 --- a/test/DocSearch-test.js +++ b/test/DocSearch-test.js @@ -111,6 +111,36 @@ describe('DocSearch', () => { // Then expect(actual.appId).toEqual('foo'); }); + it('should allow customize algoliaOptions without loosing default options', () => { + // Given + let options = { + algoliaOptions: { + facetFilters: ['version:1.0'] + }, + ...defaultOptions + }; + + // When + let actual = new DocSearch(options); + + // Then + expect(actual.algoliaOptions).toEqual({hitsPerPage: 5, facetFilters: ['version:1.0']}); + }); + it('should allow customize hitsPerPage', () => { + // Given + let options = { + algoliaOptions: { + hitsPerPage: 10 + }, + ...defaultOptions + }; + + // When + let actual = new DocSearch(options); + + // Then + expect(actual.algoliaOptions).toEqual({hitsPerPage: 10}); + }); it('should pass the input element as an instance property', () => { // Given let options = defaultOptions; @@ -128,7 +158,7 @@ describe('DocSearch', () => { // Given let options = { ...defaultOptions, - algoliaOptions: 'algoliaOptions', + algoliaOptions: {anOption: 42}, autocompleteOptions: 'autocompleteOptions' }; @@ -136,7 +166,8 @@ describe('DocSearch', () => { let actual = new DocSearch(options); // Then - expect(actual.algoliaOptions).toEqual('algoliaOptions'); + expect(typeof actual.algoliaOptions).toEqual('object'); + expect(actual.algoliaOptions.anOption).toEqual(42); expect(actual.autocompleteOptions).toEqual('autocompleteOptions'); }); it('should instantiate algoliasearch with the correct values', () => { @@ -294,8 +325,7 @@ describe('DocSearch', () => { docsearch = new DocSearch({ indexName: 'indexName', apiKey: 'apiKey', - inputSelector: '#input', - algoliaOptions: 'algoliaOptions' + inputSelector: '#input' }); }); @@ -327,7 +357,7 @@ describe('DocSearch', () => { let expectedArguments = { indexName: 'indexName', query: 'query', - params: 'algoliaOptions' + params: {hitsPerPage: 5} }; expect(client.search.calledWith([expectedArguments])).toBe(true); }); diff --git a/webpack.serve.config.babel.js b/webpack.serve.config.babel.js new file mode 100644 index 00000000..0675e8d7 --- /dev/null +++ b/webpack.serve.config.babel.js @@ -0,0 +1,40 @@ +import webpack from 'webpack'; +import {join} from 'path'; + +export default { + entry: './index.js', + devtool: 'source-map', + output: { + path: './dist/cdn', + filename: 'docsearch.js', + library: 'docsearch', + libraryTarget: 'umd' + }, + module: { + loaders: [{ + test: /\.js$/, exclude: /node_modules/, loader: 'babel' + }] + }, + devServer: { + contentBase: 'dist/cdn', + host: '0.0.0.0', + compress: true + }, + // when module not found, find locally first + // helps fixing the npm link not working with webpack + // http://stackoverflow.com/a/33722844/147079 + resolve: { + fallback: [join(__dirname, '..', 'node_modules')] + }, + // same issue, for loaders like babel + resolveLoader: { + fallback: [join(__dirname, '..', 'node_modules')] + }, + plugins: [ + new webpack.DefinePlugin({ + 'process.env': { + NODE_ENV: JSON.stringify(process.env.NODE_ENV) + } + }) + ] +};