From 214c46aaa5fb4a8676c05a0e146780d1e290cbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=97=E9=9B=81=20Cryolitia?= Date: Wed, 10 Apr 2024 09:35:31 +0800 Subject: [PATCH] fix: CopyrightInfo --- .../plugins/MarkdownSectionWrapper.ts | 36 +++++++++++++++---- docs/.vitepress/plugins/MarkdownTransform.ts | 33 ----------------- .../theme/components/CopyrightInfo.vue | 3 +- docs/vite.config.ts | 33 +++++++++-------- 4 files changed, 48 insertions(+), 57 deletions(-) delete mode 100644 docs/.vitepress/plugins/MarkdownTransform.ts diff --git a/docs/.vitepress/plugins/MarkdownSectionWrapper.ts b/docs/.vitepress/plugins/MarkdownSectionWrapper.ts index c273d21..fa8a39c 100644 --- a/docs/.vitepress/plugins/MarkdownSectionWrapper.ts +++ b/docs/.vitepress/plugins/MarkdownSectionWrapper.ts @@ -1,4 +1,4 @@ -import { relative } from 'node:path' +import { resolve, relative } from 'node:path' import type { Plugin } from 'vite' import { @@ -79,7 +79,7 @@ export interface MarkdownSectionWrapperOptions { exclude?: (id: string, context: Context) => boolean } -export function MarkdownSectionWrapper(headerTransformers: ((origin: string) => string)[], footerTransformers: ((origin: string) => string)[], options?: MarkdownSectionWrapperOptions): Plugin { +export function MarkdownSectionWrapper(headerTransformers: ((frontmatter: string|null, text: string, id: string) => string)[], footerTransformers: ((frontmatter: string|null, text: string, id: string) => string)[], options?: MarkdownSectionWrapperOptions): Plugin { const { excludes = ['index.md'], exclude = () => false, @@ -116,15 +116,18 @@ export function MarkdownSectionWrapper(headerTransformers: ((origin: string) => if (exclude(id, { helpers: { idEndsWith, idEquals, idStartsWith, pathEndsWith, pathEquals, pathStartsWith } })) return null - let headers: string[] = headerTransformers.map(f => f(code)) - let footers: string[] = footerTransformers.map(f => f(code)) + let frontmatter = (code.match(/(^---$(\s|\S)+?^---$)/m)?.[0] ?? null) + let text = code.replace(/(^---$(\s|\S)+?^---$)/m, '') - return [...headers, code, ...footers].join("") + let headers: string[] = headerTransformers.map(f => f(frontmatter, text, id)) + let footers: string[] = footerTransformers.map(f => f(frontmatter, text, id)) + + return [frontmatter, ...headers, text, ...footers].join("") }, } } -export function TemplateAppSBox(_code: string): string { +export function TemplateAppSBox(_frontmatter: string|null, _text: string, _id: string): string { return ` ## 意见反馈 @@ -134,10 +137,29 @@ export function TemplateAppSBox(_code: string): string { ` } -export function TemplateCopyrightInfo(_code: string): string { +export function TemplateCopyrightInfo(_frontmatter: string|null, _text: string, _id: string): string { return ` ` } + +const ROOT = resolve(__dirname, '../../') + +export function PageHeaderTemplate(_frontmatter: string|null, _text: string, id: string): string { + if (!id.endsWith('.md')) + return '' + + id = relative(ROOT, id) + + if (id == 'index.md') + return '' + + return ` + +# {{ $frontmatter.title }} + + + +`} diff --git a/docs/.vitepress/plugins/MarkdownTransform.ts b/docs/.vitepress/plugins/MarkdownTransform.ts deleted file mode 100644 index 6f5a6e1..0000000 --- a/docs/.vitepress/plugins/MarkdownTransform.ts +++ /dev/null @@ -1,33 +0,0 @@ -import type { Plugin } from 'vite' -import { resolve, relative } from 'path' - -const ROOT = resolve(__dirname, '../../') - -export function MarkdownTransform(): Plugin { - return { - name: 'docs-md-transform', - enforce: 'pre', - async transform(code, id) { - if (!id.endsWith('.md')) - return null - - id = relative(ROOT, id) - - if (id == 'index.md') - return null - - code = pageHeaderTemplate(code) - - return code - }, - } -} - - -const pageHeaderTemplate = (code: string) => !code.startsWith('---') ? code : code.replace(/(^---$(\s|\S)+?^---$)/m, `$1 - -# {{ $frontmatter.title }} - - - -`) diff --git a/docs/.vitepress/theme/components/CopyrightInfo.vue b/docs/.vitepress/theme/components/CopyrightInfo.vue index d9856e8..d1081ba 100644 --- a/docs/.vitepress/theme/components/CopyrightInfo.vue +++ b/docs/.vitepress/theme/components/CopyrightInfo.vue @@ -41,8 +41,6 @@ const licenseUrl = attrs?.copyright?.licenseUrl ?? 'javascript:void(0)' diff --git a/docs/vite.config.ts b/docs/vite.config.ts index 0c424ef..c31aef8 100644 --- a/docs/vite.config.ts +++ b/docs/vite.config.ts @@ -1,7 +1,11 @@ import { resolve } from 'node:path' import { defineConfig } from 'vite' -import { MarkdownTransform } from './.vitepress/plugins/MarkdownTransform' -import { MarkdownSectionWrapper, TemplateAppSBox, TemplateCopyrightInfo } from './.vitepress/plugins/MarkdownSectionWrapper' +import { + MarkdownSectionWrapper, + PageHeaderTemplate, + TemplateAppSBox, + TemplateCopyrightInfo +} from './.vitepress/plugins/MarkdownSectionWrapper' import Components from 'unplugin-vue-components/vite' import UnoCSS from 'unocss/vite' import { GitChangelog, GitChangelogMarkdownSection } from '@nolebase/vitepress-plugin-git-changelog/vite' @@ -19,7 +23,18 @@ export default defineConfig({ } }, plugins: [ - MarkdownTransform(), + MarkdownSectionWrapper( + [PageHeaderTemplate, TemplateCopyrightInfo], + [TemplateAppSBox], + { + excludes: [], + exclude: (_, { helpers }): boolean => { + if (helpers.idEquals('index.md')) + return true + + return false + }, + }), GitChangelog({ repoURL: 'https://github.com/project-trans/RLE-wiki', maxGitLogCount: 1000, @@ -43,18 +58,6 @@ export default defineConfig({ return false }, }), - MarkdownSectionWrapper( - [TemplateCopyrightInfo], - [TemplateAppSBox], - { - excludes: [], - exclude: (_, { helpers }): boolean => { - if (helpers.idEquals('index.md')) - return true - - return false - }, - }), Components({ dirs: resolve(__dirname, '.vitepress/theme/components'), include: [/\.vue$/, /\.vue\?vue/, /\.md$/],