Skip to content

Commit 7a59f41

Browse files
committed
feat: support for custom excerpt
1 parent 004a97f commit 7a59f41

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

examples/.vitepress/config.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { resolve } from "path";
2-
import { defineConfigWithTheme } from "vitepress";
3-
import type { Theme } from "../../src/types";
1+
import { resolve } from 'path'
2+
import { defineConfigWithTheme } from 'vitepress'
3+
import type { Theme } from '../../src/types'
44

55
export default defineConfigWithTheme<Theme>({
6-
title: "My Site",
7-
description: "This is my site",
6+
title: 'My Site',
7+
description: 'This is my site',
88
// srcDir: 'posts',
99
// base: '/base/',
1010
cleanUrls: true,
@@ -16,21 +16,21 @@ export default defineConfigWithTheme<Theme>({
1616
// alt: 'logo'
1717
// },
1818
cover: {
19-
src: "https://picsum.photos/1920/1080?random",
20-
alt: "cover image",
19+
src: 'https://picsum.photos/1920/1080?random',
20+
alt: 'cover image',
2121
},
2222
nav: [
23-
{ text: "Home", link: "/" },
24-
{ text: "Tag", link: "/tag" },
25-
{ text: "Category", link: "/category" },
23+
{ text: 'Home', link: '/' },
24+
{ text: 'Tag', link: '/tag' },
25+
{ text: 'Category', link: '/category' },
2626
],
27-
tag: "/tag",
28-
category: "/category",
27+
tag: '/tag',
28+
category: '/category',
2929
socialLinks: [
3030
{
31-
ariaLabel: "GitHub",
32-
link: "https://github.com/tolking/vitepress-theme-ououe",
33-
icon: "github",
31+
ariaLabel: 'GitHub',
32+
link: 'https://github.com/tolking/vitepress-theme-ououe',
33+
icon: 'github',
3434
},
3535
],
3636
pagination: {
@@ -39,12 +39,13 @@ export default defineConfigWithTheme<Theme>({
3939
// match: (path) => /^\/($|index|page-)/.test(path),
4040
// filter: (page) => page.home,
4141
},
42+
// excerpt: '<!-- more -->',
4243
createTime: {
43-
text: "Create Time",
44+
text: 'Create Time',
4445
format: (date) => new Date(date).toLocaleDateString(),
4546
},
4647
lastUpdated: {
47-
text: "Last Updated",
48+
text: 'Last Updated',
4849
format: (date) => new Date(date).toLocaleDateString(),
4950
},
5051
// readingProgress: 'bottom',
@@ -53,18 +54,18 @@ export default defineConfigWithTheme<Theme>({
5354
// { text: "Home", link: "/" },
5455
// { text: "GitHub", link: "https://github.com/tolking/vitepress-theme-ououe" },
5556
// ],
56-
copyright: "copyright © 2023",
57+
copyright: 'copyright © 2023',
5758
},
5859
search: {
59-
provider: "local",
60+
provider: 'local',
6061
},
6162
},
6263

6364
vite: {
6465
resolve: {
6566
alias: {
66-
"@src": resolve(__dirname, "../../src"),
67+
'@src': resolve(__dirname, '../../src'),
6768
},
6869
},
6970
},
70-
});
71+
})

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
],
2222
"author": "tolking <[email protected]>",
2323
"license": "MIT",
24+
"peerDependencies": {
25+
"vitepress": "^1.0.0-beta.7"
26+
},
2427
"devDependencies": {
2528
"@types/node": "^20.4.4",
2629
"@typescript-eslint/eslint-plugin": "^6.2.0",
@@ -32,7 +35,7 @@
3235
"husky": "^8.0.3",
3336
"lint-staged": "^13.2.3",
3437
"prettier": "^3.0.0",
35-
"vitepress": "1.0.0-beta.6",
38+
"vitepress": "1.0.0-beta.7",
3639
"vue": "^3.3.4",
3740
"vue-tsc": "^1.8.6"
3841
},

src/posts.data.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export { data }
99
type GlobalThis = typeof globalThis & { VITEPRESS_CONFIG: SiteConfig<Theme> }
1010

1111
const config = (globalThis as GlobalThis).VITEPRESS_CONFIG
12-
const pagination =
13-
config.site.themeConfig.pagination &&
14-
toArray(config.site.themeConfig.pagination)
12+
const themeConfig = config.site.themeConfig
13+
const pagination = themeConfig.pagination && toArray(themeConfig.pagination)
1514
const postsDir = pagination?.reduce((all, item) => {
1615
if (Array.isArray(item.dir)) {
1716
all = all.concat(item.dir)
@@ -25,7 +24,7 @@ const pattern = postsDir?.length
2524
: `${config.userConfig.srcDir || '**'}/*.md`
2625

2726
export default createContentLoader(pattern, {
28-
excerpt: true,
27+
excerpt: themeConfig.excerpt ?? true,
2928
transform(raw): PostsItem[] {
3029
const posts: PostsItem[] = []
3130

src/types.ts

+24
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ export interface Theme {
6666
* ```
6767
*/
6868
pagination?: MaybeArray<PaginationItem>
69+
/**
70+
* If `boolean`, whether to parse and include excerpt? (rendered as HTML)
71+
*
72+
* If `function`, control how the excerpt is extracted from the content.
73+
*
74+
* If `string`, define a custom separator to be used for extracting the
75+
* excerpt. Default separator is `---` if `excerpt` is `true`.
76+
*
77+
* @default true
78+
*
79+
* @example '<!-- more -->'
80+
*/
81+
excerpt?: boolean | ExcerptFunction | string
6982
/**
7083
* Link of the tag page
7184
*
@@ -230,3 +243,14 @@ export interface PaginationParams {
230243
*/
231244
limit: number
232245
}
246+
247+
type ExcerptFunction = (
248+
file: {
249+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
250+
data: { [key: string]: any }
251+
content: string
252+
excerpt?: string
253+
},
254+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
255+
options?: any,
256+
) => void

0 commit comments

Comments
 (0)