Browse Source

fix: open text files

Adam 5 months ago
parent
commit
ee6ceb4c64
2 changed files with 9 additions and 2 deletions
  1. 5 1
      packages/app/src/components/code.tsx
  2. 4 1
      packages/app/src/context/marked.tsx

+ 5 - 1
packages/app/src/components/code.tsx

@@ -12,7 +12,11 @@ export function Code(props: Props) {
   const ctx = useLocal()
   const highlighter = useShiki()
   const [local, others] = splitProps(props, ["class", "classList", "code", "path"])
-  const lang = createMemo(() => getFileExtension(local.path))
+  const lang = createMemo(() => {
+    const ext = getFileExtension(local.path)
+    if (ext in bundledLanguages) return ext
+    return "text"
+  })
 
   let container: HTMLDivElement | undefined
   let isProgrammaticSelection = false

+ 4 - 1
packages/app/src/context/marked.tsx

@@ -2,12 +2,15 @@ import { createContext, useContext, type ParentProps } from "solid-js"
 import { useShiki } from "@/context"
 import { marked } from "marked"
 import markedShiki from "marked-shiki"
-import type { BundledLanguage } from "shiki"
+import { bundledLanguages, type BundledLanguage } from "shiki"
 
 function init(highlighter: ReturnType<typeof useShiki>) {
   return marked.use(
     markedShiki({
       async highlight(code, lang) {
+        if (!(lang in bundledLanguages)) {
+          lang = "text"
+        }
         if (!highlighter.getLoadedLanguages().includes(lang)) {
           await highlighter.loadLanguage(lang as BundledLanguage)
         }