ATQQ / sugar-blog

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

超大vitepress 项目下,pagefind 插件不能正常工作 #226

Closed ykla closed 3 months ago

ykla commented 3 months ago
- building client + server bundles...
🎈 SUMMARY 解析中...
🎈 SUMMARY 解析完成...
x Build failed in 1m 42s
✖ building client + server bundles...
build error:
Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(vitepress-plugin-pagefind) load "\u0000virtual:pagefind"
Error: Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(vitepress-plugin-pagefind) load "\u0000virtual:pagefind"
    at process.handleBeforeExit (file:///home/runner/work/taophilosophy.github.io/taophilosophy.github.io/node_modules/.pnpm/rollup@4.[17](https://github.com/taophilosophy/taophilosophy.github.io/actions/runs/9171633579/job/25216376212#step:10:18).2/node_modules/rollup/dist/es/shared/node-entry.js:20115:28)
    at Object.onceWrapper (node:events:634:26)
    at process.emit (node:events:5[19](https://github.com/taophilosophy/taophilosophy.github.io/actions/runs/9171633579/job/25216376212#step:10:20):28)
 ELIFECYCLE  Command failed with exit code 1.
Error: Process completed with exit code 1.

请问应该如何做才能看到具体的错误信息?项目地址 https://github.com/taophilosophy/taophilosophy.github.io。奇怪的是本地按同样版本的软件配置就会无限卡住,内存 CPU 都没有变化,只有 git action 会抛出上述错误。

Git action 见 https://github.com/taophilosophy/taophilosophy.github.io/blob/main/.github/workflows/deploy.yml

找了找只看到了 https://github.com/vitejs/vite/issues/13367

config.mts :


import {
    defineConfig
} from 'vitepress';
import autoNav from "vite-plugin-vitepress-auto-nav";
import footnote from 'markdown-it-footnote';
import mathjax3 from 'markdown-it-mathjax3';
import taskLists from 'markdown-it-task-checkbox';
import { chineseSearchOptimize, pagefindPlugin } from 'vitepress-plugin-pagefind';

export default defineConfig({

    build: {
        chunkSizeWarningLimit: 1600
    },
    sitemap: {
        hostname: 'https://taophilosophy.github.io',
    },
    lang: 'zh-CN',
    lastUpdated: true,
    title: "斯坦福哲学百科全书简体中文版",
    description: "斯坦福哲学百科全书",
    metaChunk: true,
    markdown: {
        math: true,
        image: {
            lazyLoading: true
        },
        config(md) {
            const defaultCodeInline = md.renderer.rules.code_inline!
                md.renderer.rules.code_inline = (tokens, idx, options, env, self) => {
                    tokens[idx].attrSet('v-pre', '')
                    return defaultCodeInline(tokens, idx, options, env, self);
                    md.use(footnote);
                    md.use(mathjax3);
                    md.use(taskLists, {
                        disabled: true,
                        divWrap: false,
                        divClass: 'checkbox',
                        idPrefix: 'cbx_',
                        ulClass: 'task-list',
                        liClass: 'task-list-item',
                    });
                }
        },
    },
    head: [
        ['link', {
            rel: 'icon',
            href: '/favicon.ico'
        }],
        ['meta', {
            name: 'keywords',
            content: ''
        }],
        [
            'script',
            {
                async: '',
                src: 'https://www.googletagmanager.com/gtag/js?id=TAG_ID'
            }
        ],
        [
            'script',
            {},
            `window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'TAG_ID');`
        ],
    ],
    rewrites: {
        'README.md': 'index.md',
    },
    themeConfig: {
        siteTitle: '斯坦福哲学百科全书',
        langMenuLabel: '多语言',
        returnToTopLabel: '回到顶部',
        darkModeSwitchLabel: '主题',
        lightModeSwitchTitle: '切换到浅色模式',
        darkModeSwitchTitle: '切换到深色模式',

        docFooter: {
            prev: '上一页',
            next: '下一页'
        },

        logo: {
            src: '/logo.svg',
            width: 24,
            height: 24
        },
        nav: [{
                text: '斯坦福哲学百科全书简体中文版-分类目录',
                link: 'mu-lu-s.md'
            },
            {
                text: '原版目录(A-Z)',
                link: 'SUMMARY.md'
            },
        ],
        base: '/',
        editLink: {
            text: '在 GitHub 上编辑此页面',
            pattern: 'https://github.com/taophilosophy/SEP-CN/edit/main/:path'
        },
        outline: {
            label: '此页目录',
            level: 'deep'
        },
        sidebarMenuLabel: '目录',
        externalLinkIcon: true,
        lastUpdated: {
            text: '最后更新于',
            formatOptions: {
                dateStyle: 'short',
                timeStyle: 'medium'
            }
        },
    },

    vite: {
        plugins: [pagefindPlugin({
                customSearchQuery: chineseSearchOptimize,
                btnPlaceholder: '搜索',
                placeholder: '搜索文档',
                emptyText: '空空如也',
                heading: '共: {{searchResult}} 条结果',
                excludeSelector: ['img', 'a.header-anchor'],
            } ),
            autoNav({
                summary: {
                    target: "doc/mu-lu-s.md",
                    collapsed: false,
                }
            })
        ],
    },
})
ATQQ commented 3 months ago

收到我看一下

ATQQ commented 3 months ago

是单独使用 这个插件遇到的问题吗?

vitepress-plugin-pagefind

ykla commented 3 months ago
{
  "type": "module",
  "license": "BSD-3-Clause",
  "dependencies": {
    "vitepress": "1.2.0",
    "vite-plugin-vitepress-auto-nav": "2.3.2",
    "markdown-it-footnote": "4.0.0",
    "markdown-it-mathjax3": "4.3.2",
    "markdown-it-task-checkbox": "1.0.6",
    "vitepress-plugin-pagefind": "0.3.0",
    "pagefind": "1.1.0"
  },
  "scripts": {
    "docs:dev": "vitepress dev doc",
    "docs:build": "vitepress build doc",
    "docs:preview": "vitepress preview doc"
  }
}

基本上是,我的文本量比较大,一开始使用 vitepress 官方自带插件可编译成功,但是无法使用,点击搜索不起作用。我已经在环境变量指定了 --max-old-space-size 参数,否则会 oom。再去掉 vitepress-plugin-pagefindpagefind 后可正常编译运行。

vite-plugin-vitepress-auto-nav 是自动生成侧边栏的插件。见 https://github.com/Xaviw/vite-plugin-vitepress-auto-nav

ykla commented 3 months ago

本地无法编译的原因找到了,https://cn.vitejs.dev/guide/troubleshooting.html#requests-are-stalled-forever

ATQQ commented 3 months ago

本地无法编译的原因找到了,https://cn.vitejs.dev/guide/troubleshooting.html#requests-are-stalled-forever

修改后有解决你的问题吗。

ykla commented 3 months ago

本地无法编译的原因找到了,https://cn.vitejs.dev/guide/troubleshooting.html#requests-are-stalled-forever

修改后有解决你的问题吗。

未解决,因为这个只是本地无法编译(默认文件系统有限制)。GitHub Action 一直可以编译并且会有报错。

ATQQ commented 3 months ago

OK 我记一下,晚点拿你那个仓库我试一下

ATQQ commented 3 months ago
image

我也遇到了 ,可能是写bug 了。

ykla commented 3 months ago

可能不是这样,这个项目对于 vitepress 似乎看起来有些大了。即使是可能只有2000多个页面。我试过其他的官方文档的其他插件,都存在这些问题。他们是能编译出来,也有搜索框,但是搜索完全没反应。我一直在寻找优化的办法,但是还没有找到。如果你在编译的时候卡住了这可能是正常的(按照上面的链接解决文件系统的限制后),因为这个我本地 build 就一直过不去。CPU 会长时间 100%,可能是系统有问题,但是我使用的系统也是和 GitHub Action 一样的 24.04,各种软件版本也是一致的。但是 GitHub action 就能成功编译。

ATQQ commented 3 months ago

辛苦你使用最新的 vitepress-plugin-pagefind@0.3.3 再试试嘞,周末我更新了一下,解决了一那个构建阻塞的问题。

ykla commented 3 months ago

辛苦你使用最新的 vitepress-plugin-pagefind@0.3.3 再试试嘞,周末我更新了一下,解决了一那个构建阻塞的问题。

现在可以编译了,见https://taophilosophy.github.io/。但是所有搜索结果只有一条:#只有在构建后才生效 only support after build, only support after support,目前 GitHub action 使用的已经是 build 了,辛苦再看看。

ATQQ commented 3 months ago

辛苦你使用最新的 vitepress-plugin-pagefind@0.3.3 再试试嘞,周末我更新了一下,解决了一那个构建阻塞的问题。

现在可以编译了,见https://taophilosophy.github.io/。但是所有搜索结果只有一条:#只有在构建后才生效 only support after build, only support after support,目前 GitHub action 使用的已经是 build 了,辛苦再看看。

我看了一下日志发现没有如预期的执行 pagefind,你看看在项目里把 pagefind 依赖装上,然后修改一下构建指令

① 装依赖

pnpm add pagefind

② 构建指令添加索引生成

{
  "scripts": {
    "docs:build": "vitepress build docs && npx pagefind --site docs/.vitepress/dist"
  }
}

③插件配置改一下

pagefindPlugin({searchOptimization:false})
ykla commented 3 months ago

辛苦你使用最新的 vitepress-plugin-pagefind@0.3.3 再试试嘞,周末我更新了一下,解决了一那个构建阻塞的问题。

现在可以编译了,见https://taophilosophy.github.io/。但是所有搜索结果只有一条:#只有在构建后才生效 only support after build, only support after support,目前 GitHub action 使用的已经是 build 了,辛苦再看看。

我看了一下日志发现没有如预期的执行 pagefind,你看看在项目里把 pagefind 依赖装上,然后修改一下构建指令

① 装依赖

pnpm add pagefind

② 构建指令添加索引生成

{
  "scripts": {
    "docs:build": "vitepress build docs && npx pagefind --site docs/.vitepress/dist"
  }
}

③插件配置改一下

pagefindPlugin({searchOptimization:false})

在配置后,生成的 pc 端页面搜索框无法点击。移动端可以点击,搜索结果是 #只有在构建后才生效 only support after build, only support after support。已经去掉了我自己配置的多余的 CSS。麻烦有时间再看看。非常感谢你。

ATQQ commented 3 months ago

@ykla 迭代了一版,有空你再试试看

vitepress-plugin-pagefind@0.4.0

ykla commented 3 months ago

你好,本地测了一下,PC 界面无法点击搜框索。移动端可以点击,搜索结果是 #只有在构建后才生效 only support after build, only support after support。

A5 ZS@E~AB8W8$}7X3LHXP6

BX A~($A}B}S{}%DBU(93Q

{
  "type": "module",
  "license": "BSD-3-Clause",
  "dependencies": {
    "vitepress": "1.2.2",
    "vite-plugin-vitepress-auto-nav": "2.3.2",
    "markdown-it-footnote": "4.0.0",
    "markdown-it-mathjax3-tao": "4.3.2",
    "pagefind": "1.1.0",
    "markdown-it-task-checkbox": "1.0.6",
    "vitepress-plugin-pagefind": "0.4.0"
  },
   "trustedDependencies": [
    "markdown-it-mathjax3-tao",
    "vitepress-plugin-pagefind",
    "esbuild",
    "vue-demi"
  ],
  "scripts": {
    "docs:dev": "vitepress dev docs",
    "docs:build": "vitepress build docs && pagefind --site docs/.vitepress/dist",
    "docs:preview": "vitepress preview docs",
    "docs:serve": "vitepress serve docs --port 8080"
  }
}
ykla@ykla-ubuntu:~/taophilosophy.github.io$ bun install
bun install v1.1.12-canary.1 (43f0913c)

+ markdown-it-footnote@4.0.0
+ markdown-it-mathjax3-tao@4.3.2
+ markdown-it-task-checkbox@1.0.6
+ pagefind@1.1.0
+ vite-plugin-vitepress-auto-nav@2.3.2
+ vitepress@1.2.2
+ vitepress-plugin-pagefind@0.4.0

167 packages installed [2.99s]
ykla@ykla-ubuntu:~/taophilosophy.github.io$ 
ykla@ykla-ubuntu:~/taophilosophy.github.io$ bun --bun run docs:build
$ vitepress build docs && pagefind --site docs/.vitepress/dist

  vitepress v1.2.2

⠋ building client + server bundles...🎈 SUMMARY 解析中...
🎈 SUMMARY 解析完成...
⠸ building client + server bundles...🎈 SUMMARY 解析中...
🎈 SUMMARY 解析完成...
✓ building client + server bundles...
✓ rendering pages...
✓ generating sitemap...
build complete in 589.29s.

Running Pagefind v1.1.0 (Extended)
Running from: "/home/ykla/taophilosophy.github.io"
Source:       "docs/.vitepress/dist"
Output:       "docs/.vitepress/dist/pagefind"

[Walking source directory]
Found 2620 files matching **/*.{html}

[Parsing files]
Found a data-pagefind-body element on the site.
↳ Ignoring pages without this tag.

[Reading languages]
Discovered 1 language: zh-cn

[Building search indexes]
Total: 
  Indexed 1 language
  Indexed 2619 pages
  Indexed 232865 words
  Indexed 0 filters
  Indexed 0 sorts

Finished in 343.737 seconds
ykla@ykla-ubuntu:~/taophilosophy.github.io$ bun --bun run docs:serve
$ vitepress serve docs --port 8080

  vitepress v1.2.2

Built site served at http://localhost:8080/
^C
ykla@ykla-ubuntu:~/taophilosophy.github.io$ 
ATQQ commented 3 months ago

好滴,我看你截图里的日志 索引文件时正常生成了的 我看一下是不是哪块配置没对上

ATQQ commented 3 months ago
image

你看看你的构建产物里有这个目录吗?

ykla commented 3 months ago
image

你看看你的构建产物里有这个目录吗?

你好,有的。

ykla@ykla-ubuntu:~/taophilosophy.github.io/docs/.vitepress/dist$ ls
404.html              e             index.html    o            SUMMARY.html
a                     f             j             p            t
assets                favicon.ico   k             pagefind     u
b                     g             l             q            v
c                     guan-yu       logo.svg      qian-yan     w
CODE_OF_CONDUCT.html  h             m             r            x
CONTRIBUTING.html     hashmap.json  mu-lu-s.html  s            y
d                     i             n             sitemap.xml  z
ykla@ykla-ubuntu:~/taophilosophy.github.io/docs/.vitepress/dist$ du -h pagefind/
68M pagefind/fragment
70M pagefind/index
137M    pagefind/
ATQQ commented 3 months ago

我估计是插件的 transformHead 钩子没有被执行.

你手动配一下可以,配置如下

import { defineConfig } from 'vitepress'

export default defineConfig({
  head: [
    [
      'script',
      {},
      `import('/pagefind/pagefind.js')
        .then((module) => {
          window.__pagefind__ = module
          module.init()
        })
        .catch(() => {
          // console.log('not load /pagefind/pagefind.js')
        })`
    ]
  ]
})
ykla commented 3 months ago

经过测试加上后可以搜索了。非常感谢。

3BSF3(P4`4CFQ{N$ EM%Y%7

RVR2`X~4_3XOA@45F)S2M00

ATQQ commented 3 months ago

回头这块 我出一个 SOP,针对插件内部分能力失效的地方

ATQQ commented 3 months ago

又迭代了一版本 vitepress-plugin-pagefind@0.4.1

有空你可以试试能不能单独使用,

如果还是不能正常工作 你就可以按照现在的方式使用我也补到文档里了 示例 7