@docusaurus/plugin-content-pages 2.0.0-beta.1decd6f80 → 2.0.0-beta.20

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 +5 -4
  4. package/lib/index.js +43 -59
  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 +56 -79
  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 -4349
  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
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';
24
+ import type {LoadContext, Plugin} from '@docusaurus/types';
30
25
  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';
26
+ import {validatePageFrontMatter} from './frontMatter';
36
27
 
37
- import {
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];
@@ -49,13 +39,13 @@ export function getContentPathList(contentPaths: PagesContentPaths): string[] {
49
39
  const isMarkdownSource = (source: string) =>
50
40
  source.endsWith('.md') || source.endsWith('.mdx');
51
41
 
52
- export default function pluginContentPages(
42
+ export default async function pluginContentPages(
53
43
  context: LoadContext,
54
44
  options: PluginOptions,
55
- ): Plugin<LoadedContent | null> {
45
+ ): Promise<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,22 +204,23 @@ export default function pluginContentPages(
222
204
  rehypePlugins,
223
205
  beforeDefaultRehypePlugins,
224
206
  beforeDefaultRemarkPlugins,
225
- keepContentTitle: true,
226
- staticDir: path.join(siteDir, STATIC_DIR_NAME),
227
- // Note that metadataPath must be the same/in-sync as
228
- // 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
+ ),
229
215
  metadataPath: (mdxPath: string) => {
230
- if (excludeRegex.test(slash(mdxPath))) {
231
- return null;
232
- }
216
+ // Note that metadataPath must be the same/in-sync as
217
+ // the path from createData for each MDX.
233
218
  const aliasedSource = aliasedSitePath(mdxPath, siteDir);
234
219
  return path.join(
235
220
  dataDir,
236
221
  `${docuHash(aliasedSource)}.json`,
237
222
  );
238
223
  },
239
- forbidFrontMatter: (mdxPath: string) =>
240
- excludeRegex.test(slash(mdxPath)),
241
224
  },
242
225
  },
243
226
  {
@@ -256,10 +239,4 @@ export default function pluginContentPages(
256
239
  };
257
240
  }
258
241
 
259
- export function validateOptions({
260
- validate,
261
- options,
262
- }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions> {
263
- const validatedOptions = validate(PluginOptionSchema, options);
264
- return validatedOptions;
265
- }
242
+ export {validateOptions} from './options';
@@ -5,21 +5,18 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- // TODO temporary until Webpack5 export this type
9
- // see https://github.com/webpack/webpack/issues/11630
10
- interface Loader extends Function {
11
- (this: any, source: string): string | Buffer | void | undefined;
12
- }
8
+ import type {LoaderContext} from 'webpack';
13
9
 
14
- const markdownLoader: Loader = function (fileString) {
10
+ export default function markdownLoader(
11
+ this: LoaderContext<undefined>,
12
+ fileString: string,
13
+ ): void {
15
14
  const callback = this.async();
16
15
 
17
16
  // const options = this.getOptions();
18
17
 
19
- // TODO provide additinal md processing here? like interlinking pages?
18
+ // TODO provide additional md processing here? like interlinking pages?
20
19
  // fileString = linkify(fileString)
21
20
 
22
- return callback && callback(null, fileString);
23
- };
24
-
25
- export default markdownLoader;
21
+ return callback?.(null, fileString);
22
+ }
@@ -4,32 +4,31 @@
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 {PluginOptions} from './types';
7
+
8
+ import type {PluginOptions, Options} from '@docusaurus/plugin-content-pages';
8
9
  import {
9
10
  Joi,
10
11
  RemarkPluginsSchema,
11
12
  RehypePluginsSchema,
12
13
  AdmonitionsSchema,
13
14
  } from '@docusaurus/utils-validation';
15
+ import {GlobExcludeDefault} from '@docusaurus/utils';
16
+ import type {OptionValidationContext} from '@docusaurus/types';
14
17
 
15
18
  export const DEFAULT_OPTIONS: PluginOptions = {
16
19
  path: 'src/pages', // Path to data on filesystem, relative to site dir.
17
20
  routeBasePath: '/', // URL Route.
18
21
  include: ['**/*.{js,jsx,ts,tsx,md,mdx}'], // Extensions to include.
22
+ exclude: GlobExcludeDefault,
19
23
  mdxPageComponent: '@theme/MDXPage',
20
24
  remarkPlugins: [],
21
25
  rehypePlugins: [],
22
26
  beforeDefaultRehypePlugins: [],
23
27
  beforeDefaultRemarkPlugins: [],
24
28
  admonitions: {},
25
- exclude: [
26
- '**/_*.{js,jsx,ts,tsx,md,mdx}',
27
- '**/*.test.{js,ts}',
28
- '**/__tests__/**',
29
- ],
30
29
  };
31
30
 
32
- export const PluginOptionSchema = Joi.object({
31
+ const PluginOptionSchema = Joi.object({
33
32
  path: Joi.string().default(DEFAULT_OPTIONS.path),
34
33
  routeBasePath: Joi.string().default(DEFAULT_OPTIONS.routeBasePath),
35
34
  include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
@@ -45,3 +44,11 @@ export const PluginOptionSchema = Joi.object({
45
44
  ),
46
45
  admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
47
46
  });
47
+
48
+ export function validateOptions({
49
+ validate,
50
+ options,
51
+ }: OptionValidationContext<Options, PluginOptions>): PluginOptions {
52
+ const validatedOptions = validate(PluginOptionSchema, options);
53
+ return validatedOptions;
54
+ }
@@ -5,24 +5,66 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- declare module '@theme/MDXPage' {
9
- import type {TOCItem} from '@docusaurus/types';
10
-
11
- export type Props = {
12
- readonly content: {
13
- readonly frontMatter: {
14
- readonly title: string;
15
- readonly description: string;
16
- readonly wrapperClassName?: string;
17
- // eslint-disable-next-line camelcase
18
- readonly hide_table_of_contents?: string;
19
- };
20
- readonly metadata: {readonly permalink: string};
21
- readonly toc: readonly TOCItem[];
22
- (): JSX.Element;
23
- };
8
+ declare module '@docusaurus/plugin-content-pages' {
9
+ import type {MDXOptions} from '@docusaurus/mdx-loader';
10
+ import type {LoadContext, Plugin} from '@docusaurus/types';
11
+
12
+ export type PluginOptions = MDXOptions & {
13
+ id?: string;
14
+ path: string;
15
+ routeBasePath: string;
16
+ include: string[];
17
+ exclude: string[];
18
+ mdxPageComponent: string;
19
+ admonitions: {[key: string]: unknown};
20
+ };
21
+
22
+ export type Options = Partial<PluginOptions>;
23
+
24
+ export type FrontMatter = {
25
+ readonly title?: string;
26
+ readonly description?: string;
27
+ readonly wrapperClassName?: string;
28
+ readonly hide_table_of_contents?: string;
29
+ readonly toc_min_heading_level?: number;
30
+ readonly toc_max_heading_level?: number;
31
+ };
32
+
33
+ export type JSXPageMetadata = {
34
+ type: 'jsx';
35
+ permalink: string;
36
+ source: string;
37
+ };
38
+
39
+ export type MDXPageMetadata = {
40
+ type: 'mdx';
41
+ permalink: string;
42
+ source: string;
43
+ frontMatter: FrontMatter & {[key: string]: unknown};
44
+ title?: string;
45
+ description?: string;
24
46
  };
25
47
 
26
- const MDXPage: (props: Props) => JSX.Element;
27
- export default MDXPage;
48
+ export type Metadata = JSXPageMetadata | MDXPageMetadata;
49
+
50
+ export type LoadedContent = Metadata[];
51
+
52
+ export default function pluginContentPages(
53
+ context: LoadContext,
54
+ options: PluginOptions,
55
+ ): Promise<Plugin<LoadedContent | null>>;
56
+ }
57
+
58
+ declare module '@theme/MDXPage' {
59
+ import type {LoadedMDXContent} from '@docusaurus/mdx-loader';
60
+ import type {
61
+ MDXPageMetadata,
62
+ FrontMatter,
63
+ } from '@docusaurus/plugin-content-pages';
64
+
65
+ export interface Props {
66
+ readonly content: LoadedMDXContent<FrontMatter, MDXPageMetadata>;
67
+ }
68
+
69
+ export default function MDXPage(props: Props): JSX.Element;
28
70
  }
package/src/types.ts CHANGED
@@ -5,34 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
9
-
10
- export interface PluginOptions extends RemarkAndRehypePluginOptions {
11
- id?: string;
12
- path: string;
13
- routeBasePath: string;
14
- include: string[];
15
- exclude: string[];
16
- mdxPageComponent: string;
17
- admonitions: Record<string, unknown>;
18
- }
19
-
20
- export type JSXPageMetadata = {
21
- type: 'jsx';
22
- permalink: string;
23
- source: string;
24
- };
25
-
26
- export type MDXPageMetadata = {
27
- type: 'mdx';
28
- permalink: string;
29
- source: string;
30
- };
31
-
32
- export type Metadata = JSXPageMetadata | MDXPageMetadata;
33
-
34
- export type LoadedContent = Metadata[];
35
-
36
8
  export type PagesContentPaths = {
37
9
  contentPath: string;
38
10
  contentPathLocalized: string;