reiji-h 1 год назад
Родитель
Сommit
f93de4c63f

+ 3 - 3
apps/app/src/features/mermaid/services/mermaid.ts

@@ -11,9 +11,9 @@ function rewriteNode(node: Code) {
 
 export const remarkPlugin: Plugin = function() {
   return (tree) => {
-    visit(tree, (node) => {
-      if (node.type === 'code' && (node as Code).lang === 'mermaid') {
-        rewriteNode(node as Code);
+    visit(tree, 'code', (node: Code) => {
+      if (node.lang === 'mermaid') {
+        rewriteNode(node);
       }
     });
   };

+ 6 - 8
apps/app/src/services/renderer/rehype-plugins/relocate-toc.ts

@@ -1,6 +1,7 @@
 import rehypeToc from 'rehype-toc';
 import type { HtmlElementNode } from 'rehype-toc';
 import type { Plugin } from 'unified';
+import { visit } from 'unist-util-visit';
 
 type StoreTocPluginParams = {
   storeTocNode: (toc: HtmlElementNode) => void,
@@ -22,13 +23,10 @@ export const rehypePluginStore: Plugin<[StoreTocPluginParams]> = (options) => {
 
 
 // method for replace <ol> to <ul>
-const replaceOlToUl = (children: HtmlElementNode[]) => {
-  children.forEach((child) => {
-    if (child.type === 'element' && child.tagName === 'ol') {
-      child.tagName = 'ul';
-    }
-    if (child.children != null) {
-      replaceOlToUl(child.children as HtmlElementNode[]);
+const replaceOlToUl = (tree: HtmlElementNode) => {
+  visit(tree, 'element', (node: HtmlElementNode) => {
+    if (node.tagName === 'ol') {
+      node.tagName = 'ul';
     }
   });
 };
@@ -44,7 +42,7 @@ export const rehypePluginRestore: Plugin<[RestoreTocPluginParams]> = (options) =
     headings: ['h1', 'h2', 'h3'],
     customizeTOC: () => {
       if (tocNode != null) {
-        replaceOlToUl([tocNode]); // replace <ol> to <ul>
+        replaceOlToUl(tocNode); // replace <ol> to <ul>
 
         // restore toc
         return tocNode;

+ 4 - 6
apps/app/src/services/renderer/remark-plugins/attachment.ts

@@ -25,7 +25,7 @@ const isAttachmentLink = (url: string): boolean => {
 };
 
 const rewriteNode = (node: Link) => {
-  const attachmentId = path.basename(node.url as string);
+  const attachmentId = path.basename(node.url);
 
   const data = node.data ?? (node.data = {});
   data.hName = 'attachment';
@@ -39,11 +39,9 @@ const rewriteNode = (node: Link) => {
 
 export const remarkPlugin: Plugin = () => {
   return (tree) => {
-    visit(tree, (node) => {
-      if (node.type === 'link') {
-        if (isAttachmentLink((node as Link).url as string)) {
-          rewriteNode(node as Link);
-        }
+    visit(tree, 'link', (node: Link) => {
+      if (isAttachmentLink(node.url)) {
+        rewriteNode(node);
       }
     });
   };

+ 4 - 6
apps/app/src/services/renderer/remark-plugins/codeblock.ts

@@ -9,12 +9,10 @@ import { addClassToProperties } from '../rehype-plugins/add-class';
 
 export const remarkPlugin: Plugin = () => {
   return (tree) => {
-    visit(tree, (node) => {
-      if (node.type === 'inlineCode') {
-        const data = (node as InlineCode).data || (node.data = {});
-        // setting inline for rehypePlugin
-        data.hProperties = { inline: true };
-      }
+    visit(tree, 'inlineCode', (node: InlineCode) => {
+      const data = node.data || (node.data = {});
+      // setting inline for rehypePlugin
+      data.hProperties = { inline: true };
     });
   };
 };

+ 5 - 9
apps/app/src/services/renderer/remark-plugins/xsv-to-table.ts

@@ -1,7 +1,5 @@
 import csvToMarkdownTable from 'csv-to-markdown-table';
-import type {
-  Code, Parent,
-} from 'mdast';
+import type { Code, Parent } from 'mdast';
 import type { Options } from 'mdast-util-from-markdown';
 import { fromMarkdown } from 'mdast-util-from-markdown';
 import { gfmTableFromMarkdown } from 'mdast-util-gfm-table';
@@ -17,7 +15,7 @@ function isXsv(lang?: string | null | undefined): lang is Lang {
 }
 
 function rewriteNode(node: Node, lang: Lang) {
-  const tableContents = (node as Code).value as string;
+  const tableContents = (node as Code).value;
 
   const tableDoc = csvToMarkdownTable(
     tableContents,
@@ -38,11 +36,9 @@ function rewriteNode(node: Node, lang: Lang) {
 
 export const remarkPlugin: Plugin = function() {
   return (tree) => {
-    visit(tree, (node) => {
-      if (node.type === 'code') {
-        if (isXsv((node as Code).lang)) {
-          rewriteNode(node, (node as Code).lang as Lang);
-        }
+    visit(tree, 'code', (node: Code) => {
+      if (isXsv(node.lang)) {
+        rewriteNode(node, node.lang);
       }
     });
   };

+ 3 - 5
packages/remark-drawio/src/services/renderer/remark-drawio.ts

@@ -42,11 +42,9 @@ function rewriteNode(node: Node, index: number) {
 
 export const remarkPlugin: Plugin = function() {
   return (tree) => {
-    visit(tree, (node, index) => {
-      if (node.type === 'code') {
-        if (isDrawioBlock((node as Code).lang)) {
-          rewriteNode(node, index ?? 0);
-        }
+    visit(tree, 'code', (node: Code, index) => {
+      if (isDrawioBlock(node.lang)) {
+        rewriteNode(node, index ?? 0);
       }
     });
   };