Skip to content

Commit

Permalink
feat: support for custom excerpt
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking committed Aug 6, 2023
1 parent 004a97f commit 7a59f41
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
43 changes: 22 additions & 21 deletions examples/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -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<Theme>({
title: "My Site",
description: "This is my site",
title: 'My Site',
description: 'This is my site',
// srcDir: 'posts',
// base: '/base/',
cleanUrls: true,
Expand All @@ -16,21 +16,21 @@ export default defineConfigWithTheme<Theme>({
// 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: {
Expand All @@ -39,12 +39,13 @@ export default defineConfigWithTheme<Theme>({
// match: (path) => /^\/($|index|page-)/.test(path),
// filter: (page) => page.home,
},
// excerpt: '<!-- more -->',
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',
Expand All @@ -53,18 +54,18 @@ export default defineConfigWithTheme<Theme>({
// { 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'),
},
},
},
});
})
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
],
"author": "tolking <[email protected]>",
"license": "MIT",
"peerDependencies": {
"vitepress": "^1.0.0-beta.7"
},
"devDependencies": {
"@types/node": "^20.4.4",
"@typescript-eslint/eslint-plugin": "^6.2.0",
Expand All @@ -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"
},
Expand Down
7 changes: 3 additions & 4 deletions src/posts.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ export { data }
type GlobalThis = typeof globalThis & { VITEPRESS_CONFIG: SiteConfig<Theme> }

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)
Expand All @@ -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[] = []

Expand Down
24 changes: 24 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ export interface Theme {
* ```
*/
pagination?: MaybeArray<PaginationItem>
/**
* 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 '<!-- more -->'
*/
excerpt?: boolean | ExcerptFunction | string
/**
* Link of the tag page
*
Expand Down Expand Up @@ -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

0 comments on commit 7a59f41

Please sign in to comment.