Skip to content
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

feat(mdx-loader): Support MDX source-map #11083

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"remark-emoji": "^4.0.0",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"source-map": "^0.7.4",
"stringify-object": "^3.3.0",
"tslib": "^2.6.0",
"unified": "^11.0.3",
Expand Down
19 changes: 14 additions & 5 deletions packages/docusaurus-mdx-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import type {WebpackCompilerName} from '@docusaurus/utils';
import type {Options} from './options';
import type {LoaderContext} from 'webpack';
import type {Map as SourceMap} from 'vfile';

// TODO as of April 2023, no way to import/re-export this ESM type easily :/
// This might change soon, likely after TS 5.2
Expand All @@ -30,6 +31,11 @@ type Pluggable = any; // TODO fix this asap

export type MDXPlugin = Pluggable;

type LoadMDXResult = {
content: string;
sourceMap: SourceMap | null | undefined;
};

async function loadMDX({
fileContent,
filePath,
Expand All @@ -40,7 +46,7 @@ async function loadMDX({
filePath: string;
options: Options;
compilerName: WebpackCompilerName;
}): Promise<string> {
}): Promise<LoadMDXResult> {
const {frontMatter} = await options.markdownConfig.parseFrontMatter({
filePath,
fileContent,
Expand Down Expand Up @@ -120,7 +126,10 @@ ${exportsCode}
${result.content}
`;

return code;
return {
content: code,
sourceMap: result.map,
};
}

// Note: we cache promises instead of strings
Expand All @@ -138,7 +147,7 @@ async function loadMDXWithCaching({
fileContent: string;
options: Options;
compilerName: WebpackCompilerName;
}): Promise<string> {
}): Promise<LoadMDXResult> {
const {crossCompilerCache} = options;
if (!crossCompilerCache) {
return loadMDX({
Expand Down Expand Up @@ -202,7 +211,7 @@ async function loadMDXWithCaching({
deleteCacheEntry();
return cacheEntry.promise;
} else {
const {promise, resolve, reject} = promiseWithResolvers<string>();
const {promise, resolve, reject} = promiseWithResolvers<LoadMDXResult>();
crossCompilerCache.set(cacheKey, {promise, resolve, reject});
return promise;
}
Expand All @@ -227,7 +236,7 @@ export async function mdxLoader(
options,
compilerName,
});
return callback(null, result);
return callback(null, result.content, result.sourceMap || undefined);
} catch (error) {
return callback(error as Error);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-mdx-loader/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {MDXOptions, SimpleProcessors} from './processor';
import type {MarkdownConfig} from '@docusaurus/types';
import type {ResolveMarkdownLink} from './remark/resolveMarkdownLinks';
import type {PromiseWithResolvers} from './utils';
import type {Map as SourceMap} from 'vfile';

export type Options = Partial<MDXOptions> & {
dependencies?: string[];
Expand All @@ -31,4 +32,7 @@ export type Options = Partial<MDXOptions> & {
crossCompilerCache?: Map<string, CrossCompilerCacheEntry>; // MDX => Promise<JSX> cache
};

type CrossCompilerCacheEntry = PromiseWithResolvers<string>;
type CrossCompilerCacheEntry = PromiseWithResolvers<{
content: string;
sourceMap: SourceMap | null | undefined;
}>;
5 changes: 5 additions & 0 deletions packages/docusaurus-mdx-loader/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {SourceMapGenerator} from 'source-map';
import headings from './remark/headings';
import contentTitle from './remark/contentTitle';
import toc from './remark/toc';
Expand All @@ -23,6 +24,7 @@ import type {MDXFrontMatter} from './frontMatter';
import type {Options} from './options';
import type {AdmonitionOptions} from './remark/admonitions';
import type {ProcessorOptions} from '@mdx-js/mdx';
import type {Map as SourceMap} from 'vfile';

// TODO as of April 2023, no way to import/re-export this ESM type easily :/
// This might change soon, likely after TS 5.2
Expand All @@ -31,6 +33,7 @@ type Pluggable = any; // TODO fix this asap

export type SimpleProcessorResult = {
content: string;
map: SourceMap | null | undefined;
data: {[key: string]: unknown};
};

Expand Down Expand Up @@ -187,6 +190,7 @@ async function createProcessorFactory() {
...processorOptions,
remarkRehypeOptions: options.markdownConfig.remarkRehypeOptions,
format,
SourceMapGenerator,
});

return {
Expand All @@ -202,6 +206,7 @@ async function createProcessorFactory() {
return mdxProcessor.process(vfile).then((result) => ({
content: result.toString(),
data: result.data,
map: result.map,
}));
},
};
Expand Down
20 changes: 0 additions & 20 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ beforeinstallprompt
Bhatt
Blockquotes
blockquotes
BLUESKY
Bluesky
bluesky
Bokmål
bunx
caabernathy
Expand All @@ -36,14 +33,12 @@ cdabcdab
cdpath
Cena
cena
changefreq
Chedeau
chedeau
Clément
Codegen
codegen
codesandbox
Codespaces
commonmark
contravariance
corejs
Expand All @@ -61,9 +56,6 @@ dedup
devto
dingers
Dmitry
Docsearch
docsearch
Docsify
Docu
docu
docusuarus
Expand Down Expand Up @@ -117,8 +109,6 @@ Héctor
héllô
IANAD
Infima
infima
Infima's
inlines
interactiveness
Interpolatable
Expand All @@ -140,14 +130,11 @@ Knapen
Koyeb
Koyeb's
Lamana
Lastmod
lastmod
Lifecycles
lifecycles
lightningcss
Linkify
linkify
lockb
Lorber
lorber
Lorber's
Expand Down Expand Up @@ -196,7 +183,6 @@ navigations
navlink
netrc
newtab
Nextra
ngryman
Nisarag
noflash
Expand Down Expand Up @@ -230,16 +216,12 @@ paraiso
pathinfo
paularmstrong
philpl
Photoshop
photoshop
Pipeable
playbtn
Plushie
plushie
plushies
posthog
Precache
precache
precached
precaching
preconfigured
Expand Down Expand Up @@ -310,11 +292,9 @@ stackoverflow
Stormkit
Strikethrough
strikethroughs
stylelintrc
sublabel
sublicensable
sublist
subpage
subsetting
subsubcategory
subsubfolder
Expand Down