Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Able to load doc components inside markdown files #34243

Merged
merged 13 commits into from
Sep 28, 2022
43 changes: 35 additions & 8 deletions docs/packages/markdown/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ module.exports = async function demoLoader() {
const { docs } = prepareMarkdown({ pageFilename, translations, componentPackageMapping });

const demos = {};
const components = {};
const demoModuleIDs = new Set();
const componentModuleIDs = new Set();
const demoNames = Array.from(
new Set(
docs.en.rendered
Expand All @@ -145,6 +147,7 @@ module.exports = async function demoLoader() {
`pages/${pageFilename.replace(/^\/src\/pages\//, '')}/`,
'',
)}`;

const moduleFilepath = path.join(
path.dirname(this.resourcePath),
moduleID.replace(/\//g, path.sep),
Expand Down Expand Up @@ -187,27 +190,51 @@ module.exports = async function demoLoader() {
}),
);

/**
* @param {string} moduleID
*/
function getDemoIdentifier(moduleID) {
return moduleIDToJSIdentifier(moduleID);
}
const componentNames = Array.from(
new Set(
docs.en.rendered
.filter((markdownOrComponentConfig) => {
return (
typeof markdownOrComponentConfig !== 'string' && markdownOrComponentConfig.component
);
})
.map((componentConfig) => {
return componentConfig.component;
}),
),
);

componentNames.forEach((componentName) => {
const moduleID = path.join(this.rootContext, 'src', componentName);

components[moduleID] = componentName;
componentModuleIDs.add(moduleID);
});

const transformed = `
${Array.from(demoModuleIDs)
.map((moduleID) => {
return `import ${getDemoIdentifier(moduleID)} from '${moduleID}';`;
return `import ${moduleIDToJSIdentifier(moduleID)} from '${moduleID}';`;
})
.join('\n')}
${Array.from(componentModuleIDs)
.map((moduleID) => {
return `import ${moduleIDToJSIdentifier(moduleID)} from '${moduleID}';`;
})
.join('\n')}

export const docs = ${JSON.stringify(docs, null, 2)};
export const demos = ${JSON.stringify(demos, null, 2)};
export const demoComponents = {${Array.from(demoModuleIDs)
.map((moduleID) => {
return `${JSON.stringify(moduleID)}: ${getDemoIdentifier(moduleID)},`;
return `${JSON.stringify(moduleID)}: ${moduleIDToJSIdentifier(moduleID)},`;
})
.join('\n')}};
export const srcComponents = {${Array.from(componentModuleIDs)
.map((moduleID) => {
return `${JSON.stringify(components[moduleID])}: ${moduleIDToJSIdentifier(moduleID)},`;
})
.join('\n')}};
`;

return transformed;
Expand Down
8 changes: 2 additions & 6 deletions docs/pages/base/getting-started/customization.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/getting-started/customization/customization.md?@mui/markdown';
import * as pageProps from 'docs/data/base/getting-started/customization/customization.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/getting-started/installation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/getting-started/installation/installation.md?@mui/markdown';
import * as pageProps from 'docs/data/base/getting-started/installation/installation.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/getting-started/overview.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/getting-started/overview/overview.md?@mui/markdown';
import * as pageProps from 'docs/data/base/getting-started/overview/overview.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/getting-started/usage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/getting-started/usage/usage.md?@mui/markdown';
import * as pageProps from 'docs/data/base/getting-started/usage/usage.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/guides/working-with-tailwind-css.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/guides/working-with-tailwind-css/working-with-tailwind-css.md?@mui/markdown';
import * as pageProps from 'docs/data/base/guides/working-with-tailwind-css/working-with-tailwind-css.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-badge.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/badge/badge.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/badge/badge.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-button.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/button/button.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/button/button.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-click-away-listener.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/click-away-listener/click-away-listener.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/click-away-listener/click-away-listener.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-focus-trap.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/focus-trap/focus-trap.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/focus-trap/focus-trap.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-form-control.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/form-control/form-control.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/form-control/form-control.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-input.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/input/input.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/input/input.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
4 changes: 2 additions & 2 deletions docs/pages/base/react-menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import { demos, docs, demoComponents } from 'docs/data/base/components/menu/menu.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/menu/menu.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-modal.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/modal/modal.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/modal/modal.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-no-ssr.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/no-ssr/no-ssr.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/no-ssr/no-ssr.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-popper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/popper/popper.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/popper/popper.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-portal.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/portal/portal.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/portal/portal.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-select.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/select/select.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/select/select.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-slider.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/slider/slider.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/slider/slider.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-switch.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/switch/switch.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/switch/switch.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-table-pagination.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/table-pagination/table-pagination.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/table-pagination/table-pagination.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
4 changes: 2 additions & 2 deletions docs/pages/base/react-tabs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import { demos, docs, demoComponents } from 'docs/data/base/components/tabs/tabs.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/tabs/tabs.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/base/react-textarea-autosize.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs';
import {
demos,
docs,
demoComponents,
} from 'docs/data/base/components/textarea-autosize/textarea-autosize.md?@mui/markdown';
import * as pageProps from 'docs/data/base/components/textarea-autosize/textarea-autosize.md?@mui/markdown';

export default function Page() {
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />;
return <MarkdownDocs {...pageProps} />;
}
8 changes: 2 additions & 6 deletions docs/pages/careers/fullstack-engineer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import TopLayoutCareers from 'docs/src/modules/components/TopLayoutCareers';
import {
demos,
docs,
demoComponents,
} from 'docs/src/pages/careers/full-stack-engineer.md?@mui/markdown';
import * as pageProps from 'docs/src/pages/careers/full-stack-engineer.md?@mui/markdown';

export default function Page() {
return <TopLayoutCareers demos={demos} docs={docs} demoComponents={demoComponents} />;
return <TopLayoutCareers {...pageProps} />;
}
Loading