diff --git a/packages/docusaurus-mdx-loader/package.json b/packages/docusaurus-mdx-loader/package.json index 3c2e7cb9d347..a8849218524d 100644 --- a/packages/docusaurus-mdx-loader/package.json +++ b/packages/docusaurus-mdx-loader/package.json @@ -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", diff --git a/packages/docusaurus-mdx-loader/src/loader.ts b/packages/docusaurus-mdx-loader/src/loader.ts index 3aa7fc35ba54..74c987a8dbe7 100644 --- a/packages/docusaurus-mdx-loader/src/loader.ts +++ b/packages/docusaurus-mdx-loader/src/loader.ts @@ -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 @@ -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, @@ -40,7 +46,7 @@ async function loadMDX({ filePath: string; options: Options; compilerName: WebpackCompilerName; -}): Promise { +}): Promise { const {frontMatter} = await options.markdownConfig.parseFrontMatter({ filePath, fileContent, @@ -120,7 +126,10 @@ ${exportsCode} ${result.content} `; - return code; + return { + content: code, + sourceMap: result.map, + }; } // Note: we cache promises instead of strings @@ -138,7 +147,7 @@ async function loadMDXWithCaching({ fileContent: string; options: Options; compilerName: WebpackCompilerName; -}): Promise { +}): Promise { const {crossCompilerCache} = options; if (!crossCompilerCache) { return loadMDX({ @@ -202,7 +211,7 @@ async function loadMDXWithCaching({ deleteCacheEntry(); return cacheEntry.promise; } else { - const {promise, resolve, reject} = promiseWithResolvers(); + const {promise, resolve, reject} = promiseWithResolvers(); crossCompilerCache.set(cacheKey, {promise, resolve, reject}); return promise; } @@ -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); } diff --git a/packages/docusaurus-mdx-loader/src/options.ts b/packages/docusaurus-mdx-loader/src/options.ts index 5190953b0a0b..515f0f14e1a2 100644 --- a/packages/docusaurus-mdx-loader/src/options.ts +++ b/packages/docusaurus-mdx-loader/src/options.ts @@ -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 & { dependencies?: string[]; @@ -31,4 +32,7 @@ export type Options = Partial & { crossCompilerCache?: Map; // MDX => Promise cache }; -type CrossCompilerCacheEntry = PromiseWithResolvers; +type CrossCompilerCacheEntry = PromiseWithResolvers<{ + content: string; + sourceMap: SourceMap | null | undefined; +}>; diff --git a/packages/docusaurus-mdx-loader/src/processor.ts b/packages/docusaurus-mdx-loader/src/processor.ts index 30a2143c9d80..846883d7f183 100644 --- a/packages/docusaurus-mdx-loader/src/processor.ts +++ b/packages/docusaurus-mdx-loader/src/processor.ts @@ -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'; @@ -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 @@ -31,6 +33,7 @@ type Pluggable = any; // TODO fix this asap export type SimpleProcessorResult = { content: string; + map: SourceMap | null | undefined; data: {[key: string]: unknown}; }; @@ -187,6 +190,7 @@ async function createProcessorFactory() { ...processorOptions, remarkRehypeOptions: options.markdownConfig.remarkRehypeOptions, format, + SourceMapGenerator, }); return { @@ -202,6 +206,7 @@ async function createProcessorFactory() { return mdxProcessor.process(vfile).then((result) => ({ content: result.toString(), data: result.data, + map: result.map, })); }, }; diff --git a/project-words.txt b/project-words.txt index 7c109ead9f05..2151d4883770 100644 --- a/project-words.txt +++ b/project-words.txt @@ -25,9 +25,6 @@ beforeinstallprompt Bhatt Blockquotes blockquotes -BLUESKY -Bluesky -bluesky Bokmål bunx caabernathy @@ -36,14 +33,12 @@ cdabcdab cdpath Cena cena -changefreq Chedeau chedeau Clément Codegen codegen codesandbox -Codespaces commonmark contravariance corejs @@ -61,9 +56,6 @@ dedup devto dingers Dmitry -Docsearch -docsearch -Docsify Docu docu docusuarus @@ -117,8 +109,6 @@ Héctor héllô IANAD Infima -infima -Infima's inlines interactiveness Interpolatable @@ -140,14 +130,11 @@ Knapen Koyeb Koyeb's Lamana -Lastmod -lastmod Lifecycles lifecycles lightningcss Linkify linkify -lockb Lorber lorber Lorber's @@ -196,7 +183,6 @@ navigations navlink netrc newtab -Nextra ngryman Nisarag noflash @@ -230,16 +216,12 @@ paraiso pathinfo paularmstrong philpl -Photoshop -photoshop Pipeable playbtn Plushie plushie plushies posthog -Precache -precache precached precaching preconfigured @@ -310,11 +292,9 @@ stackoverflow Stormkit Strikethrough strikethroughs -stylelintrc sublabel sublicensable sublist -subpage subsetting subsubcategory subsubfolder