Skip to content

Commit

Permalink
fix: allow html syntax in MDX v2 with format md (#8960)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
slorber authored May 12, 2023
1 parent af9a4f2 commit 07ad635
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 173 deletions.
4 changes: 0 additions & 4 deletions jest/deps.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ declare module 'to-vfile' {
export function read(path: string, encoding?: string): Promise<VFile>;
}

declare module 'remark-rehype';

declare module 'rehype-stringify';

declare module '@testing-utils/git' {
const createTempRepo: typeof import('./utils/git').createTempRepo;
export {createTempRepo};
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"image-size": "^1.0.2",
"mdast-util-to-string": "^3.0.0",
"mdast-util-mdx": "^2.0.0",
"rehype-raw": "^6.1.1",
"remark-comment": "^1.0.0",
"remark-gfm": "^3.0.1",
"remark-directive": "^2.0.1",
Expand Down
52 changes: 52 additions & 0 deletions packages/docusaurus-mdx-loader/src/__tests__/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {getFormat} from '../format';

describe('getFormat', () => {
it('uses frontMatter format over anything else', () => {
expect(getFormat({frontMatterFormat: 'md', filePath: 'xyz.md'})).toBe('md');
expect(getFormat({frontMatterFormat: 'md', filePath: 'xyz.mdx'})).toBe(
'md',
);
expect(getFormat({frontMatterFormat: 'mdx', filePath: 'xyz.md'})).toBe(
'mdx',
);
expect(getFormat({frontMatterFormat: 'mdx', filePath: 'xyz.mdx'})).toBe(
'mdx',
);
});

it('detects appropriate format from file extension', () => {
expect(getFormat({frontMatterFormat: 'detect', filePath: 'xyz.md'})).toBe(
'md',
);
expect(
getFormat({frontMatterFormat: 'detect', filePath: 'xyz.markdown'}),
).toBe('md');

expect(
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.md'}),
).toBe('md');
expect(
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.markdown'}),
).toBe('md');
expect(getFormat({frontMatterFormat: 'detect', filePath: 'xyz.mdx'})).toBe(
'mdx',
);
expect(
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.mdx'}),
).toBe('mdx');

expect(
getFormat({frontMatterFormat: 'detect', filePath: 'xyz.unknown'}),
).toBe('mdx');
expect(
getFormat({frontMatterFormat: 'detect', filePath: 'folder/xyz.unknown'}),
).toBe('mdx');
});
});
33 changes: 33 additions & 0 deletions packages/docusaurus-mdx-loader/src/__tests__/processor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// import {createProcessor} from '../processor';
// import type {Options} from '../loader';

/*
async function testProcess({
format,
options,
}: {
format: 'md' | 'mdx';
options: Options;
}) {
return async (content: string) => {
const processor = await createProcessor({format, options});
return processor.process(content);
};
}
*/

describe('md processor', () => {
it('parses simple commonmark', async () => {
// TODO no tests for now, wait until ESM support
// Jest does not support well ESM modules
// It would require to vendor too much Unified modules as CJS
// See https://mdxjs.com/docs/troubleshooting-mdx/#esm
});
});
40 changes: 40 additions & 0 deletions packages/docusaurus-mdx-loader/src/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import type {MDXFrontMatter} from './frontMatter';

// Copied from https://mdxjs.com/packages/mdx/#optionsmdextensions
// Although we are likely to only use .md / .mdx anyway...
const mdFormatExtensions = [
'.md',
'.markdown',
'.mdown',
'.mkdn',
'.mkd',
'.mdwn',
'.mkdown',
'.ron',
];

function isMDFormat(filepath: string) {
return mdFormatExtensions.includes(path.extname(filepath));
}

export function getFormat({
filePath,
frontMatterFormat,
}: {
filePath: string;
frontMatterFormat: MDXFrontMatter['format'];
}): 'md' | 'mdx' {
if (frontMatterFormat !== 'detect') {
return frontMatterFormat;
}
// Bias toward mdx if unknown extension
return isMDFormat(filePath) ? 'md' : 'mdx';
}
4 changes: 3 additions & 1 deletion packages/docusaurus-mdx-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
readonly assets: Assets;
(): JSX.Element;
};
export type {Options, MDXPlugin, MDXOptions} from './loader';

export type {Options, MDXPlugin} from './loader';
export type {MDXOptions} from './processor';
Loading

0 comments on commit 07ad635

Please sign in to comment.