-
Notifications
You must be signed in to change notification settings - Fork 18
chore: mdxish cleanup #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
chore: mdxish cleanup #1353
Changes from 11 commits
91918ee
8953da4
037833c
b99ea83
d70d1a1
6bdea11
4849cf6
45d1c46
159f335
5e47943
9912224
0cc9888
5f0397d
f3fbea3
5ee167e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay yea this makes sense but requires us to change this basically adding the |
||||||
|
|
||||||
| /** 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 => { | ||||||
|
|
@@ -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. */ | ||||||
|
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 }); | ||||||
| }; | ||||||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.