feat(v2): add support to ignore files in pages plugin (#3196)

* add support to ignore pages

* fix import problem

* Update website/docs/guides/creating-pages.md

Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>

* Revert "fix import problem"

This reverts commit 4457a2e938.

* revert

* fix slash

* forbid frontmatter

* fix formatting

* Update website/docs/guides/creating-pages.md

* Update website/src/pages/examples/_chapter1.md

* Update website/src/pages/examples/_chapter2.mdx

* Update website/src/pages/examples/markdownPageExample.md

* Update website/src/pages/examples/markdownPageExample.md

* Update website/src/pages/examples/markdownPageExample.md

* Update website/src/pages/examples/markdownPageExample.md

Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
This commit is contained in:
Anshul Goyal 2020-08-06 01:05:55 +05:30 committed by GitHub
parent 592fc48fd3
commit f234c407f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 82 additions and 7 deletions

View file

@ -0,0 +1 @@
export default (a:number,b:number)=>a+b;

View file

@ -10,7 +10,7 @@ import {PluginOptions} from '../types';
export default function normalizePluginOptions(
options: Partial<PluginOptions>,
) {
): PluginOptions {
const {value, error} = PluginOptionSchema.validate(options, {
convert: false,
});
@ -37,6 +37,7 @@ describe('normalizePagesPluginOptions', () => {
path: 'src/my-pages',
routeBasePath: 'my-pages',
include: ['**/*.{js,jsx,ts,tsx}'],
exclude: ['**/$*/'],
};
const value = normalizePluginOptions(userOptions);
expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});

View file

@ -8,6 +8,8 @@
import globby from 'globby';
import fs from 'fs';
import path from 'path';
import minimatch from 'minimatch';
import slash from 'slash';
import {
encodePath,
fileToPath,
@ -51,6 +53,11 @@ export default function pluginContentPages(
);
const dataDir = path.join(pluginDataDirRoot, options.id ?? DEFAULT_PLUGIN_ID);
const excludeRegex = new RegExp(
options.exclude
.map((pattern) => minimatch.makeRe(pattern).source)
.join('|'),
);
return {
name: 'docusaurus-plugin-content-pages',
@ -81,6 +88,7 @@ export default function pluginContentPages(
const {baseUrl} = siteConfig;
const pagesFiles = await globby(include, {
cwd: pagesDir,
ignore: options.exclude,
});
function toMetadata(relativeSource: string): Metadata {
@ -173,12 +181,17 @@ export default function pluginContentPages(
// Note that metadataPath must be the same/in-sync as
// the path from createData for each MDX.
metadataPath: (mdxPath: string) => {
const aliasedPath = aliasedSitePath(mdxPath, siteDir);
if (excludeRegex.test(slash(mdxPath))) {
return null;
}
const aliasedSource = aliasedSitePath(mdxPath, siteDir);
return path.join(
dataDir,
`${docuHash(aliasedPath)}.json`,
`${docuHash(aliasedSource)}.json`,
);
},
forbidFrontMatter: (mdxPath: string) =>
excludeRegex.test(slash(mdxPath)),
},
},
{

View file

@ -20,12 +20,18 @@ export const DEFAULT_OPTIONS: PluginOptions = {
remarkPlugins: [],
rehypePlugins: [],
admonitions: {},
exclude: [
'**/_*.{js,jsx,ts,tsx,md,mdx}',
'**/*.test.{js,ts}',
'**/__tests__/**',
],
};
export const PluginOptionSchema = Joi.object({
path: Joi.string().default(DEFAULT_OPTIONS.path),
routeBasePath: Joi.string().default(DEFAULT_OPTIONS.routeBasePath),
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),

View file

@ -10,6 +10,7 @@ export interface PluginOptions {
path: string;
routeBasePath: string;
include: string[];
exclude: string[];
mdxPageComponent: string;
remarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[];