diff --git a/examples/.vitepress/config.ts b/examples/.vitepress/config.ts index fbd687f..1b19bd8 100644 --- a/examples/.vitepress/config.ts +++ b/examples/.vitepress/config.ts @@ -1,10 +1,10 @@ -import { resolve } from "path"; -import { defineConfigWithTheme } from "vitepress"; -import type { Theme } from "../../src/types"; +import { resolve } from 'path' +import { defineConfigWithTheme } from 'vitepress' +import type { Theme } from '../../src/types' export default defineConfigWithTheme({ - title: "My Site", - description: "This is my site", + title: 'My Site', + description: 'This is my site', // srcDir: 'posts', // base: '/base/', cleanUrls: true, @@ -16,21 +16,21 @@ export default defineConfigWithTheme({ // alt: 'logo' // }, cover: { - src: "https://picsum.photos/1920/1080?random", - alt: "cover image", + src: 'https://picsum.photos/1920/1080?random', + alt: 'cover image', }, nav: [ - { text: "Home", link: "/" }, - { text: "Tag", link: "/tag" }, - { text: "Category", link: "/category" }, + { text: 'Home', link: '/' }, + { text: 'Tag', link: '/tag' }, + { text: 'Category', link: '/category' }, ], - tag: "/tag", - category: "/category", + tag: '/tag', + category: '/category', socialLinks: [ { - ariaLabel: "GitHub", - link: "https://github.com/tolking/vitepress-theme-ououe", - icon: "github", + ariaLabel: 'GitHub', + link: 'https://github.com/tolking/vitepress-theme-ououe', + icon: 'github', }, ], pagination: { @@ -39,12 +39,13 @@ export default defineConfigWithTheme({ // match: (path) => /^\/($|index|page-)/.test(path), // filter: (page) => page.home, }, + // excerpt: '', createTime: { - text: "Create Time", + text: 'Create Time', format: (date) => new Date(date).toLocaleDateString(), }, lastUpdated: { - text: "Last Updated", + text: 'Last Updated', format: (date) => new Date(date).toLocaleDateString(), }, // readingProgress: 'bottom', @@ -53,18 +54,18 @@ export default defineConfigWithTheme({ // { text: "Home", link: "/" }, // { text: "GitHub", link: "https://github.com/tolking/vitepress-theme-ououe" }, // ], - copyright: "copyright © 2023", + copyright: 'copyright © 2023', }, search: { - provider: "local", + provider: 'local', }, }, vite: { resolve: { alias: { - "@src": resolve(__dirname, "../../src"), + '@src': resolve(__dirname, '../../src'), }, }, }, -}); +}) diff --git a/package.json b/package.json index 240dbce..da18058 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,9 @@ ], "author": "tolking ", "license": "MIT", + "peerDependencies": { + "vitepress": "^1.0.0-beta.7" + }, "devDependencies": { "@types/node": "^20.4.4", "@typescript-eslint/eslint-plugin": "^6.2.0", @@ -32,7 +35,7 @@ "husky": "^8.0.3", "lint-staged": "^13.2.3", "prettier": "^3.0.0", - "vitepress": "1.0.0-beta.6", + "vitepress": "1.0.0-beta.7", "vue": "^3.3.4", "vue-tsc": "^1.8.6" }, diff --git a/src/posts.data.ts b/src/posts.data.ts index a381f78..835ad27 100644 --- a/src/posts.data.ts +++ b/src/posts.data.ts @@ -9,9 +9,8 @@ export { data } type GlobalThis = typeof globalThis & { VITEPRESS_CONFIG: SiteConfig } const config = (globalThis as GlobalThis).VITEPRESS_CONFIG -const pagination = - config.site.themeConfig.pagination && - toArray(config.site.themeConfig.pagination) +const themeConfig = config.site.themeConfig +const pagination = themeConfig.pagination && toArray(themeConfig.pagination) const postsDir = pagination?.reduce((all, item) => { if (Array.isArray(item.dir)) { all = all.concat(item.dir) @@ -25,7 +24,7 @@ const pattern = postsDir?.length : `${config.userConfig.srcDir || '**'}/*.md` export default createContentLoader(pattern, { - excerpt: true, + excerpt: themeConfig.excerpt ?? true, transform(raw): PostsItem[] { const posts: PostsItem[] = [] diff --git a/src/types.ts b/src/types.ts index 3aacd34..2ac5274 100644 --- a/src/types.ts +++ b/src/types.ts @@ -66,6 +66,19 @@ export interface Theme { * ``` */ pagination?: MaybeArray + /** + * If `boolean`, whether to parse and include excerpt? (rendered as HTML) + * + * If `function`, control how the excerpt is extracted from the content. + * + * If `string`, define a custom separator to be used for extracting the + * excerpt. Default separator is `---` if `excerpt` is `true`. + * + * @default true + * + * @example '' + */ + excerpt?: boolean | ExcerptFunction | string /** * Link of the tag page * @@ -230,3 +243,14 @@ export interface PaginationParams { */ limit: number } + +type ExcerptFunction = ( + file: { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data: { [key: string]: any } + content: string + excerpt?: string + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + options?: any, +) => void