Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions __tests__/helpers.ts
Comment thread
maximilianfalco marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
import type { Element, Root, RootContent } from 'hast';

import * as rdmd from '@readme/markdown-legacy';

import { vi } from 'vitest';

import { run, compile, migrate as baseMigrate, mdastV6 } from '../index';
import { type RMDXModule } from '../types';

export function findElementByTagName(node: Root | RootContent, tagName: string): Element | null {
if ('type' in node && node.type === 'element' && 'tagName' in node && node.tagName === tagName) {
return node;
}

if ('children' in node && Array.isArray(node.children)) {
return node.children.reduce<Element | null>((found, child) => {
if (found) return found;
return findElementByTagName(child, tagName);
}, null);
}

return null;
}

export function findElementsByTagName(node: Root | RootContent, tagName: string): Element[] {
const results: Element[] = [];

if ('type' in node && node.type === 'element' && 'tagName' in node && node.tagName === tagName) {
results.push(node);
}

if ('children' in node && Array.isArray(node.children)) {
node.children.forEach(child => {
results.push(...findElementsByTagName(child, tagName));
});
}

return results;
}
Comment on lines +10 to +41

@kevinports kevinports Feb 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty for moving these into shared helpers, cleans up so much repetetive code!


export const stubModule = {
default: () => null,
Toc: null,
toc: [],
} as unknown as RMDXModule;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor detail but is it possible to avoid casting with an assertion here?

Suggested change
} as unknown as RMDXModule;
} satisfies RMDXModule;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay yea this makes sense but requires us to change this

default: () => null as unknown as React.JSX.Element,

basically adding the as unknown as above it


export const makeComponents = (...names: string[]) =>
names.reduce<Record<string, RMDXModule>>((acc, name) => {
acc[name] = stubModule;
return acc;
}, {});

export const silenceConsole =
(prop: keyof Console = 'error', impl = () => {}) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mix } from '../../../lib';
import { mix } from '../../lib';

describe('gemoji transformer', () => {
it('should transform shortcodes back to emojis', () => {
Expand Down
121 changes: 0 additions & 121 deletions __tests__/lib/mdxish/demo-docs/mdxish.md

This file was deleted.

79 changes: 0 additions & 79 deletions __tests__/lib/mdxish/demo-docs/rdmd.md

This file was deleted.

108 changes: 0 additions & 108 deletions __tests__/lib/mdxish/demo-docs/rmdx.md

This file was deleted.

17 changes: 1 addition & 16 deletions __tests__/lib/mdxish/legacy-variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,7 @@ import type { CustomComponents } from '../../../types';
import type { Element, Text } from 'hast';

import { mdxish } from '../../../lib';

function findElementByTagName(tree: Element, tagName: string): Element | null {
if (tree.tagName === tagName) {
return tree;
}
// Recursively search children
let result: Element | null = null;
tree.children.some(child => {
if ('tagName' in child) {
result = findElementByTagName(child as Element, tagName);
return result !== null;
}
return false;
});
return result;
}
import { findElementByTagName } from '../../helpers';

describe('legacy variables resolution', () => {
describe('basic resolution', () => {
Expand Down
Loading