docusaurus/packages/docusaurus-mdx-loader/src/preprocessor.ts
Sébastien Lorber 5bab0b5432
Some checks failed
Argos CI / take-screenshots (push) Has been cancelled
Build Hash Router / Build Hash Router (push) Has been cancelled
Canary Release / Publish Canary (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Continuous Releases / Continuous Releases (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (18.0) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (20) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (22) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (node-modules, -s) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (node-modules, -st) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (pnp, -s) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (pnp, -st) (push) Has been cancelled
E2E Tests / E2E — npm (push) Has been cancelled
E2E Tests / E2E — pnpm (push) Has been cancelled
feat(core, mdx-loader): deduplicate MDX compilation - `siteConfig.future.experimental_faster.mdxCrossCompilerCache` (#10479)
2024-09-06 16:07:09 +02:00

48 lines
1.4 KiB
TypeScript

/**
* 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 {
escapeMarkdownHeadingIds,
unwrapMdxCodeBlocks,
admonitionTitleToDirectiveLabel,
} from '@docusaurus/utils';
import {normalizeAdmonitionOptions} from './remark/admonitions';
import type {Options} from './options';
/**
* Preprocess the string before passing it to MDX
* This is not particularly recommended but makes it easier to upgrade to MDX 2
*/
export default function preprocessContent({
fileContent: initialFileContent,
filePath,
markdownConfig,
admonitions,
}: {
fileContent: string;
filePath: string;
markdownConfig: Options['markdownConfig'];
admonitions: Options['admonitions'] | undefined;
}): string {
let fileContent = initialFileContent;
if (markdownConfig.preprocessor) {
fileContent = markdownConfig.preprocessor({
fileContent,
filePath,
});
}
fileContent = unwrapMdxCodeBlocks(fileContent);
if (markdownConfig.mdx1Compat.headingIds) {
fileContent = escapeMarkdownHeadingIds(fileContent);
}
if (markdownConfig.mdx1Compat.admonitions && admonitions) {
const {keywords} = normalizeAdmonitionOptions(admonitions);
fileContent = admonitionTitleToDirectiveLabel(fileContent, keywords);
}
return fileContent;
}