-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(build): added algolia indices builder
- Loading branch information
Showing
5 changed files
with
77 additions
and
6 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 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,66 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const algoliasearch = require('algoliasearch') | ||
const slugify = require('transliteration').slugify | ||
const algoliaKey = process.env.ALGOLIA_KEY | ||
|
||
const client = algoliasearch('0R3ZAIXMAH', algoliaKey) | ||
const langs = { | ||
'zh-CN': 'vue-cesium-zh', | ||
'en-US': 'vue-cesium-en' | ||
} | ||
|
||
const fg = require('fast-glob') | ||
|
||
;['zh-CN', 'en-US'].forEach(lang => { | ||
const indexName = langs[lang] | ||
const index = client.initIndex(indexName) | ||
index.clearObjects().then(() => { | ||
const files = fg.sync(`website/docs/${lang}/**/*.md`) | ||
let indices = [] | ||
files.forEach(file => { | ||
const regExp = new RegExp(`website\/docs\/${lang}\/(.*).md`) | ||
const pathContent = file.match(regExp) | ||
const path = pathContent[1] | ||
const index = path.lastIndexOf('/') | ||
const names = index !== -1 ? path.split('/') : [] | ||
const component = names.length ? names[names.length - 1] : path | ||
const content = fs.readFileSync(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 | ||
}) | ||
let i = 0 | ||
indices = indices.concat( | ||
matches.map(match => { | ||
const title = match[0].replace(/#{2,4}/, '').trim() | ||
const index = { component, title } | ||
index.anchor = slugify(title) | ||
index.content = (match[1] || title).replace(/<[^>]+>/g, '') | ||
index.path = path | ||
index.sort = i++ | ||
return index | ||
}) | ||
) | ||
}) | ||
|
||
index.saveObjects(indices, { | ||
autoGenerateObjectIDIfNotExist: true | ||
}) | ||
}) | ||
}) |
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 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 |
---|---|---|
|
@@ -32,6 +32,8 @@ yarn build:theme | |
|
||
yarn build:helper | ||
|
||
yarn build:indices | ||
|
||
# Post build clean up | ||
|
||
rm ./es.gz |
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