From 2ff45e2b839299c494f38e507b955e87fa94897b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=B3=E6=99=93=E4=BA=AE?= <2220124666@qq.com> Date: Tue, 13 Aug 2024 10:03:51 +0800 Subject: [PATCH] feat(docs): Changed overview.md from manual editing to auto-generation --- .github/workflows/gh-pages.yml | 6 +- docs/.vitepress/config.ts | 102 ++++++++++++++++++++++++++++++++- docs/_demos/overview.config.ts | 16 ++++++ docs/guide/overview.md | 26 +++------ package.json | 1 + pnpm-lock.yaml | 11 ++++ 6 files changed, 142 insertions(+), 20 deletions(-) create mode 100644 docs/_demos/overview.config.ts diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index b179efb..20a274a 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -8,8 +8,10 @@ on: pull_request: branches: - main - -permissions: + + workflow_dispatch: + +permissions: contents: write jobs: diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index e861355..d806ef3 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -1,15 +1,17 @@ import _path from 'node:path' import fs from 'node:fs' -import { mkdir, readFile, writeFile } from 'node:fs/promises' +import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises' import type { Plugin } from 'vitepress' import { defineConfig } from 'vitepress' import container from 'markdown-it-container' import { snippet } from '@mdit/plugin-snippet' import { type MarkdownItIncludeOptions, include } from '@mdit/plugin-include' +import { minimatch } from 'minimatch' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { TDesignResolver } from 'unplugin-vue-components/resolvers' +import { toPairs } from 'lodash-es' export default defineConfig({ title: 'wuxianx-charts', @@ -78,6 +80,7 @@ export default defineConfig({ }, vite: { plugins: [ + generateOverview(), buildChartsMeta(), AutoImport({ resolvers: [TDesignResolver({ @@ -203,3 +206,100 @@ function buildChartsMeta() { return str } } + +function generateOverview() { + let root: string + + return { + name: 'generate-overview', + + async configResolved(config) { + root = config.root + }, + + async watchChange(id, { event }) { + if (!id.includes('docs/_demos')) + return + + if (!['create', 'delete'].includes(event)) + return + + await gen() + }, + + } as Plugin + + async function gen() { + const overviewConfig = (await import('../_demos/overview.config')).default + + const _ignores = [ + '*.vue', + ...(overviewConfig?.includes || []), + ] + + const group: Record = {} + function setGroup(key: string, item: string) { + if (!group[key]) { + group[key] = [] + } + + group[key].push(item) + } + + const files = (await readdir(_path.join(root, '_demos'))) + .filter((f) => { + return _ignores.every(includes => minimatch(f, includes)) + }) + + files.forEach((f) => { + const types = overviewConfig.typesOf[f] + + if (typeof types === 'string') { + setGroup(types, f) + return + } + + if (Array.isArray(types)) { + types.forEach((type) => { + setGroup(type, f) + }) + + return + } + + setGroup(f.split('-')[0], f) + }) + + let result = toPairs(group) + .sort((_a, _b) => { + const a = _a[0].toLocaleLowerCase() + const b = _b[0].toLocaleLowerCase() + + return a.localeCompare(b) + }) + .map((group) => { + const [type, items] = group + items.sort((a, b) => a.length - b.length) + + return `## ${type}\n\n${items.map((item) => { + const group = overviewConfig.groupOf[item] || '' + const name = item.replace('.vue', '') + const s + = `::: chart-preview ${name} ${group}\n` + + `<<< @demos/${item}\n` + + `:::` + + return s + }).join('\n\n')}` + }).join('\n\n') + + result + = `---\n` + + `aside: false\n` + + `---\n\n` + + `# Overview\n\n` + + `${result}\n` + + await writeFile(_path.join(root, 'guide/overview.md'), result) + } +} diff --git a/docs/_demos/overview.config.ts b/docs/_demos/overview.config.ts new file mode 100644 index 0000000..270a8d7 --- /dev/null +++ b/docs/_demos/overview.config.ts @@ -0,0 +1,16 @@ +const typesOf: DefineObject = { + 'ring-three-quarter-comment.vue': ['pie', 'doughnut'], + 'pie-simple-configured.vue': ['pie', 'doughnut'], +} + +const groupOf: StringObject = { + 'line-simple-configured.vue': 'line-simple', + 'bar-simple-configured.vue': 'bar-simple', + 'pie-simple-configured.vue': 'pie-simple', +} + +export default { + typesOf, + groupOf, + includes: ['!guide-*.vue'], +} diff --git a/docs/guide/overview.md b/docs/guide/overview.md index 8aab7f9..cd4beef 100644 --- a/docs/guide/overview.md +++ b/docs/guide/overview.md @@ -7,61 +7,53 @@ aside: false ## bar ::: chart-preview bar-simple - <<< @demos/bar-simple.vue - ::: ::: chart-preview bar-simple-configured bar-simple - <<< @demos/bar-simple-configured.vue +::: +## doughnut + +::: chart-preview pie-simple-configured pie-simple +<<< @demos/pie-simple-configured.vue +::: + +::: chart-preview ring-three-quarter-comment +<<< @demos/ring-three-quarter-comment.vue ::: ## line ::: chart-preview line-simple - <<< @demos/line-simple.vue - ::: ::: chart-preview line-simple-configured line-simple - <<< @demos/line-simple-configured.vue - ::: ## pie ::: chart-preview pie-simple - <<< @demos/pie-simple.vue - ::: ::: chart-preview pie-simple-configured pie-simple - <<< @demos/pie-simple-configured.vue - ::: ::: chart-preview ring-three-quarter-comment - <<< @demos/ring-three-quarter-comment.vue - ::: ## radar ::: chart-preview radar-simple - <<< @demos/radar-simple.vue - ::: ::: chart-preview radar-rainbow - <<< @demos/radar-rainbow.vue - ::: diff --git a/package.json b/package.json index 0d0a316..373a43a 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "lint-staged": "^15.2.7", "lodash-es": "^4.17.21", "markdown-it-container": "^4.0.0", + "minimatch": "^10.0.1", "mockjs": "^1.1.0", "pnpm": "^9.4.0", "rimraf": "^5.0.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a54b476..d874ba6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -77,6 +77,9 @@ importers: markdown-it-container: specifier: ^4.0.0 version: 4.0.0 + minimatch: + specifier: ^10.0.1 + version: 10.0.1 mockjs: specifier: ^1.1.0 version: 1.1.0 @@ -2023,6 +2026,10 @@ packages: resolution: {integrity: sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==} engines: {node: '>=4'} + minimatch@10.0.1: + resolution: {integrity: sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==} + engines: {node: 20 || >=22} + minimatch@3.1.2: resolution: {integrity: sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==} @@ -4828,6 +4835,10 @@ snapshots: min-indent@1.0.1: {} + minimatch@10.0.1: + dependencies: + brace-expansion: 2.0.1 + minimatch@3.1.2: dependencies: brace-expansion: 1.1.11