Skip to content

Commit

Permalink
use transformNode everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 24, 2023
1 parent 55a0abe commit 5fb34ae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
6 changes: 4 additions & 2 deletions packages/docusaurus-mdx-loader/src/remark/mermaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import visit from 'unist-util-visit';
import {transformNode} from '../utils';

// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
import type {Transformer} from 'unified';
import type {Code} from 'mdast';
Expand All @@ -16,10 +18,10 @@ import type {Code} from 'mdast';
// by theme-mermaid itself
export default function plugin(): Transformer {
return (root) => {
visit(root, 'code', (node: Code, index, parent) => {
visit(root, 'code', (node: Code) => {
if (node.lang === 'mermaid') {
// TODO migrate to mdxJsxFlowElement? cf admonitions
parent!.children.splice(index, 1, {
transformNode(node, {
type: 'mermaidCodeBlock',
data: {
hName: 'mermaid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import visit from 'unist-util-visit';
import escapeHtml from 'escape-html';
import sizeOf from 'image-size';
import logger from '@docusaurus/logger';
import {assetRequireAttributeValue} from '../utils';
import {assetRequireAttributeValue, transformNode} from '../utils';
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
import type {Transformer} from 'unified';
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
Expand Down Expand Up @@ -110,14 +110,12 @@ ${(err as Error).message}`;
}
}

Object.keys(jsxNode).forEach(
(key) => delete jsxNode[key as keyof typeof jsxNode],
);

jsxNode.type = 'mdxJsxTextElement';
jsxNode.name = 'img';
jsxNode.attributes = attributes;
jsxNode.children = [];
transformNode(jsxNode, {
type: 'mdxJsxTextElement',
name: 'img',
attributes,
children: [],
});
}

async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@docusaurus/utils';
import visit from 'unist-util-visit';
import escapeHtml from 'escape-html';
import {assetRequireAttributeValue} from '../utils';
import {assetRequireAttributeValue, transformNode} from '../utils';
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
import type {Transformer} from 'unified';
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
Expand Down Expand Up @@ -90,14 +90,12 @@ async function toAssetRequireNode(

const {children} = node;

Object.keys(jsxNode).forEach(
(key) => delete jsxNode[key as keyof typeof jsxNode],
);

jsxNode.type = 'mdxJsxTextElement';
jsxNode.name = 'a';
jsxNode.attributes = attributes;
jsxNode.children = children;
transformNode(jsxNode, {
type: 'mdxJsxTextElement',
name: 'a',
attributes,
children,
});
}

async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ function isSimpleTextDirective(
): directive is TextDirective {
if (isTextDirective(directive)) {
// Attributes in MDAST = Directive props
const hasAttributes = Object.keys(directive.attributes ?? {}).length > 0;
const hasAttributes =
directive.attributes && Object.keys(directive.attributes).length > 0;
// Children in MDAST = Directive label
const hasChildren = directive.children.length > 0;
return !hasAttributes && !hasChildren;
Expand Down

0 comments on commit 5fb34ae

Please sign in to comment.