|
| 1 | +import type { ThemedToken } from 'shiki' |
1 | 2 | import type { ShikiStreamTokenizerOptions } from '../src' |
2 | 3 | import { createHighlighter } from 'shiki' |
3 | 4 | import { expect, it } from 'vitest' |
@@ -64,3 +65,113 @@ it('exported', async () => { |
64 | 65 |
|
65 | 66 | expect.soft(merged).toBe(onepass) |
66 | 67 | }) |
| 68 | + |
| 69 | +const tokensOptions = { |
| 70 | + lang: 'vue', |
| 71 | + themes: { |
| 72 | + dark: 'vitesse-dark', |
| 73 | + light: 'vitesse-light', |
| 74 | + }, |
| 75 | +} as const |
| 76 | + |
| 77 | +async function tokenizeInChunks(options: ShikiStreamTokenizerOptions, code: string, size: number): Promise<ThemedToken[]> { |
| 78 | + const streamer = new ShikiStreamTokenizer(options) |
| 79 | + for (let i = 0; i < code.length; i += size) |
| 80 | + await streamer.enqueue(code.slice(i, i + size)) |
| 81 | + return [...streamer.tokensStable, ...streamer.close().stable] |
| 82 | +} |
| 83 | + |
| 84 | +// The streamed tokens, apart from the inserted `\n` ones, match the one-pass tokens, |
| 85 | +// every token's offset points at its content in the code, and each inserted `\n` sits at its own LF |
| 86 | +function expectSameTokens(options: ShikiStreamTokenizerOptions, code: string, tokens: ThemedToken[]): void { |
| 87 | + expect(tokens.filter(token => token.content !== '\n')).toEqual(options.highlighter.codeToTokens(code, options).tokens.flat()) |
| 88 | + for (const token of tokens) |
| 89 | + expect(code.slice(token.offset, token.offset + token.content.length)).toBe(token.content) |
| 90 | + expect(tokens.filter(token => token.content === '\n').map(token => token.offset)) |
| 91 | + .toEqual([...code.matchAll(/\n/g)].map(match => match.index)) |
| 92 | +} |
| 93 | + |
| 94 | +it('handles CRLF line endings like codeToTokens', async () => { |
| 95 | + const highlighter = await createHighlighter({ |
| 96 | + langs: ['vue'], |
| 97 | + themes: ['vitesse-dark', 'vitesse-light'], |
| 98 | + }) |
| 99 | + const code = fixture.trimEnd().replaceAll('\n', '\r\n') |
| 100 | + const onepass = tokensToHtml( |
| 101 | + highlighter.codeToTokens(code, tokensOptions) |
| 102 | + .tokens |
| 103 | + .flatMap((i, idx) => idx === 0 ? i : [{ content: '\n', offset: 0 }, ...i]), |
| 104 | + ) |
| 105 | + |
| 106 | + // A chunk size of 1 splits every `\r\n` across two chunks |
| 107 | + for (const size of [1, 5, code.length]) { |
| 108 | + const tokens = await tokenizeInChunks({ highlighter, ...tokensOptions }, code, size) |
| 109 | + expect(tokens.filter(token => token.content.includes('\r'))).toEqual([]) |
| 110 | + expect(tokensToHtml(tokens)).toBe(onepass) |
| 111 | + } |
| 112 | +}) |
| 113 | + |
| 114 | +it('reports token offsets relative to the whole code', async () => { |
| 115 | + const highlighter = await createHighlighter({ |
| 116 | + langs: ['vue'], |
| 117 | + themes: ['vitesse-dark', 'vitesse-light'], |
| 118 | + }) |
| 119 | + |
| 120 | + const options: ShikiStreamTokenizerOptions = { highlighter, ...tokensOptions } |
| 121 | + |
| 122 | + for (const code of [fixture.trimEnd(), fixture.trimEnd().replaceAll('\n', '\r\n')]) { |
| 123 | + for (const size of [1, 5, code.length]) |
| 124 | + expectSameTokens(options, code, await tokenizeInChunks(options, code, size)) |
| 125 | + } |
| 126 | +}) |
| 127 | + |
| 128 | +it('reports whole-code offsets after every chunk', async () => { |
| 129 | + const highlighter = await createHighlighter({ |
| 130 | + langs: ['ts'], |
| 131 | + themes: ['vitesse-light'], |
| 132 | + }) |
| 133 | + const options: ShikiStreamTokenizerOptions = { highlighter, lang: 'ts', theme: 'vitesse-light' } |
| 134 | + const streamer = new ShikiStreamTokenizer(options) |
| 135 | + |
| 136 | + // `\r`, an empty chunk, `\n` and the two halves of the emoji arrive separately |
| 137 | + const chunks = ['const a = 1', '\r', '', '\n', 'const b = \'\uD83D', '\uDE00\'\r\n', 'b'] |
| 138 | + let code = '' |
| 139 | + const received: ThemedToken[] = [] |
| 140 | + for (const chunk of chunks) { |
| 141 | + code += chunk |
| 142 | + const { stable, unstable } = await streamer.enqueue(chunk) |
| 143 | + received.push(...stable) |
| 144 | + expectSameTokens(options, code, [...received, ...unstable]) |
| 145 | + } |
| 146 | + expectSameTokens(options, code, [...received, ...streamer.close().stable]) |
| 147 | +}) |
| 148 | + |
| 149 | +it('continues offsets in a clone', async () => { |
| 150 | + const highlighter = await createHighlighter({ |
| 151 | + langs: ['ts'], |
| 152 | + themes: ['vitesse-light'], |
| 153 | + }) |
| 154 | + const options: ShikiStreamTokenizerOptions = { highlighter, lang: 'ts', theme: 'vitesse-light' } |
| 155 | + const streamer = new ShikiStreamTokenizer(options) |
| 156 | + await streamer.enqueue('const a = 1\r\nconst b') |
| 157 | + |
| 158 | + const clone = streamer.clone() |
| 159 | + await clone.enqueue(' = 2\nc') |
| 160 | + expectSameTokens(options, 'const a = 1\r\nconst b = 2\nc', [...clone.tokensStable, ...clone.close().stable]) |
| 161 | +}) |
| 162 | + |
| 163 | +it('restarts offsets after clear() and close()', async () => { |
| 164 | + const highlighter = await createHighlighter({ |
| 165 | + langs: ['ts'], |
| 166 | + themes: ['vitesse-light'], |
| 167 | + }) |
| 168 | + const options: ShikiStreamTokenizerOptions = { highlighter, lang: 'ts', theme: 'vitesse-light' } |
| 169 | + |
| 170 | + for (const reset of ['clear', 'close'] as const) { |
| 171 | + const streamer = new ShikiStreamTokenizer(options) |
| 172 | + await streamer.enqueue('const a = 1\nconst b') |
| 173 | + streamer[reset]() |
| 174 | + const { stable, unstable } = await streamer.enqueue('let c\nd') |
| 175 | + expectSameTokens(options, 'let c\nd', [...stable, ...unstable]) |
| 176 | + } |
| 177 | +}) |
0 commit comments