本文介绍: 最近忙于使用VuePress搭建个人知识库发现好多代码无法高亮显示,根据官方文档提示改用。又因为这个只是自己本地项目,所以就打算直接修改他的代码。时会遇到一个问题,即对无法识别语言报错(如下边的提示,说找不到component组件。如果您有更好方法,请在评论留言

问题描述

最近忙于使用VuePress搭建个人知识库发现好多代码无法高亮显示,根据官方文档提示改用Shiki插件

"@vuepress/plugin-shiki": "2.0.0-rc.0",

但是在使用Shiki时会遇到一个小问题,即对无法识别的语言报错(Error: No language registration for xxxxx)。

如下边的提示,说找不到component组件

Error: No language registration for component
    at getGrammar (F:codefrontblognode_modules.pnpmshiki@0.14.5node_modulesshikidistindex.js:3170:13)
    at codeToThemedTokens (F:codefrontblognode_modules.pnpmshiki@0.14.5node_modulesshikidistindex.js:3182:26)
    at Object.codeToHtml (F:codefrontblognode_modules.pnpmshiki@0.14.5node_modulesshikidistindex.js:3204:20)
    at md.options.highlight (file:///F:/code/front/blog/node_modules/.pnpm/@vuepress+plugin-shiki@2.0.0-rc.0/node_modules/@vuepress/plugin-shiki/lib/node/shikiPlugin.js:9:60)
    at md.renderer.rules.fence (file:///F:/code/front/blog/node_modules/.pnpm/@vuepress+markdown@2.0.0-rc.0/node_modules/@vuepress/markdown/dist/index.js:248:37)
    at Renderer.render (F:codefrontblognode_modules.pnpmmarkdown-it@13.0.2node_modulesmarkdown-itlibrenderer.js:332:28)
    at md.renderer.render (file:///F:/code/front/blog/node_modules/.pnpm/@mdit-vue+plugin-headers@1.0.0/node_modules/@mdit-vue/plugin-headers/dist/index.mjs:19:12)
    at md.renderer.render (file:///F:/code/front/blog/node_modules/.pnpm/@mdit-vue+plugin-title@1.0.0/node_modules/@mdit-vue/plugin-title/dist/index.mjs:11:12)
    at MarkdownIt.render (F:codefrontblognode_modules.pnpmmarkdown-it@13.0.2node_modulesmarkdown-itlibindex.js:544:24)
    at md.render (file:///F:/code/front/blog/node_modules/.pnpm/@mdit-vue+plugin-frontmatter@1.0.0/node_modules/@mdit-vue/plugin-frontmatter/dist/index.mjs:22:12)

这就比较坑了,原本在typora或其他md平台支持的语言在这里不仅不支持,而且编译都不通过。想要通过编译简单方法就是将“`后边的语言去掉,但是去掉之后就不能在其他平台使用了,所以需求产生了!

需求 在不更改原文本的前提下,基于Shiki,保证其他语言的兼容性

又因为这个只是自己本地项目,所以就打算直接修改他的代码。

源代码位置node_modules.pnpmshiki@0.14.5node_modulesshikidist 第3167行

  function getGrammar(lang) {
    const _grammar = _registry.getGrammar(lang);
    if (!_grammar) {
   	// 如果_grammar存在直接给我一个warning提示就行,不要抛出异常
      // throw Error(`No language registration for ${lang}`);
      console.log(`Warning:No language registration for ${lang}`);
    }
    return { _grammar };
  }
  function codeToThemedTokens(code, lang = "text", theme, options2 = { includeExplanation: true }) {
    
    const {_grammar} = getGrammar(lang);
    // 如果_grammar不存在,即不支持该语言,就以Plaintext的形式展示
    if (isPlaintext(lang) || !_grammar) {
      const lines = code.split(/rn|r|n/);
      return [...lines.map((line) => [{ content: line }])];
    }
    const { _theme, _colorMap } = getTheme(theme);
    return tokenizeWithTheme(_theme, _colorMap, code, _grammar, options2);
  }

如果您有更好方法,请在评论区留言!

原文地址:https://blog.csdn.net/m0_38072683/article/details/134750455

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_44320.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注