mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-28 17:57:48 +02:00
refactor(v2): add typing for pages plugin (#1813)
* refactor(v2): add typing for pages plugin * misc: new lines
This commit is contained in:
parent
95f0552bad
commit
c4cc7f881b
13 changed files with 70 additions and 22 deletions
|
@ -15,3 +15,4 @@ packages/docusaurus/lib/
|
|||
packages/docusaurus-init/lib/
|
||||
packages/docusaurus-plugin-content-blog/lib/
|
||||
packages/docusaurus-plugin-content-docs-legacy/lib/
|
||||
packages/docusaurus-plugin-content-pages/lib/
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -17,3 +17,4 @@ packages/docusaurus/lib/
|
|||
packages/docusaurus-init/lib/
|
||||
packages/docusaurus-plugin-content-blog/lib/
|
||||
packages/docusaurus-plugin-content-docs-legacy/lib/
|
||||
packages/docusaurus-plugin-content-pages/lib/
|
||||
|
|
|
@ -7,3 +7,4 @@ packages/docusaurus/lib/
|
|||
packages/docusaurus-init/lib/
|
||||
packages/docusaurus-plugin-content-blog/lib/
|
||||
packages/docusaurus-plugin-content-docs-legacy/lib/
|
||||
packages/docusaurus-plugin-content-pages/lib/
|
||||
|
|
|
@ -19,6 +19,7 @@ module.exports = {
|
|||
'/packages/docusaurus-utils/lib',
|
||||
'/packages/docusaurus-plugin-content-blog/lib',
|
||||
'/packages/docusaurus-plugin-content-docs-legacy/lib',
|
||||
'/packages/docusaurus-plugin-content-pages/lib',
|
||||
],
|
||||
transform: {
|
||||
'^.+\\.[jt]sx?$': 'babel-jest',
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import path from 'path';
|
||||
|
||||
import pluginContentDocs from '../index';
|
||||
import {LoadContext} from '@docusaurus/types';
|
||||
|
||||
|
@ -24,7 +25,7 @@ describe('loadDocs', () => {
|
|||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const pluginPath = 'docs';
|
||||
const plugin = pluginContentDocs(context, {
|
||||
path: 'docs',
|
||||
path: pluginPath,
|
||||
sidebarPath,
|
||||
});
|
||||
const {docs: docsMetadata} = await plugin.loadContent();
|
||||
|
@ -38,7 +39,7 @@ describe('loadDocs', () => {
|
|||
sidebar: 'docs',
|
||||
source: path.join('@site', pluginPath, 'hello.md'),
|
||||
title: 'Hello, World !',
|
||||
description: `Hi, Endilie here :)`,
|
||||
description: 'Hi, Endilie here :)',
|
||||
});
|
||||
|
||||
expect(docsMetadata['foo/bar']).toEqual({
|
||||
|
|
|
@ -9,11 +9,11 @@ import globby from 'globby';
|
|||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import {idx, normalizeUrl, docuHash} from '@docusaurus/utils';
|
||||
import {LoadContext, Plugin, DocusaurusConfig} from '@docusaurus/types';
|
||||
|
||||
import createOrder from './order';
|
||||
import loadSidebars from './sidebars';
|
||||
import processMetadata from './metadata';
|
||||
import {LoadContext, Plugin, DocusaurusConfig} from '@docusaurus/types';
|
||||
import {
|
||||
PluginOptions,
|
||||
Sidebar,
|
||||
|
@ -46,7 +46,7 @@ export default function pluginContentDocs(
|
|||
let sourceToPermalink: SourceToPermalink = {};
|
||||
|
||||
return {
|
||||
name: 'docusaurus-plugin-content-docs',
|
||||
name: 'docusaurus-plugin-content-docs-legacy',
|
||||
|
||||
getPathsToWatch() {
|
||||
const {include = []} = options;
|
||||
|
@ -66,7 +66,7 @@ export default function pluginContentDocs(
|
|||
|
||||
const docsSidebars: Sidebar = loadSidebars(sidebarPath);
|
||||
|
||||
// Build the docs ordering such as next, previous, category and sidebar
|
||||
// Build the docs ordering such as next, previous, category and sidebar.
|
||||
const order: Order = createOrder(docsSidebars);
|
||||
|
||||
// Prepare metadata container.
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
export interface PluginOptions {
|
||||
path: string;
|
||||
routeBasePath: string;
|
||||
|
@ -42,6 +43,7 @@ export type SidebarItem =
|
|||
| SidebarItemDoc
|
||||
| SidebarItemLink
|
||||
| SidebarItemCategory;
|
||||
|
||||
export type SidebarItemRaw =
|
||||
| string
|
||||
| SidebarItemDoc
|
||||
|
|
|
@ -4,6 +4,6 @@
|
|||
"incremental": true,
|
||||
"tsBuildInfoFile": "./lib/.tsbuildinfo",
|
||||
"rootDir": "src",
|
||||
"outDir": "lib",
|
||||
"outDir": "lib"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,12 +2,16 @@
|
|||
"name": "@docusaurus/plugin-content-pages",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"description": "Pages content plugin for Docusaurus",
|
||||
"main": "src/index.js",
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"tsc": "tsc"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@docusaurus/types": "^2.0.0-alpha.25",
|
||||
"@docusaurus/utils": "^2.0.0-alpha.25",
|
||||
"globby": "^10.0.1"
|
||||
},
|
||||
|
|
|
@ -8,29 +8,34 @@
|
|||
import path from 'path';
|
||||
|
||||
import pluginContentPages from '../index';
|
||||
import {LoadContext} from '@docusaurus/types';
|
||||
|
||||
describe('docusaurus-plugin-content-pages', () => {
|
||||
test('simple pages', async () => {
|
||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||
const siteConfig = {
|
||||
title: 'Hello',
|
||||
baseUrl: '/',
|
||||
url: 'https://docusaurus.io',
|
||||
};
|
||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||
const plugin = pluginContentPages({
|
||||
const context = {
|
||||
siteDir,
|
||||
siteConfig,
|
||||
} as LoadContext;
|
||||
const pluginPath = 'src/pages';
|
||||
const plugin = pluginContentPages(context, {
|
||||
path: pluginPath,
|
||||
});
|
||||
const pagesMetadatas = await plugin.loadContent();
|
||||
const pagesPath = path.relative(siteDir, plugin.contentPath);
|
||||
|
||||
expect(pagesMetadatas).toEqual([
|
||||
{
|
||||
permalink: '/',
|
||||
source: path.join('@site', pagesPath, 'index.js'),
|
||||
source: path.join('@site', pluginPath, 'index.js'),
|
||||
},
|
||||
{
|
||||
permalink: '/hello/world',
|
||||
source: path.join('@site', pagesPath, 'hello', 'world.js'),
|
||||
source: path.join('@site', pluginPath, 'hello', 'world.js'),
|
||||
},
|
||||
]);
|
||||
});
|
|
@ -5,26 +5,30 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const globby = require('globby');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const {encodePath, fileToPath} = require('@docusaurus/utils');
|
||||
import globby from 'globby';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {encodePath, fileToPath} from '@docusaurus/utils';
|
||||
import {LoadContext, Plugin} from '@docusaurus/types';
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
import {PluginOptions, LoadedContent} from './types';
|
||||
|
||||
const DEFAULT_OPTIONS: PluginOptions = {
|
||||
path: 'src/pages', // Path to data on filesystem, relative to site dir.
|
||||
routeBasePath: '', // URL Route.
|
||||
include: ['**/*.{js,jsx}'], // Extensions to include.
|
||||
};
|
||||
|
||||
module.exports = function(context, opts) {
|
||||
export default function pluginContentPages(
|
||||
context: LoadContext,
|
||||
opts: Partial<PluginOptions>,
|
||||
): Plugin<LoadedContent | null> {
|
||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
||||
const contentPath = path.resolve(context.siteDir, options.path);
|
||||
|
||||
return {
|
||||
name: 'docusaurus-plugin-content-pages',
|
||||
|
||||
contentPath,
|
||||
|
||||
getPathsToWatch() {
|
||||
const {include = []} = options;
|
||||
const globPattern = include.map(pattern => `${contentPath}/${pattern}`);
|
||||
|
@ -52,7 +56,7 @@ module.exports = function(context, opts) {
|
|||
const pathName = encodePath(fileToPath(relativeSource));
|
||||
// Default Language.
|
||||
return {
|
||||
permalink: pathName.replace(/^\//, baseUrl),
|
||||
permalink: pathName.replace(/^\//, baseUrl || ''),
|
||||
source: aliasedSource,
|
||||
};
|
||||
});
|
||||
|
@ -77,4 +81,4 @@ module.exports = function(context, opts) {
|
|||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
19
packages/docusaurus-plugin-content-pages/src/types.ts
Normal file
19
packages/docusaurus-plugin-content-pages/src/types.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
export interface PluginOptions {
|
||||
path: string;
|
||||
routeBasePath: string;
|
||||
include: string[];
|
||||
}
|
||||
|
||||
export interface Metadata {
|
||||
permalink: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export type LoadedContent = Metadata[];
|
9
packages/docusaurus-plugin-content-pages/tsconfig.json
Normal file
9
packages/docusaurus-plugin-content-pages/tsconfig.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"incremental": true,
|
||||
"tsBuildInfoFile": "./lib/.tsbuildinfo",
|
||||
"rootDir": "src",
|
||||
"outDir": "lib"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue