ATQQ / sugar-blog

✍️📚我写博客的地方🤪🤪🤪记录随笔与学习笔记,仓库包含:博客内容,博客主题、博客模板、vitepress离线全文搜索插件、VitePress RSS 支持插件;a blog theme use vitepress
https://sugarat.top
MIT License
308 stars 56 forks source link

vitepress-plugin-pagefind: v0.4.4 broke dynamic route pages #264

Closed slusarz closed 1 week ago

slusarz commented 2 weeks ago

Pages created via dynamic routes (https://vitepress.dev/guide/routing#dynamic-routes) are broken with v0.4.4.

Problem is that new frontmatter code that looks for index exclusion config tries to load non-existent source files.

Example:

build error:
[vitepress-plugin-pagefind] ENOENT: no such file or directory, open '/home/foo/docs/release_notes/3.x/3.0.0.md'

This file is auto generated by vitepress from the /home/foo/docs/release_notes/3.x/[version].md and /home/foo/docs/release_notes/3.x/[version].paths.js files.

Possible solution (verified working): wrap load code in try/catch, and ignore file if it doesn't exist in source.

From https://github.com/ATQQ/sugar-blog/blob/master/packages/vitepress-plugin-pagefind/src/index.ts#L92:

if (id.endsWith('.md')) {
  let fileContent
  try {
    fileContent = await fs.promises.readFile(id, 'utf-8')
  } catch (err) {
    if (err.code === 'ENOENT') {
      return code
    }
    throw(err)
  }
  const { data: frontmatter, content } = grayMatter(fileContent, {
    excerpt: true
  })
ATQQ commented 2 weeks ago

Understood the issue. I plan to refactor this part later as there are indeed quite a few problems at the moment.

ATQQ commented 1 week ago

you can try vitepress-plugin-pagefind@0.4.7

ATQQ commented 1 week ago

now latest is vitepress-plugin-pagefind@0.4.9

I'm sure it's compatible with the use of dynamic routing