Skip to content

Using transformerRenderWhitespace together with the structure "inline" option throws an error #954

@marpe

Description

@marpe

Validations

Describe the bug

Hi,

When using transformerRenderWhitespace together with the structure: "inline" setting I receive this error:

TypeError: Cannot read properties of undefined (reading 'forEach')
    at Object.root (http://localhost:3000/node_modules/.vite/deps/@shikijs_transformers.js?v=82b8e6ae:538:21)
    at tokensToHast (http://localhost:3000/node_modules/.vite/deps/chunk-EBWDM6ZB.js?v=82b8e6ae:1315:92)
    at codeToHast (http://localhost:3000/node_modules/.vite/deps/chunk-EBWDM6ZB.js?v=82b8e6ae:1188:10)
    at codeToHtml (http://localhost:3000/node_modules/.vite/deps/chunk-EBWDM6ZB.js?v=82b8e6ae:1400:27)
    at Object.codeToHtml (http://localhost:3000/node_modules/.vite/deps/chunk-EBWDM6ZB.js?v=82b8e6ae:1842:37)
    at Proxy.highlightCode (http://localhost:3000/lib/markdownHelpers.ts:89:22)
    at Proxy._sfc_render (http://localhost:3000/components/CodeBlock.vue:37:23)
    at renderComponentRoot (http://localhost:3000/node_modules/.vite/deps/chunk-IWYC5R2O.js?v=82b8e6ae:8581:17)
    at ReactiveEffect.componentUpdateFn [as fn] (http://localhost:3000/node_modules/.vite/deps/chunk-IWYC5R2O.js?v=82b8e6ae:7403:46)
    at ReactiveEffect.run (http://localhost:3000/node_modules/.vite/deps/chunk-IWYC5R2O.js?v=82b8e6ae:481:19)

Here is my test setup:

const code = `
  const foo = 'bar'
  console.log(foo)
  `;

appEl.innerHTML = await codeToHtml(code, {
  lang: 'js',
  theme: 'nord',
  structure: "inline", // try removing this
  transformers: [
    transformerRenderWhitespace(),
  ],
});

Packages:

{
  "name": "shiki-bug2",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview"
  },
  "devDependencies": {
    "typescript": "~5.7.2",
    "vite": "^6.2.0"
  },
  "dependencies": {
    "@shikijs/transformers": "^3.1.0",
    "shiki": "^3.1.0"
  }
}

Repro over here

Reproduction

https://stackblitz.com/~/github.com/marpe/shiki-structure-whitespace-bug

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions