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 {
AutoFrontmatterOptions,
FormatterArray,
FormatterObject,
FrontmatterArray,
FrontmatterObject,
} from '../shared/index.js'
import { autoFrontmatterPlugin } from './plugin.js'
export * from './plugin.js'
export { AutoFrontmatterOptions, FormatterArray, FormatterObject }
export { AutoFrontmatterOptions, FrontmatterArray, FrontmatterObject }
export default autoFrontmatterPlugin

View File

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

View File

@ -7,19 +7,19 @@ export interface MarkdownFile {
stats: fs.Stats
}
export type FormatterFn<T = any, K = object> = (
export type FrontmatterFn<T = any, K = object> = (
value: T,
file: MarkdownFile,
data: K
) => T | PromiseLike<T>
export type FormatterObject<K = object, T = any> = {
[P: string]: FormatterFn<T, K>
export type FrontmatterObject<K = object, T = any> = {
[P: string]: FrontmatterFn<T, K>
}
export type FormatterArray = {
export type FrontmatterArray = {
include: string | string[]
formatter: FormatterObject
frontmatter: FrontmatterObject
}[]
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 { h } from '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 NotFound from './layouts/NotFound.vue'
import './styles/index.scss'
export default defineClientConfig({
enhance({ app, router }) {
// global component

View File

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

View File

@ -113,7 +113,7 @@
--c-brand: var(--vp-c-brand);
}
.dark {
html.dark {
--vp-c-bg: #1e1e20;
--vp-c-bg-elv: #252529;
@ -242,7 +242,7 @@
--vp-code-tab-active-bar-color: var(--vp-c-brand);
}
.dark {
html.dark {
--vp-code-block-bg: #161618;
}
@ -282,7 +282,7 @@
--vp-button-sponsor-active-bg: transparent;
}
.dark {
html.dark {
--vp-button-sponsor-border: var(--vp-c-gray-dark-1);
--vp-button-sponsor-text: var(--vp-c-text-dark-2);
}
@ -415,7 +415,7 @@
}
:root {
--t-color: 0.25s;
--t-color: 250ms ease;
--code-tabs-nav-bg-color: var(--vp-code-tab-b);
--code-bg-color: var(--vp-code-block-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 type {
AutoFrontmatterOptions,
FormatterArray,
FormatterObject,
FrontmatterArray,
FrontmatterObject,
} from '@vuepress-plume/vuepress-plugin-auto-frontmatter'
import type { NotesItem } from '@vuepress-plume/vuepress-plugin-notes-data'
import { format } from 'date-fns'
@ -44,7 +44,7 @@ export default function autoFrontmatter(
})
.filter(Boolean)
const baseFormatter: FormatterObject = {
const baseFrontmatter: FrontmatterObject = {
author(author: string) {
if (author) return author
return localeOption.avatar?.name || pkg.author || ''
@ -86,21 +86,21 @@ export default function autoFrontmatter(
}
return {
include: ['**/*.md'],
formatter: [
frontmatter: [
localesNotesDirs.length
? {
// note 首页链接
include: localesNotesDirs.map((dir) =>
normalizePath(path.join(dir, '**/{readme,README,index}.md'))
),
formatter: {
frontmatter: {
title(title: string, { filepath }) {
if (title) return title
const note = findNote(filepath)
if (note?.text) return note.text
return getCurrentDirname(note, filepath) || ''
},
...baseFormatter,
...baseFrontmatter,
permalink(permalink: string, { filepath }) {
if (permalink) return permalink
const locale = resolveLocale(filepath)
@ -121,15 +121,15 @@ export default function autoFrontmatter(
localesNotesDirs.length
? {
include: localesNotesDirs.map((dir) =>
normalizePath(path.join(dir, '**/**.md').replace(/\\+/g, '/'))
normalizePath(path.join(dir, '**/**.md'))
),
formatter: {
frontmatter: {
title(title: string, { filepath }) {
if (title) return title
const basename = path.basename(filepath, '.md')
return basename
},
...baseFormatter,
...baseFrontmatter,
permalink(permalink: string, { filepath }) {
if (permalink) return permalink
const locale = resolveLocale(filepath)
@ -150,17 +150,17 @@ export default function autoFrontmatter(
: '',
{
include: '**/{readme,README,index}.md',
formatter: {},
frontmatter: {},
},
{
include: '*',
formatter: {
frontmatter: {
title(title: string, { filepath }) {
if (title) return title
const basename = path.basename(filepath, '.md')
return basename
},
...baseFormatter,
...baseFrontmatter,
permalink(permalink: string, { filepath }) {
if (permalink) return permalink
const locale = resolveLocale(filepath)
@ -170,6 +170,6 @@ export default function autoFrontmatter(
},
},
},
].filter(Boolean) as FormatterArray,
].filter(Boolean) as FrontmatterArray,
}
}