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

feat: synchronous Shiki usage #764

Merged
merged 4 commits into from
Sep 1, 2024
Merged

feat: synchronous Shiki usage #764

merged 4 commits into from
Sep 1, 2024

Conversation

antfu
Copy link
Member

@antfu antfu commented Aug 31, 2024

This PR introduces two exports createShikiInternalSync and createHighlighterCoreSync that allow completely synchronous usage of Shiki. (made possible by forking vscode-textmate as https://github.com/shikijs/vscode-textmate)

For example:

import { createHighlighterCoreSync, createJavaScriptRegexEngine } from 'shiki/core'
import js from 'shiki/core/langs/javascript.mjs'
import nord from 'shiki/core/themes/nord.mjs'

const shiki = createHighlighterCoreSync({
  themes: [nord],
  langs: [js],
  engine: createJavaScriptRegexEngine()
})

const html = shiki.highlight('console.log(1)', { lang: 'js', theme: 'nord' }) 

These APIs require explicit engine field to be provided, and all langs and themes should be provided as already-resolved objects. Combining with #761, it's able to achieve full synchronous.

If you still want to use Oniguruma, it's also possible if you are able to init the engine beforehand:

import { createHighlighterCoreSync, createWasmOnigEngine } from 'shiki/core'
import js from 'shiki/core/langs/javascript.mjs'
import nord from 'shiki/core/themes/nord.mjs'

// Load this somewhere beforehand
const engine = await createWasmOnigEngine(import('shiki/wasm'))

const shiki = createHighlighterCoreSync({
  themes: [nord],
  langs: [js],
  engine, // if a resolved engine passed in, the rest can still be synced.
})

const html = shiki.highlight('console.log(1)', { lang: 'js', theme: 'nord' }) 

Additionally, this PR also provides two additional properties to the highlighter instance, loadThemeSync and loadLanugageSync:

import { createHighlighterCoreSync, createWasmOnigEngine } from 'shiki/core'
import js from 'shiki/core/langs/javascript.mjs'
import nord from 'shiki/core/themes/nord.mjs'

const shiki = createHighlighterCoreSync({
  // ...
})

// Instead of:
await shiki.loadLanguage(js)
// Now you can have:
shiki.loadLanguageSync(js)

const html = shiki.highlight('console.log(1)', { lang: 'js', theme: 'nord' }) 

Note

Note that these features are only available to the fine-grain bundle, the main bundled usage would still need asynchronous to load langs/themes on-demand.

@shikijs shikijs deleted a comment from netlify bot Aug 31, 2024
@shikijs shikijs deleted a comment from netlify bot Aug 31, 2024
@shikijs shikijs deleted a comment from codecov bot Aug 31, 2024
Copy link

codecov bot commented Aug 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.40%. Comparing base (eb842a3) to head (9091add).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #764      +/-   ##
==========================================
+ Coverage   92.33%   92.40%   +0.07%     
==========================================
  Files          68       70       +2     
  Lines        4489     4531      +42     
  Branches      999     1006       +7     
==========================================
+ Hits         4145     4187      +42     
  Misses        339      339              
  Partials        5        5              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@antfu antfu merged commit 9396a6f into main Sep 1, 2024
8 of 13 checks passed
@antfu antfu deleted the feat/sync branch September 1, 2024 16:00
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 this pull request may close these issues.

None yet

1 participant