Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: shikijs/shiki
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.11.2
Choose a base ref
...
head repository: shikijs/shiki
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.12.0
Choose a head ref
  • 6 commits
  • 22 files changed
  • 2 contributors

Commits on Jul 26, 2024

  1. chore: dedupe

    antfu committed Jul 26, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    antfu Anthony Fu
    Copy the full SHA
    193f73f View commit details
  2. fix(core): fix decoration offset edge cases (#728)

    timothycohen authored Jul 26, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    1349bb4 View commit details
  3. feat: add static initial method for GrammarState, #715

    antfu committed Jul 26, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    antfu Anthony Fu
    Copy the full SHA
    be07da8 View commit details
  4. feat: new ts-tags grammar, lit support, close #725

    antfu committed Jul 26, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    antfu Anthony Fu
    Copy the full SHA
    3107a2d View commit details
  5. chore: update snapshot

    antfu committed Jul 26, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    antfu Anthony Fu
    Copy the full SHA
    2686088 View commit details
  6. chore: release v1.12.0

    antfu committed Jul 26, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    antfu Anthony Fu
    Copy the full SHA
    4a58472 View commit details
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
"@unocss/reset": "^0.61.5",
"@vueuse/core": "^10.11.0",
"floating-vue": "^5.2.2",
"pinia": "^2.1.7",
"pinia": "^2.2.0",
"shiki": "workspace:*",
"unocss": "^0.61.5",
"unplugin-vue-components": "^0.27.3",
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"private": true,
"packageManager": "pnpm@9.6.0",
"scripts": {
@@ -64,7 +64,7 @@
"vitepress-plugin-mermaid": "^2.0.16",
"vitest": "^2.0.4",
"vue-tsc": "^2.0.29",
"wrangler": "^3.67.0"
"wrangler": "^3.67.1"
},
"resolutions": {
"@shikijs/compat": "workspace:*",
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/cli",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Shiki in the command line",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/compat/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/compat",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Shiki v0.x compatible API",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/core",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Core of Shiki",
"author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
14 changes: 11 additions & 3 deletions packages/core/src/grammar-state.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import type { StateStackImpl } from '../vendor/vscode-textmate/src/grammar'
import { ShikiError } from './error'
import type { StateStack } from './textmate'
import { INITIAL } from './textmate'

/**
* GrammarState is a special reference object that holds the state of a grammar.
*
* It's used to highlight code snippets that are part of the target language.
*/
export class GrammarState {
/**
* Static method to create a initial grammar state.
*/
static initial(lang: string, theme: string) {
return new GrammarState(INITIAL, lang, theme)
}

constructor(
private _stack: StateStack,
public lang: string,
public theme: string,
private readonly _stack: StateStack,
public readonly lang: string,
public readonly theme: string,
) {}

get scopes() {
9 changes: 9 additions & 0 deletions packages/core/src/transformer-decorations.ts
Original file line number Diff line number Diff line change
@@ -21,12 +21,21 @@ export function transformerDecorations(): ShikiTransformer {

function normalizePosition(p: OffsetOrPosition): ResolvedPosition {
if (typeof p === 'number') {
if (p < 0 || p > shiki.source.length)
throw new ShikiError(`Invalid decoration offset: ${p}. Code length: ${shiki.source.length}`)

return {
...converter.indexToPos(p),
offset: p,
}
}
else {
const line = converter.lines[p.line]
if (line === undefined)
throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Lines length: ${converter.lines.length}`)
if (p.character < 0 || p.character > line.length)
throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Line ${p.line} length: ${line.length}`)

return {
...p,
offset: converter.posToIndex(p.line, p.character),
2 changes: 0 additions & 2 deletions packages/core/src/types/decorations.ts
Original file line number Diff line number Diff line change
@@ -14,8 +14,6 @@ export interface DecorationItem {
start: OffsetOrPosition
/**
* End offset or position of the decoration.
*
* If the
*/
end: OffsetOrPosition
/**
11 changes: 10 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ export function toArray<T>(x: MaybeArray<T>): T[] {
}

/**
* Slipt a string into lines, each line preserves the line ending.
* Split a string into lines, each line preserves the line ending.
*/
export function splitLines(code: string, preserveEnding = false): [string, number][] {
const parts = code.split(/(\r?\n)/g)
@@ -192,11 +192,20 @@ export function stringifyTokenStyle(token: Record<string, string>) {

/**
* Creates a converter between index and position in a code block.
*
* Overflow/underflow are unchecked.
*/
export function createPositionConverter(code: string) {
const lines = splitLines(code, true).map(([line]) => line)

function indexToPos(index: number): Position {
if (index === code.length) {
return {
line: lines.length - 1,
character: lines[lines.length - 1].length,
}
}

let character = index
let line = 0
for (const lineText of lines) {
2 changes: 1 addition & 1 deletion packages/markdown-it/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/markdown-it",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "markdown-it integration for shiki",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/monaco/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/monaco",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Use Shiki for Monaco Editor",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/rehype/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/rehype",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "rehype integration for shiki",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
4 changes: 2 additions & 2 deletions packages/shiki/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shiki",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "A beautiful Syntax Highlighter.",
"author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
@@ -104,7 +104,7 @@
"@types/hast": "^3.0.4"
},
"devDependencies": {
"tm-grammars": "^1.14.0",
"tm-grammars": "^1.15.1",
"tm-themes": "^1.5.7",
"vscode-oniguruma": "^1.7.0"
}
10 changes: 10 additions & 0 deletions packages/shiki/src/assets/langs-bundle-full.ts
Original file line number Diff line number Diff line change
@@ -1085,6 +1085,14 @@ export const bundledLanguagesInfo: BundledLanguageInfo[] = [
'name': 'TOML',
'import': (() => import('./langs/toml')) as DynamicImportLanguageRegistration
},
{
'id': 'ts-tags',
'name': 'TypeScript with Tags',
'aliases': [
'lit'
],
'import': (() => import('./langs/ts-tags')) as DynamicImportLanguageRegistration
},
{
'id': 'tsv',
'name': 'TSV',
@@ -1401,6 +1409,7 @@ export type BundledLanguage =
| 'less'
| 'liquid'
| 'lisp'
| 'lit'
| 'log'
| 'logo'
| 'lua'
@@ -1505,6 +1514,7 @@ export type BundledLanguage =
| 'tfvars'
| 'toml'
| 'ts'
| 'ts-tags'
| 'tsp'
| 'tsv'
| 'tsx'
10 changes: 10 additions & 0 deletions packages/shiki/src/assets/langs-bundle-web.ts
Original file line number Diff line number Diff line change
@@ -274,6 +274,14 @@ export const bundledLanguagesInfo: BundledLanguageInfo[] = [
'name': 'TOML',
'import': (() => import('./langs/toml')) as DynamicImportLanguageRegistration
},
{
'id': 'ts-tags',
'name': 'TypeScript with Tags',
'aliases': [
'lit'
],
'import': (() => import('./langs/ts-tags')) as DynamicImportLanguageRegistration
},
{
'id': 'tsx',
'name': 'TSX',
@@ -362,6 +370,7 @@ export type BundledLanguage =
| 'jsx'
| 'julia'
| 'less'
| 'lit'
| 'lua'
| 'markdown'
| 'marko'
@@ -389,6 +398,7 @@ export type BundledLanguage =
| 'svelte'
| 'toml'
| 'ts'
| 'ts-tags'
| 'tsx'
| 'typescript'
| 'vue'
4 changes: 2 additions & 2 deletions packages/shiki/test/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ it('bundle-full', async () => {
}))

expect(highlighter.getLoadedLanguages().length)
.toMatchInlineSnapshot(`299`)
.toMatchInlineSnapshot(`306`)
})

it('bundle-web', async () => {
@@ -17,5 +17,5 @@ it('bundle-web', async () => {
}))

expect(highlighter.getLoadedLanguages().length)
.toMatchInlineSnapshot(`84`)
.toMatchInlineSnapshot(`91`)
})
47 changes: 44 additions & 3 deletions packages/shiki/test/decorations.test.ts
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ export function codeToHtml(
let result = hastToHtml(codeToHast(internal, code, options, context))
return result
}
`
// final`

describe('decorations', () => {
it('works', async () => {
@@ -78,6 +78,13 @@ describe('decorations', () => {
end: { line: 8, character: 25 },
properties: { class: 'highlighted' },
},
// "// final"
// Testing offset === code.length edge case
{
start: code.length - 8,
end: code.length,
properties: { class: 'highlighted' },
},
],
})

@@ -126,7 +133,7 @@ describe('decorations errors', () => {
],
})
}).rejects
.toThrowErrorMatchingInlineSnapshot(`[TypeError: Cannot read properties of undefined (reading 'length')]`)
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Invalid decoration position {"line":100,"character":0}. Lines length: 12]`)
})

it('throws when chars overflow', async () => {
@@ -139,6 +146,40 @@ describe('decorations errors', () => {
],
})
}).rejects
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Failed to find end index for decoration {"line":0,"character":10,"offset":10}]`)
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Invalid decoration position {"line":0,"character":10}. Line 0 length: 4]`)

expect(async () => {
await codeToHtml(code, {
theme: 'vitesse-light',
lang: 'ts',
decorations: [
{
start: { line: 2, character: 1 },
end: { line: 1, character: 36 }, // actual position is { line: 2, character: 3, offset 40 }
},
],
})
}).rejects
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Invalid decoration position {"line":1,"character":36}. Line 1 length: 33]`)
})

it('throws when offset underflows/overflows', async () => {
expect(async () => {
await codeToHtml(code, {
theme: 'vitesse-light',
lang: 'ts',
decorations: [{ start: 1, end: 1000 }],
})
}).rejects
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Invalid decoration offset: 1000. Code length: 252]`)

expect(async () => {
await codeToHtml(code, {
theme: 'vitesse-light',
lang: 'ts',
decorations: [{ start: -3, end: 5 }],
})
}).rejects
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Invalid decoration offset: -3. Code length: 252]`)
})
})
2 changes: 1 addition & 1 deletion packages/shiki/test/out/decorations/basic.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/transformers/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/transformers",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Collective of common transformers transformers for Shiki",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/twoslash/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/twoslash",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Shiki transformer for twoslash",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/vitepress-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shikijs/vitepress-twoslash",
"type": "module",
"version": "1.11.2",
"version": "1.12.0",
"description": "Enable Twoslash support in VitePress",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
466 changes: 64 additions & 402 deletions pnpm-lock.yaml

Large diffs are not rendered by default.