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

如何让node-pandoc报错时返回对应Markdown文件名?

解决Eleventy中node-pandoc报错时显示文件路径的问题
  • 修改自定义Markdown处理器函数,接收Eleventy自动传递的data参数,该参数包含当前处理文件的路径信息
  • 在错误处理逻辑中,通过data.page.inputPath获取文件路径,并添加到错误输出中
  • 返回占位HTML避免构建中断(可根据需求调整)

完整代码示例

const nodePandoc = require('node-pandoc');
module.exports = function(eleventyConfig) {
  async function convertMarkdownToHtml(markdown, data) {
    return new Promise((resolve) => {
      nodePandoc(markdown, '-d _data/defaults.yaml', (err, result) => {
        if (err) {
          // 控制台打印带文件路径的错误信息
          console.error(`Pandoc error in file: ${data.page.inputPath}`);
          console.error(`Error details:\n${err.message}`);
          // 返回带错误提示的HTML,避免构建中断
          return resolve(`<div style="border: 2px solid #dc2626; padding: 1rem; background: #fef2f2;">
            <h4>文件处理错误:${data.page.inputPath}</h4>
            <p>${err.message.replace(/\n/g, '<br>')}</p>
          </div>`);
        }
        resolve(result);
      });
    });
  }

  // 注册自定义Markdown处理器
  eleventyConfig.setLibrary('md', {
    render: convertMarkdownToHtml
  });
}

关键说明

  • data.page.inputPath:Eleventy自动传递的文件路径,指向当前正在处理的Markdown文件(相对项目根目录的路径)
  • 错误处理时不使用reject:确保构建流程继续运行,符合你允许轻微错误的需求
  • 页面内错误提示:返回的HTML会在生成的页面中显示错误信息,方便直观排查问题;如果不需要,可替换为返回空字符串或原Markdown内容

内容的提问来源于stack exchange,提问作者Pedro Palazzo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:16:07