Kaynağa Gözat

Add codeblock remarkPlugin to renderer and remove dynamic import

arvid-e 8 ay önce
ebeveyn
işleme
ef7576773a

+ 3 - 0
apps/app/src/client/services/renderer/renderer.tsx

@@ -67,6 +67,7 @@ export const generateViewOptions = (
     mermaid.remarkPlugin,
     xsvToTable.remarkPlugin,
     attachment.remarkPlugin,
+    codeBlock.remarkPlugin,
     remarkGithubAdmonitionsToDirectives,
     callout.remarkPlugin,
     lsxGrowiDirective.remarkPlugin,
@@ -175,6 +176,7 @@ export const generateSimpleViewOptions = (
     mermaid.remarkPlugin,
     xsvToTable.remarkPlugin,
     attachment.remarkPlugin,
+    codeBlock.remarkPlugin,
     remarkGithubAdmonitionsToDirectives,
     callout.remarkPlugin,
     lsxGrowiDirective.remarkPlugin,
@@ -272,6 +274,7 @@ export const generatePreviewOptions = (config: RendererConfigExt, pagePath: stri
     mermaid.remarkPlugin,
     xsvToTable.remarkPlugin,
     attachment.remarkPlugin,
+    codeBlock.remarkPlugin,
     remarkGithubAdmonitionsToDirectives,
     callout.remarkPlugin,
     lsxGrowiDirective.remarkPlugin,

+ 0 - 3
apps/app/src/features/openai/server/utils/convert-markdown-to-html.ts

@@ -9,8 +9,6 @@ import type * as RemarkRehype from 'remark-rehype';
 import type * as Unified from 'unified';
 import type * as UnistUtilVisit from 'unist-util-visit';
 
-import { remarkPlugin as inlineCodeRemarkPlugin } from '../../../../services/renderer/remark-plugins/codeblock';
-
 interface ModuleCache {
   unified?: typeof Unified.unified;
   visit?: typeof UnistUtilVisit.visit;
@@ -90,7 +88,6 @@ export const convertMarkdownToHtml = async(revisionBody: string, args: ConvertMa
 
   const processor = unified()
     .use(remarkParse)
-    .use(inlineCodeRemarkPlugin)
     .use(sanitizeMarkdown)
     .use(remarkRehype)
     .use(rehypeMeta, {

+ 2 - 39
apps/app/src/services/renderer/remark-plugins/codeblock.ts

@@ -3,7 +3,7 @@ import type { Schema as SanitizeOption } from 'hast-util-sanitize';
 import type { InlineCode as MdastInlineCode, Html, Text } from 'mdast';
 import type { Plugin } from 'unified';
 import type { Node, Parent, Point } from 'unist';
-import type { visit as UnistUtilVisitFunction } from 'unist-util-visit';
+import { visit } from 'unist-util-visit';
 
 
 type InlineCode = MdastInlineCode & {
@@ -16,47 +16,10 @@ type InlineCode = MdastInlineCode & {
 const SUPPORTED_CODE = ['inline'];
 
 export const remarkPlugin: Plugin = () => {
-  let visit: typeof UnistUtilVisitFunction | undefined;
-  let loadPromise: Promise<void> | undefined;
-  let isLoaded = false;
 
-  // Function to ensure visit is loaded
-  const ensureVisitLoaded = async() => {
-    if (isLoaded) {
-      return;
-    }
-    if (loadPromise) {
-      return loadPromise;
-    }
-
-    loadPromise = (async() => {
-      try {
-        const mod = await import('unist-util-visit');
-        visit = mod.visit;
-        isLoaded = true;
-      }
-      catch (error) {
-        throw error;
-      }
-      finally {
-        loadPromise = undefined;
-      }
-    })();
-    return loadPromise;
-  };
-
-  return async(tree: Node) => {
+  return (tree: Node) => {
     const defaultPoint: Point = { line: 1, column: 1, offset: 0 };
 
-    // Ensure visit is loaded before proceeding
-    if (!visit) {
-      await ensureVisitLoaded();
-    }
-
-    if (typeof visit === 'undefined') {
-      throw new Error("Failed to load 'unist-util-visit' dependency.");
-    }
-
     visit(tree, 'html', (node: Html, index: number | undefined, parent: Parent | undefined) => {
       // Find <code> tag
       if (typeof node.value === 'string' && node.value.toLowerCase() === '<code>') {