fluid-dev / hexo-theme-fluid

:ocean: 一款 Material Design 风格的 Hexo 主题 / An elegant Material-Design theme for Hexo
https://hexo.fluid-dev.com/
GNU General Public License v3.0
7.24k stars 1.12k forks source link

标题问题 #1061

Closed kamaitaching closed 3 months ago

kamaitaching commented 7 months ago

使用一级标题写的md文件,生成之后,在页面目录点击一级标题自动跳转到文章页首了,怎么才能让他跳转到指定页面

jinghu-moon commented 6 months ago

如果你参考主题配置文档 「LaTeX 数学公式」部分,将公式引擎更换为 katex,并执行了以下代码。

npm uninstall hexo-renderer-marked --save
npm install hexo-renderer-markdown-it --save
npm install @traptitech/markdown-it-katex --save

你只需要将上述代码反过来执行,如下:

npm install hexo-renderer-marked --save
npm uninstall hexo-renderer-markdown-it --save
npm uninstall @traptitech/markdown-it-katex --save

然后就可以在页面目录点击任意一级标题时,跳转到对应位置。

至于公式引擎可以使用 mathjax。

milong26 commented 6 months ago

+1我也有这个问题,用的是新hexo库,我也用了katex,不太希望换成mathjax f12检查了一下,目录超链接都是href="#"

jinghu-moon commented 6 months ago

补充一下,我的博客 package.json 如下:

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "7.0.0"
  },
  "dependencies": {
    "@traptitech/markdown-it-katex": "^3.6.0",
    "hexo": "^7.0.0",
    "hexo-abbrlink": "^2.2.1",
    "hexo-browsersync": "^0.3.0",
    "hexo-deployer-git": "^4.0.0",
    "hexo-filter-nofollow": "^2.0.2",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-index": "^3.0.0",
    "hexo-generator-tag": "^2.0.0",
    "hexo-hash": "^1.0.4",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-markdown-it": "^7.1.0",
    "hexo-renderer-stylus": "^3.0.0",
    "hexo-server": "^3.0.0",
    "hexo-theme-landscape": "^1.0.0"
  },
  "devDependencies": {
    "gulp": "^4.0.2",
    "gulp-clean-css": "^4.3.0",
    "gulp-html-minifier-terser": "^7.1.0",
    "gulp-run-command": "^0.0.10",
    "gulp-terser": "^2.1.0"
  }
}
Nanchengjiumeng123 commented 5 months ago

确实是这个问题,可以可以额,解决了;

HPCesia commented 1 month ago

不换回 mathjax,继续用 KaTeX 一样可以解决问题。标题不能跳转是因为 hexo-markdown-it 插件生成的 <h1> 标签里没有 id 属性,导致访问网页时 tocbot 读取不到标题内容,生成的右侧目录里一级标题会跳转到页面顶部。文章渲染完以后用 filter 加上就行。

用GBT生成的修复bug的代码,实测可用,放到 scripts 目录下面就行:

hexo.extend.filter.register('after_post_render', (post) => {
    if (post.content) {
        const uniqueIdStore = {};
        post.content = post.content.replace(/<h1>(.*?)<\/h1>/g, function (match, p1) {
            const cleanId = p1.trim().toLowerCase()
                .replace(/\s+/g, '-')
                .replace(/[?#&]/g, '');

            let uniqueId = cleanId;
            if (cleanId === '') {
                uniqueId = 'default';
            }
            if (uniqueIdStore[cleanId]) {
                uniqueId = `${cleanId}-${uniqueIdStore[cleanId]}`;
                uniqueIdStore[cleanId] += 1;
            } else {
                uniqueIdStore[cleanId] = 1;
            }
            if (!/<h1 id=".*?">/.test(match)) {
                return `<h1 id="${uniqueId}">${p1}</h1>`;
            }
            return match;
        });
    }
});