Skip to content

Commit

Permalink
[docs] Fix migration to have singular urls (mui#30695)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp authored Jan 19, 2022
1 parent 5e3d995 commit 9316900
Show file tree
Hide file tree
Showing 20 changed files with 1,149 additions and 274 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ module.exports = {
},
// demos
{
files: ['docs/src/pages/**/*.js', 'docs/src/pages/**/*.tsx'],
files: [
'docs/src/pages/**/*.js',
'docs/src/pages/**/*.tsx',
'docs/data/**/*.js',
'docs/data/**/*.tsx',
],
rules: {
// This most often reports data that is defined after the component definition.
// This is safe to do and helps readability of the demo code since the data is mostly irrelevant.
Expand Down
59 changes: 33 additions & 26 deletions docs/data/material/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const pages = [
},
{
pathname: '/material/react-',
scopePathnames: [
'/material/icons',
'/material/material-icons',
'/material/about-the-lab',
'/material/transitions',
'/material/pickers',
],
title: 'Components',
icon: 'ToggleOnIcon',
children: [
Expand All @@ -26,16 +33,16 @@ const pages = [
subheader: 'inputs',
children: [
{ pathname: '/material/react-autocomplete' },
{ pathname: '/material/react-buttons', title: 'Button' },
{ pathname: '/material/react-button', title: 'Button' },
{ pathname: '/material/react-button-group' },
{ pathname: '/material/react-checkboxes', title: 'Checkbox' },
{ pathname: '/material/react-checkbox', title: 'Checkbox' },
{ pathname: '/material/react-floating-action-button' },
{ pathname: '/material/react-radio-buttons', title: 'Radio button' },
{ pathname: '/material/react-radio-button', title: 'Radio button' },
{ pathname: '/material/react-rating' },
{ pathname: '/material/react-selects', title: 'Select' },
{ pathname: '/material/react-select', title: 'Select' },
{ pathname: '/material/react-slider' },
{ pathname: '/material/react-switches', title: 'Switch' },
{ pathname: '/material/react-text-fields', title: 'Text field' },
{ pathname: '/material/react-switch', title: 'Switch' },
{ pathname: '/material/react-text-field', title: 'Text field' },
{ pathname: '/material/react-transfer-list' },
{ pathname: '/material/react-toggle-button' },
],
Expand All @@ -44,15 +51,15 @@ const pages = [
pathname: '/material/components/data-display',
subheader: 'data-display',
children: [
{ pathname: '/material/react-avatars', title: 'Avatar' },
{ pathname: '/material/react-badges', title: 'Badge' },
{ pathname: '/material/react-chips', title: 'Chip' },
{ pathname: '/material/react-dividers', title: 'Divider' },
{ pathname: '/material/react-icons' },
{ pathname: '/material/react-material-icons' },
{ pathname: '/material/react-lists', title: 'List' },
{ pathname: '/material/react-tables', title: 'Table' },
{ pathname: '/material/react-tooltips', title: 'Tooltip' },
{ pathname: '/material/react-avatar', title: 'Avatar' },
{ pathname: '/material/react-badge', title: 'Badge' },
{ pathname: '/material/react-chip', title: 'Chip' },
{ pathname: '/material/react-divider', title: 'Divider' },
{ pathname: '/material/icons' },
{ pathname: '/material/material-icons' },
{ pathname: '/material/react-list', title: 'List' },
{ pathname: '/material/react-table', title: 'Table' },
{ pathname: '/material/react-tooltip', title: 'Tooltip' },
{ pathname: '/material/react-typography' },
],
},
Expand All @@ -62,10 +69,10 @@ const pages = [
children: [
{ pathname: '/material/react-alert' },
{ pathname: '/material/react-backdrop' },
{ pathname: '/material/react-dialogs' },
{ pathname: '/material/react-dialog' },
{ pathname: '/material/react-progress' },
{ pathname: '/material/react-skeleton' },
{ pathname: '/material/react-snackbars', title: 'Snackbar' },
{ pathname: '/material/react-snackbar', title: 'Snackbar' },
],
},
{
Expand All @@ -74,7 +81,7 @@ const pages = [
children: [
{ pathname: '/material/react-accordion' },
{ pathname: '/material/react-app-bar' },
{ pathname: '/material/react-cards', title: 'Card' },
{ pathname: '/material/react-card', title: 'Card' },
{ pathname: '/material/react-paper' },
],
},
Expand All @@ -84,12 +91,12 @@ const pages = [
children: [
{ pathname: '/material/react-bottom-navigation' },
{ pathname: '/material/react-breadcrumbs' },
{ pathname: '/material/react-drawers', title: 'Drawer' },
{ pathname: '/material/react-links', title: 'Link' },
{ pathname: '/material/react-menus', title: 'Menu' },
{ pathname: '/material/react-drawer', title: 'Drawer' },
{ pathname: '/material/react-link', title: 'Link' },
{ pathname: '/material/react-menu', title: 'Menu' },
{ pathname: '/material/react-pagination' },
{ pathname: '/material/react-speed-dial' },
{ pathname: '/material/react-steppers', title: 'Stepper' },
{ pathname: '/material/react-stepper', title: 'Stepper' },
{ pathname: '/material/react-tabs' },
],
},
Expand Down Expand Up @@ -117,12 +124,12 @@ const pages = [
{ pathname: '/material/react-popper' },
{ pathname: '/material/react-portal' },
{ pathname: '/material/react-textarea-autosize' },
{ pathname: '/material/react-transitions' },
{ pathname: '/material/transitions' },
{ pathname: '/material/react-use-media-query', title: 'useMediaQuery' },
],
},
{
pathname: '/x/data-grid',
pathname: '/x/data-grid/',
subheader: 'data-grid',
},
{
Expand All @@ -131,11 +138,11 @@ const pages = [
children: [
{ pathname: '/material/about-the-lab', title: 'About the lab 🧪' },
{
pathname: '/material/lab/pickers',
pathname: '/material/pickers',
subheader: 'pickers',
title: 'Date / Time',
children: [
{ pathname: '/material/react-pickers', title: 'Introduction' },
{ pathname: '/material/pickers', title: 'Introduction' },
{ pathname: '/material/react-date-picker' },
{
pathname: '/material/react-date-range-picker',
Expand Down
51 changes: 39 additions & 12 deletions docs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,30 +259,57 @@ module.exports = {
permanent: false,
},
{
source: '/components/about-the-lab',
destination: '/material/about-the-lab',
source: '/components/data-grid/:path*',
destination: '/x/react-data-grid/:path*',
permanent: false,
},
{
source: '/components/data-grid/:path*',
destination: '/x/react-data-grid/:path*',
source: '/components/:slug(icons|material-icons|about-the-lab|transitions|pickers)',
destination: '/material/:slug',
permanent: false,
},
{
source: '/components/:path*',
destination: '/material/react-:path*',
source: '/components/:path(tabs|breadcrumbs)',
destination: '/material/react-:path',
permanent: false,
},
...['checkboxes', 'switches'].map((component) => ({
source: `/components/${component}`,
destination: `/material/react-${component.replace(/es$/, '')}`,
permanent: false,
})),
...[
'buttons',
'radio-buttons',
'selects',
'text-fields',
'avatars',
'badges',
'chips',
'dividers',
'lists',
'tables',
'tooltips',
'dialogs',
'snackbars',
'cards',
'drawers',
'links',
'menus',
'steppers',
].map((component) => ({
source: `/components/${component}`,
destination: `/material/react-${component.replace(/s$/, '')}`,
permanent: false,
})),
{
source: '/api/data-grid/:path*',
destination: '/x/api/data-grid/:path*',
source: '/components/:path',
destination: '/material/react-:path',
permanent: false,
},
{
source:
// if this regex change, make sure to update `replaceMarkdownLinks`
'/api/:path(loading-button|tab-list|tab-panel|date-picker|date-time-picker|time-picker|calendar-picker|calendar-picker-skeleton|desktop-picker|mobile-date-picker|month-picker|pickers-day|static-date-picker|year-picker|masonry|timeline|timeline-connector|timeline-content|timeline-dot|timeline-item|timeline-opposite-content|timeline-separator|unstable-trap-focus|tree-item|tree-view)',
destination: '/material/api/:path*',
source: '/api/data-grid/:path*',
destination: '/x/api/data-grid/:path*',
permanent: false,
},
{
Expand Down
77 changes: 77 additions & 0 deletions docs/scripts/postMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs from 'fs-extra';
import path from 'path';
import prettier from 'prettier';
import replaceMarkdownLinks from '../src/modules/utils/replaceMarkdownLinks';

const workspaceRoot = path.resolve(__dirname, '../../');
const prettierConfigPath = path.join(workspaceRoot, 'prettier.config.js');

function writePrettifiedFile(filename: string, data: string, options: object = {}) {
const prettierConfig = prettier.resolveConfig.sync(filename, {
config: prettierConfigPath,
});
if (prettierConfig === null) {
throw new Error(
`Could not resolve config for '${filename}' using prettier config path '${prettierConfigPath}'.`,
);
}

fs.writeFileSync(filename, prettier.format(data, { ...prettierConfig, filepath: filename }), {
encoding: 'utf8',
...options,
});
}

const readdirDeep = (directory: string, pathsProp: string[] = []) => {
const paths: string[] = pathsProp;
const items = fs.readdirSync(directory);
items.forEach((item) => {
const itemPath = path.resolve(directory, item);

if (fs.statSync(itemPath).isDirectory()) {
readdirDeep(itemPath, paths);
}

if (itemPath.match(/.*\/[^/]+\.[^.]+/)) {
// ends with extension
paths.push(itemPath);
}
});

return paths;
};

function run() {
fs.removeSync(path.resolve(`docs/pages/api-docs`));
fs.removeSync(path.resolve(`docs/pages/components`));
fs.removeSync(path.resolve(`docs/pages/customization`));
fs.removeSync(path.resolve(`docs/pages/discover-more`));
fs.removeSync(path.resolve(`docs/pages/getting-started`));
fs.removeSync(path.resolve(`docs/pages/guides`));
fs.removeSync(path.resolve(`docs/pages/styles`));

fs.removeSync(path.resolve(`test/e2e-website/material-current.spec.ts`));

const dataDir = readdirDeep(path.resolve(`docs/data`));
dataDir.forEach((filePath) => {
if (filePath.endsWith('.md')) {
let data = fs.readFileSync(filePath, { encoding: 'utf-8' });

data = replaceMarkdownLinks(data);

writePrettifiedFile(filePath, data);
}
});

// Turn feature toggle `enable_product_scope: true`
const featureTogglePath = path.join(process.cwd(), 'docs/src/featureToggle.js');
let featureToggle = fs.readFileSync(featureTogglePath, { encoding: 'utf8' });

featureToggle = featureToggle
.replace(`enable_redirects: false`, `enable_redirects: true`)
.replace(`enable_system_scope: false`, `enable_system_scope: true`);

fs.writeFileSync(featureTogglePath, featureToggle);
}

run();
44 changes: 24 additions & 20 deletions docs/scripts/restructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ const workspaceRoot = path.resolve(__dirname, '../../');
const prettierConfigPath = path.join(workspaceRoot, 'prettier.config.js');

function writePrettifiedFile(filename: string, data: string, options: object = {}) {
const prettierConfig = prettier.resolveConfig.sync(filename, {
config: prettierConfigPath,
});
if (prettierConfig === null) {
throw new Error(
`Could not resolve config for '${filename}' using prettier config path '${prettierConfigPath}'.`,
);
}
if (filename.endsWith('.tsx.preview')) {
fs.writeFileSync(filename, data);
} else {
const prettierConfig = prettier.resolveConfig.sync(filename, {
config: prettierConfigPath,
});
if (prettierConfig === null) {
throw new Error(
`Could not resolve config for '${filename}' using prettier config path '${prettierConfigPath}'.`,
);
}

fs.writeFileSync(filename, prettier.format(data, { ...prettierConfig, filepath: filename }), {
encoding: 'utf8',
...options,
});
fs.writeFileSync(filename, prettier.format(data, { ...prettierConfig, filepath: filename }), {
encoding: 'utf8',
...options,
});
}
}

const appendSource = (target: string, template: string, source: string) => {
Expand Down Expand Up @@ -107,7 +111,7 @@ function run() {
}
}
fs.mkdirSync(info.directory, { recursive: true });
fs.writeFileSync(info.path, data); // (A)
writePrettifiedFile(info.path, data); // (A)

fs.rmSync(filePath);
}
Expand All @@ -128,17 +132,17 @@ function run() {
}

fs.mkdirSync(info.directory, { recursive: true });
fs.writeFileSync(info.path, data);
writePrettifiedFile(info.path, data);

fs.writeFileSync(filePath, data);
writePrettifiedFile(filePath, data);
}
}
} else {
let data = fs.readFileSync(filePath, { encoding: 'utf-8' });
if (filePath.endsWith('.js')) {
data = data.replace(`/src/pages/`, `/data/`); // point to data path (A) in new directory
}
fs.writeFileSync(filePath, data);
writePrettifiedFile(filePath, data);
}
});
});
Expand All @@ -157,7 +161,7 @@ function run() {
}
const match = filePath.match(/^(.*)\/[^/]+\.(ts|js|tsx|md|json|tsx\.preview)$/);
fs.mkdirSync(match[1].replace('src/pages', 'data'), { recursive: true });
fs.writeFileSync(filePath.replace('src/pages', 'data'), data);
writePrettifiedFile(filePath.replace('src/pages', 'data'), data);

fs.rmSync(filePath);
});
Expand All @@ -170,12 +174,12 @@ function run() {
}

// replace the old file
fs.writeFileSync(filePath, data);
writePrettifiedFile(filePath, data);

// add to /system
const match = filePath.match(/^(.*)\/[^/]+\.(ts|js|tsx|md|json|tsx\.preview)$/);
fs.mkdirSync(match[1].replace('pages/styles', 'pages/system/styles'), { recursive: true });
fs.writeFileSync(filePath.replace('pages/styles', 'pages/system/styles'), data);
writePrettifiedFile(filePath.replace('pages/styles', 'pages/system/styles'), data);
});
// =======================================================================

Expand All @@ -191,7 +195,7 @@ function run() {
`enable_product_scope: true`,
);

fs.writeFileSync(featureTogglePath, featureToggle);
writePrettifiedFile(featureTogglePath, featureToggle);
}

run();
2 changes: 1 addition & 1 deletion docs/scripts/restructureUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ githubLabel: 'component: Avatar'

expect(getNewPageLocation('material-ui/docs/pages/components/buttons.js')).to.deep.equal({
directory: 'material-ui/docs/pages/material',
path: 'material-ui/docs/pages/material/react-buttons.js',
path: 'material-ui/docs/pages/material/react-button.js',
});

expect(getNewPageLocation('material-ui/docs/pages/components/about-the-lab.js')).to.deep.equal({
Expand Down
Loading

0 comments on commit 9316900

Please sign in to comment.