mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 17:17:28 +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
|
@ -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[];
|
Loading…
Add table
Add a link
Reference in a new issue