From ec65bf962612def8a0a7797e68b50efe51d04e04 Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 5 Oct 2022 18:36:53 +0800 Subject: [PATCH] refactor(build): optimize the conversion of attributes in documents (#9970) --- internal/build/package.json | 2 +- internal/build/src/tasks/helper.ts | 31 ++++++++++++++---------------- pnpm-lock.yaml | 8 ++++---- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/internal/build/package.json b/internal/build/package.json index bd886277f0..36670a35b7 100644 --- a/internal/build/package.json +++ b/internal/build/package.json @@ -23,7 +23,7 @@ "@vitejs/plugin-vue": "^2.3.3", "@vitejs/plugin-vue-jsx": "^1.3.10", "chalk": "^5.0.1", - "components-helper": "^2.1.2", + "components-helper": "^2.1.3", "consola": "^2.15.3", "esbuild": "^0.14.47", "fast-glob": "^3.2.11", diff --git a/internal/build/src/tasks/helper.ts b/internal/build/src/tasks/helper.ts index c8aba91587..e7ed8ba2a6 100644 --- a/internal/build/src/tasks/helper.ts +++ b/internal/build/src/tasks/helper.ts @@ -4,7 +4,6 @@ import { getTypeSymbol, hyphenate, isCommonType, - isEnumType, isUnionType, main, } from 'components-helper' @@ -25,7 +24,7 @@ import type { } from 'components-helper' const typeMap = { - vue: ['Component', 'VNode'], + vue: ['Component', 'VNode', 'CSSProperties', 'StyleValue'], } const reComponentName: ReComponentName = (title) => @@ -48,13 +47,14 @@ const reWebTypesSource: ReWebTypesSource = (title) => { return { symbol } } -const reAttribute: ReAttribute = (value, key, _, title) => { +const reAttribute: ReAttribute = (value, key) => { const str = value - .replace(/^\*\*(.*)\*\*$/, (_, item) => item) - .replace(/^`(.*)`$/, (_, item) => item) + .replace(/^\*\*(.*)\*\*$/, '$1') + .replace(/^`(.*)`$/, '$1') + .replace(/^~~(.*)~~$/, '') .replaceAll(/.*<\/del>/g, '') - if (title === 'Events' && key === 'Name' && /^(-|—)$/.test(str)) { + if (key === 'Name' && /^(-|—)$/.test(str)) { return 'default' } else if (str === '' || /^(-|—)$/.test(str)) { return undefined @@ -74,8 +74,13 @@ const reAttribute: ReAttribute = (value, key, _, title) => { return str .replaceAll(/\bfunction(\(.*\))?(:\s*\w+)?\b/gi, 'Function') .replaceAll(/\bdate\b/g, 'Date') - .replaceAll(/\bstring \| Component\b/g, 'string / Component') .replaceAll(/\([^)]*\)(?!\s*=>)/g, '') + .replaceAll(/(<[^>]*>|\{[^}]*}|\([^)]*\))/g, (item) => { + return item.replaceAll(/(\/|\|)/g, '=_0!') + }) + .replaceAll(/(\b\w+)\s*\|/g, '$1 /') + .replaceAll(/\|\s*(\b\w+)/g, '/ $1') + .replaceAll(/=_0!/g, '|') } else if (key === 'Accepted Values') { return /\[.+\]\(.+\)/.test(str) || /^\*$/.test(str) ? undefined @@ -94,20 +99,12 @@ const reAttribute: ReAttribute = (value, key, _, title) => { } const reWebTypesType: ReWebTypesType = (type) => { - const isEnum = isEnumType(type) - const isTuple = /^\[.*\]$/.test(type) - const isArrowFunction = /^\(.*\)\s*=>\s*\w+/.test(type) const isPublicType = isCommonType(type) const symbol = getTypeSymbol(type) const isUnion = isUnionType(symbol) - const module = findModule(type) + const module = findModule(symbol) - return isEnum || - isTuple || - isArrowFunction || - isPublicType || - !symbol || - isUnion + return isPublicType || !symbol || isUnion ? type : { name: type, source: { symbol, module } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8a29f1ae06..b7d805b760 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -242,7 +242,7 @@ importers: '@vitejs/plugin-vue': ^2.3.3 '@vitejs/plugin-vue-jsx': ^1.3.10 chalk: ^5.0.1 - components-helper: ^2.1.2 + components-helper: ^2.1.3 consola: ^2.15.3 esbuild: ^0.14.47 fast-glob: ^3.2.11 @@ -264,7 +264,7 @@ importers: '@vitejs/plugin-vue': 2.3.3_vue@3.2.37 '@vitejs/plugin-vue-jsx': 1.3.10 chalk: 5.0.1 - components-helper: 2.1.2 + components-helper: 2.1.3 consola: 2.15.3 esbuild: 0.14.47 fast-glob: 3.2.11 @@ -5050,8 +5050,8 @@ packages: resolution: {integrity: sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==} dev: false - /components-helper/2.1.2: - resolution: {integrity: sha512-KGzom/KUa/y8nL59Q+XPV/ARiHzVIAR1AJlJEmrV4zCJBVCXqbvGbpFxdtEugy6mICBrWdpeDzcewzwCjHewyw==} + /components-helper/2.1.3: + resolution: {integrity: sha512-t3PFNxbtPtzAY2GEoJBq6OipxwunqZKvaohkRL2uPK5hsuekM47RzhfxPxIqZ9Kd6Nnc2rTUaDnjx9gz2dImEQ==} engines: {node: '>10.0.0'} dependencies: fast-glob: 3.2.11