Skip to content

Commit

Permalink
feat(v2): add beforeDefaultRemarkPlugins/beforeDefaultRehypePlugins o…
Browse files Browse the repository at this point in the history
…ptions 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
  • Loading branch information
ayshiff authored Sep 29, 2020
1 parent f343450 commit d3a0145
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,13 @@ export default function pluginContentBlog(
isServer: boolean,
{getBabelLoader, getCacheLoader}: ConfigureWebpackUtils,
) {
const {rehypePlugins, remarkPlugins, truncateMarker} = options;
const {
rehypePlugins,
remarkPlugins,
truncateMarker,
beforeDefaultRemarkPlugins,
beforeDefaultRehypePlugins,
} = options;
return {
resolve: {
alias: {
Expand All @@ -363,6 +369,8 @@ export default function pluginContentBlog(
options: {
remarkPlugins,
rehypePlugins,
beforeDefaultRemarkPlugins,
beforeDefaultRehypePlugins,
staticDir: path.join(siteDir, STATIC_DIR_NAME),
// Note that metadataPath must be the same/in-sync as
// the path from createData for each MDX.
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface PluginOptions {
blogTagsPostsComponent: string;
blogDescription: string;
remarkPlugins: ([Function, object] | Function)[];
beforeDefaultRehypePlugins: ([Function, object] | Function)[];
beforeDefaultRemarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[];
truncateMarker: RegExp;
showReadingTime: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe('normalizeDocsPluginOptions', () => {
docItemComponent: '@theme/DocItem',
remarkPlugins: [markdownPluginsObjectStub],
rehypePlugins: [markdownPluginsFunctionStub],
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
showLastUpdateTime: true,
showLastUpdateAuthor: true,
admonitions: {},
Expand All @@ -55,6 +57,8 @@ describe('normalizeDocsPluginOptions', () => {
test('should accept correctly defined remark and rehype plugin options', async () => {
const userOptions = {
...DEFAULT_OPTIONS,
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [
markdownPluginsObjectStub,
Expand Down
9 changes: 8 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,12 @@ export default function pluginContentDocs(

configureWebpack(_config, isServer, utils) {
const {getBabelLoader, getCacheLoader} = utils;
const {rehypePlugins, remarkPlugins} = options;
const {
rehypePlugins,
remarkPlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
} = options;

const docsMarkdownOptions: DocsMarkdownOption = {
siteDir,
Expand Down Expand Up @@ -323,6 +328,8 @@ export default function pluginContentDocs(
options: {
remarkPlugins,
rehypePlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
staticDir: path.join(siteDir, STATIC_DIR_NAME),
metadataPath: (mdxPath: string) => {
// Note that metadataPath must be the same/in-sync as
Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const DEFAULT_OPTIONS: Omit<PluginOptions, 'id'> = {
docItemComponent: '@theme/DocItem',
remarkPlugins: [],
rehypePlugins: [],
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [],
showLastUpdateTime: false,
showLastUpdateAuthor: false,
admonitions: {},
Expand Down Expand Up @@ -60,6 +62,12 @@ export const OptionsSchema = Joi.object({
docItemComponent: Joi.string().default(DEFAULT_OPTIONS.docItemComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),
beforeDefaultRemarkPlugins: RemarkPluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRemarkPlugins,
),
beforeDefaultRehypePlugins: RehypePluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRehypePlugins,
),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
showLastUpdateTime: Joi.bool().default(DEFAULT_OPTIONS.showLastUpdateTime),
showLastUpdateAuthor: Joi.bool().default(
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export type PluginOptions = MetadataOptions &
docItemComponent: string;
remarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[];
beforeDefaultRemarkPlugins: ([Function, object] | Function)[];
beforeDefaultRehypePlugins: ([Function, object] | Function)[];
admonitions: any;
disableVersioning: boolean;
excludeNextVersionDocs?: boolean;
Expand Down
9 changes: 8 additions & 1 deletion packages/docusaurus-plugin-content-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ export default function pluginContentPages(
isServer: boolean,
{getBabelLoader, getCacheLoader}: ConfigureWebpackUtils,
) {
const {rehypePlugins, remarkPlugins} = options;
const {
rehypePlugins,
remarkPlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
} = options;
return {
resolve: {
alias: {
Expand All @@ -181,6 +186,8 @@ export default function pluginContentPages(
options: {
remarkPlugins,
rehypePlugins,
beforeDefaultRehypePlugins,
beforeDefaultRemarkPlugins,
staticDir: path.join(siteDir, STATIC_DIR_NAME),
// Note that metadataPath must be the same/in-sync as
// the path from createData for each MDX.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const DEFAULT_OPTIONS: PluginOptions = {
mdxPageComponent: '@theme/MDXPage',
remarkPlugins: [],
rehypePlugins: [],
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
admonitions: {},
exclude: [
'**/_*.{js,jsx,ts,tsx,md,mdx}',
Expand All @@ -35,5 +37,11 @@ export const PluginOptionSchema = Joi.object({
mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),
beforeDefaultRehypePlugins: RehypePluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRehypePlugins,
),
beforeDefaultRemarkPlugins: RemarkPluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRemarkPlugins,
),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
});
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-content-pages/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface PluginOptions {
mdxPageComponent: string;
remarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[];
beforeDefaultRemarkPlugins: ([Function, object] | Function)[];
beforeDefaultRehypePlugins: ([Function, object] | Function)[];
admonitions: any;
}

Expand Down
6 changes: 6 additions & 0 deletions website/docs/using-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ module.exports = {
*/
remarkPlugins: [],
rehypePlugins: [],
/**
* Custom Remark and Rehype plugins passed to MDX before
* the default Docusaurus Remark and Rehype plugins.
*/
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [],
},
],
],
Expand Down

0 comments on commit d3a0145

Please sign in to comment.