Skip to content

Commit

Permalink
[docs-infra] Support rendering markdown outside of docs
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jun 24, 2023
1 parent 0121c0a commit cb28c9a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/src/modules/components/AppLayoutDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function AppLayoutDocs(props) {
*/}
<StyledAppContainer disableAd={disableAd} hasTabs={hasTabs}>
<ActionsDiv>
<EditPage markdownLocation={location} />
<EditPage sourceLocation={location} />
</ActionsDiv>
{children}
<NoSsr>
Expand Down
10 changes: 5 additions & 5 deletions docs/src/modules/components/AppLayoutDocsWithoutAppFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ActionsDiv = styled('div')(({ theme }) => ({
},
}));

function AppLayoutDocs(props) {
function AppLayoutDocsWithoutAppFrame(props) {
const router = useRouter();
const {
children,
Expand Down Expand Up @@ -133,7 +133,7 @@ function AppLayoutDocs(props) {
*/}
<StyledAppContainer disableAd={disableAd} hasTabs={hasTabs}>
<ActionsDiv>
<EditPage markdownLocation={location} />
<EditPage sourceLocation={location} />
</ActionsDiv>
{children}
<NoSsr>
Expand All @@ -148,7 +148,7 @@ function AppLayoutDocs(props) {
);
}

AppLayoutDocs.propTypes = {
AppLayoutDocsWithoutAppFrame.propTypes = {
children: PropTypes.node.isRequired,
description: PropTypes.string.isRequired,
disableAd: PropTypes.bool.isRequired,
Expand All @@ -160,7 +160,7 @@ AppLayoutDocs.propTypes = {
};

if (process.env.NODE_ENV !== 'production') {
AppLayoutDocs.propTypes = exactProp(AppLayoutDocs.propTypes);
AppLayoutDocsWithoutAppFrame.propTypes = exactProp(AppLayoutDocsWithoutAppFrame.propTypes);
}

export default AppLayoutDocs;
export default AppLayoutDocsWithoutAppFrame;
11 changes: 6 additions & 5 deletions docs/src/modules/components/EditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import Button from '@mui/material/Button';
import { useUserLanguage, useTranslate } from 'docs/src/modules/utils/i18n';
import GitHubIcon from '@mui/icons-material/GitHub';

const LOCALES = { zh: 'zh-CN', pt: 'pt-BR', es: 'es-ES' };

export default function EditPage(props) {
const { markdownLocation } = props;
const { sourceLocation } = props;
const t = useTranslate();
const userLanguage = useUserLanguage();
const LOCALES = { zh: 'zh-CN', pt: 'pt-BR', es: 'es-ES' };
const CROWDIN_ROOT_URL = 'https://translate.mui.com/project/material-ui-docs/';
const crowdInLocale = LOCALES[userLanguage] || userLanguage;
const crowdInPath = markdownLocation.substring(0, markdownLocation.lastIndexOf('/'));
const crowdInPath = sourceLocation.substring(0, sourceLocation.lastIndexOf('/'));

return (
<Button
component="a"
href={
userLanguage === 'en'
? `${process.env.SOURCE_CODE_ROOT_URL}${markdownLocation}`
? `${process.env.SOURCE_CODE_ROOT_URL}${sourceLocation}`
: `${CROWDIN_ROOT_URL}${crowdInLocale}#/${process.env.SOURCE_CODE_ROOT_URL.replace(
'https://github.com/mui/',
'',
Expand Down Expand Up @@ -52,5 +53,5 @@ export default function EditPage(props) {
}

EditPage.propTypes = {
markdownLocation: PropTypes.string.isRequired,
sourceLocation: PropTypes.string.isRequired,
};
8 changes: 4 additions & 4 deletions docs/src/modules/components/MarkdownDocsV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import HooksApiContent from 'docs/src/modules/components/HooksApiContent';
import { getTranslatedHeader as getComponentTranslatedHeader } from 'docs/src/modules/components/ApiPage';
import MarkdownElement from 'docs/src/modules/components/MarkdownElementV2';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
import AppLayoutDocs from 'docs/src/modules/components/AppLayoutDocsWithoutAppFrame';
import AppLayoutDocsWithoutAppFrame from 'docs/src/modules/components/AppLayoutDocsWithoutAppFrame';
import { useTranslate, useUserLanguage } from 'docs/src/modules/utils/i18n';
import BrandingProvider from 'docs/src/BrandingProvider';
import Ad from 'docs/src/modules/components/Ad';
Expand Down Expand Up @@ -211,11 +211,11 @@ export default function MarkdownDocsV2(props) {
}

return (
<AppLayoutDocs
<AppLayoutDocsWithoutAppFrame
description={description}
disableAd={disableAd}
disableToc={disableToc}
location={location}
sourceLocation={location}
title={title}
toc={activeToc}
hasTabs
Expand Down Expand Up @@ -270,7 +270,7 @@ export default function MarkdownDocsV2(props) {
)}
</Provider>
</div>
</AppLayoutDocs>
</AppLayoutDocsWithoutAppFrame>
);
}

Expand Down
11 changes: 7 additions & 4 deletions packages/markdown/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ module.exports = async function demoLoader() {
}),
);

const pageFilename = this.context
.replace(this.rootContext, '')
// Use .. as the docs runs from the /docs folder
const repositoryRoot = path.join(this.rootContext, '..');
const fileRelativeContext = path
.relative(repositoryRoot, this.context)
// win32 to posix
.replace(/\\/g, '/');

const { docs } = prepareMarkdown({
pageFilename,
fileRelativeContext,
translations,
componentPackageMapping,
options,
Expand Down Expand Up @@ -157,7 +160,7 @@ module.exports = async function demoLoader() {
// The import paths currently use a completely different format.
// They should just use relative imports.
let moduleID = `./${demoName.replace(
`pages/${pageFilename.replace(/^\/src\/pages\//, '')}/`,
`pages/${fileRelativeContext.replace(/^docs\/src\/pages\//, '')}/`,
'',
)}`;

Expand Down
6 changes: 3 additions & 3 deletions packages/markdown/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,11 @@ function resolveComponentApiUrl(product, componentPkg, component) {
/**
* @param {object} config
* @param {Array<{ markdown: string, filename: string, userLanguage: string }>} config.translations - Mapping of locale to its markdown
* @param {string} config.pageFilename - posix filename relative to nextjs pages directory
* @param {string} config.fileRelativeContext - posix filename relative to repository root directory
* @param {object} config.options - provided to the webpack loader
*/
function prepareMarkdown(config) {
const { pageFilename, translations, componentPackageMapping = {}, options } = config;
const { fileRelativeContext, translations, componentPackageMapping = {}, options } = config;

const demos = {};
/**
Expand All @@ -446,7 +446,7 @@ function prepareMarkdown(config) {
.forEach((translation) => {
const { filename, markdown, userLanguage } = translation;
const headers = getHeaders(markdown);
const location = headers.filename || `/docs${pageFilename}/${filename}`;
const location = headers.filename || `/${fileRelativeContext}/${filename}`;
const title = headers.title || getTitle(markdown);
const description = headers.description || getDescription(markdown);

Expand Down

0 comments on commit cb28c9a

Please sign in to comment.