From a3d0d05d82cafee9d68411fb16bb1c6bcfae8bb6 Mon Sep 17 00:00:00 2001 From: jeremywu <15975785+JeremyWuuuuu@users.noreply.github.com> Date: Tue, 24 Aug 2021 16:52:44 +0800 Subject: [PATCH] fix replaceAll is undefined (#3024) --- build/build-indices.ts | 69 ++++++++++++++++++++++++++++++++++++++++++ build/gen-dts.ts | 6 ++-- build/gen-entry-dts.ts | 2 +- build/gulp-rewriter.ts | 4 +-- build/gulpfile.ts | 8 ++--- package.json | 2 +- 6 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 build/build-indices.ts diff --git a/build/build-indices.ts b/build/build-indices.ts new file mode 100644 index 0000000000..e592e8b0bb --- /dev/null +++ b/build/build-indices.ts @@ -0,0 +1,69 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +'use strict' + +import fs from 'fs' +import algoliasearch from 'algoliasearch' +import { slugify } from 'transliteration' +import fg from 'fast-glob' + +const algoliaKey = process.env.ALGOLIA_KEY + +const client = algoliasearch('7DCTSU0WBW', algoliaKey) +const langs = { + 'zh-CN': 'element-zh', + 'en-US': 'element-en', + es: 'element-es', + 'fr-FR': 'element-fr', + jp: 'element-jp', +} +;['zh-CN', 'en-US', 'es', 'fr-FR', 'jp'].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, + }) + }) +}) diff --git a/build/gen-dts.ts b/build/gen-dts.ts index bce6f63bce..63fbc9a7ee 100644 --- a/build/gen-dts.ts +++ b/build/gen-dts.ts @@ -112,9 +112,9 @@ const genVueTypes = async (root, outDir = path.resolve(__dirname, '../dist/types await fs.promises.writeFile(filepath, outputFile .getText() - .replaceAll('@element-plus/components', 'element-plus/es') - .replaceAll('@element-plus/theme-chalk', 'element-plus/theme-chalk') - .replaceAll('@element-plus', 'element-plus/es'), + .replace(new RegExp('@element-plus/components', 'g'), 'element-plus/es') + .replace(new RegExp('@element-plus/theme-chalk', 'g'), 'element-plus/theme-chalk') + .replace(new RegExp('@element-plus', 'g'), 'element-plus/es'), 'utf8') console.log( chalk.green( diff --git a/build/gen-entry-dts.ts b/build/gen-entry-dts.ts index b6e28d16d5..d0e9be4204 100644 --- a/build/gen-entry-dts.ts +++ b/build/gen-entry-dts.ts @@ -47,7 +47,7 @@ const gen = async () => { await fs.promises.writeFile(filepath, outputFile .getText() - .replaceAll('@element-plus', '.'), + .replace(new RegExp('@element-plus', 'g'), '.'), // .replaceAll('@element-plus/theme-chalk', 'element-plus/theme-chalk'), 'utf8') console.log( diff --git a/build/gulp-rewriter.ts b/build/gulp-rewriter.ts index 20de9f3b20..c95f6a598a 100644 --- a/build/gulp-rewriter.ts +++ b/build/gulp-rewriter.ts @@ -2,12 +2,12 @@ import through2 from 'through2' const rewriter = (rewriteTo = '../..') => { return through2.obj(function(file, _, cb) { - const compIdentifier = '@element-plus' + const compIdentifier = new RegExp('@element-plus', 'g') file.contents = Buffer.from( file.contents .toString() - .replaceAll(compIdentifier, rewriteTo), + .replace(compIdentifier, rewriteTo), ) cb(null, file) }) diff --git a/build/gulpfile.ts b/build/gulpfile.ts index 0ae381c3e9..a80510521d 100644 --- a/build/gulpfile.ts +++ b/build/gulpfile.ts @@ -14,15 +14,15 @@ const tsProject = ts.createProject('tsconfig.json', { const rewriter = () => { return through2.obj(function(file, _, cb) { - const compIdentifier = '@element-plus/components' + const compIdentifier = new RegExp('@element-plus/components', 'g') const compReplacer = '../../../components' - const themeIdentifier = '@element-plus/theme-chalk' + const themeIdentifier = new RegExp('@element-plus/theme-chalk', 'g') const themeReplacer = '../../../../theme-chalk' file.contents = Buffer.from( file.contents .toString() - .replaceAll(compIdentifier, compReplacer) - .replaceAll(themeIdentifier, themeReplacer), + .replace(compIdentifier, compReplacer) + .replace(themeIdentifier, themeReplacer), ) cb(null, file) }) diff --git a/package.json b/package.json index b9d1509dbc..89c35d228c 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "build:esm-bundle": "rollup --config ./build/rollup.config.bundle.js && yarn build:type", "build:locale-umd": "esno ./build/build-locale.ts", "build:helper": "esno build/build-helper.ts", - "build:indices": "node build/build-indices.js", + "build:indices": "esno build/build-indices.ts", "update:version": "esno build/update-version.ts", "build:comps": "rimraf dist/components && esno build/components.ts", "build:style": "gulp --cwd ./build",