fix: auto frontmatter fail

This commit is contained in:
pengzhanbo 2023-07-01 16:06:45 +08:00
parent 6fd089418d
commit 53bca6a680
7 changed files with 54 additions and 48 deletions

View File

@ -1,12 +1,12 @@
import type { import type {
AutoFrontmatterOptions, AutoFrontmatterOptions,
FormatterArray, FrontmatterArray,
FormatterObject, FrontmatterObject,
} from '../shared/index.js' } from '../shared/index.js'
import { autoFrontmatterPlugin } from './plugin.js' import { autoFrontmatterPlugin } from './plugin.js'
export * from './plugin.js' export * from './plugin.js'
export { AutoFrontmatterOptions, FormatterArray, FormatterObject } export { AutoFrontmatterOptions, FrontmatterArray, FrontmatterObject }
export default autoFrontmatterPlugin export default autoFrontmatterPlugin

View File

@ -6,8 +6,8 @@ import grayMatter from 'gray-matter'
import jsonToYaml from 'json2yaml' import jsonToYaml from 'json2yaml'
import type { import type {
AutoFrontmatterOptions, AutoFrontmatterOptions,
FormatterArray, FrontmatterArray,
FormatterObject, FrontmatterObject,
MarkdownFile, MarkdownFile,
} from '../shared/index.js' } from '../shared/index.js'
import { readMarkdown, readMarkdownList } from './readFiles.js' import { readMarkdown, readMarkdownList } from './readFiles.js'
@ -16,27 +16,27 @@ import { ensureArray, isEmptyObject } from './utils.js'
export const autoFrontmatterPlugin = ({ export const autoFrontmatterPlugin = ({
include = ['**/*.{md,MD}'], include = ['**/*.{md,MD}'],
exclude = ['.vuepress/**/*', 'node_modules'], exclude = ['.vuepress/**/*', 'node_modules'],
formatter = {}, frontmatter = {},
}: AutoFrontmatterOptions = {}): Plugin => { }: AutoFrontmatterOptions = {}): Plugin => {
include = ensureArray(include) include = ensureArray(include)
exclude = ensureArray(exclude) exclude = ensureArray(exclude)
const globFilter = createFilter(include, exclude, { resolve: false }) const globFilter = createFilter(include, exclude, { resolve: false })
const matterFormatter: FormatterArray = Array.isArray(formatter) const matterFrontmatter: FrontmatterArray = Array.isArray(frontmatter)
? formatter ? frontmatter
: [{ include: '*', formatter }] : [{ include: '*', frontmatter }]
const globFormatter: FormatterObject = const globFormatter: FrontmatterObject =
matterFormatter.find(({ include }) => include === '*')?.formatter || {} matterFrontmatter.find(({ include }) => include === '*')?.frontmatter || {}
const otherFormatters = matterFormatter const otherFormatters = matterFrontmatter
.filter(({ include }) => include !== '*') .filter(({ include }) => include !== '*')
.map(({ include, formatter }) => { .map(({ include, frontmatter }) => {
return { return {
include, include,
filter: createFilter(ensureArray(include), [], { resolve: false }), filter: createFilter(ensureArray(include), [], { resolve: false }),
formatter, frontmatter,
} }
}) })
@ -44,7 +44,7 @@ export const autoFrontmatterPlugin = ({
const { filepath, relativePath } = file const { filepath, relativePath } = file
const current = otherFormatters.find(({ filter }) => filter(relativePath)) const current = otherFormatters.find(({ filter }) => filter(relativePath))
const formatter = current?.formatter || globFormatter const formatter = current?.frontmatter || globFormatter
const { data, content } = grayMatter(file.content) const { data, content } = grayMatter(file.content)
for (const key in formatter) { for (const key in formatter) {

View File

@ -7,19 +7,19 @@ export interface MarkdownFile {
stats: fs.Stats stats: fs.Stats
} }
export type FormatterFn<T = any, K = object> = ( export type FrontmatterFn<T = any, K = object> = (
value: T, value: T,
file: MarkdownFile, file: MarkdownFile,
data: K data: K
) => T | PromiseLike<T> ) => T | PromiseLike<T>
export type FormatterObject<K = object, T = any> = { export type FrontmatterObject<K = object, T = any> = {
[P: string]: FormatterFn<T, K> [P: string]: FrontmatterFn<T, K>
} }
export type FormatterArray = { export type FrontmatterArray = {
include: string | string[] include: string | string[]
formatter: FormatterObject frontmatter: FrontmatterObject
}[] }[]
export interface AutoFrontmatterOptions { export interface AutoFrontmatterOptions {
@ -37,5 +37,5 @@ export interface AutoFrontmatterOptions {
* } * }
* } * }
*/ */
formatter?: FormatterArray | FormatterObject frontmatter?: FrontmatterArray | FrontmatterObject
} }

View File

@ -1,3 +1,5 @@
import './styles/index.scss'
import { defineClientConfig } from '@vuepress/client' import { defineClientConfig } from '@vuepress/client'
import { h } from 'vue' import { h } from 'vue'
import Badge from './components/global/Badge.vue' import Badge from './components/global/Badge.vue'
@ -5,8 +7,6 @@ import { setupDarkMode, useScrollPromise } from './composables/index.js'
import Layout from './layouts/Layout.vue' import Layout from './layouts/Layout.vue'
import NotFound from './layouts/NotFound.vue' import NotFound from './layouts/NotFound.vue'
import './styles/index.scss'
export default defineClientConfig({ export default defineClientConfig({
enhance({ app, router }) { enhance({ app, router }) {
// global component // global component

View File

@ -5,9 +5,15 @@
} }
html { html {
line-height: 1.4;
font-size: 16px; font-size: 16px;
}
body {
-webkit-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
} }
html.dark { html.dark {
@ -21,16 +27,16 @@ body {
min-height: 100vh; min-height: 100vh;
line-height: 24px; line-height: 24px;
font-family: var(--vp-font-family-base); font-family: var(--vp-font-family-base);
font-size: 16px;
font-weight: 400; font-weight: 400;
direction: ltr;
}
html,
body {
color: var(--vp-c-text-1); color: var(--vp-c-text-1);
background-color: var(--vp-c-bg); background-color: var(--vp-c-bg);
direction: ltr; transition: var(--t-color);
font-synthesis: none; transition-property: color, background-color;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transition: all 0.25s;
} }
main { main {

View File

@ -113,7 +113,7 @@
--c-brand: var(--vp-c-brand); --c-brand: var(--vp-c-brand);
} }
.dark { html.dark {
--vp-c-bg: #1e1e20; --vp-c-bg: #1e1e20;
--vp-c-bg-elv: #252529; --vp-c-bg-elv: #252529;
@ -242,7 +242,7 @@
--vp-code-tab-active-bar-color: var(--vp-c-brand); --vp-code-tab-active-bar-color: var(--vp-c-brand);
} }
.dark { html.dark {
--vp-code-block-bg: #161618; --vp-code-block-bg: #161618;
} }
@ -282,7 +282,7 @@
--vp-button-sponsor-active-bg: transparent; --vp-button-sponsor-active-bg: transparent;
} }
.dark { html.dark {
--vp-button-sponsor-border: var(--vp-c-gray-dark-1); --vp-button-sponsor-border: var(--vp-c-gray-dark-1);
--vp-button-sponsor-text: var(--vp-c-text-dark-2); --vp-button-sponsor-text: var(--vp-c-text-dark-2);
} }
@ -415,7 +415,7 @@
} }
:root { :root {
--t-color: 0.25s; --t-color: 250ms ease;
--code-tabs-nav-bg-color: var(--vp-code-tab-b); --code-tabs-nav-bg-color: var(--vp-code-tab-b);
--code-bg-color: var(--vp-code-block-bg); --code-bg-color: var(--vp-code-block-bg);
--medium-zoom-bg-color: var(--vp-c-bg); --medium-zoom-bg-color: var(--vp-c-bg);

View File

@ -4,8 +4,8 @@ import type { App } from '@vuepress/core'
import { resolveLocalePath } from '@vuepress/shared' import { resolveLocalePath } from '@vuepress/shared'
import type { import type {
AutoFrontmatterOptions, AutoFrontmatterOptions,
FormatterArray, FrontmatterArray,
FormatterObject, FrontmatterObject,
} from '@vuepress-plume/vuepress-plugin-auto-frontmatter' } from '@vuepress-plume/vuepress-plugin-auto-frontmatter'
import type { NotesItem } from '@vuepress-plume/vuepress-plugin-notes-data' import type { NotesItem } from '@vuepress-plume/vuepress-plugin-notes-data'
import { format } from 'date-fns' import { format } from 'date-fns'
@ -44,7 +44,7 @@ export default function autoFrontmatter(
}) })
.filter(Boolean) .filter(Boolean)
const baseFormatter: FormatterObject = { const baseFrontmatter: FrontmatterObject = {
author(author: string) { author(author: string) {
if (author) return author if (author) return author
return localeOption.avatar?.name || pkg.author || '' return localeOption.avatar?.name || pkg.author || ''
@ -86,21 +86,21 @@ export default function autoFrontmatter(
} }
return { return {
include: ['**/*.md'], include: ['**/*.md'],
formatter: [ frontmatter: [
localesNotesDirs.length localesNotesDirs.length
? { ? {
// note 首页链接 // note 首页链接
include: localesNotesDirs.map((dir) => include: localesNotesDirs.map((dir) =>
normalizePath(path.join(dir, '**/{readme,README,index}.md')) normalizePath(path.join(dir, '**/{readme,README,index}.md'))
), ),
formatter: { frontmatter: {
title(title: string, { filepath }) { title(title: string, { filepath }) {
if (title) return title if (title) return title
const note = findNote(filepath) const note = findNote(filepath)
if (note?.text) return note.text if (note?.text) return note.text
return getCurrentDirname(note, filepath) || '' return getCurrentDirname(note, filepath) || ''
}, },
...baseFormatter, ...baseFrontmatter,
permalink(permalink: string, { filepath }) { permalink(permalink: string, { filepath }) {
if (permalink) return permalink if (permalink) return permalink
const locale = resolveLocale(filepath) const locale = resolveLocale(filepath)
@ -121,15 +121,15 @@ export default function autoFrontmatter(
localesNotesDirs.length localesNotesDirs.length
? { ? {
include: localesNotesDirs.map((dir) => include: localesNotesDirs.map((dir) =>
normalizePath(path.join(dir, '**/**.md').replace(/\\+/g, '/')) normalizePath(path.join(dir, '**/**.md'))
), ),
formatter: { frontmatter: {
title(title: string, { filepath }) { title(title: string, { filepath }) {
if (title) return title if (title) return title
const basename = path.basename(filepath, '.md') const basename = path.basename(filepath, '.md')
return basename return basename
}, },
...baseFormatter, ...baseFrontmatter,
permalink(permalink: string, { filepath }) { permalink(permalink: string, { filepath }) {
if (permalink) return permalink if (permalink) return permalink
const locale = resolveLocale(filepath) const locale = resolveLocale(filepath)
@ -150,17 +150,17 @@ export default function autoFrontmatter(
: '', : '',
{ {
include: '**/{readme,README,index}.md', include: '**/{readme,README,index}.md',
formatter: {}, frontmatter: {},
}, },
{ {
include: '*', include: '*',
formatter: { frontmatter: {
title(title: string, { filepath }) { title(title: string, { filepath }) {
if (title) return title if (title) return title
const basename = path.basename(filepath, '.md') const basename = path.basename(filepath, '.md')
return basename return basename
}, },
...baseFormatter, ...baseFrontmatter,
permalink(permalink: string, { filepath }) { permalink(permalink: string, { filepath }) {
if (permalink) return permalink if (permalink) return permalink
const locale = resolveLocale(filepath) const locale = resolveLocale(filepath)
@ -170,6 +170,6 @@ export default function autoFrontmatter(
}, },
}, },
}, },
].filter(Boolean) as FormatterArray, ].filter(Boolean) as FrontmatterArray,
} }
} }