From 1e5fc4d32de95606ec9496464f97d17f9fec4015 Mon Sep 17 00:00:00 2001 From: ryo-gk Date: Sun, 24 Oct 2021 16:21:07 +0900 Subject: [PATCH] feat: change to pass fullpath to getPostList --- README.md | 14 +++++++++++++- src/scripts/post.ts | 9 +++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 67b0038..7f106fd 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/scripts/post.ts b/src/scripts/post.ts index d0a193c..d20b2b2 100644 --- a/src/scripts/post.ts +++ b/src/scripts/post.ts @@ -5,7 +5,7 @@ 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 @@ -13,7 +13,7 @@ function getPostList(dir: string): Post[] { 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 { @@ -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 } \ No newline at end of file