Skip to content

Commit

Permalink
[Feature] Offline usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lnfel committed Aug 15, 2023
1 parent 9db4e71 commit 833dfd1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/lib/components/LamyDebugbar.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { onMount, afterUpdate } from "svelte"
import { onMount, afterUpdate, tick } from "svelte"
import Shiki, { defaultHighlighterOptions } from "$lib/shiki.js"
/**
* Using Twind in library mode
Expand All @@ -18,6 +18,8 @@
export let noIcon = false
/** @type {any} */
export let customTheme = undefined
/** @type {Boolean} */
export let offline = false
let shiki = Shiki
/** @type {import('shiki-es').IShikiTheme | undefined} */
Expand Down Expand Up @@ -138,6 +140,15 @@
}
onMount(async () => {
// wait for offline prop to populate from localStorage
// in most cases we don't need doing this as users would
// only need to set offline prop either true or false
await tick()
if (offline) {
const wasm = await fetch('shiki/onig.wasm')
shiki.setWasm(wasm)
shiki.setCDN('shiki/')
}
await shiki.getHighlighter(highlighter)
if (customTheme) {
await loadCustomTheme(customTheme)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/LamyDebugbar.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare const __propDef: {
highlighter?: import("shiki-es").HighlighterOptions | undefined;
noIcon?: boolean;
customTheme?: any;
offline?: boolean;
};
events: {
[evt: string]: CustomEvent<any>;
Expand All @@ -23,6 +24,7 @@ export default class LamyDebugbar extends SvelteComponent<{
highlighter?: import("shiki-es").HighlighterOptions | undefined;
noIcon?: boolean;
customTheme?: any;
offline?: boolean;
}, {
[evt: string]: CustomEvent<any>;
}, {
Expand Down
33 changes: 31 additions & 2 deletions src/lib/shiki.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getHighlighter, toShikiTheme, renderToHtml } from 'shiki-es'
// import { readFile } from 'fs/promises'
import { getHighlighter, toShikiTheme, renderToHtml, setCDN, setWasm } from 'shiki-es'

/**
* Host the onig.wasm file, loading from CDN is slow af
Expand Down Expand Up @@ -57,6 +56,36 @@ class Shiki {
this.highlighter = await getHighlighter(Object.assign(defaultHighlighterOptions, highlighterOptions))
return this.highlighter
}

/**
* Set the route for loading the assets
*
* URL should end with `/`
*
* For example:
* ```js
* setCDN('https://unpkg.com/shiki/') // use unpkg
* setCDN('/assets/shiki/') // serve by yourself
* ```
*
* @param {String} root
* @returns {void}
*/
setCDN(root) {
setCDN(root)
}

/**
* Explicitly set the source for loading the oniguruma web assembly module.
*
* Accepts ArrayBuffer or Response (usage of string is deprecated)
*
* @param {ArrayBuffer | Response} data
* @returns {void}
*/
setWasm(data) {
setWasm(data)
}
}

export default new Shiki()
Expand Down

0 comments on commit 833dfd1

Please sign in to comment.