Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorations API throws TypeError when decorating last index #719

Closed
5 tasks done
timothycohen opened this issue Jul 23, 2024 · 0 comments · Fixed by #728
Closed
5 tasks done

Decorations API throws TypeError when decorating last index #719

timothycohen opened this issue Jul 23, 2024 · 0 comments · Fixed by #728

Comments

@timothycohen
Copy link
Contributor

Validations

Describe the bug

Thanks for the project! I'm running into an undefined access bug preventing highlighting of the final index when using the decorations API. Looks like normalizePosition adds an invalid decoration (line is lines.length) and therefore const lineEl = lines[line]; in applyLineSection is undefined. I can submit a test and PR within the next couple days if it's still open.

Basic Reproduction

import { createHighlighter } from 'shiki';

const highlighterCore = await createHighlighter({ langs: ['ts'], themes: ['nord'] });

function highlight(str: string, start: number, end: number) {
	return highlighterCore
		.codeToHtml(str, {
			lang: 'ts',
			theme: 'nord',
			decorations: [{ start, end, properties: { ['data-wrap']: '' } }],
		})
}

// ✅ Add `data-wrap` to `.line` around `a`:
highlight(`a\nbcd`, 0, 1);

// ✅ Add a `data-wrap` span after `a` in first `.line`:
highlight(`a\nbcd`, 1, 1);

// ✅ Add `data-wrap` around `bc` in second `.line`:
highlight(`a\nbcd`, 2, 4);

// ❌ Expect to add `data-wrap` to `.line` around `bcd`, but throws TypeError
highlight(`a\nbcd`, 2, 5);

// ❌ Expect to add `data-wrap` after `bcd` in second `.line`, but throws TypeError
highlight(`a\nbcd`, 5, 5);

Error Log

file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:1022
                const children = lineEl.children.slice(startIndex, endIndex);
                                        ^

TypeError: Cannot read properties of undefined (reading 'children')
    at applyLineSection (file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:1022:41)
    at Object.code (file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:1066:21)
    at tokensToHast (file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:1215:43)
    at codeToHast (file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:1113:12)
    at codeToHtml (file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:4685:25)
    at Object.codeToHtml (file:///Users/~/shiki-error/node_modules/.pnpm/@shikijs+core@1.11.0/node_modules/@shikijs/core/dist/index.mjs:5605:40)
    at highlight (/Users/~/shiki-error/src/index.ts:9:4)
    at <anonymous> (/Users/~/shiki-error/src/index.ts:22:13)

Node.js v20.14.0

Reproduction

https://stackblitz.com/edit/stackblitz-starters-w5wlgw

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant