Sfoglia il codice sorgente

update remark plugin type

ryoji-s 2 anni fa
parent
commit
472d4c3e14
1 ha cambiato i file con 15 aggiunte e 20 eliminazioni
  1. 15 20
      apps/app/src/services/renderer/remark-plugins/attachment.ts

+ 15 - 20
apps/app/src/services/renderer/remark-plugins/attachment.ts

@@ -1,5 +1,6 @@
 import { Schema as SanitizeOption } from 'hast-util-sanitize';
 import { Plugin } from 'unified';
+import { Node } from 'unist';
 import { visit } from 'unist-util-visit';
 
 const SUPPORTED_ATTRIBUTES = ['attachmentId', 'url', 'attachmentName'];
@@ -10,29 +11,23 @@ const isAttachmentLink = (url: string) => {
   return url.match(attachmentUrlFormat);
 };
 
+const rewriteNode = (node: Node) => {
+  const pathName = (node.url as string).split('/');
+  const data = node.data ?? (node.data = {});
+  data.hName = 'attachment';
+  data.hProperties = {
+    attachmentId:  pathName[2],
+    url: node.url,
+    attachmentName: (node.children as any)[0]?.value,
+  };
+};
+
 export const remarkPlugin: Plugin = () => {
   return (tree) => {
-    // TODO: do not use any for node.children[0].value
-    visit(tree, (node: any) => {
+    visit(tree, (node) => {
       if (node.type === 'link') {
-        if (isAttachmentLink(node.url)) {
-          const pathName = node.url.split('/');
-          const data = node.data ?? (node.data = {});
-          data.hName = 'attachment';
-          data.hProperties = {
-            attachmentId: pathName[2],
-            url: node.url,
-            attachmentName: node.children[0].value,
-          };
-
-          // omit position to fix the key regardless of its position
-          // see:
-          //   https://github.com/remarkjs/react-markdown/issues/703
-          //   https://github.com/remarkjs/react-markdown/issues/466
-          //
-          //   https://github.com/remarkjs/react-markdown/blob/a80dfdee2703d84ac2120d28b0e4998a5b417c85/lib/ast-to-react.js#L201-L204
-          //   https://github.com/remarkjs/react-markdown/blob/a80dfdee2703d84ac2120d28b0e4998a5b417c85/lib/ast-to-react.js#L217-L222
-          delete node.position;
+        if (isAttachmentLink(node.url as string)) {
+          rewriteNode(node);
         }
       }
     });