You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pug无法调用已安装的Highlight.js JSTransformer过滤器求助

解决Pug中highlight.js过滤器未知的问题

步骤1:安装正确的jstransformer包

你当前安装的jstransformer-highlight2不是官方维护的标准包,先卸载它,安装官方的jstransformer-highlight:

npm uninstall jstransformer-highlight2
npm install --save jstransformer-highlight

步骤2:确认过滤器名称与使用方式

根据该包的源码定义,过滤器的正确名称是highlight.js,你的Pug代码可以保持不变:

:highlight.js(lang='c')
  int f(int x) {
      return x + 1;
  }

步骤3:如果自动加载失败,手动注册过滤器

如果Pug仍无法自动识别过滤器,可手动将其注册到Pug编译器中。在你的编译脚本中添加以下代码:

const pug = require('pug');
const highlightTransformer = require('jstransformer-highlight');

// 注册highlight.js过滤器
pug.filters['highlight.js'] = (text, options) => {
  return highlightTransformer.render(text, options).body;
};

// 编译Pug模板(示例)
const template = pug.compileFile('./your-template.pug');
console.log(template());

额外检查

  • 确保你的Pug版本在v2.0及以上,旧版本不支持自动加载jstransformer过滤器。
  • 检查node_modules目录下是否存在jstransformer-highlight文件夹,确认安装成功。

内容的提问来源于stack exchange,提问作者Matheus Moreira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:23:21