使用unified处理Markdown时遇Cannot `process` without `Compiler`错误求助
解决Unified转换Markdown到HTML时的"Cannot
process without Compiler"错误 问题根源
代码中重复导入了remarkParse模块,导致ES模块导入冲突,破坏了Unified的解析器配置,进而触发"缺少Compiler"的错误——因为解析器未正确加载,后续流程无法找到对应的编译环节。另外,要正确解析代码里的GFM表格,还需要补充对应的插件支持。
修正后的代码
import remarkParse from 'remark-parse' import remarkGfm from 'remark-gfm' import remarkRehype from 'remark-rehype' import rehypeSanitize from 'rehype-sanitize' import rehypeStringify from "rehype-stringify" import {unified} from 'unified' const markdownTable = `Here is a 200+ word response providing insights on the search query and suggested follow-up questions: India and China are two of the world's largest producers of cement. Cement production is an important economic indicator, as the construction industry is a major driver of the economy in both countries. Tracking cement production over time can provide insights into infrastructure development, housing construction, and overall economic growth. A table comparing India and China's cement production over the last 5 years would look something like this: | Year | India (million tonnes) | China (million tonnes) | |-|-|-| | 2019 | 337 | 2,200 | | 2018 | 322 | 2,130 | | 2017 | 297 | 2,410 | | 2016 | 283 | 2,350 | | 2015 | 270 | 2,500 | The table shows that China's cement production far exceeds India's, which is expected given the size of its economy. However, India's cement production has been steadily increasing over the past 5 years, reflecting its rapid infrastructure and housing growth. China's cement production peaked in 2014 and has slightly declined since then as its economy has matured. ### India vs China ` const file = await unified() .use(remarkParse) .use(remarkGfm) // 启用GFM支持,解析表格等元素 .use(remarkRehype) .use(rehypeSanitize) .use(rehypeStringify) .process(markdownTable) console.log("html(remarkParse):"); console.log(String(file))
关键修正点
- 删除重复的
import remarkParse语句,确保模块仅导入一次 - 添加
remarkGfm插件,保证GFM格式(如表格)能被正确转换为HTML - 确认所有依赖已安装,可执行以下命令安装:
npm install unified remark-parse remark-gfm remark-rehype rehype-sanitize rehype-stringify
内容的提问来源于stack exchange,提问作者ishandutta2007
相关产品推荐
相关产品推荐

