Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
54 changes: 54 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,9 +1,60 @@
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';

/** Recursively searches a hast tree and returns the first element matching the given tag name. */
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;
}

/** Recursively searches a hast tree and returns all elements matching the given tag name. */
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!


/** A minimal stub satisfying the RMDXModule interface. Use when tests need a component module without real rendering. */
export const stubModule = {
default: () => null as unknown as React.JSX.Element,
Toc: null,
toc: [],
} satisfies RMDXModule;

/** Builds a component map from a list of names, each mapped to `stubModule`. Use to mock the components option in compile/run. */
export const makeComponents = (...names: string[]) =>
names.reduce<Record<string, RMDXModule>>((acc, name) => {
acc[name] = stubModule;
return acc;
}, {});

/** Temporarily suppresses console output for the given method (default: `error`) while executing a callback. Restores the original implementation afterward. */
export const silenceConsole =
(prop: keyof Console = 'error', impl = () => {}) =>
fn => {
Expand All @@ -18,17 +69,20 @@ export const silenceConsole =
}
};

/** Compiles and runs a markdown string through the full mdx pipeline. Returns the default export (React component) unless `getDefault` is false, in which case the full module is returned. */
export const execute = (doc: string, compileOpts = {}, runOpts = {}, { getDefault = true } = {}) => {
const code = compile(doc, compileOpts);
const mod = run(code, runOpts);

return getDefault ? mod.default : mod;
};

/** Runs the legacy-to-v7 migration on a markdown string using the legacy rdmd parser. */
export const migrate = (doc: string) => {
return baseMigrate(doc, { rdmd });
};

/** Parses a markdown string into an mdast tree using the v6-compatible parser with legacy rdmd support. */
export const mdastV6Wrapper = (doc: string) => {
return mdastV6(doc, { rdmd });
};
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.

Loading
Loading