@docusaurus/plugin-content-pages 2.0.0-beta.14 → 2.0.0-beta.15

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.
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, OptionValidationContext, ValidationResult } from '@docusaurus/types';
8
+ import type { LoadedContent, PagesContentPaths } from './types';
9
+ import type { PluginOptions } from '@docusaurus/plugin-content-pages';
9
10
  export declare function getContentPathList(contentPaths: PagesContentPaths): string[];
10
- export default function pluginContentPages(context: LoadContext, options: PluginOptions): Plugin<LoadedContent | null>;
11
+ export default function pluginContentPages(context: LoadContext, options: PluginOptions): Promise<Plugin<LoadedContent | null>>;
11
12
  export declare function validateOptions({ validate, options, }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions>;
package/lib/index.js CHANGED
@@ -8,17 +8,18 @@
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 fs_1 = (0, tslib_1.__importDefault)(require("fs"));
11
+ const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
12
12
  const path_1 = (0, tslib_1.__importDefault)(require("path"));
13
13
  const utils_1 = require("@docusaurus/utils");
14
14
  const remark_admonitions_1 = (0, tslib_1.__importDefault)(require("remark-admonitions"));
15
15
  const pluginOptionSchema_1 = require("./pluginOptionSchema");
16
+ const pageFrontMatter_1 = require("./pageFrontMatter");
16
17
  function getContentPathList(contentPaths) {
17
18
  return [contentPaths.contentPathLocalized, contentPaths.contentPath];
18
19
  }
19
20
  exports.getContentPathList = getContentPathList;
20
21
  const isMarkdownSource = (source) => source.endsWith('.md') || source.endsWith('.mdx');
21
- function pluginContentPages(context, options) {
22
+ async function pluginContentPages(context, options) {
22
23
  var _a;
23
24
  if (options.admonitions) {
24
25
  options.remarkPlugins = options.remarkPlugins.concat([
@@ -45,7 +46,7 @@ function pluginContentPages(context, options) {
45
46
  },
46
47
  async loadContent() {
47
48
  const { include } = options;
48
- if (!fs_1.default.existsSync(contentPaths.contentPath)) {
49
+ if (!(await fs_extra_1.default.pathExists(contentPaths.contentPath))) {
49
50
  return null;
50
51
  }
51
52
  const { baseUrl } = siteConfig;
@@ -54,6 +55,7 @@ function pluginContentPages(context, options) {
54
55
  ignore: options.exclude,
55
56
  });
56
57
  async function toMetadata(relativeSource) {
58
+ var _a, _b;
57
59
  // Lookup in localized folder in priority
58
60
  const contentPath = await (0, utils_1.getFolderContainingFile)(getContentPathList(contentPaths), relativeSource);
59
61
  const source = path_1.default.join(contentPath, relativeSource);
@@ -64,11 +66,16 @@ function pluginContentPages(context, options) {
64
66
  (0, utils_1.encodePath)((0, utils_1.fileToPath)(relativeSource)),
65
67
  ]);
66
68
  if (isMarkdownSource(relativeSource)) {
67
- // TODO: missing frontmatter validation/normalization here
69
+ const content = await fs_extra_1.default.readFile(source, 'utf-8');
70
+ const { frontMatter: unsafeFrontMatter, contentTitle, excerpt, } = (0, utils_1.parseMarkdownString)(content);
71
+ const frontMatter = (0, pageFrontMatter_1.validatePageFrontMatter)(unsafeFrontMatter);
68
72
  return {
69
73
  type: 'mdx',
70
74
  permalink,
71
75
  source: aliasedSourcePath,
76
+ title: (_a = frontMatter.title) !== null && _a !== void 0 ? _a : contentTitle,
77
+ description: (_b = frontMatter.description) !== null && _b !== void 0 ? _b : excerpt,
78
+ frontMatter,
72
79
  };
73
80
  }
74
81
  else {
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
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
14
  return callback && callback(null, fileString);
15
15
  }
@@ -0,0 +1,8 @@
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 { FrontMatter } from '@docusaurus/plugin-content-pages';
8
+ export declare function validatePageFrontMatter(frontMatter: Record<string, unknown>): 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;
@@ -4,7 +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
- import { PluginOptions } from './types';
7
+ import type { PluginOptions } from '@docusaurus/plugin-content-pages';
8
8
  import { Joi } from '@docusaurus/utils-validation';
9
9
  export declare const DEFAULT_OPTIONS: PluginOptions;
10
10
  export declare const PluginOptionSchema: Joi.ObjectSchema<any>;
package/lib/types.d.ts CHANGED
@@ -4,27 +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
- import type { RemarkAndRehypePluginOptions } from '@docusaurus/mdx-loader';
8
- export declare type PluginOptions = 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;
7
+ import type { Metadata } from '@docusaurus/plugin-content-pages';
28
8
  export declare type LoadedContent = Metadata[];
29
9
  export declare type PagesContentPaths = {
30
10
  contentPath: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-pages",
3
- "version": "2.0.0-beta.14",
3
+ "version": "2.0.0-beta.15",
4
4
  "description": "Pages plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-content-pages.d.ts",
@@ -18,17 +18,18 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "2.0.0-beta.14",
22
- "@docusaurus/mdx-loader": "2.0.0-beta.14",
23
- "@docusaurus/utils": "2.0.0-beta.14",
24
- "@docusaurus/utils-validation": "2.0.0-beta.14",
21
+ "@docusaurus/core": "2.0.0-beta.15",
22
+ "@docusaurus/mdx-loader": "2.0.0-beta.15",
23
+ "@docusaurus/utils": "2.0.0-beta.15",
24
+ "@docusaurus/utils-validation": "2.0.0-beta.15",
25
+ "fs-extra": "^10.0.0",
25
26
  "globby": "^11.0.2",
26
27
  "remark-admonitions": "^1.2.1",
27
28
  "tslib": "^2.3.1",
28
29
  "webpack": "^5.61.0"
29
30
  },
30
31
  "devDependencies": {
31
- "@docusaurus/types": "2.0.0-beta.14"
32
+ "@docusaurus/types": "2.0.0-beta.15"
32
33
  },
33
34
  "peerDependencies": {
34
35
  "react": "^16.8.4 || ^17.0.0",
@@ -37,5 +38,5 @@
37
38
  "engines": {
38
39
  "node": ">=14"
39
40
  },
40
- "gitHead": "c4824a8937d8f1aa0806667749cbc74058e2b294"
41
+ "gitHead": "6cfad16436c07d8d11e5c2e1486dc59afd483e33"
41
42
  }
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import fs from 'fs';
8
+ import fs from 'fs-extra';
9
9
  import path from 'path';
10
10
  import {
11
11
  encodePath,
@@ -19,24 +19,22 @@ import {
19
19
  createAbsoluteFilePathMatcher,
20
20
  normalizeUrl,
21
21
  DEFAULT_PLUGIN_ID,
22
+ parseMarkdownString,
22
23
  } from '@docusaurus/utils';
23
- import {
24
+ import type {
24
25
  LoadContext,
25
26
  Plugin,
26
27
  OptionValidationContext,
27
28
  ValidationResult,
28
29
  ConfigureWebpackUtils,
29
30
  } from '@docusaurus/types';
30
- import {Configuration} from 'webpack';
31
+ import type {Configuration} from 'webpack';
31
32
  import admonitions from 'remark-admonitions';
32
33
  import {PluginOptionSchema} from './pluginOptionSchema';
34
+ import {validatePageFrontMatter} from './pageFrontMatter';
33
35
 
34
- import {
35
- PluginOptions,
36
- LoadedContent,
37
- Metadata,
38
- PagesContentPaths,
39
- } from './types';
36
+ import type {LoadedContent, PagesContentPaths} from './types';
37
+ import type {PluginOptions, Metadata} from '@docusaurus/plugin-content-pages';
40
38
 
41
39
  export function getContentPathList(contentPaths: PagesContentPaths): string[] {
42
40
  return [contentPaths.contentPathLocalized, contentPaths.contentPath];
@@ -45,10 +43,10 @@ export function getContentPathList(contentPaths: PagesContentPaths): string[] {
45
43
  const isMarkdownSource = (source: string) =>
46
44
  source.endsWith('.md') || source.endsWith('.mdx');
47
45
 
48
- export default function pluginContentPages(
46
+ export default async function pluginContentPages(
49
47
  context: LoadContext,
50
48
  options: PluginOptions,
51
- ): Plugin<LoadedContent | null> {
49
+ ): Promise<Plugin<LoadedContent | null>> {
52
50
  if (options.admonitions) {
53
51
  options.remarkPlugins = options.remarkPlugins.concat([
54
52
  [admonitions, options.admonitions || {}],
@@ -90,7 +88,7 @@ export default function pluginContentPages(
90
88
  async loadContent() {
91
89
  const {include} = options;
92
90
 
93
- if (!fs.existsSync(contentPaths.contentPath)) {
91
+ if (!(await fs.pathExists(contentPaths.contentPath))) {
94
92
  return null;
95
93
  }
96
94
 
@@ -115,11 +113,20 @@ export default function pluginContentPages(
115
113
  encodePath(fileToPath(relativeSource)),
116
114
  ]);
117
115
  if (isMarkdownSource(relativeSource)) {
118
- // TODO: missing frontmatter validation/normalization here
116
+ const content = await fs.readFile(source, 'utf-8');
117
+ const {
118
+ frontMatter: unsafeFrontMatter,
119
+ contentTitle,
120
+ excerpt,
121
+ } = parseMarkdownString(content);
122
+ const frontMatter = validatePageFrontMatter(unsafeFrontMatter);
119
123
  return {
120
124
  type: 'mdx',
121
125
  permalink,
122
126
  source: aliasedSourcePath,
127
+ title: frontMatter.title ?? contentTitle,
128
+ description: frontMatter.description ?? excerpt,
129
+ frontMatter,
123
130
  };
124
131
  } else {
125
132
  return {
@@ -15,7 +15,7 @@ export default function markdownLoader(
15
15
 
16
16
  // const options = this.getOptions();
17
17
 
18
- // TODO provide additinal md processing here? like interlinking pages?
18
+ // TODO provide additional md processing here? like interlinking pages?
19
19
  // fileString = linkify(fileString)
20
20
 
21
21
  return callback && callback(null, fileString);
@@ -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(
24
+ frontMatter: Record<string, unknown>,
25
+ ): FrontMatter {
26
+ return validateFrontMatter(frontMatter, PageFrontMatterSchema);
27
+ }
@@ -6,23 +6,58 @@
6
6
  */
7
7
 
8
8
  declare module '@docusaurus/plugin-content-pages' {
9
- export type Options = Partial<import('./types').PluginOptions>;
9
+ import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
10
+
11
+ export type PluginOptions = RemarkAndRehypePluginOptions & {
12
+ id?: string;
13
+ path: string;
14
+ routeBasePath: string;
15
+ include: string[];
16
+ exclude: string[];
17
+ mdxPageComponent: string;
18
+ admonitions: Record<string, unknown>;
19
+ };
20
+
21
+ export type Options = Partial<PluginOptions>;
22
+
23
+ export type FrontMatter = {
24
+ readonly title?: string;
25
+ readonly description?: string;
26
+ readonly wrapperClassName?: string;
27
+ readonly hide_table_of_contents?: string;
28
+ readonly toc_min_heading_level?: number;
29
+ readonly toc_max_heading_level?: number;
30
+ };
31
+
32
+ export type JSXPageMetadata = {
33
+ type: 'jsx';
34
+ permalink: string;
35
+ source: string;
36
+ };
37
+
38
+ export type MDXPageMetadata = {
39
+ type: 'mdx';
40
+ permalink: string;
41
+ source: string;
42
+ frontMatter: FrontMatter & Record<string, unknown>;
43
+ title?: string;
44
+ description?: string;
45
+ };
46
+
47
+ export type Metadata = JSXPageMetadata | MDXPageMetadata;
10
48
  }
11
49
 
12
50
  declare module '@theme/MDXPage' {
13
51
  import type {TOCItem} from '@docusaurus/types';
52
+ import type {
53
+ MDXPageMetadata,
54
+ FrontMatter,
55
+ } from '@docusaurus/plugin-content-pages';
14
56
 
15
57
  export interface Props {
16
58
  readonly content: {
17
- readonly frontMatter: {
18
- readonly title: string;
19
- readonly description: string;
20
- readonly wrapperClassName?: string;
21
- readonly hide_table_of_contents?: string;
22
- readonly toc_min_heading_level?: number;
23
- readonly toc_max_heading_level?: number;
24
- };
25
- readonly metadata: {readonly permalink: string};
59
+ readonly frontMatter: FrontMatter;
60
+ readonly metadata: MDXPageMetadata;
26
61
  readonly toc: readonly TOCItem[];
27
62
  (): JSX.Element;
28
63
  };
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {PluginOptions} from './types';
8
+ import type {PluginOptions} from '@docusaurus/plugin-content-pages';
9
9
  import {
10
10
  Joi,
11
11
  RemarkPluginsSchema,
package/src/types.ts CHANGED
@@ -5,31 +5,7 @@
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 type PluginOptions = 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;
8
+ import type {Metadata} from '@docusaurus/plugin-content-pages';
33
9
 
34
10
  export type LoadedContent = Metadata[];
35
11