Skip to content

Commit

Permalink
feat(v2): debug pages + debug layout + ability to debug content (#3229)
Browse files Browse the repository at this point in the history
* improve debug plugin:
- add multiple debug pages + debug layout
- ability to debug plugin contentLoaded data

* add missing dependency

* fix broken test

* improve content rendering a bit

* create basic DebugJsonView

* fix ReactJson SSR issues
  • Loading branch information
slorber authored Aug 7, 2020
1 parent be210a1 commit fe281a8
Show file tree
Hide file tree
Showing 30 changed files with 511 additions and 110 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-client-redirects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"license": "MIT",
"dependencies": {
"@docusaurus/core": "^2.0.0-alpha.61",
"@docusaurus/types": "^2.0.0-alpha.61",
"@docusaurus/utils": "^2.0.0-alpha.61",
"@hapi/joi": "^17.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('normalizePluginOptions', () => {
redirects: [{from: '/x', to: '/y'}],
}),
).toEqual({
id: 'default',
fromExtensions: ['exe', 'zip'],
toExtensions: ['html'],
createRedirects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import {PluginOptions, RedirectOption, UserPluginOptions} from './types';
import * as Joi from '@hapi/joi';
import {PathnameValidator} from './redirectValidation';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';

export const DefaultPluginOptions: PluginOptions = {
id: DEFAULT_PLUGIN_ID, // TODO temporary
fromExtensions: [],
toExtensions: [],
redirects: [],
Expand All @@ -26,6 +28,7 @@ const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
const isString = Joi.string().required().not(null);

const UserOptionsSchema = Joi.object<UserPluginOptions>({
id: Joi.string().optional(), // TODO remove once validation migrated to new system
fromExtensions: Joi.array().items(isString),
toExtensions: Joi.array().items(isString),
redirects: Joi.array().items(RedirectPluginOptionValidation),
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-client-redirects/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {Props} from '@docusaurus/types';

export type PluginOptions = {
id: string;
fromExtensions: string[];
toExtensions: string[];
redirects: RedirectOption[];
Expand Down
9 changes: 1 addition & 8 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import {PluginOptionSchema} from './pluginOptionSchema';
import {
LoadContext,
PluginContentLoadedActions,
ConfigureWebpackUtils,
Props,
Plugin,
Expand Down Expand Up @@ -195,13 +194,7 @@ export default function pluginContentBlog(
};
},

async contentLoaded({
content: blogContents,
actions,
}: {
content: BlogContent;
actions: PluginContentLoadedActions;
}) {
async contentLoaded({content: blogContents, actions}) {
if (!blogContents) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"license": "MIT",
"dependencies": {
"@docusaurus/types": "^2.0.0-alpha.61",
"@docusaurus/utils": "^2.0.0-alpha.61"
"@docusaurus/utils": "^2.0.0-alpha.61",
"react-json-view": "^1.19.1"
},
"peerDependencies": {
"@docusaurus/core": "^2.0.0",
Expand Down
66 changes: 62 additions & 4 deletions packages/docusaurus-plugin-debug/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,84 @@
*/

import {LoadContext, Plugin} from '@docusaurus/types';
import {normalizeUrl} from '@docusaurus/utils';

import {docuHash, normalizeUrl} from '@docusaurus/utils';
import path from 'path';

export default function pluginContentPages({
siteConfig: {baseUrl},
generatedFilesDir,
}: LoadContext): Plugin<void> {
const pluginDataDirRoot = path.join(
generatedFilesDir,
'docusaurus-plugin-debug',
);
const aliasedSource = (source: string) =>
`~debug/${path.relative(pluginDataDirRoot, source)}`;

return {
name: 'docusaurus-plugin-debug',

getThemePath() {
return path.resolve(__dirname, '../src/theme');
},

contentLoaded({actions: {addRoute}}) {
async contentLoaded({actions: {createData, addRoute}, allContent}) {
const allContentPath = await createData(
// Note that this created data path must be in sync with
// metadataPath provided to mdx-loader.
`${docuHash('docusaurus-debug-allContent')}.json`,
JSON.stringify(allContent, null, 2),
);

// Home is config (duplicate for now)
addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug']),
component: '@theme/Debug',
component: '@theme/DebugConfig',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/config']),
component: '@theme/DebugConfig',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/metadata']),
component: '@theme/DebugMetadata',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/registry']),
component: '@theme/DebugRegistry',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/routes']),
component: '@theme/DebugRoutes',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/content']),
component: '@theme/DebugContent',
exact: true,
modules: {
allContent: aliasedSource(allContentPath),
},
});
},

configureWebpack() {
return {
resolve: {
alias: {
'~debug': pluginDataDirRoot,
},
},
};
},
};
}
70 changes: 0 additions & 70 deletions packages/docusaurus-plugin-debug/src/theme/Debug/index.js

This file was deleted.

23 changes: 23 additions & 0 deletions packages/docusaurus-plugin-debug/src/theme/DebugConfig/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';

import DebugLayout from '../DebugLayout';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

function DebugMetadata() {
const {siteConfig} = useDocusaurusContext();
return (
<DebugLayout>
<h2>Site config</h2>
<div>{JSON.stringify(siteConfig, null, 2)}</div>
</DebugLayout>
);
}

export default DebugMetadata;
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,3 @@
* LICENSE file in the root directory of this source tree.
*/

.Container {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 1em;
}

.Section {
width: 500px;
}
84 changes: 84 additions & 0 deletions packages/docusaurus-plugin-debug/src/theme/DebugContent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, {useState} from 'react';

import DebugLayout from '../DebugLayout';
import DebugJsonView from '../DebugJsonView';

const PluginInstanceContent = ({pluginId, pluginInstanceContent}) => (
<section style={{marginBottom: 30}}>
<h4>{`>> ${pluginId}`}</h4>
<div
style={{
marginTop: 10,
padding: 10,
border: 'thin cyan solid',
borderRadius: 5,
backgroundColor: 'lightgrey',
}}>
<DebugJsonView src={pluginInstanceContent} />
</div>
</section>
);

const PluginContent = ({pluginName, pluginContent}) => {
const [visible, setVisible] = useState(true);
return (
<section style={{marginBottom: 60}}>
<h3 onClick={() => setVisible((v) => !v)} style={{cursor: 'pointer'}}>
{pluginName}
</h3>
{visible && (
<div>
{Object.entries(pluginContent)
// filter plugin instances with no content
.filter(
([_pluginId, pluginInstanceContent]) => !!pluginInstanceContent,
)
.map(([pluginId, pluginInstanceContent]) => {
return (
<PluginInstanceContent
key={pluginId}
pluginId={pluginId}
pluginInstanceContent={pluginInstanceContent}
/>
);
})}
</div>
)}
</section>
);
};

function DebugContent({allContent}) {
return (
<DebugLayout>
<h2>Plugin content</h2>
<div>
{Object.entries(allContent)
// filter plugins with no content
.filter(([_pluginName, pluginContent]) =>
Object.values(pluginContent).some(
(instanceContent) => !!instanceContent,
),
)
.map(([pluginName, pluginContent]) => {
return (
<PluginContent
key={pluginName}
pluginName={pluginName}
pluginContent={pluginContent}
/>
);
})}
</div>
</DebugLayout>
);
}

export default DebugContent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

Loading

0 comments on commit fe281a8

Please sign in to comment.