feat(v2): add beforeDefaultRemarkPlugins/beforeDefaultRehypePlugins options to all md content plugins (#3467)

* fix(plugin-content-docs): add beforeDefaultRemarkPlugins + beforeDefaultRehypePlugins options

* fix(plugin-content-blog): add beforeDefaultRemarkPlugins + beforeDefaultRehypePlugins options

* fix(plugin-content-pages): add beforeDefaultRemarkPlugins + beforeDefaultRehypePlugins options

* feat(website-docs): overriding remark-rehype plugins

* fix(plugin-content): update beforeDefaultRehypePlugins/beforeDefaultRemarkPlugins types

* fix(plugin-content-docs): fix tests
This commit is contained in:
Rémi Doreau 2020-09-29 14:48:34 +02:00 committed by GitHub
parent f343450e85
commit d3a01458a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 57 additions and 3 deletions

View file

@ -343,7 +343,13 @@ export default function pluginContentBlog(
isServer: boolean, isServer: boolean,
{getBabelLoader, getCacheLoader}: ConfigureWebpackUtils, {getBabelLoader, getCacheLoader}: ConfigureWebpackUtils,
) { ) {
const {rehypePlugins, remarkPlugins, truncateMarker} = options; const {
rehypePlugins,
remarkPlugins,
truncateMarker,
beforeDefaultRemarkPlugins,
beforeDefaultRehypePlugins,
} = options;
return { return {
resolve: { resolve: {
alias: { alias: {
@ -363,6 +369,8 @@ export default function pluginContentBlog(
options: { options: {
remarkPlugins, remarkPlugins,
rehypePlugins, rehypePlugins,
beforeDefaultRemarkPlugins,
beforeDefaultRehypePlugins,
staticDir: path.join(siteDir, STATIC_DIR_NAME), staticDir: path.join(siteDir, STATIC_DIR_NAME),
// Note that metadataPath must be the same/in-sync as // Note that metadataPath must be the same/in-sync as
// the path from createData for each MDX. // the path from createData for each MDX.

View file

@ -31,6 +31,8 @@ export interface PluginOptions {
blogTagsPostsComponent: string; blogTagsPostsComponent: string;
blogDescription: string; blogDescription: string;
remarkPlugins: ([Function, object] | Function)[]; remarkPlugins: ([Function, object] | Function)[];
beforeDefaultRehypePlugins: ([Function, object] | Function)[];
beforeDefaultRemarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[]; rehypePlugins: string[];
truncateMarker: RegExp; truncateMarker: RegExp;
showReadingTime: boolean; showReadingTime: boolean;

View file

@ -30,6 +30,8 @@ describe('normalizeDocsPluginOptions', () => {
docItemComponent: '@theme/DocItem', docItemComponent: '@theme/DocItem',
remarkPlugins: [markdownPluginsObjectStub], remarkPlugins: [markdownPluginsObjectStub],
rehypePlugins: [markdownPluginsFunctionStub], rehypePlugins: [markdownPluginsFunctionStub],
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
showLastUpdateTime: true, showLastUpdateTime: true,
showLastUpdateAuthor: true, showLastUpdateAuthor: true,
admonitions: {}, admonitions: {},
@ -55,6 +57,8 @@ describe('normalizeDocsPluginOptions', () => {
test('should accept correctly defined remark and rehype plugin options', async () => { test('should accept correctly defined remark and rehype plugin options', async () => {
const userOptions = { const userOptions = {
...DEFAULT_OPTIONS, ...DEFAULT_OPTIONS,
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]], remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [ rehypePlugins: [
markdownPluginsObjectStub, markdownPluginsObjectStub,

View file

@ -295,7 +295,12 @@ export default function pluginContentDocs(
configureWebpack(_config, isServer, utils) { configureWebpack(_config, isServer, utils) {
const {getBabelLoader, getCacheLoader} = utils; const {getBabelLoader, getCacheLoader} = utils;
const {rehypePlugins, remarkPlugins} = options; const {
rehypePlugins,
remarkPlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
} = options;
const docsMarkdownOptions: DocsMarkdownOption = { const docsMarkdownOptions: DocsMarkdownOption = {
siteDir, siteDir,
@ -323,6 +328,8 @@ export default function pluginContentDocs(
options: { options: {
remarkPlugins, remarkPlugins,
rehypePlugins, rehypePlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
staticDir: path.join(siteDir, STATIC_DIR_NAME), staticDir: path.join(siteDir, STATIC_DIR_NAME),
metadataPath: (mdxPath: string) => { metadataPath: (mdxPath: string) => {
// Note that metadataPath must be the same/in-sync as // Note that metadataPath must be the same/in-sync as

View file

@ -27,6 +27,8 @@ export const DEFAULT_OPTIONS: Omit<PluginOptions, 'id'> = {
docItemComponent: '@theme/DocItem', docItemComponent: '@theme/DocItem',
remarkPlugins: [], remarkPlugins: [],
rehypePlugins: [], rehypePlugins: [],
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [],
showLastUpdateTime: false, showLastUpdateTime: false,
showLastUpdateAuthor: false, showLastUpdateAuthor: false,
admonitions: {}, admonitions: {},
@ -60,6 +62,12 @@ export const OptionsSchema = Joi.object({
docItemComponent: Joi.string().default(DEFAULT_OPTIONS.docItemComponent), docItemComponent: Joi.string().default(DEFAULT_OPTIONS.docItemComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins), remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins), rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),
beforeDefaultRemarkPlugins: RemarkPluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRemarkPlugins,
),
beforeDefaultRehypePlugins: RehypePluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRehypePlugins,
),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions), admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
showLastUpdateTime: Joi.bool().default(DEFAULT_OPTIONS.showLastUpdateTime), showLastUpdateTime: Joi.bool().default(DEFAULT_OPTIONS.showLastUpdateTime),
showLastUpdateAuthor: Joi.bool().default( showLastUpdateAuthor: Joi.bool().default(

View file

@ -59,6 +59,8 @@ export type PluginOptions = MetadataOptions &
docItemComponent: string; docItemComponent: string;
remarkPlugins: ([Function, object] | Function)[]; remarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[]; rehypePlugins: string[];
beforeDefaultRemarkPlugins: ([Function, object] | Function)[];
beforeDefaultRehypePlugins: ([Function, object] | Function)[];
admonitions: any; admonitions: any;
disableVersioning: boolean; disableVersioning: boolean;
excludeNextVersionDocs?: boolean; excludeNextVersionDocs?: boolean;

View file

@ -161,7 +161,12 @@ export default function pluginContentPages(
isServer: boolean, isServer: boolean,
{getBabelLoader, getCacheLoader}: ConfigureWebpackUtils, {getBabelLoader, getCacheLoader}: ConfigureWebpackUtils,
) { ) {
const {rehypePlugins, remarkPlugins} = options; const {
rehypePlugins,
remarkPlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
} = options;
return { return {
resolve: { resolve: {
alias: { alias: {
@ -181,6 +186,8 @@ export default function pluginContentPages(
options: { options: {
remarkPlugins, remarkPlugins,
rehypePlugins, rehypePlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
staticDir: path.join(siteDir, STATIC_DIR_NAME), staticDir: path.join(siteDir, STATIC_DIR_NAME),
// Note that metadataPath must be the same/in-sync as // Note that metadataPath must be the same/in-sync as
// the path from createData for each MDX. // the path from createData for each MDX.

View file

@ -19,6 +19,8 @@ export const DEFAULT_OPTIONS: PluginOptions = {
mdxPageComponent: '@theme/MDXPage', mdxPageComponent: '@theme/MDXPage',
remarkPlugins: [], remarkPlugins: [],
rehypePlugins: [], rehypePlugins: [],
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
admonitions: {}, admonitions: {},
exclude: [ exclude: [
'**/_*.{js,jsx,ts,tsx,md,mdx}', '**/_*.{js,jsx,ts,tsx,md,mdx}',
@ -35,5 +37,11 @@ export const PluginOptionSchema = Joi.object({
mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent), mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins), remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins), rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),
beforeDefaultRehypePlugins: RehypePluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRehypePlugins,
),
beforeDefaultRemarkPlugins: RemarkPluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRemarkPlugins,
),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions), admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
}); });

View file

@ -14,6 +14,8 @@ export interface PluginOptions {
mdxPageComponent: string; mdxPageComponent: string;
remarkPlugins: ([Function, object] | Function)[]; remarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[]; rehypePlugins: string[];
beforeDefaultRemarkPlugins: ([Function, object] | Function)[];
beforeDefaultRehypePlugins: ([Function, object] | Function)[];
admonitions: any; admonitions: any;
} }

View file

@ -424,6 +424,12 @@ module.exports = {
*/ */
remarkPlugins: [], remarkPlugins: [],
rehypePlugins: [], rehypePlugins: [],
/**
* Custom Remark and Rehype plugins passed to MDX before
* the default Docusaurus Remark and Rehype plugins.
*/
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [],
}, },
], ],
], ],