-
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
Open
maximilianfalco
wants to merge
15
commits into
next
Choose a base branch
from
falco/mdxish-cleanup
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
chore: mdxish cleanup #1353
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
91918ee
chore: delete demo docs
maximilianfalco 8953da4
chore(tests): extract shared test helpers into single module
maximilianfalco 037833c
chore: move shared helpers into root helper file
maximilianfalco b99ea83
chore: removed dedundant vitest imports
maximilianfalco d70d1a1
chore: consolidate and unified mdxish import from barrel file
maximilianfalco 6bdea11
chore: move safeMode test from mdxish.test to mdxishAstProcessor.test
maximilianfalco 4849cf6
chore: rename existing mdxish test files
maximilianfalco 45d1c46
chore: split transformers to have an mdxish tests subdir
maximilianfalco 159f335
chore: revert some git mvs
maximilianfalco 5e47943
chore: hoist gemoji to root __tests__
maximilianfalco 9912224
chore: add one liner comments to helper functions
maximilianfalco 0cc9888
update helper function comments
maximilianfalco 5f0397d
chore: move gemoji and use satisfies
maximilianfalco f3fbea3
Merge branch 'falco/mdxish-cleanup' of https://github.com/readmeio/maβ¦
maximilianfalco 5ee167e
Merge branch 'next' of https://github.com/readmeio/markdown into falcβ¦
maximilianfalco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 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 => { | ||
|
|
@@ -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 }); | ||
| }; | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.