fix: CopyrightInfo

pull/288/head^2
北雁 Cryolitia 2024-04-10 09:35:31 +08:00
parent fe31b9ef6b
commit 214c46aaa5
No known key found for this signature in database
GPG Key ID: 3E5D1772FC8A8EDD
4 changed files with 48 additions and 57 deletions

View File

@ -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 `
<CopyrightInfo />
`
}
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 }}
<PageInfo />
`}

View File

@ -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 }}
<PageInfo />
`)

View File

@ -41,8 +41,6 @@ const licenseUrl = attrs?.copyright?.licenseUrl ?? 'javascript:void(0)'
<template>
<div v-if="attrs?.copyright?.enable ?? false">
<br />
<hr />
<div class="tip custom-block">
<p class="custom-block-title">Copyright</p>
<p>
@ -61,5 +59,6 @@ const licenseUrl = attrs?.copyright?.licenseUrl ?? 'javascript:void(0)'
<span></span>
</p>
</div>
<hr />
</div>
</template>

View File

@ -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$/],