diff --git a/.gitignore b/.gitignore index 7e4e70fe0..c750822cf 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ fe.element/element-ui .npmrc coverage waiter.config.js +build/bin/algolia-key.js diff --git a/build/bin/gen-indices.js b/build/bin/gen-indices.js new file mode 100644 index 000000000..1af503971 --- /dev/null +++ b/build/bin/gen-indices.js @@ -0,0 +1,52 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const algoliasearch = require('algoliasearch'); +const slugify = require('transliteration').slugify; +const key = require('./algolia-key'); + +const client = algoliasearch('9NLTR1QH8B', key); + +['zh-CN', 'en-US'].forEach(lang => { + const indexName = lang === 'zh-CN' ? 'element-zh' : 'element-en'; + const index = client.initIndex(indexName); + index.clearIndex(err => { + if (err) return; + fs.readdir(path.resolve(__dirname, `../../examples/docs/${ lang }`), (err, files) => { + if (err) return; + let indices = []; + files.forEach(file => { + const component = file.replace('.md', ''); + const content = fs.readFileSync(path.resolve(__dirname, `../../examples/docs/${ lang }/${ file }`), 'utf8'); + const matches = content + .replace(/:::[\s\S]*?:::/g, '') + .replace(/```[\s\S]*?```/g, '') + .match(/#{2,4}[^#]*/g) + .map(match => match.replace(/\n+/g, '\n').split('\n').filter(part => !!part)) + .map(match => { + const length = match.length; + if (length > 2) { + const desc = match.slice(1, length).join(''); + return [match[0], desc]; + } + return match; + }); + + indices = indices.concat(matches.map(match => { + const isComponent = match[0].indexOf('###') < 0; + const title = match[0].replace(/#{2,4}/, '').trim(); + const index = { component, title }; + index.ranking = isComponent ? 2 : 1; + index.anchor = slugify(title); + index.content = (match[1] || title).replace(/<[^>]+>/g, ''); + return index; + })); + }); + + index.addObjects(indices, (err, res) => { + console.log(err, res); + }); + }); + }); +}); diff --git a/examples/assets/images/search-by-algolia.svg b/examples/assets/images/search-by-algolia.svg new file mode 100644 index 000000000..989ed6ab8 --- /dev/null +++ b/examples/assets/images/search-by-algolia.svg @@ -0,0 +1 @@ +search-by-algolia \ No newline at end of file diff --git a/examples/components/header.vue b/examples/components/header.vue index f116920e1..f0debedab 100644 --- a/examples/components/header.vue +++ b/examples/components/header.vue @@ -314,6 +314,9 @@