react-shiki 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,24 +1,22 @@
1
1
  > [!WARNING]
2
2
  > This package is still a work in progress, it is not yet recommended for production use. Contributions are welcome! My goal is to eventually build this out as a near-drop-in replacement for `react-syntax-highlighter`
3
3
 
4
-
5
4
  # 🎨 [react-shiki](https://react-shiki.vercel.app/)
6
5
 
7
6
  Syntax highlighting component and hook for react using [Shiki](https://shiki.matsu.io/)
8
7
 
9
-
10
8
  [See the demo page for a version of this README with highlighted code blocks showcasing several Shiki themes!](https://react-shiki.vercel.app/)
11
9
 
12
10
  ## Features
11
+
13
12
  - 🖼️ Provides a `ShikiHighlighter` component for highlighting code as children, as well as a `useShikiHighlighter` hook for more flexibility
14
- - 🔐 No `dangerouslySetInnerHTML`, output from Shiki is parsed using `html-react-parser`
13
+ - 🔐 No `dangerouslySetInnerHTML`, output from Shiki is parsed using `html-react-parser`
15
14
  - 📦 Supports all Shiki languages and themes
16
15
  - 📚 Includes minimal default styles for code blocks
17
16
  - 🚀 Shiki dynamically imports only the languages and themes used on a page, optimizing for performance
18
17
  - 🖥️ `ShikiHighlighter` component displays a language label for each code block when `showLanguage` is set to `true` (default)
19
18
  - 🎨 Users can customize the styling of the generated code blocks by passing a `style` object or a `className`
20
19
 
21
-
22
20
  ## Installation
23
21
 
24
22
  ```bash
@@ -30,6 +28,7 @@ Syntax highlighting component and hook for react using [Shiki](https://shiki.mat
30
28
  You can use the `ShikiHighlighter` component, or the `useShikiHighlighter` hook to highlight code.
31
29
 
32
30
  `useShikiHighlighter` is a hook that takes in the code to be highlighted, the language, and the theme, and returns the highlighted code as React elements. It's useful for users who want full control over the rendering of highlighted code.
31
+
33
32
  ```tsx
34
33
  const highlightedCode = useShikiHighlighter(code, language, theme);
35
34
  ```
@@ -48,7 +47,6 @@ function CodeBlock() {
48
47
  }
49
48
  ```
50
49
 
51
-
52
50
  The component accepts several props in addition to language and theme:
53
51
 
54
52
  - `showLanguage: boolean` - Shows the language name in the top right corner of the code block.
@@ -67,7 +65,7 @@ function Houston() {
67
65
  addDefaultStyles={true}
68
66
  as="div"
69
67
  style={{
70
- textAlign: 'left',
68
+ textAlign: "left",
71
69
  }}
72
70
  >
73
71
  {code.trim()}
@@ -77,10 +75,10 @@ function Houston() {
77
75
  ```
78
76
 
79
77
  It can also be used with `react-markdown`:
78
+
80
79
  ```tsx
81
- import type { ReactNode } from 'react';
82
- import type { BundledLanguage } from 'shiki';
83
- import ShikiHighlighter, { isInlineCode, type Element } from 'react-shiki';
80
+ import type { ReactNode } from "react";
81
+ import ShikiHighlighter, { isInlineCode, type Element } from "react-shiki";
84
82
 
85
83
  interface CodeHighlightProps {
86
84
  className?: string | undefined;
@@ -95,16 +93,12 @@ export const CodeHighlight = ({
95
93
  ...props
96
94
  }: CodeHighlightProps): JSX.Element => {
97
95
  const match = className?.match(/language-(\w+)/);
98
- // TODO: remove need for consumer use of BundledLanguage from shiki
99
- const language = match ? (match[1] as BundledLanguage) : undefined;
96
+ const language = match ? match[1] : undefined;
100
97
 
101
98
  const isInline: boolean | undefined = node ? isInlineCode(node) : undefined;
102
99
 
103
100
  return !isInline ? (
104
- <ShikiHighlighter
105
- language={language as BundledLanguage}
106
- theme={'houston'}
107
- {...props}>
101
+ <ShikiHighlighter language={language} theme={"houston"} {...props}>
108
102
  {String(children)}
109
103
  </ShikiHighlighter>
110
104
  ) : (
@@ -116,9 +110,10 @@ export const CodeHighlight = ({
116
110
  ```
117
111
 
118
112
  Pass CodeHighlight to `react-markdown` as a code component:
113
+
119
114
  ```tsx
120
- import ReactMarkdown from 'react-markdown';
121
- import { CodeHighlight } from './CodeHighlight';
115
+ import ReactMarkdown from "react-markdown";
116
+ import { CodeHighlight } from "./CodeHighlight";
122
117
 
123
118
  <ReactMarkdown
124
119
  components={{
@@ -126,10 +121,11 @@ import { CodeHighlight } from './CodeHighlight';
126
121
  }}
127
122
  >
128
123
  {markdown}
129
- </ReactMarkdown>
124
+ </ReactMarkdown>;
130
125
  ```
131
126
 
132
127
  This works great for highlighting in realtime on the client, I use it for an LLM chatbot UI, it renders markdown and highlights code in memoized chat messages:
128
+
133
129
  ```tsx
134
130
  const RenderedMessage = React.memo(({ message }: { message: Message }) => (
135
131
  <div className={cn(messageStyles[message.role])}>
@@ -141,11 +137,12 @@ const RenderedMessage = React.memo(({ message }: { message: Message }) => (
141
137
 
142
138
  export const ChatMessages = ({ messages }: { messages: Message[] }) => {
143
139
  return (
144
- <div className='space-y-4'>
140
+ <div className="space-y-4">
145
141
  {messages.map((message) => (
146
142
  <RenderedMessage key={message.id} message={message} />
147
143
  ))}
148
144
  </div>
149
145
  );
150
146
  };
151
- ```
147
+ ```
148
+
package/dist/index.d.ts CHANGED
@@ -1,23 +1,54 @@
1
1
  import React, { ReactNode } from 'react';
2
- import { BundledLanguage, BundledTheme } from 'shiki';
2
+ import { BundledLanguage, ThemeRegistration, BundledTheme } from 'shiki';
3
3
  import { Element } from 'hast';
4
4
  export { Element } from 'hast';
5
5
 
6
- interface ShikiHighlighterProps {
6
+ /**
7
+ * Languages for syntax highlighting.
8
+ * @see https://shiki.style/languages
9
+ */
10
+ type Language = BundledLanguage | "plaintext" | (string & {}) | undefined;
11
+ /**
12
+ * A textmate theme object or a Shiki BundledTheme
13
+ * @see https://shiki.style/themes
14
+ */
15
+ type Theme = ThemeRegistration | BundledTheme;
16
+ /**
17
+ * Configuration options for the syntax highlighter
18
+ */
19
+ type HighlighterOptions = {
20
+ /**
21
+ * Minimum time (in milliseconds) between highlight operations.
22
+ * Defaults to undefined (no throttling)
23
+ */
24
+ delay?: number;
25
+ };
26
+
27
+ /**
28
+ * Props for the ShikiHighlighter component
29
+ */
30
+ interface ShikiHighlighterProps extends HighlighterOptions {
7
31
  /**
8
32
  * The programming language for syntax highlighting
9
33
  * @see https://shiki.style/languages
10
34
  */
11
- language: BundledLanguage;
35
+ language: Language;
12
36
  /**
13
37
  * The code to be highlighted
14
38
  */
15
39
  children: string;
16
40
  /**
17
- * The color theme for syntax highlighting, only Shiki themes are supported at this time
41
+ * The color theme for syntax highlighting
42
+ * Supports Shiki and custom textmate themes
18
43
  * @see https://shiki.style/themes
19
44
  */
20
- theme: BundledTheme;
45
+ theme: Theme;
46
+ /**
47
+ * The delay in milliseconds between streamed updates
48
+ * Use this when highlighting code being streamed on the client
49
+ * @default undefined (no delay)
50
+ */
51
+ delay?: number;
21
52
  /**
22
53
  * Controls the application of default styles to the generated code blocks
23
54
  *
@@ -46,29 +77,26 @@ interface ShikiHighlighterProps {
46
77
  }
47
78
  /**
48
79
  * ShikiHighlighter is a React component that provides syntax highlighting for code snippets.
49
- *
50
- * It uses the Shiki library to render beautiful, theme-based syntax highlighting.
80
+ * It uses Shiki to render beautiful, theme-based syntax highlighting with optimized performance.
51
81
  *
52
82
  * @example
53
83
  * ```tsx
54
- * <ShikiHighlighter language="tsx" theme="vitesse-black">
55
- * {code.trim()}
84
+ * <ShikiHighlighter
85
+ * language="typescript"
86
+ * theme="github-dark"
87
+ * delay={100} // Optional throttling for streamed updates
88
+ * >
89
+ * {code}
56
90
  * </ShikiHighlighter>
57
91
  * ```
58
92
  */
59
- declare const ShikiHighlighter: ({ language, theme, addDefaultStyles, style, className, showLanguage, children: code, as: Element, }: ShikiHighlighterProps) => React.ReactElement;
93
+ declare const ShikiHighlighter: ({ language, theme, delay, addDefaultStyles, style, className, showLanguage, children: code, as: Element, }: ShikiHighlighterProps) => React.ReactElement;
60
94
 
61
- /**
62
- *
63
- * `useShikiHighlighter` is a custom hook that takes in the code to be highlighted, the language, and the theme, and returns the highlighted code parsed as a ReactNode.
64
- *
65
- * @example
66
- * ```tsx
67
- * const highlightedCode = useShikiHighlighter(code, language, theme);
68
- * ```
69
- *
70
- */
71
- declare const useShikiHighlighter: (code: string, lang: BundledLanguage | undefined, theme: BundledTheme) => ReactNode | null;
95
+ /************************************************************************
96
+ ** A React hook that provides syntax highlighting using Shiki. Uses a **
97
+ ** singleton highlighter instance and supports throttled updates. **
98
+ *************************************************************************/
99
+ declare const useShikiHighlighter: (code: string, lang: Language, theme: Theme, options?: HighlighterOptions) => ReactNode | null;
72
100
 
73
101
  declare const isInlineCode: (node: Element) => boolean;
74
102
 
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- function a(e,{insertAt:o}={}){if(!e||typeof document>"u")return;let r=document.head||document.getElementsByTagName("head")[0],t=document.createElement("style");t.type="text/css",o==="top"&&r.firstChild?r.insertBefore(t,r.firstChild):r.appendChild(t),t.styleSheet?t.styleSheet.cssText=e:t.appendChild(document.createTextNode(e))}a(`.relative{position:relative}.container pre{overflow:auto;border-radius:.5rem;padding:1.25rem 1.5rem}.languageLabel{position:absolute;right:.75rem;top:.5rem;font-family:monospace;font-size:.75rem;letter-spacing:-.05em;color:#6b7280d9}
2
- `);import g from"react";import p from"html-react-parser";import{useEffect as f,useState as y}from"react";import{codeToHtml as h}from"shiki";var c=e=>e.position?.start.line===e.position?.end.line,s={pre(e){e.properties.tabindex="-1"}};var d=(e,o,r)=>{let[t,i]=y(null);return f(()=>{(async()=>{try{let n=await h(e,{lang:o,theme:r,transformers:[s]});i(p(n))}catch{let l=await h(e,{lang:"plaintext",theme:r,transformers:[s]});i(p(l))}})()},[e,r,o]),t};import{clsx as x}from"clsx";var C=({language:e,theme:o,addDefaultStyles:r=!0,style:t,className:i,showLanguage:m=!0,children:n,as:l="pre"})=>{let u=d(n,e,o);return g.createElement(l,{className:x("relative",r&&"container",i),style:t},m&&e?g.createElement("span",{className:"languageLabel",style:t},e):null,u)};export{C as default,c as isInlineCode,d as useShikiHighlighter};
1
+ function g(e,{insertAt:i}={}){if(!e||typeof document>"u")return;let t=document.head||document.getElementsByTagName("head")[0],r=document.createElement("style");r.type="text/css",i==="top"&&t.firstChild?t.insertBefore(r,t.firstChild):t.appendChild(r),r.styleSheet?r.styleSheet.cssText=e:r.appendChild(document.createTextNode(e))}g(`.relative{position:relative}.container pre{overflow:auto;border-radius:.5rem;padding:1.25rem 1.5rem}.languageLabel{position:absolute;right:.75rem;top:.5rem;font-family:monospace;font-size:.75rem;letter-spacing:-.05em;color:#6b7280d9}
2
+ `);import p from"react";import{clsx as I}from"clsx";import{useEffect as T,useRef as H,useState as b}from"react";import S from"html-react-parser";import{createHighlighter as w,ShikiError as u}from"shiki";var x=e=>e.position?.start.line===e.position?.end.line,d={pre(e){e.properties.tabindex="-1"}};var m=null,L=async e=>{m||(m=w({themes:[],langs:[]}));let i=await m;return await E(i,e),i},E=async(e,i)=>{try{await e.loadTheme(i)}catch(t){throw t instanceof u&&console.warn("Error loading theme:",t.message),t}},P=async(e,i)=>{let t=i??"plaintext";try{return await e.loadLanguage(t),t}catch(r){return r instanceof u&&r.message.includes("not included in this bundle")&&console.warn(`Language '${t}' not supported, falling back to plaintext`),"plaintext"}},k=(e,i,t)=>{let r=Date.now();clearTimeout(i.current.timeoutId);let o=Math.max(0,i.current.nextAllowedTime-r);i.current.timeoutId=setTimeout(()=>{e().catch(console.error),i.current.nextAllowedTime=r+t},o)},c=(e,i,t,r={})=>{let[o,h]=b(null),n=H({nextAllowedTime:0,timeoutId:void 0});return T(()=>{let a=!0,l=async()=>{let s=await L(t),f=await P(s,i),y=s.codeToHtml(e,{lang:f,theme:t,transformers:[d]});a&&h(S(y))};return r.delay?k(l,n,r.delay):l().catch(console.error),()=>{a=!1,clearTimeout(n.current.timeoutId)}},[e,i]),o};var R=({language:e,theme:i,delay:t,addDefaultStyles:r=!0,style:o,className:h,showLanguage:n=!0,children:a,as:l="pre"})=>{let s=c(a,e,i,{delay:t});return p.createElement(l,{className:I("relative",r&&"container",h),style:o},n&&e?p.createElement("span",{className:"languageLabel",style:o},e):null,s)};export{R as default,x as isInlineCode,c as useShikiHighlighter};
3
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["#style-inject:#style-inject","../src/styles.css","../src/ShikiHighlighter.tsx","../src/useShiki.ts","../src/utils.ts"],"sourcesContent":["\n export default function styleInject(css, { insertAt } = {}) {\n if (!css || typeof document === 'undefined') return\n \n const head = document.head || document.getElementsByTagName('head')[0]\n const style = document.createElement('style')\n style.type = 'text/css'\n \n if (insertAt === 'top') {\n if (head.firstChild) {\n head.insertBefore(style, head.firstChild)\n } else {\n head.appendChild(style)\n }\n } else {\n head.appendChild(style)\n }\n \n if (style.styleSheet) {\n style.styleSheet.cssText = css\n } else {\n style.appendChild(document.createTextNode(css))\n }\n }\n ","import styleInject from '#style-inject';styleInject(\".relative{position:relative}.container pre{overflow:auto;border-radius:.5rem;padding:1.25rem 1.5rem}.languageLabel{position:absolute;right:.75rem;top:.5rem;font-family:monospace;font-size:.75rem;letter-spacing:-.05em;color:#6b7280d9}\\n\")","import './styles.css';\n// biome-ignore lint/style/useImportType: Package needs React to be imported\nimport React from 'react';\nimport type { BundledLanguage, BundledTheme } from 'shiki';\nimport { useShikiHighlighter } from '@/useShiki';\nimport { clsx } from 'clsx';\n\nexport interface ShikiHighlighterProps {\n /** \n * The programming language for syntax highlighting\n * @see https://shiki.style/languages\n */\n language: BundledLanguage;\n\n /** \n * The code to be highlighted \n */\n children: string;\n\n /** \n * The color theme for syntax highlighting, only Shiki themes are supported at this time\n * @see https://shiki.style/themes\n */\n theme: BundledTheme;\n\n /**\n * Controls the application of default styles to the generated code blocks\n *\n * Default styles include padding, overflow handling, border radius, language label styling, and font settings\n * @default true\n */\n addDefaultStyles?: boolean;\n\n /** \n * Add custom inline styles to the generated code block \n */\n style?: React.CSSProperties;\n\n /** \n * Add custom CSS class names to the generated code block \n */\n className?: string;\n\n /** \n * Whether to show the language label\n * @default true\n */\n showLanguage?: boolean;\n\n /** \n * The HTML element that wraps the generated code block.\n * @default 'pre'\n */\n as?: React.ElementType;\n}\n\n/**\n * ShikiHighlighter is a React component that provides syntax highlighting for code snippets.\n *\n * It uses the Shiki library to render beautiful, theme-based syntax highlighting.\n *\n * @example\n * ```tsx\n * <ShikiHighlighter language=\"tsx\" theme=\"vitesse-black\">\n * {code.trim()}\n * </ShikiHighlighter>\n * ```\n */\nexport const ShikiHighlighter = ({\n language,\n theme,\n addDefaultStyles = true,\n style,\n className,\n showLanguage = true,\n children: code,\n as: Element = 'pre',\n}: ShikiHighlighterProps): React.ReactElement => {\n const highlightedCode = useShikiHighlighter(code, language, theme);\n\n return (\n <Element\n className={clsx(\n 'relative',\n addDefaultStyles && 'container',\n className\n )}\n style={style}\n >\n {showLanguage && language ? (\n <span className=\"languageLabel\" style={style}>\n {language}\n </span>\n ) : null}\n {highlightedCode}\n </Element>\n );\n};\n","import parse from 'html-react-parser';\nimport { type ReactNode, useEffect, useState } from 'react';\nimport { type BundledLanguage, type BundledTheme, codeToHtml } from 'shiki';\nimport { removeTabIndexFromPre } from '@/utils';\n\n/**\n * \n * `useShikiHighlighter` is a custom hook that takes in the code to be highlighted, the language, and the theme, and returns the highlighted code parsed as a ReactNode.\n *\n * @example\n * ```tsx\n * const highlightedCode = useShikiHighlighter(code, language, theme);\n * ```\n * \n */\nexport const useShikiHighlighter = (\n code: string,\n lang: BundledLanguage | undefined,\n theme: BundledTheme\n): ReactNode | null => {\n const [highlightedCode, setHighlightedCode] = useState<ReactNode | null>(\n null\n );\n\n useEffect(() => {\n const highlightCode = async () => {\n try {\n const html = await codeToHtml(code, {\n lang: lang as BundledLanguage,\n theme,\n transformers: [removeTabIndexFromPre],\n });\n setHighlightedCode(parse(html));\n } catch (error) {\n const fallbackHtml = await codeToHtml(code, {\n lang: 'plaintext',\n theme,\n transformers: [removeTabIndexFromPre],\n });\n setHighlightedCode(parse(fallbackHtml));\n }\n };\n\n highlightCode();\n }, [code, theme, lang]);\n\n return highlightedCode;\n};\n","import type { Element } from 'hast';\n\nexport const isInlineCode = (node: Element): boolean => {\n return node.position?.start.line === node.position?.end.line;\n};\n\n\nexport const removeTabIndexFromPre = {\n pre(node: Element) {\n node.properties.tabindex = '-1';\n },\n};\n\nexport type { Element };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Dd,IAAMC,EAAmB,CAAC,CAC/B,SAAAC,EACA,MAAAC,EACA,iBAAAC,EAAmB,GACnB,MAAAC,EACA,UAAAC,EACA,aAAAC,EAAe,GACf,SAAUC,EACV,GAAIC,EAAU,KAChB,IAAiD,CAC/C,IAAMC,EAAkBC,EAAoBH,EAAMN,EAAUC,CAAK,EAEjE,OACES,EAAA,cAACH,EAAA,CACC,UAAWT,EACT,WACAI,GAAoB,YACpBE,CACF,EACA,MAAOD,GAENE,GAAgBL,EACfU,EAAA,cAAC,QAAK,UAAU,gBAAgB,MAAOP,GACpCH,CACH,EACE,KACHQ,CACH,CAEJ","names":["styleInject","css","insertAt","head","style","styleInject","React","parse","useEffect","useState","codeToHtml","isInlineCode","node","removeTabIndexFromPre","useShikiHighlighter","code","lang","theme","highlightedCode","setHighlightedCode","useState","useEffect","html","codeToHtml","removeTabIndexFromPre","parse","fallbackHtml","clsx","ShikiHighlighter","language","theme","addDefaultStyles","style","className","showLanguage","code","Element","highlightedCode","useShikiHighlighter","React"]}
1
+ {"version":3,"sources":["#style-inject:#style-inject","../src/styles.css","../src/ShikiHighlighter.tsx","../src/useShiki.ts","../src/utils.ts"],"sourcesContent":["\n export default function styleInject(css, { insertAt } = {}) {\n if (!css || typeof document === 'undefined') return\n \n const head = document.head || document.getElementsByTagName('head')[0]\n const style = document.createElement('style')\n style.type = 'text/css'\n \n if (insertAt === 'top') {\n if (head.firstChild) {\n head.insertBefore(style, head.firstChild)\n } else {\n head.appendChild(style)\n }\n } else {\n head.appendChild(style)\n }\n \n if (style.styleSheet) {\n style.styleSheet.cssText = css\n } else {\n style.appendChild(document.createTextNode(css))\n }\n }\n ","import styleInject from '#style-inject';styleInject(\".relative{position:relative}.container pre{overflow:auto;border-radius:.5rem;padding:1.25rem 1.5rem}.languageLabel{position:absolute;right:.75rem;top:.5rem;font-family:monospace;font-size:.75rem;letter-spacing:-.05em;color:#6b7280d9}\\n\")","import './styles.css';\nimport React from 'react';\nimport { clsx } from 'clsx';\nimport { useShikiHighlighter } from './useShiki';\nimport type { Language, Theme, HighlighterOptions } from './types';\n\n/**\n * Props for the ShikiHighlighter component\n */\nexport interface ShikiHighlighterProps extends HighlighterOptions {\n /** \n * The programming language for syntax highlighting\n * @see https://shiki.style/languages\n */\n language: Language;\n\n /** \n * The code to be highlighted \n */\n children: string;\n\n /** \n * The color theme for syntax highlighting\n * Supports Shiki and custom textmate themes\n * @see https://shiki.style/themes\n */\n theme: Theme;\n\n /** \n * The delay in milliseconds between streamed updates\n * Use this when highlighting code being streamed on the client\n * @default undefined (no delay)\n */\n delay?: number;\n\n /**\n * Controls the application of default styles to the generated code blocks\n *\n * Default styles include padding, overflow handling, border radius, language label styling, and font settings\n * @default true\n */\n addDefaultStyles?: boolean;\n\n /** \n * Add custom inline styles to the generated code block \n */\n style?: React.CSSProperties;\n\n /** \n * Add custom CSS class names to the generated code block \n */\n className?: string;\n\n /** \n * Whether to show the language label\n * @default true\n */\n showLanguage?: boolean;\n\n /** \n * The HTML element that wraps the generated code block.\n * @default 'pre'\n */\n as?: React.ElementType;\n}\n\n/**\n * ShikiHighlighter is a React component that provides syntax highlighting for code snippets.\n * It uses Shiki to render beautiful, theme-based syntax highlighting with optimized performance.\n *\n * @example\n * ```tsx\n * <ShikiHighlighter \n * language=\"typescript\" \n * theme=\"github-dark\"\n * delay={100} // Optional throttling for streamed updates\n * >\n * {code}\n * </ShikiHighlighter>\n * ```\n */\nexport const ShikiHighlighter = ({\n language,\n theme,\n delay,\n addDefaultStyles = true,\n style,\n className,\n showLanguage = true,\n children: code,\n as: Element = 'pre',\n}: ShikiHighlighterProps): React.ReactElement => {\n const highlightedCode = useShikiHighlighter(code, language, theme, { delay });\n\n return (\n <Element\n className={clsx(\n 'relative',\n addDefaultStyles && 'container',\n className\n )}\n style={style}\n >\n {showLanguage && language ? (\n <span className=\"languageLabel\" style={style}>\n {language}\n </span>\n ) : null}\n {highlightedCode}\n </Element>\n );\n};\n","import { useEffect, useRef, useState, type ReactNode } from 'react';\nimport parse from 'html-react-parser';\n\nimport {\n createHighlighter,\n ShikiError,\n type BundledLanguage,\n type Highlighter\n} from 'shiki';\n\nimport type {\n Language,\n Theme,\n HighlighterOptions,\n TimeoutState\n} from './types';\n\nimport { removeTabIndexFromPre } from '@/utils';\n\n\n/************************************\n ** Singleton highlighter instance **\n ************************************/\nlet highlighterPromise: Promise<Highlighter> | null = null;\n\n\n/***********************************************************\n ** Creates or returns the singleton highlighter instance **\n ***********************************************************/\nconst makeHighlighter = async (theme: Theme): Promise<Highlighter> => {\n if (!highlighterPromise) {\n highlighterPromise = createHighlighter({\n themes: [],\n langs: []\n });\n }\n\n const highlighter = await highlighterPromise;\n await loadTheme(highlighter, theme);\n return highlighter;\n};\n\n\n/************************************************************\n ** Loads a theme dynamically if it hasn't been loaded yet **\n ************************************************************/\nconst loadTheme = async (\n highlighter: Highlighter,\n theme: Theme\n): Promise<void> => {\n try {\n await highlighter.loadTheme(theme);\n } catch (error) {\n if (error instanceof ShikiError) {\n console.warn('Error loading theme:', error.message);\n }\n throw error;\n }\n};\n\n\n/******************************************************************************\n ** Loads a language dynamically, falling back to plaintext if not available ** **\n ******************************************************************************/\nconst loadLanguage = async (\n highlighter: Highlighter,\n lang: Language\n): Promise<string> => {\n const resolvedLanguage = lang ?? 'plaintext';\n\n try {\n await highlighter.loadLanguage(resolvedLanguage as BundledLanguage);\n return resolvedLanguage;\n } catch (error) {\n if (error instanceof ShikiError && error.message.includes('not included in this bundle')) {\n console.warn(`Language '${resolvedLanguage}' not supported, falling back to plaintext`);\n }\n return 'plaintext';\n }\n};\n\n\n/*******************************************************************************\n ** Throttles highlighting operations to prevent overwhelming the highlighter ** **\n *******************************************************************************/\nconst throttleHighlighting = (\n performHighlight: () => Promise<void>,\n timeoutControl: React.MutableRefObject<TimeoutState>,\n throttleMs: number\n) => {\n const now = Date.now();\n clearTimeout(timeoutControl.current.timeoutId);\n\n const delay = Math.max(0, timeoutControl.current.nextAllowedTime - now);\n timeoutControl.current.timeoutId = setTimeout(() => {\n performHighlight().catch(console.error);\n timeoutControl.current.nextAllowedTime = now + throttleMs;\n }, delay);\n};\n\n\n/************************************************************************\n ** A React hook that provides syntax highlighting using Shiki. Uses a **\n ** singleton highlighter instance and supports throttled updates. **\n*************************************************************************/\nexport const useShikiHighlighter = (\n code: string,\n lang: Language,\n theme: Theme,\n options: HighlighterOptions = {}\n): ReactNode | null => {\n const [highlightedCode, setHighlightedCode] = useState<ReactNode | null>(null);\n const delayRef = useRef<TimeoutState>({\n nextAllowedTime: 0,\n timeoutId: undefined\n });\n\n useEffect(() => {\n let isMounted = true;\n\n const highlightCode = async () => {\n const highlighter = await makeHighlighter(theme);\n const language = await loadLanguage(highlighter, lang);\n\n const html = highlighter.codeToHtml(code, {\n lang: language,\n theme,\n transformers: [removeTabIndexFromPre],\n });\n\n if (isMounted) {\n setHighlightedCode(parse(html));\n }\n };\n\n if (options.delay) {\n throttleHighlighting(highlightCode, delayRef, options.delay);\n } else {\n highlightCode().catch(console.error);\n }\n\n return () => {\n isMounted = false;\n clearTimeout(delayRef.current.timeoutId);\n };\n }, [code, lang]);\n\n return highlightedCode;\n};\n","import type { Element } from 'hast';\n\nexport const isInlineCode = (node: Element): boolean => {\n return node.position?.start.line === node.position?.end.line;\n};\n\n\nexport const removeTabIndexFromPre = {\n pre(node: Element) {\n node.properties.tabindex = '-1';\n },\n};\n\nexport type { Element };\n"],"mappings":"AACyB,SAARA,EAA6BC,EAAK,CAAE,SAAAC,CAAS,EAAI,CAAC,EAAG,CAC1D,GAAI,CAACD,GAAO,OAAO,SAAa,IAAa,OAE7C,IAAME,EAAO,SAAS,MAAQ,SAAS,qBAAqB,MAAM,EAAE,CAAC,EAC/DC,EAAQ,SAAS,cAAc,OAAO,EAC5CA,EAAM,KAAO,WAETF,IAAa,OACXC,EAAK,WACPA,EAAK,aAAaC,EAAOD,EAAK,UAAU,EAK1CA,EAAK,YAAYC,CAAK,EAGpBA,EAAM,WACRA,EAAM,WAAW,QA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styleInject","React","clsx","useEffect","useRef","useState","parse","createHighlighter","ShikiError","isInlineCode","node","removeTabIndexFromPre","highlighterPromise","makeHighlighter","theme","createHighlighter","highlighter","loadTheme","error","ShikiError","loadLanguage","lang","resolvedLanguage","throttleHighlighting","performHighlight","timeoutControl","throttleMs","now","delay","useShikiHighlighter","code","options","highlightedCode","setHighlightedCode","useState","delayRef","useRef","useEffect","isMounted","highlightCode","language","html","removeTabIndexFromPre","parse","ShikiHighlighter","language","theme","delay","addDefaultStyles","style","className","showLanguage","code","Element","highlightedCode","useShikiHighlighter","React","clsx"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-shiki",
3
3
  "description": "Syntax highlighter component for react using shiki",
4
- "version": "0.1.2",
4
+ "version": "0.2.0",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Bassim Shahidy",
@@ -46,6 +46,7 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/hast": "^3.0.4",
49
+ "@types/node": "22.10.10",
49
50
  "@types/react": "^18.3.3",
50
51
  "tsup": "^8.2.4"
51
52
  },