Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
An ESM-focused rewrite of shiki, a beautiful syntax highlighter based on TextMate grammars.
npm install -D shikiji
Basic usage is pretty much the same as shiki
, only that some APIs are dropped, (for example, the singular theme
options). Each theme and language file are dynamically imported ES modules, it would be better to list the languages and themes explicitly to have the best performance.
import { getHighlighter } from 'shikiji'
const shiki = await getHighlighter({
themes: ['nord'],
langs: ['javascript'],
})
const code = shiki.codeToHtml('const a = 1', { lang: 'javascript' })
When importing shikiji
, all the themes and languages are bundled as async chunks. Normally it won't be a concern to you as they are not being loaded if you don't use them. While in some cases you want to control what to bundle size, you can use the core and compose your own bundle.
// `shikiji/core` entry does not include any themes or languages or the wasm binary.
import { getHighlighterCore } from 'shikiji/core'
// `shikiji/wasm` contains the wasm binary inlined as base64 string.
import { getWasmInlined } from 'shikiji/wasm'
// directly import the theme and language modules, only the ones you imported will be bundled.
import nord from 'shikiji/themes/nord.mjs'
const shiki = await getHighlighterCore({
themes: [
// instead of strings, you need to pass the imported module
nord,
// or a dynamic import if you want to do chunk splitting
import('shikiji/themes/vitesse-light.mjs')
],
langs: [
import('shikiji/langs/javascript.mjs'),
// shikiji will try to interop the module with the default export
() => import('shikiji/langs/css.mjs'),
// or a getter that returns custom grammar
async () => JSON.parse(await fs.readFile('my-grammar.json', 'utf-8'))
],
loadWasm: getWasmInlined
})
// optionally, load themes and languages after creation
await shiki.loadTheme(import('shikiji/themes/vitesse-light.mjs'))
const code = shiki.codeToHtml('const a = 1', { lang: 'javascript' })
Cloudflare Workers does not support initializing WebAssembly from binary data, so the default wasm build won't work. You need to upload the wasm as assets and import it directly.
Meanwhile, it's also recommended to use the Fine-grained Bundling approach to reduce the bundle size.
import { getHighlighterCore, loadWasm } from 'shikiji/core'
import nord from 'shikiji/themes/nord.mjs'
import js from 'shikiji/langs/javascript.mjs'
// import wasm as assets
import wasm from 'shikiji/onig.wasm'
// load wasm outside of `fetch` so it can be reused
await loadWasm(obj => WebAssembly.instantiate(wasm, obj))
export default {
async fetch() {
const highlighter = await getHighlighterCore({
themes: [nord],
langs: [js],
})
return new Response(highlighter.codeToHtml('console.log(\'shiki\');', { lang: 'js' }))
},
}
FAQs
An ESM-focused rewrite of shiki
The npm package shikiji receives a total of 7,700 weekly downloads. As such, shikiji popularity was classified as popular.
We found that shikiji demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.