Skip to content

Commit

Permalink
feat: change to pass fullpath to getPostList
Browse files Browse the repository at this point in the history
  • Loading branch information
ryo-gk committed Oct 24, 2021
1 parent b114843 commit 1e5fc4d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ module.exports = {
title: 'Title',
description: 'Vitepress blog',
themeConfig: {
postList: getPostList('pages/posts'),
postList: getPostList('./docs/posts'),
},
}
```

#### - Directory
```
|- docs
|- .vitepress
|- posts
|- sample1.md
|- sample2.md
|- index.md
|- package.json
|...
```

### `usePostList()`
This can be used to get the list of blog posts.
Post has the below.
Expand Down
9 changes: 7 additions & 2 deletions src/scripts/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const { readFileSync, readdirSync } = require('fs')

function getPostList(dir: string): Post[] {
// ex) 'pages/posts'
const dirPath = `./docs/${dir}`
const dirPath = `${dir}`
const fileNames = readdirSync(`${dirPath}`, { withFileTypes: true })
.flatMap((dirent: Dirent) => {
return dirent.name
})

const post = fileNames.map((name: string) => {
const path = `${dirPath}/${name}`
const relativePath = `/${dir}/${extractFileName(name)}`
const relativePath = toRelativePath(`${dir}/${extractFileName(name)}`)
const file = readFileSync(path)
const { content, data: frontmatter } = matter(file)
return {
Expand All @@ -29,6 +29,11 @@ function extractFileName(fileName: string) {
return fileName.substr(0, fileName.lastIndexOf('.')) || fileName
}

function toRelativePath(dir: string) {
const root = '/docs'
return dir.replace(root, '')
}

module.exports = {
getPostList
}

0 comments on commit 1e5fc4d

Please sign in to comment.