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

+ 1 - 1
apps/app/src/services/renderer/rehype-plugins/relative-links-by-pukiwiki-like-linker.spec.ts

@@ -1,5 +1,5 @@
+import type { Nodes as HastNode } from 'hast';
 import { select } from 'hast-util-select';
 import { select } from 'hast-util-select';
-import type { Node as HastNode } from 'hast-util-select/lib/types';
 import parse from 'remark-parse';
 import parse from 'remark-parse';
 import rehype from 'remark-rehype';
 import rehype from 'remark-rehype';
 import { unified } from 'unified';
 import { unified } from 'unified';

+ 1 - 1
apps/app/src/services/renderer/rehype-plugins/relative-links.spec.ts

@@ -1,6 +1,6 @@
 
 
+import type { Nodes as HastNode } from 'hast';
 import { select } from 'hast-util-select';
 import { select } from 'hast-util-select';
-import type { Node as HastNode } from 'hast-util-select/lib/types';
 import parse from 'remark-parse';
 import parse from 'remark-parse';
 import remarkRehype from 'remark-rehype';
 import remarkRehype from 'remark-rehype';
 import { unified } from 'unified';
 import { unified } from 'unified';

+ 12 - 1
apps/app/src/services/renderer/remark-plugins/attachment.ts

@@ -3,9 +3,19 @@ import path from 'path';
 import type { Schema as SanitizeOption } from 'hast-util-sanitize';
 import type { Schema as SanitizeOption } from 'hast-util-sanitize';
 import type { Link } from 'mdast';
 import type { Link } from 'mdast';
 import type { Plugin } from 'unified';
 import type { Plugin } from 'unified';
-import type { Node } from 'unist';
 import { visit } from 'unist-util-visit';
 import { visit } from 'unist-util-visit';
 
 
+declare module 'mdast' {
+  interface LinkData {
+    hName?: string,
+    hProperties?: {
+      attachmentId?: string,
+      url?: string,
+      attachmentName?: PhrasingContent,
+    }
+  }
+}
+
 const SUPPORTED_ATTRIBUTES = ['attachmentId', 'url', 'attachmentName'];
 const SUPPORTED_ATTRIBUTES = ['attachmentId', 'url', 'attachmentName'];
 
 
 const isAttachmentLink = (url: string): boolean => {
 const isAttachmentLink = (url: string): boolean => {
@@ -16,6 +26,7 @@ const isAttachmentLink = (url: string): boolean => {
 
 
 const rewriteNode = (node: Link) => {
 const rewriteNode = (node: Link) => {
   const attachmentId = path.basename(node.url as string);
   const attachmentId = path.basename(node.url as string);
+
   const data = node.data ?? (node.data = {});
   const data = node.data ?? (node.data = {});
   data.hName = 'attachment';
   data.hName = 'attachment';
   data.hProperties = {
   data.hProperties = {

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

@@ -1,5 +1,8 @@
 import csvToMarkdownTable from 'csv-to-markdown-table';
 import csvToMarkdownTable from 'csv-to-markdown-table';
-import type { Code, Table } from 'mdast';
+import type {
+  Code, Parent,
+} from 'mdast';
+import type { Options } from 'mdast-util-from-markdown';
 import { fromMarkdown } from 'mdast-util-from-markdown';
 import { fromMarkdown } from 'mdast-util-from-markdown';
 import { gfmTableFromMarkdown } from 'mdast-util-gfm-table';
 import { gfmTableFromMarkdown } from 'mdast-util-gfm-table';
 import { gfmTable } from 'micromark-extension-gfm-table';
 import { gfmTable } from 'micromark-extension-gfm-table';
@@ -14,7 +17,7 @@ function isXsv(lang?: string | null | undefined): lang is Lang {
 }
 }
 
 
 function rewriteNode(node: Node, lang: Lang) {
 function rewriteNode(node: Node, lang: Lang) {
-  const tableContents = node.value as string;
+  const tableContents = (node as Code).value as string;
 
 
   const tableDoc = csvToMarkdownTable(
   const tableDoc = csvToMarkdownTable(
     tableContents,
     tableContents,
@@ -24,12 +27,12 @@ function rewriteNode(node: Node, lang: Lang) {
   const tableTree = fromMarkdown(tableDoc, {
   const tableTree = fromMarkdown(tableDoc, {
     extensions: [gfmTable],
     extensions: [gfmTable],
     mdastExtensions: [gfmTableFromMarkdown],
     mdastExtensions: [gfmTableFromMarkdown],
-  });
+  } as Options);
 
 
   // replace node
   // replace node
   if (tableTree.children[0] != null) {
   if (tableTree.children[0] != null) {
     node.type = 'table';
     node.type = 'table';
-    (node as Table).children = tableTree.children[0].children;
+    (node as Parent).children = (tableTree.children[0] as Parent).children;
   }
   }
 }
 }
 
 
@@ -38,7 +41,7 @@ export const remarkPlugin: Plugin = function() {
     visit(tree, (node) => {
     visit(tree, (node) => {
       if (node.type === 'code') {
       if (node.type === 'code') {
         if (isXsv((node as Code).lang)) {
         if (isXsv((node as Code).lang)) {
-          rewriteNode((node as Code), (node as Code).lang as Lang);
+          rewriteNode(node, (node as Code).lang as Lang);
         }
         }
       }
       }
     });
     });

+ 21 - 6
packages/remark-drawio/src/services/renderer/remark-drawio.ts

@@ -1,21 +1,36 @@
 import type { Schema as SanitizeOption } from 'hast-util-sanitize';
 import type { Schema as SanitizeOption } from 'hast-util-sanitize';
+import type {
+  Code, Node, Parent,
+} from 'mdast';
 import type { Plugin } from 'unified';
 import type { Plugin } from 'unified';
-import type { Node } from 'unist';
 import { visit } from 'unist-util-visit';
 import { visit } from 'unist-util-visit';
 
 
 const SUPPORTED_ATTRIBUTES = ['diagramIndex', 'bol', 'eol'];
 const SUPPORTED_ATTRIBUTES = ['diagramIndex', 'bol', 'eol'];
 
 
+declare module 'mdast' {
+  interface Data {
+    hName?: string,
+    hProperties?: {
+      diagramIndex?: number,
+      bol?: number,
+      eol?: number,
+      key?: string,
+    }
+  }
+}
+
 type Lang = 'drawio';
 type Lang = 'drawio';
 
 
-function isDrawioBlock(lang: unknown): lang is Lang {
-  return /^drawio$/.test(lang as string);
+function isDrawioBlock(lang: string | undefined | null): lang is Lang {
+  return /^drawio$/.test(lang ?? '');
 }
 }
 
 
 function rewriteNode(node: Node, index: number) {
 function rewriteNode(node: Node, index: number) {
-  const data = node.data ?? (node.data = {});
 
 
   node.type = 'paragraph';
   node.type = 'paragraph';
-  node.children = [{ type: 'text', value: node.value }];
+  (node as Parent).children = [{ type: 'text', value: (node as Code).value }];
+
+  const data = node.data ?? (node.data = {});
   data.hName = 'drawio';
   data.hName = 'drawio';
   data.hProperties = {
   data.hProperties = {
     diagramIndex: index,
     diagramIndex: index,
@@ -29,7 +44,7 @@ export const remarkPlugin: Plugin = function() {
   return (tree) => {
   return (tree) => {
     visit(tree, (node, index) => {
     visit(tree, (node, index) => {
       if (node.type === 'code') {
       if (node.type === 'code') {
-        if (isDrawioBlock(node.lang)) {
+        if (isDrawioBlock((node as Code).lang)) {
           rewriteNode(node, index ?? 0);
           rewriteNode(node, index ?? 0);
         }
         }
       }
       }