Skip to content

Commit

Permalink
add visit type
Browse files Browse the repository at this point in the history
  • Loading branch information
reiji-h committed Aug 13, 2024
1 parent 0de8aaf commit f93de4c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 37 deletions.
6 changes: 3 additions & 3 deletions apps/app/src/features/mermaid/services/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
};
Expand Down
14 changes: 6 additions & 8 deletions apps/app/src/services/renderer/rehype-plugins/relocate-toc.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
}
});
};
Expand All @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions apps/app/src/services/renderer/remark-plugins/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
});
};
Expand Down
10 changes: 4 additions & 6 deletions apps/app/src/services/renderer/remark-plugins/codeblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
};
};
Expand Down
14 changes: 5 additions & 9 deletions apps/app/src/services/renderer/remark-plugins/xsv-to-table.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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);
}
});
};
Expand Down
8 changes: 3 additions & 5 deletions packages/remark-drawio/src/services/renderer/remark-drawio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
};
Expand Down

0 comments on commit f93de4c

Please sign in to comment.