Skip to content

Commit

Permalink
build(build): added algolia indices builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyaoji committed Apr 26, 2021
1 parent 102acfd commit 151a2ef
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
run: yarn fast-build
env:
TAG_VERSION: ${{env.TAG_VERSION}}
ALGOLIA_KEY: ${{secrets.ALGOLIA_KEY}}
- name: generate npm token
run: echo "//registry.npmjs.com/:_authToken=${{ secrets.NPM_PUBLISH_TOKEN }}" > ./.npmrc
- name: Publish npm
Expand Down
66 changes: 66 additions & 0 deletions build/build-indices.js
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
})
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"build:css": "cross-env BABEL_ENV=utils babel packages/utils --extensions .ts --out-dir lib/utils",
"build:theme": "rimraf packages/theme-default/lib && gulp build --gulpfile packages/theme-default/gulpfile.js && cp-cli packages/theme-default/lib lib/theme-default && rimraf packages/theme-default/lib",
"build:helper": "node build/build-helper.js",
"build:indices": "node build/build-indices.js",
"lint": "eslint ./packages --ext .vue,.js,.ts",
"lint-fix": "eslint --fix ./packages --ext .vue,.js,.ts",
"website-build": "rimraf website-dist && cross-env NODE_ENV=production webpack --config ./website/webpack.config.js",
Expand Down Expand Up @@ -75,7 +76,7 @@
"css-loader": "^4.2.2",
"css-minimizer-webpack-plugin": "1.1.5",
"cz-conventional-changelog": "^3.3.0",
"element-plus": "^1.0.2-beta.36",
"element-plus": "^1.0.2-beta.40",
"element-theme-chalk": "^2.15.1",
"eslint": "^7.19.0",
"eslint-plugin-vue": "^7.9.0",
Expand Down
2 changes: 2 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ yarn build:theme

yarn build:helper

yarn build:indices

# Post build clean up

rm ./es.gz
11 changes: 6 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,7 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==

"@types/lodash@^4.14.168":
"@types/lodash@^4.14.161", "@types/lodash@^4.14.168":
version "4.14.168"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==
Expand Down Expand Up @@ -6153,12 +6153,13 @@ electron@^6.0.7:
electron-download "^4.1.0"
extract-zip "^1.0.3"

element-plus@^1.0.2-beta.36:
version "1.0.2-beta.36"
resolved "https://registry.yarnpkg.com/element-plus/-/element-plus-1.0.2-beta.36.tgz#78a7e1be10b9622c4fc342fdce0801650f30bb01"
integrity sha512-5Qc54rH0GGtFRXpfA4ilpcOzdAw6t2m4mBXMXYjs9c5RSYP+EV+gJyfhyolXqZoOtYZ35ovBcqljlIJJD6Nz7g==
element-plus@^1.0.2-beta.40:
version "1.0.2-beta.40"
resolved "https://registry.yarnpkg.com/element-plus/-/element-plus-1.0.2-beta.40.tgz#30fc9b161496ae587fab86235c80b728ea43d909"
integrity sha512-8S7JbBpJLUa3ggexuKuKeyrTo451gMDHa/BKJVFiHSyexcHPgnHQwHf2nIoZvY1w6FzmZPdBdZ3NoDLXu51SIw==
dependencies:
"@popperjs/core" "^2.4.4"
"@types/lodash" "^4.14.161"
async-validator "^3.4.0"
dayjs "1.x"
lodash "^4.17.20"
Expand Down

0 comments on commit 151a2ef

Please sign in to comment.