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.12.1
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.13.0
Choose a head ref
  • 4 commits
  • 31 files changed
  • 3 contributors

Commits on Aug 14, 2024

  1. feat(core): lighter token explanation with scopeName (#739)

    antfu authored Aug 14, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    efbb7fb View commit details
  2. feat: new grammar, new themes, update deps

    antfu committed Aug 14, 2024
    Copy the full SHA
    b68ee52 View commit details
  3. feat(rehype): trim trailing new line passed in from rehype/remark (#729)

    Co-authored-by: Anthony Fu <github@antfu.me>
    Ovyerus and antfu authored Aug 14, 2024
    Copy the full SHA
    aea7f9e View commit details
  4. chore: release v1.13.0

    antfu committed Aug 14, 2024
    Copy the full SHA
    f308dc2 View commit details
18 changes: 9 additions & 9 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -7,21 +7,21 @@
"docs:preview": "vitepress preview"
},
"dependencies": {
"@iconify-json/ph": "^1.1.13",
"@iconify-json/ph": "^1.1.14",
"fuse.js": "^7.0.0"
},
"devDependencies": {
"@iconify-json/svg-spinners": "^1.1.2",
"@iconify-json/svg-spinners": "^1.1.3",
"@shikijs/transformers": "workspace:*",
"@shikijs/twoslash": "workspace:*",
"@unocss/reset": "^0.61.9",
"@vueuse/core": "^10.11.0",
"@unocss/reset": "^0.62.1",
"@vueuse/core": "^10.11.1",
"floating-vue": "^5.2.2",
"pinia": "^2.2.0",
"pinia": "^2.2.1",
"shiki": "workspace:*",
"unocss": "^0.61.9",
"unplugin-vue-components": "^0.27.3",
"vitepress": "^1.3.1",
"vue": "^3.4.35"
"unocss": "^0.62.1",
"unplugin-vue-components": "^0.27.4",
"vitepress": "^1.3.2",
"vue": "^3.4.37"
}
}
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"type": "module",
"version": "1.12.1",
"version": "1.13.0",
"private": true,
"packageManager": "pnpm@9.6.0",
"packageManager": "pnpm@9.7.1",
"scripts": {
"lint": "eslint . --cache",
"release": "bumpp && pnpm -r publish",
@@ -14,8 +14,8 @@
"prepare": "simple-git-hooks"
},
"devDependencies": {
"@antfu/eslint-config": "^2.24.1",
"@antfu/ni": "^0.22.0",
"@antfu/eslint-config": "^2.25.1",
"@antfu/ni": "^0.22.4",
"@antfu/utils": "^0.7.10",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "^26.0.1",
@@ -30,41 +30,41 @@
"@shikijs/vitepress-twoslash": "workspace:*",
"@types/fs-extra": "^11.0.4",
"@types/hast": "^3.0.4",
"@types/node": "^22.0.2",
"@types/node": "^22.3.0",
"@vitest/coverage-v8": "^2.0.5",
"ansi-sequence-parser": "^1.1.1",
"bumpp": "^9.4.1",
"eslint": "^9.8.0",
"bumpp": "^9.5.1",
"eslint": "^9.9.0",
"eslint-plugin-format": "^0.1.2",
"esno": "^4.7.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"jsonc-parser": "^3.3.1",
"lint-staged": "^15.2.7",
"lint-staged": "^15.2.9",
"markdown-it": "^14.1.0",
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-gfm": "^3.0.0",
"mdast-util-to-hast": "^13.2.0",
"ofetch": "^1.3.4",
"pnpm": "^9.6.0",
"pnpm": "^9.7.1",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"rollup": "^4.19.2",
"rollup": "^4.20.0",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-typescript2": "^0.36.0",
"shiki": "workspace:*",
"simple-git-hooks": "^2.11.1",
"taze": "^0.16.3",
"taze": "^0.16.6",
"typescript": "^5.5.4",
"unbuild": "^2.0.0",
"vite": "^5.3.5",
"vite-tsconfig-paths": "^4.3.2",
"vite": "^5.4.0",
"vite-tsconfig-paths": "^5.0.1",
"vitepress-plugin-mermaid": "^2.0.16",
"vitest": "^2.0.5",
"vue-tsc": "^2.0.29",
"wrangler": "^3.68.0"
"wrangler": "^3.71.0"
},
"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.12.1",
"version": "1.13.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.12.1",
"version": "1.13.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.12.1",
"version": "1.13.0",
"description": "Core of Shiki",
"author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
65 changes: 39 additions & 26 deletions packages/core/src/code-to-tokens-base.ts
Original file line number Diff line number Diff line change
@@ -125,28 +125,6 @@ function _tokenizeWithTheme(
let actual: ThemedToken[] = []
const final: ThemedToken[][] = []

const themeSettingsSelectors: ThemeSettingsSelectors[] = []
if (options.includeExplanation) {
for (const setting of theme.settings) {
let selectors: string[]
switch (typeof setting.scope) {
case 'string':
selectors = setting.scope.split(/,/).map(scope => scope.trim())
break
case 'object':
selectors = setting.scope
break
default:
continue
}

themeSettingsSelectors.push({
settings: setting,
selectors: selectors.map(selector => selector.split(/ /)),
})
}
}

for (let i = 0, len = lines.length; i < len; i++) {
const [line, lineOffset] = lines[i]
if (line === '') {
@@ -201,6 +179,29 @@ function _tokenizeWithTheme(
}

if (options.includeExplanation) {
const themeSettingsSelectors: ThemeSettingsSelectors[] = []

if (options.includeExplanation !== 'scopeName') {
for (const setting of theme.settings) {
let selectors: string[]
switch (typeof setting.scope) {
case 'string':
selectors = setting.scope.split(/,/).map(scope => scope.trim())
break
case 'object':
selectors = setting.scope
break
default:
continue
}

themeSettingsSelectors.push({
settings: setting,
selectors: selectors.map(selector => selector.split(/ /)),
})
}
}

token.explanation = []
let offset = 0
while (startIndex + offset < nextStartIndex) {
@@ -213,7 +214,14 @@ function _tokenizeWithTheme(
offset += tokenWithScopesText.length
token.explanation.push({
content: tokenWithScopesText,
scopes: explainThemeScopes(themeSettingsSelectors, tokenWithScopes.scopes),
scopes: options.includeExplanation === 'scopeName'
? explainThemeScopesNameOnly(
tokenWithScopes.scopes,
)
: explainThemeScopesFull(
themeSettingsSelectors,
tokenWithScopes.scopes,
),
})

tokensWithScopesIndex! += 1
@@ -233,17 +241,22 @@ function _tokenizeWithTheme(
}
}

function explainThemeScopes(
function explainThemeScopesNameOnly(
scopes: string[],
): ThemedTokenScopeExplanation[] {
return scopes.map(scope => ({ scopeName: scope }))
}

function explainThemeScopesFull(
themeSelectors: ThemeSettingsSelectors[],
scopes: string[],
): ThemedTokenScopeExplanation[] {
const result: ThemedTokenScopeExplanation[] = []
for (let i = 0, len = scopes.length; i < len; i++) {
const parentScopes = scopes.slice(0, i)
const scope = scopes[i]
result[i] = {
scopeName: scope,
themeMatches: explainThemeScope(themeSelectors, scope, parentScopes),
themeMatches: explainThemeScope(themeSelectors, scope, scopes.slice(0, i)),
}
}
return result
8 changes: 6 additions & 2 deletions packages/core/src/types/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GrammarState } from '../grammar-state'
import type { IRawThemeSetting } from '../textmate'
import type { SpecialLanguage } from './langs'
import type { SpecialTheme, ThemeRegistrationAny } from './themes'
import type { CodeOptionsThemes } from './options'
@@ -36,7 +37,7 @@ export interface CodeToTokensWithThemesOptions<Languages = string, Themes = stri

export interface ThemedTokenScopeExplanation {
scopeName: string
themeMatches: any[]
themeMatches?: IRawThemeSetting[]
}

export interface ThemedTokenExplanation {
@@ -149,9 +150,12 @@ export interface TokenizeWithThemeOptions {
/**
* Include explanation of why a token is given a color.
*
* You can optionally pass `scopeName` to only include explanation for scopes,
* which is more performant than full explanation.
*
* @default false
*/
includeExplanation?: boolean
includeExplanation?: boolean | 'scopeName'

/**
* A map of color names to new color values.
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.12.1",
"version": "1.13.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.12.1",
"version": "1.13.0",
"description": "Use Shiki for Monaco Editor",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
2 changes: 1 addition & 1 deletion packages/monaco/playground/package.json
Original file line number Diff line number Diff line change
@@ -8,6 +8,6 @@
},
"devDependencies": {
"typescript": "^5.5.4",
"vite": "^5.3.5"
"vite": "^5.4.0"
}
}
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.12.1",
"version": "1.13.0",
"description": "rehype integration for shiki",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
17 changes: 16 additions & 1 deletion packages/rehype/src/core.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,16 @@ export interface RehypeShikiExtraOptions {
*/
fallbackLanguage?: string

/**
* `mdast-util-to-hast` adds a newline to the end of code blocks
*
* This option strips that newline from the code block
*
* @default true
* @see https://github.com/syntax-tree/mdast-util-to-hast/blob/f511a93817b131fb73419bf7d24d73a5b8b0f0c2/lib/handlers/code.js#L22
*/
stripEndNewline?: boolean

/**
* Custom meta string parser
* Return an object to merge with `meta`
@@ -73,6 +83,7 @@ function rehypeShikiFromHighlighter(
defaultLanguage,
fallbackLanguage,
onError,
stripEndNewline = true,
...rest
} = options

@@ -107,7 +118,11 @@ function rehypeShikiFromHighlighter(
if (fallbackLanguage && !langs.includes(lang))
lang = fallbackLanguage

const code = toString(head)
let code = toString(head)

if (stripEndNewline && code.endsWith('\n'))
code = code.slice(0, -1)

const cachedValue = cache?.get(code)

if (cachedValue) {
6 changes: 2 additions & 4 deletions packages/rehype/test/fixtures/a.core.out.html

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

6 changes: 2 additions & 4 deletions packages/rehype/test/fixtures/a.out.html

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

3 changes: 1 addition & 2 deletions packages/rehype/test/fixtures/b.out.html

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

6 changes: 2 additions & 4 deletions packages/rehype/test/fixtures/c.out.html

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

Loading