-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
70 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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,34 +1,19 @@ | ||
import assert from 'node:assert/strict' | ||
import { readdir, readFile, writeFile } from 'node:fs/promises' | ||
import { test } from 'node:test' | ||
|
||
import { compile } from '@mdx-js/mdx' | ||
import prettier from 'prettier' | ||
import rehypeMdxImportMedia from 'rehype-mdx-import-media' | ||
import { compileSync } from '@mdx-js/mdx' | ||
import rehypeMdxImportMedia, { type RehypeMdxImportMediaOptions } from 'rehype-mdx-import-media' | ||
import rehypeRaw from 'rehype-raw' | ||
import { read } from 'to-vfile' | ||
|
||
const fixturesDir = new URL('../fixtures/', import.meta.url) | ||
const tests = await readdir(fixturesDir) | ||
import { testFixturesDirectory } from 'snapshot-fixtures' | ||
|
||
for (const name of tests) { | ||
test(name, async () => { | ||
const path = new URL(`${name}/`, fixturesDir) | ||
const input = await read(new URL('input.md', path)) | ||
const expected = new URL('expected.jsx', path) | ||
const options = JSON.parse(await readFile(new URL('options.json', path), 'utf8')) | ||
const result = await compile(input, { | ||
rehypePlugins: [rehypeRaw, [rehypeMdxImportMedia, options]], | ||
jsx: true | ||
}) | ||
const prettierConfig = await prettier.resolveConfig(expected.pathname, { editorconfig: true }) | ||
const output = await prettier.format( | ||
String(result), | ||
{ ...prettierConfig, filepath: expected.pathname }! | ||
) | ||
if (process.argv.includes('update')) { | ||
await writeFile(expected, output) | ||
testFixturesDirectory<RehypeMdxImportMediaOptions>({ | ||
directory: new URL('../fixtures', import.meta.url), | ||
prettier: true, | ||
tests: { | ||
'expected.jsx'(file, options) { | ||
return String( | ||
compileSync(file, { | ||
rehypePlugins: [rehypeRaw, [rehypeMdxImportMedia, options]], | ||
jsx: true | ||
}) | ||
) | ||
} | ||
assert.equal(output, await readFile(expected, 'utf8')) | ||
}) | ||
} | ||
} | ||
}) |