Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
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 All @@ -13,6 +13,5 @@ describe('gemoji transformer', () => {
<p>πŸ˜ƒ</p>
<p><img src="/public/img/emojis/owlbert.png" alt=":owlbert:" title=":owlbert:" class="emoji" align="absmiddle" height="20" width="20"></p>"
`);

});
});
});
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,
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


/** 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 pipeline. Returns the default export (React component) unless `getDefault` is false, in which case the full module is returned. */
Comment thread
maximilianfalco marked this conversation as resolved.
Outdated
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.

Loading