@docusaurus/plugin-content-pages 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/{src/__tests__/__fixtures__/website/i18n/fr/docusaurus-plugin-content-pages/hello/translatedJs.js → lib/frontMatter.d.ts} +4 -8
  2. package/lib/frontMatter.js +21 -0
  3. package/lib/index.d.ts +4 -3
  4. package/lib/index.js +42 -57
  5. package/lib/markdownLoader.d.ts +2 -6
  6. package/lib/markdownLoader.js +4 -4
  7. package/lib/options.d.ts +10 -0
  8. package/lib/{pluginOptionSchema.js → options.js} +15 -7
  9. package/lib/types.d.ts +0 -22
  10. package/package.json +13 -14
  11. package/{types.d.ts → src/deps.d.ts} +1 -1
  12. package/src/frontMatter.ts +27 -0
  13. package/src/index.ts +54 -76
  14. package/src/markdownLoader.ts +8 -11
  15. package/src/{pluginOptionSchema.ts → options.ts} +14 -7
  16. package/src/plugin-content-pages.d.ts +60 -18
  17. package/src/types.ts +0 -28
  18. package/lib/.tsbuildinfo +0 -1
  19. package/lib/pluginOptionSchema.d.ts +0 -10
  20. package/src/__tests__/__fixtures__/website/docusaurus.config.js +0 -14
  21. package/src/__tests__/__fixtures__/website/i18n/fr/docusaurus-plugin-content-pages/hello/translatedMd.md +0 -1
  22. package/src/__tests__/__fixtures__/website/src/pages/hello/_ignore.js +0 -1
  23. package/src/__tests__/__fixtures__/website/src/pages/hello/_ignore.md +0 -1
  24. package/src/__tests__/__fixtures__/website/src/pages/hello/_ignore.mdx +0 -1
  25. package/src/__tests__/__fixtures__/website/src/pages/hello/_ignore.tsx +0 -1
  26. package/src/__tests__/__fixtures__/website/src/pages/hello/index.md +0 -2
  27. package/src/__tests__/__fixtures__/website/src/pages/hello/mdxPage.mdx +0 -5
  28. package/src/__tests__/__fixtures__/website/src/pages/hello/translatedJs.js +0 -14
  29. package/src/__tests__/__fixtures__/website/src/pages/hello/translatedMd.md +0 -1
  30. package/src/__tests__/__fixtures__/website/src/pages/hello/world.js +0 -22
  31. package/src/__tests__/__fixtures__/website/src/pages/index.js +0 -23
  32. package/src/__tests__/__fixtures__/website/src/pages/typescript.tsx +0 -22
  33. package/src/__tests__/index.test.ts +0 -139
  34. package/src/__tests__/pluginOptionSchema.test.ts +0 -54
  35. package/tsconfig.json +0 -9
@@ -4,11 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
-
8
- import React from 'react';
9
-
10
- export default class TranslatedJs extends React.Component {
11
- render() {
12
- return <div>TranslatedJsPage (fr)</div>;
13
- }
14
- }
7
+ import type { FrontMatter } from '@docusaurus/plugin-content-pages';
8
+ export declare function validatePageFrontMatter(frontMatter: {
9
+ [key: string]: unknown;
10
+ }): FrontMatter;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.validatePageFrontMatter = void 0;
10
+ const utils_validation_1 = require("@docusaurus/utils-validation");
11
+ const PageFrontMatterSchema = utils_validation_1.Joi.object({
12
+ title: utils_validation_1.Joi.string(),
13
+ description: utils_validation_1.Joi.string(),
14
+ wrapperClassName: utils_validation_1.Joi.string(),
15
+ hide_table_of_contents: utils_validation_1.Joi.boolean(),
16
+ ...utils_validation_1.FrontMatterTOCHeadingLevels,
17
+ });
18
+ function validatePageFrontMatter(frontMatter) {
19
+ return (0, utils_validation_1.validateFrontMatter)(frontMatter, PageFrontMatterSchema);
20
+ }
21
+ exports.validatePageFrontMatter = validatePageFrontMatter;
package/lib/index.d.ts CHANGED
@@ -4,8 +4,9 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { LoadContext, Plugin, OptionValidationContext, ValidationResult } from '@docusaurus/types';
8
- import { PluginOptions, LoadedContent, PagesContentPaths } from './types';
7
+ import type { LoadContext, Plugin } from '@docusaurus/types';
8
+ import type { PagesContentPaths } from './types';
9
+ import type { PluginOptions, LoadedContent } from '@docusaurus/plugin-content-pages';
9
10
  export declare function getContentPathList(contentPaths: PagesContentPaths): string[];
10
11
  export default function pluginContentPages(context: LoadContext, options: PluginOptions): Plugin<LoadedContent | null>;
11
- export declare function validateOptions({ validate, options, }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions>;
12
+ export { validateOptions } from './options';
package/lib/index.js CHANGED
@@ -8,32 +8,26 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.validateOptions = exports.getContentPathList = void 0;
10
10
  const tslib_1 = require("tslib");
11
- const globby_1 = tslib_1.__importDefault(require("globby"));
12
- const fs_1 = tslib_1.__importDefault(require("fs"));
11
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
12
  const path_1 = tslib_1.__importDefault(require("path"));
14
- const minimatch_1 = tslib_1.__importDefault(require("minimatch"));
15
- const slash_1 = tslib_1.__importDefault(require("slash"));
16
13
  const utils_1 = require("@docusaurus/utils");
17
14
  const remark_admonitions_1 = tslib_1.__importDefault(require("remark-admonitions"));
18
- const pluginOptionSchema_1 = require("./pluginOptionSchema");
19
- const constants_1 = require("@docusaurus/core/lib/constants");
20
- const lodash_1 = require("lodash");
15
+ const frontMatter_1 = require("./frontMatter");
21
16
  function getContentPathList(contentPaths) {
22
17
  return [contentPaths.contentPathLocalized, contentPaths.contentPath];
23
18
  }
24
19
  exports.getContentPathList = getContentPathList;
25
20
  const isMarkdownSource = (source) => source.endsWith('.md') || source.endsWith('.mdx');
26
21
  function pluginContentPages(context, options) {
27
- var _a;
28
22
  if (options.admonitions) {
29
23
  options.remarkPlugins = options.remarkPlugins.concat([
30
- [remark_admonitions_1.default, options.admonitions || {}],
24
+ [remark_admonitions_1.default, options.admonitions],
31
25
  ]);
32
26
  }
33
27
  const { siteConfig, siteDir, generatedFilesDir, i18n: { currentLocale }, } = context;
34
28
  const contentPaths = {
35
29
  contentPath: path_1.default.resolve(siteDir, options.path),
36
- contentPathLocalized: utils_1.getPluginI18nPath({
30
+ contentPathLocalized: (0, utils_1.getPluginI18nPath)({
37
31
  siteDir,
38
32
  locale: currentLocale,
39
33
  pluginName: 'docusaurus-plugin-content-pages',
@@ -41,56 +35,51 @@ function pluginContentPages(context, options) {
41
35
  }),
42
36
  };
43
37
  const pluginDataDirRoot = path_1.default.join(generatedFilesDir, 'docusaurus-plugin-content-pages');
44
- const dataDir = path_1.default.join(pluginDataDirRoot, (_a = options.id) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PLUGIN_ID);
45
- const excludeRegex = new RegExp(options.exclude
46
- .map((pattern) => minimatch_1.default.makeRe(pattern).source)
47
- .join('|'));
38
+ const dataDir = path_1.default.join(pluginDataDirRoot, options.id ?? utils_1.DEFAULT_PLUGIN_ID);
48
39
  return {
49
40
  name: 'docusaurus-plugin-content-pages',
50
41
  getPathsToWatch() {
51
- const { include = [] } = options;
52
- return lodash_1.flatten(getContentPathList(contentPaths).map((contentPath) => {
53
- return include.map((pattern) => `${contentPath}/${pattern}`);
54
- }));
55
- },
56
- getClientModules() {
57
- const modules = [];
58
- if (options.admonitions) {
59
- modules.push(require.resolve('remark-admonitions/styles/infima.css'));
60
- }
61
- return modules;
42
+ const { include } = options;
43
+ return getContentPathList(contentPaths).flatMap((contentPath) => include.map((pattern) => `${contentPath}/${pattern}`));
62
44
  },
63
45
  async loadContent() {
64
46
  const { include } = options;
65
- if (!fs_1.default.existsSync(contentPaths.contentPath)) {
47
+ if (!(await fs_extra_1.default.pathExists(contentPaths.contentPath))) {
66
48
  return null;
67
49
  }
68
50
  const { baseUrl } = siteConfig;
69
- const pagesFiles = await globby_1.default(include, {
51
+ const pagesFiles = await (0, utils_1.Globby)(include, {
70
52
  cwd: contentPaths.contentPath,
71
53
  ignore: options.exclude,
72
54
  });
73
55
  async function toMetadata(relativeSource) {
74
56
  // Lookup in localized folder in priority
75
- const contentPath = await utils_1.getFolderContainingFile(getContentPathList(contentPaths), relativeSource);
57
+ const contentPath = await (0, utils_1.getFolderContainingFile)(getContentPathList(contentPaths), relativeSource);
76
58
  const source = path_1.default.join(contentPath, relativeSource);
77
- const aliasedSourcePath = utils_1.aliasedSitePath(source, siteDir);
78
- const pathName = utils_1.encodePath(utils_1.fileToPath(relativeSource));
79
- const permalink = pathName.replace(/^\//, baseUrl || '');
80
- if (isMarkdownSource(relativeSource)) {
81
- return {
82
- type: 'mdx',
83
- permalink,
84
- source: aliasedSourcePath,
85
- };
86
- }
87
- else {
59
+ const aliasedSourcePath = (0, utils_1.aliasedSitePath)(source, siteDir);
60
+ const permalink = (0, utils_1.normalizeUrl)([
61
+ baseUrl,
62
+ options.routeBasePath,
63
+ (0, utils_1.encodePath)((0, utils_1.fileToPath)(relativeSource)),
64
+ ]);
65
+ if (!isMarkdownSource(relativeSource)) {
88
66
  return {
89
67
  type: 'jsx',
90
68
  permalink,
91
69
  source: aliasedSourcePath,
92
70
  };
93
71
  }
72
+ const content = await fs_extra_1.default.readFile(source, 'utf-8');
73
+ const { frontMatter: unsafeFrontMatter, contentTitle, excerpt, } = (0, utils_1.parseMarkdownString)(content);
74
+ const frontMatter = (0, frontMatter_1.validatePageFrontMatter)(unsafeFrontMatter);
75
+ return {
76
+ type: 'mdx',
77
+ permalink,
78
+ source: aliasedSourcePath,
79
+ title: frontMatter.title ?? contentTitle,
80
+ description: frontMatter.description ?? excerpt,
81
+ frontMatter,
82
+ };
94
83
  }
95
84
  return Promise.all(pagesFiles.map(toMetadata));
96
85
  },
@@ -105,7 +94,7 @@ function pluginContentPages(context, options) {
105
94
  await createData(
106
95
  // Note that this created data path must be in sync with
107
96
  // metadataPath provided to mdx-loader.
108
- `${utils_1.docuHash(metadata.source)}.json`, JSON.stringify(metadata, null, 2));
97
+ `${(0, utils_1.docuHash)(metadata.source)}.json`, JSON.stringify(metadata, null, 2));
109
98
  addRoute({
110
99
  path: permalink,
111
100
  component: options.mdxPageComponent,
@@ -127,8 +116,9 @@ function pluginContentPages(context, options) {
127
116
  }
128
117
  }));
129
118
  },
130
- configureWebpack(_config, isServer, { getJSLoader }) {
119
+ configureWebpack(config, isServer, { getJSLoader }) {
131
120
  const { rehypePlugins, remarkPlugins, beforeDefaultRehypePlugins, beforeDefaultRemarkPlugins, } = options;
121
+ const contentDirs = getContentPathList(contentPaths);
132
122
  return {
133
123
  resolve: {
134
124
  alias: {
@@ -138,8 +128,8 @@ function pluginContentPages(context, options) {
138
128
  module: {
139
129
  rules: [
140
130
  {
141
- test: /(\.mdx?)$/,
142
- include: getContentPathList(contentPaths)
131
+ test: /\.mdx?$/i,
132
+ include: contentDirs
143
133
  // Trailing slash is important, see https://github.com/facebook/docusaurus/pull/3970
144
134
  .map(utils_1.addTrailingPathSeparator),
145
135
  use: [
@@ -151,17 +141,15 @@ function pluginContentPages(context, options) {
151
141
  rehypePlugins,
152
142
  beforeDefaultRehypePlugins,
153
143
  beforeDefaultRemarkPlugins,
154
- staticDir: path_1.default.join(siteDir, constants_1.STATIC_DIR_NAME),
155
- // Note that metadataPath must be the same/in-sync as
156
- // the path from createData for each MDX.
144
+ staticDirs: siteConfig.staticDirectories.map((dir) => path_1.default.resolve(siteDir, dir)),
145
+ siteDir,
146
+ isMDXPartial: (0, utils_1.createAbsoluteFilePathMatcher)(options.exclude, contentDirs),
157
147
  metadataPath: (mdxPath) => {
158
- if (excludeRegex.test(slash_1.default(mdxPath))) {
159
- return null;
160
- }
161
- const aliasedSource = utils_1.aliasedSitePath(mdxPath, siteDir);
162
- return path_1.default.join(dataDir, `${utils_1.docuHash(aliasedSource)}.json`);
148
+ // Note that metadataPath must be the same/in-sync as
149
+ // the path from createData for each MDX.
150
+ const aliasedSource = (0, utils_1.aliasedSitePath)(mdxPath, siteDir);
151
+ return path_1.default.join(dataDir, `${(0, utils_1.docuHash)(aliasedSource)}.json`);
163
152
  },
164
- forbidFrontMatter: (mdxPath) => excludeRegex.test(slash_1.default(mdxPath)),
165
153
  },
166
154
  },
167
155
  {
@@ -180,8 +168,5 @@ function pluginContentPages(context, options) {
180
168
  };
181
169
  }
182
170
  exports.default = pluginContentPages;
183
- function validateOptions({ validate, options, }) {
184
- const validatedOptions = validate(pluginOptionSchema_1.PluginOptionSchema, options);
185
- return validatedOptions;
186
- }
187
- exports.validateOptions = validateOptions;
171
+ var options_1 = require("./options");
172
+ Object.defineProperty(exports, "validateOptions", { enumerable: true, get: function () { return options_1.validateOptions; } });
@@ -4,9 +4,5 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- /// <reference types="node" />
8
- interface Loader extends Function {
9
- (this: any, source: string): string | Buffer | void | undefined;
10
- }
11
- declare const markdownLoader: Loader;
12
- export default markdownLoader;
7
+ import type { LoaderContext } from 'webpack';
8
+ export default function markdownLoader(this: LoaderContext<undefined>, fileString: string): void;
@@ -6,11 +6,11 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- const markdownLoader = function (fileString) {
9
+ function markdownLoader(fileString) {
10
10
  const callback = this.async();
11
11
  // const options = this.getOptions();
12
- // TODO provide additinal md processing here? like interlinking pages?
12
+ // TODO provide additional md processing here? like interlinking pages?
13
13
  // fileString = linkify(fileString)
14
- return callback && callback(null, fileString);
15
- };
14
+ return callback(null, fileString);
15
+ }
16
16
  exports.default = markdownLoader;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { OptionValidationContext } from '@docusaurus/types';
8
+ import type { PluginOptions, Options } from '@docusaurus/plugin-content-pages';
9
+ export declare const DEFAULT_OPTIONS: PluginOptions;
10
+ export declare function validateOptions({ validate, options, }: OptionValidationContext<Options, PluginOptions>): PluginOptions;
@@ -1,24 +1,27 @@
1
1
  "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
2
8
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PluginOptionSchema = exports.DEFAULT_OPTIONS = void 0;
9
+ exports.validateOptions = exports.DEFAULT_OPTIONS = void 0;
4
10
  const utils_validation_1 = require("@docusaurus/utils-validation");
11
+ const utils_1 = require("@docusaurus/utils");
5
12
  exports.DEFAULT_OPTIONS = {
6
13
  path: 'src/pages',
7
14
  routeBasePath: '/',
8
15
  include: ['**/*.{js,jsx,ts,tsx,md,mdx}'],
16
+ exclude: utils_1.GlobExcludeDefault,
9
17
  mdxPageComponent: '@theme/MDXPage',
10
18
  remarkPlugins: [],
11
19
  rehypePlugins: [],
12
20
  beforeDefaultRehypePlugins: [],
13
21
  beforeDefaultRemarkPlugins: [],
14
22
  admonitions: {},
15
- exclude: [
16
- '**/_*.{js,jsx,ts,tsx,md,mdx}',
17
- '**/*.test.{js,ts}',
18
- '**/__tests__/**',
19
- ],
20
23
  };
21
- exports.PluginOptionSchema = utils_validation_1.Joi.object({
24
+ const PluginOptionSchema = utils_validation_1.Joi.object({
22
25
  path: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.path),
23
26
  routeBasePath: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.routeBasePath),
24
27
  include: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.include),
@@ -30,3 +33,8 @@ exports.PluginOptionSchema = utils_validation_1.Joi.object({
30
33
  beforeDefaultRemarkPlugins: utils_validation_1.RemarkPluginsSchema.default(exports.DEFAULT_OPTIONS.beforeDefaultRemarkPlugins),
31
34
  admonitions: utils_validation_1.AdmonitionsSchema.default(exports.DEFAULT_OPTIONS.admonitions),
32
35
  });
36
+ function validateOptions({ validate, options, }) {
37
+ const validatedOptions = validate(PluginOptionSchema, options);
38
+ return validatedOptions;
39
+ }
40
+ exports.validateOptions = validateOptions;
package/lib/types.d.ts CHANGED
@@ -4,28 +4,6 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { RemarkAndRehypePluginOptions } from '@docusaurus/mdx-loader';
8
- export interface PluginOptions extends RemarkAndRehypePluginOptions {
9
- id?: string;
10
- path: string;
11
- routeBasePath: string;
12
- include: string[];
13
- exclude: string[];
14
- mdxPageComponent: string;
15
- admonitions: Record<string, unknown>;
16
- }
17
- export declare type JSXPageMetadata = {
18
- type: 'jsx';
19
- permalink: string;
20
- source: string;
21
- };
22
- export declare type MDXPageMetadata = {
23
- type: 'mdx';
24
- permalink: string;
25
- source: string;
26
- };
27
- export declare type Metadata = JSXPageMetadata | MDXPageMetadata;
28
- export declare type LoadedContent = Metadata[];
29
7
  export declare type PagesContentPaths = {
30
8
  contentPath: string;
31
9
  contentPathLocalized: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-pages",
3
- "version": "2.0.0-beta.1ec2c95e3",
3
+ "version": "2.0.0-beta.21",
4
4
  "description": "Pages plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-content-pages.d.ts",
@@ -18,25 +18,24 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "2.0.0-beta.1ec2c95e3",
22
- "@docusaurus/mdx-loader": "2.0.0-beta.1ec2c95e3",
23
- "@docusaurus/types": "2.0.0-beta.1ec2c95e3",
24
- "@docusaurus/utils": "2.0.0-beta.1ec2c95e3",
25
- "@docusaurus/utils-validation": "2.0.0-beta.1ec2c95e3",
26
- "globby": "^11.0.2",
27
- "lodash": "^4.17.20",
28
- "minimatch": "^3.0.4",
21
+ "@docusaurus/core": "2.0.0-beta.21",
22
+ "@docusaurus/mdx-loader": "2.0.0-beta.21",
23
+ "@docusaurus/utils": "2.0.0-beta.21",
24
+ "@docusaurus/utils-validation": "2.0.0-beta.21",
25
+ "fs-extra": "^10.1.0",
29
26
  "remark-admonitions": "^1.2.1",
30
- "slash": "^3.0.0",
31
- "tslib": "^2.1.0",
32
- "webpack": "^5.40.0"
27
+ "tslib": "^2.4.0",
28
+ "webpack": "^5.72.1"
29
+ },
30
+ "devDependencies": {
31
+ "@docusaurus/types": "2.0.0-beta.21"
33
32
  },
34
33
  "peerDependencies": {
35
34
  "react": "^16.8.4 || ^17.0.0",
36
35
  "react-dom": "^16.8.4 || ^17.0.0"
37
36
  },
38
37
  "engines": {
39
- "node": ">=12.13.0"
38
+ "node": ">=16.14"
40
39
  },
41
- "gitHead": "05c1ed75bb06738f274fe71baf71749a05b2db24"
40
+ "gitHead": "69ac49fc6909517f13615ee40290c4bd00c39df4"
42
41
  }
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  declare module 'remark-admonitions' {
9
- type Options = Record<string, unknown>;
9
+ type Options = {[key: string]: unknown};
10
10
 
11
11
  const plugin: (options?: Options) => void;
12
12
  export = plugin;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {
9
+ Joi,
10
+ validateFrontMatter,
11
+ FrontMatterTOCHeadingLevels,
12
+ } from '@docusaurus/utils-validation';
13
+ import type {FrontMatter} from '@docusaurus/plugin-content-pages';
14
+
15
+ const PageFrontMatterSchema = Joi.object<FrontMatter>({
16
+ title: Joi.string(),
17
+ description: Joi.string(),
18
+ wrapperClassName: Joi.string(),
19
+ hide_table_of_contents: Joi.boolean(),
20
+ ...FrontMatterTOCHeadingLevels,
21
+ });
22
+
23
+ export function validatePageFrontMatter(frontMatter: {
24
+ [key: string]: unknown;
25
+ }): FrontMatter {
26
+ return validateFrontMatter(frontMatter, PageFrontMatterSchema);
27
+ }
package/src/index.ts CHANGED
@@ -5,11 +5,8 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import globby from 'globby';
9
- import fs from 'fs';
8
+ import fs from 'fs-extra';
10
9
  import path from 'path';
11
- import minimatch from 'minimatch';
12
- import slash from 'slash';
13
10
  import {
14
11
  encodePath,
15
12
  fileToPath,
@@ -18,29 +15,22 @@ import {
18
15
  getPluginI18nPath,
19
16
  getFolderContainingFile,
20
17
  addTrailingPathSeparator,
18
+ Globby,
19
+ createAbsoluteFilePathMatcher,
20
+ normalizeUrl,
21
+ DEFAULT_PLUGIN_ID,
22
+ parseMarkdownString,
21
23
  } from '@docusaurus/utils';
22
- import {
23
- LoadContext,
24
- Plugin,
25
- OptionValidationContext,
26
- ValidationResult,
27
- ConfigureWebpackUtils,
28
- } from '@docusaurus/types';
29
- import {Configuration} from 'webpack';
30
24
  import admonitions from 'remark-admonitions';
31
- import {PluginOptionSchema} from './pluginOptionSchema';
32
- import {
33
- DEFAULT_PLUGIN_ID,
34
- STATIC_DIR_NAME,
35
- } from '@docusaurus/core/lib/constants';
25
+ import {validatePageFrontMatter} from './frontMatter';
36
26
 
37
- import {
27
+ import type {LoadContext, Plugin} from '@docusaurus/types';
28
+ import type {PagesContentPaths} from './types';
29
+ import type {
38
30
  PluginOptions,
39
- LoadedContent,
40
31
  Metadata,
41
- PagesContentPaths,
42
- } from './types';
43
- import {flatten} from 'lodash';
32
+ LoadedContent,
33
+ } from '@docusaurus/plugin-content-pages';
44
34
 
45
35
  export function getContentPathList(contentPaths: PagesContentPaths): string[] {
46
36
  return [contentPaths.contentPathLocalized, contentPaths.contentPath];
@@ -55,7 +45,7 @@ export default function pluginContentPages(
55
45
  ): Plugin<LoadedContent | null> {
56
46
  if (options.admonitions) {
57
47
  options.remarkPlugins = options.remarkPlugins.concat([
58
- [admonitions, options.admonitions || {}],
48
+ [admonitions, options.admonitions],
59
49
  ]);
60
50
  }
61
51
  const {
@@ -81,42 +71,25 @@ export default function pluginContentPages(
81
71
  );
82
72
  const dataDir = path.join(pluginDataDirRoot, options.id ?? DEFAULT_PLUGIN_ID);
83
73
 
84
- const excludeRegex = new RegExp(
85
- options.exclude
86
- .map((pattern) => minimatch.makeRe(pattern).source)
87
- .join('|'),
88
- );
89
74
  return {
90
75
  name: 'docusaurus-plugin-content-pages',
91
76
 
92
77
  getPathsToWatch() {
93
- const {include = []} = options;
94
- return flatten(
95
- getContentPathList(contentPaths).map((contentPath) => {
96
- return include.map((pattern) => `${contentPath}/${pattern}`);
97
- }),
78
+ const {include} = options;
79
+ return getContentPathList(contentPaths).flatMap((contentPath) =>
80
+ include.map((pattern) => `${contentPath}/${pattern}`),
98
81
  );
99
82
  },
100
83
 
101
- getClientModules() {
102
- const modules = [];
103
-
104
- if (options.admonitions) {
105
- modules.push(require.resolve('remark-admonitions/styles/infima.css'));
106
- }
107
-
108
- return modules;
109
- },
110
-
111
84
  async loadContent() {
112
85
  const {include} = options;
113
86
 
114
- if (!fs.existsSync(contentPaths.contentPath)) {
87
+ if (!(await fs.pathExists(contentPaths.contentPath))) {
115
88
  return null;
116
89
  }
117
90
 
118
91
  const {baseUrl} = siteConfig;
119
- const pagesFiles = await globby(include, {
92
+ const pagesFiles = await Globby(include, {
120
93
  cwd: contentPaths.contentPath,
121
94
  ignore: options.exclude,
122
95
  });
@@ -130,21 +103,33 @@ export default function pluginContentPages(
130
103
 
131
104
  const source = path.join(contentPath, relativeSource);
132
105
  const aliasedSourcePath = aliasedSitePath(source, siteDir);
133
- const pathName = encodePath(fileToPath(relativeSource));
134
- const permalink = pathName.replace(/^\//, baseUrl || '');
135
- if (isMarkdownSource(relativeSource)) {
136
- return {
137
- type: 'mdx',
138
- permalink,
139
- source: aliasedSourcePath,
140
- };
141
- } else {
106
+ const permalink = normalizeUrl([
107
+ baseUrl,
108
+ options.routeBasePath,
109
+ encodePath(fileToPath(relativeSource)),
110
+ ]);
111
+ if (!isMarkdownSource(relativeSource)) {
142
112
  return {
143
113
  type: 'jsx',
144
114
  permalink,
145
115
  source: aliasedSourcePath,
146
116
  };
147
117
  }
118
+ const content = await fs.readFile(source, 'utf-8');
119
+ const {
120
+ frontMatter: unsafeFrontMatter,
121
+ contentTitle,
122
+ excerpt,
123
+ } = parseMarkdownString(content);
124
+ const frontMatter = validatePageFrontMatter(unsafeFrontMatter);
125
+ return {
126
+ type: 'mdx',
127
+ permalink,
128
+ source: aliasedSourcePath,
129
+ title: frontMatter.title ?? contentTitle,
130
+ description: frontMatter.description ?? excerpt,
131
+ frontMatter,
132
+ };
148
133
  }
149
134
 
150
135
  return Promise.all(pagesFiles.map(toMetadata));
@@ -189,17 +174,14 @@ export default function pluginContentPages(
189
174
  );
190
175
  },
191
176
 
192
- configureWebpack(
193
- _config: Configuration,
194
- isServer: boolean,
195
- {getJSLoader}: ConfigureWebpackUtils,
196
- ) {
177
+ configureWebpack(config, isServer, {getJSLoader}) {
197
178
  const {
198
179
  rehypePlugins,
199
180
  remarkPlugins,
200
181
  beforeDefaultRehypePlugins,
201
182
  beforeDefaultRemarkPlugins,
202
183
  } = options;
184
+ const contentDirs = getContentPathList(contentPaths);
203
185
  return {
204
186
  resolve: {
205
187
  alias: {
@@ -209,8 +191,8 @@ export default function pluginContentPages(
209
191
  module: {
210
192
  rules: [
211
193
  {
212
- test: /(\.mdx?)$/,
213
- include: getContentPathList(contentPaths)
194
+ test: /\.mdx?$/i,
195
+ include: contentDirs
214
196
  // Trailing slash is important, see https://github.com/facebook/docusaurus/pull/3970
215
197
  .map(addTrailingPathSeparator),
216
198
  use: [
@@ -222,21 +204,23 @@ export default function pluginContentPages(
222
204
  rehypePlugins,
223
205
  beforeDefaultRehypePlugins,
224
206
  beforeDefaultRemarkPlugins,
225
- staticDir: path.join(siteDir, STATIC_DIR_NAME),
226
- // Note that metadataPath must be the same/in-sync as
227
- // the path from createData for each MDX.
207
+ staticDirs: siteConfig.staticDirectories.map((dir) =>
208
+ path.resolve(siteDir, dir),
209
+ ),
210
+ siteDir,
211
+ isMDXPartial: createAbsoluteFilePathMatcher(
212
+ options.exclude,
213
+ contentDirs,
214
+ ),
228
215
  metadataPath: (mdxPath: string) => {
229
- if (excludeRegex.test(slash(mdxPath))) {
230
- return null;
231
- }
216
+ // Note that metadataPath must be the same/in-sync as
217
+ // the path from createData for each MDX.
232
218
  const aliasedSource = aliasedSitePath(mdxPath, siteDir);
233
219
  return path.join(
234
220
  dataDir,
235
221
  `${docuHash(aliasedSource)}.json`,
236
222
  );
237
223
  },
238
- forbidFrontMatter: (mdxPath: string) =>
239
- excludeRegex.test(slash(mdxPath)),
240
224
  },
241
225
  },
242
226
  {
@@ -255,10 +239,4 @@ export default function pluginContentPages(
255
239
  };
256
240
  }
257
241
 
258
- export function validateOptions({
259
- validate,
260
- options,
261
- }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions> {
262
- const validatedOptions = validate(PluginOptionSchema, options);
263
- return validatedOptions;
264
- }
242
+ export {validateOptions} from './options';