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

fix shikierror #185

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cli/inbox/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ interface CodeBlockProps {
code: string;
}

const highlighter = await getSingletonHighlighter({
themes: ["github-light"],
langs: ["http", "json"],
});
const highlighter = await getSingletonHighlighter();
await highlighter.loadTheme("github-light");
await highlighter.loadLanguage("http");
await highlighter.loadLanguage("json");
Comment on lines +111 to +114
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider moving highlighter initialization into a lazy-loaded singleton pattern.

The current module-level initialization of the highlighter could block module loading. Consider refactoring to a lazy-loaded singleton pattern with proper error handling.

Here's a suggested implementation:

-const highlighter = await getSingletonHighlighter();
-await highlighter.loadTheme("github-light");
-await highlighter.loadLanguage("http");
-await highlighter.loadLanguage("json");
+let highlighterInstance: Awaited<ReturnType<typeof getSingletonHighlighter>> | null = null;
+
+async function getHighlighter() {
+  if (highlighterInstance) return highlighterInstance;
+  try {
+    highlighterInstance = await getSingletonHighlighter();
+    await highlighterInstance.loadTheme("github-light");
+    await Promise.all([
+      highlighterInstance.loadLanguage("http"),
+      highlighterInstance.loadLanguage("json")
+    ]);
+    return highlighterInstance;
+  } catch (error) {
+    console.error("Failed to initialize syntax highlighter:", error);
+    throw error;
+  }
+}

Then update the CodeBlock component to handle loading and error states:

 const CodeBlock: FC<CodeBlockProps> = ({ language, code }: CodeBlockProps) => {
+  const [html, setHtml] = useState<string | null>(null);
+  const [error, setError] = useState<Error | null>(null);
+
+  useEffect(() => {
+    async function highlight() {
+      try {
+        const highlighter = await getHighlighter();
+        const result = highlighter.codeToHtml(code, {
+          lang: language,
+          theme: "github-light",
+        });
+        setHtml(result);
+      } catch (e) {
+        setError(e instanceof Error ? e : new Error(String(e)));
+      }
+    }
+    highlight();
+  }, [code, language]);
+
+  if (error) {
+    return <div class="alert alert-danger m-3">Failed to highlight code: {error.message}</div>;
+  }
+  if (!html) {
+    return <div class="placeholder-glow m-3"><span class="placeholder col-12"></span></div>;
+  }
-  const result = highlighter.codeToHtml(code, {
-    lang: language,
-    theme: "github-light",
-  });
-  return <div dangerouslySetInnerHTML={{ __html: result }} class="m-3" />;
+  return <div dangerouslySetInnerHTML={{ __html: html }} class="m-3" />;
 };

Benefits:

  • Lazy loading prevents blocking module initialization
  • Parallel loading of languages improves performance
  • Proper error handling and loading states
  • Better user experience with loading indicators

Committable suggestion skipped: line range outside the PR's diff.


const CodeBlock: FC<CodeBlockProps> = ({ language, code }: CodeBlockProps) => {
const result = highlighter.codeToHtml(code, {
Expand Down