Skip to content

Commit 07ad635

Browse files
authored
fix: allow html syntax in MDX v2 with format md (#8960)
* attempt to support html embeds in mdx with format md * refactor mdx loader + support embedding html in commonmark thanks to rehype-raw * extract processor code * refactor processor code * extract format + unit test * try to refactor processor * try to refactor processor * adjust md page * do not apply rehype-raw when format is mdx * fix lint issue
1 parent af9a4f2 commit 07ad635

File tree

11 files changed

+491
-173
lines changed

11 files changed

+491
-173
lines changed

jest/deps.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ declare module 'to-vfile' {
1313
export function read(path: string, encoding?: string): Promise<VFile>;
1414
}
1515

16-
declare module 'remark-rehype';
17-
18-
declare module 'rehype-stringify';
19-
2016
declare module '@testing-utils/git' {
2117
const createTempRepo: typeof import('./utils/git').createTempRepo;
2218
export {createTempRepo};

packages/docusaurus-mdx-loader/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"image-size": "^1.0.2",
3333
"mdast-util-to-string": "^3.0.0",
3434
"mdast-util-mdx": "^2.0.0",
35+
"rehype-raw": "^6.1.1",
3536
"remark-comment": "^1.0.0",
3637
"remark-gfm": "^3.0.1",
3738
"remark-directive": "^2.0.1",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {getFormat} from '../format';
9+
10+
describe('getFormat', () => {
11+
it('uses frontMatter format over anything else', () => {
12+
expect(getFormat({frontMatterFormat: 'md', filePath: 'xyz.md'})).toBe('md');
13+
expect(getFormat({frontMatterFormat: 'md', filePath: 'xyz.mdx'})).toBe(
14+
'md',
15+
);
16+
expect(getFormat({frontMatterFormat: 'mdx', filePath: 'xyz.md'})).toBe(
17+
'mdx',
18+
);
19+
expect(getFormat({frontMatterFormat: 'mdx', filePath: 'xyz.mdx'})).toBe(
20+
'mdx',
21+
);
22+
});
23+
24+
it('detects appropriate format from file extension', () => {
25+
expect(getFormat({frontMatterFormat: 'detect', filePath: 'xyz.md'})).toBe(
26+
'md',
27+
);
28+
expect(
29+
getFormat({frontMatterFormat: 'detect', filePath: 'xyz.markdown'}),
30+
).toBe('md');
31+
32+
expect(
33+
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.md'}),
34+
).toBe('md');
35+
expect(
36+
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.markdown'}),
37+
).toBe('md');
38+
expect(getFormat({frontMatterFormat: 'detect', filePath: 'xyz.mdx'})).toBe(
39+
'mdx',
40+
);
41+
expect(
42+
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.mdx'}),
43+
).toBe('mdx');
44+
45+
expect(
46+
getFormat({frontMatterFormat: 'detect', filePath: 'xyz.unknown'}),
47+
).toBe('mdx');
48+
expect(
49+
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.unknown'}),
50+
).toBe('mdx');
51+
});
52+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// import {createProcessor} from '../processor';
9+
// import type {Options} from '../loader';
10+
11+
/*
12+
async function testProcess({
13+
format,
14+
options,
15+
}: {
16+
format: 'md' | 'mdx';
17+
options: Options;
18+
}) {
19+
return async (content: string) => {
20+
const processor = await createProcessor({format, options});
21+
return processor.process(content);
22+
};
23+
}
24+
*/
25+
26+
describe('md processor', () => {
27+
it('parses simple commonmark', async () => {
28+
// TODO no tests for now, wait until ESM support
29+
// Jest does not support well ESM modules
30+
// It would require to vendor too much Unified modules as CJS
31+
// See https://mdxjs.com/docs/troubleshooting-mdx/#esm
32+
});
33+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import path from 'path';
9+
import type {MDXFrontMatter} from './frontMatter';
10+
11+
// Copied from https://mdxjs.com/packages/mdx/#optionsmdextensions
12+
// Although we are likely to only use .md / .mdx anyway...
13+
const mdFormatExtensions = [
14+
'.md',
15+
'.markdown',
16+
'.mdown',
17+
'.mkdn',
18+
'.mkd',
19+
'.mdwn',
20+
'.mkdown',
21+
'.ron',
22+
];
23+
24+
function isMDFormat(filepath: string) {
25+
return mdFormatExtensions.includes(path.extname(filepath));
26+
}
27+
28+
export function getFormat({
29+
filePath,
30+
frontMatterFormat,
31+
}: {
32+
filePath: string;
33+
frontMatterFormat: MDXFrontMatter['format'];
34+
}): 'md' | 'mdx' {
35+
if (frontMatterFormat !== 'detect') {
36+
return frontMatterFormat;
37+
}
38+
// Bias toward mdx if unknown extension
39+
return isMDFormat(filePath) ? 'md' : 'mdx';
40+
}

packages/docusaurus-mdx-loader/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
3030
readonly assets: Assets;
3131
(): JSX.Element;
3232
};
33-
export type {Options, MDXPlugin, MDXOptions} from './loader';
33+
34+
export type {Options, MDXPlugin} from './loader';
35+
export type {MDXOptions} from './processor';

0 commit comments

Comments
 (0)