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

[core] Move the internal packages from docs/packages #35305

Merged
merged 7 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Valid languages to server-side render in production
const LANGUAGES = ['en'];

// Server side rendered languages
const LANGUAGES_SSR = ['en'];

// Work in progress
const LANGUAGES_IN_PROGRESS = LANGUAGES.slice();

const LANGUAGES_IGNORE_PAGES = (pathname) => {
// We don't have the bandwidth like Qt to translate our blog posts
// https://www.qt.io/zh-cn/blog
if (pathname === '/blog' || pathname.startsWith('/blog/')) {
return true;
}

if (pathname === '/size-snapshot/') {
return true;
}

return false;
};

module.exports = {
Copy link
Member

@Janpot Janpot Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this is cjs so it can be used in next.config.js but also imported in application code as ESM. We could convert to next.config.mjs and author this module in ESM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't yet converted the scripts that use it to ESM. It is indeed imported in few places but only in TypeScript files that are transpiled to CJS. After I convert the scripts to ESM, I can update this config file as well.

LANGUAGES,
LANGUAGES_IN_PROGRESS,
LANGUAGES_SSR,
LANGUAGES_IGNORE_PAGES,
};
18 changes: 16 additions & 2 deletions docs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const pkg = require('../package.json');
const withDocsInfra = require('./nextConfigDocsInfra');
const { findPages } = require('./src/modules/utils/find');
const { LANGUAGES, LANGUAGES_SSR, LANGUAGES_IGNORE_PAGES } = require('./src/modules/constants');
const {
LANGUAGES,
LANGUAGES_SSR,
LANGUAGES_IGNORE_PAGES,
LANGUAGES_IN_PROGRESS,
} = require('./config');

const workspaceRoot = path.join(__dirname, '../');

Expand Down Expand Up @@ -87,7 +92,16 @@ module.exports = withDocsInfra({
oneOf: [
{
resourceQuery: /@mui\/markdown/,
use: [options.defaultLoaders.babel, require.resolve('@mui/markdown/loader')],
use: [
options.defaultLoaders.babel,
{
loader: require.resolve('@mui/markdown/loader'),
options: {
ignoreLanguagePages: LANGUAGES_IGNORE_PAGES,
languagesInProgress: LANGUAGES_IN_PROGRESS,
},
},
],
},
{
// used in some /getting-started/templates
Expand Down
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@mui/icons-material": "^5.10.16",
"@mui/joy": "5.0.0-alpha.56",
"@mui/lab": "5.0.0-alpha.110",
"@mui/markdown": "*",
"@mui/material": "^5.10.16",
"@mui/material-next": "6.0.0-alpha.64",
"@mui/styled-engine": "^5.10.16",
Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/ApiBuilders/ComponentApiBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Link } from 'mdast';
import { defaultHandlers, parse as docgenParse, ReactDocgenApi } from 'react-docgen';
import { unstable_generateUtilityClass as generateUtilityClass } from '@mui/utils';
import muiDefaultPropsHandler from 'docs/src/modules/utils/defaultPropsHandler';
import { LANGUAGES } from 'docs/src/modules/constants';
import { LANGUAGES } from 'docs/config';
import parseTest from 'docs/src/modules/utils/parseTest';
import generatePropTypeDescription, {
getChained,
Expand Down
8 changes: 7 additions & 1 deletion docs/scripts/reportBrokenLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const fse = require('fs-extra');
const { createRender } = require('@mui/markdown');
const { marked } = require('marked');
const { LANGUAGES_IGNORE_PAGES } = require('../config');

// Use renderer to extract all links into a markdown document
const getPageLinks = (markdown) => {
Expand Down Expand Up @@ -66,7 +67,12 @@ function getLinksAndAnchors(fileName) {
const toc = [];
const headingHashes = {};
const userLanguage = 'en';
const render = createRender({ headingHashes, toc, userLanguage });
const render = createRender({
headingHashes,
toc,
userLanguage,
ignoreLanguagePages: LANGUAGES_IGNORE_PAGES,
});

const data = fse.readFileSync(fileName, { encoding: 'utf8' });
render(data);
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/AppSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import KeyboardArrowRightRounded from '@mui/icons-material/KeyboardArrowRightRou
import SearchIcon from '@mui/icons-material/Search';
import GlobalStyles from '@mui/material/GlobalStyles';
import { alpha, styled } from '@mui/material/styles';
import { LANGUAGES_SSR } from 'docs/src/modules/constants';
import { LANGUAGES_SSR } from 'docs/config';
import Link from 'docs/src/modules/components/Link';
import { useTranslate, useUserLanguage } from 'docs/src/modules/utils/i18n';
import useLazyCSS from 'docs/src/modules/utils/useLazyCSS';
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/Head.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import NextHead from 'next/head';
import { useRouter } from 'next/router';
import { LANGUAGES_SSR } from 'docs/src/modules/constants';
import { LANGUAGES_SSR } from 'docs/config';
import { useUserLanguage, useTranslate } from 'docs/src/modules/utils/i18n';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';

Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import NextLink, { LinkProps as NextLinkProps } from 'next/link';
import MuiLink, { LinkProps as MuiLinkProps } from '@mui/material/Link';
import { styled } from '@mui/material/styles';
import { useUserLanguage } from 'docs/src/modules/utils/i18n';
import { LANGUAGES_IGNORE_PAGES } from 'docs/src/modules/constants';
import { LANGUAGES_IGNORE_PAGES } from 'docs/config';

/**
* File to keep in sync with:
Expand Down
27 changes: 0 additions & 27 deletions docs/src/modules/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ const CODE_VARIANTS = {
TS: 'TS',
};

// Valid languages to server-side render in production
const LANGUAGES = ['en'];

// Server side rendered languages
const LANGUAGES_SSR = ['en'];

// Work in progress
const LANGUAGES_IN_PROGRESS = LANGUAGES.slice();

// Valid languages to use in production
const LANGUAGES_LABEL = [
{
Expand All @@ -20,25 +11,7 @@ const LANGUAGES_LABEL = [
},
];

const LANGUAGES_IGNORE_PAGES = (pathname) => {
// We don't have the bandwidth like Qt to translate our blog posts
// https://www.qt.io/zh-cn/blog
if (pathname === '/blog' || pathname.startsWith('/blog/')) {
return true;
}

if (pathname === '/size-snapshot/') {
return true;
}

return false;
};

module.exports = {
CODE_VARIANTS,
LANGUAGES,
LANGUAGES_SSR,
LANGUAGES_LABEL,
LANGUAGES_IN_PROGRESS,
LANGUAGES_IGNORE_PAGES,
};
2 changes: 1 addition & 1 deletion docs/src/modules/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import upperFirst from 'lodash/upperFirst';
import camelCase from 'lodash/camelCase';
import { LANGUAGES } from '../constants';
import { LANGUAGES } from 'docs/config';

function pascalCase(str: string) {
return upperFirst(camelCase(str));
Expand Down
2 changes: 2 additions & 0 deletions docs/src/modules/utils/mapApiPageTranslations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRender } from '@mui/markdown';
import { LANGUAGES_IGNORE_PAGES } from '../../../config';

const notEnglishJsonRegExp = /-([a-z]{2})\.json$/;

Expand Down Expand Up @@ -29,6 +30,7 @@ export default function mapApiPageTranslations(req) {
toc: componentDescriptionToc,
userLanguage,
location: filenames,
ignoreLanguagePages: LANGUAGES_IGNORE_PAGES,
});
translation.componentDescription = render(translation.componentDescription);
translation.componentDescriptionToc = componentDescriptionToc;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@
"benchmark",
"packages/*",
"docs",
"docs/packages/*",
"test"
]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions packages/markdown/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface TableOfContentsEntry {
children: TableOfContentsEntry;
hash: string;
level: number;
text: string;
}

export function createRender(context: {
headingHashes: Record<string, string>;
toc: TableOfContentsEntry[];
userLanguage: string;
ignoreLanguagePages: (path: string) => boolean;
}): (markdown: string) => string;

export function getHeaders(markdown: string): Record<string, string | string[]>;

export function getTitle(markdown: string): string;

export function renderInline(markdown: string): string;
File renamed without changes.
20 changes: 12 additions & 8 deletions docs/packages/markdown/loader.js → packages/markdown/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const { prepareMarkdown } = require('./parseMarkdown');
const extractImports = require('./extractImports');

const notEnglishMarkdownRegExp = /-([a-z]{2})\.md$/;
// TODO: pass as argument
const LANGUAGES_IN_PROGRESS = ['en', 'zh', 'ru', 'pt', 'es', 'fr', 'de', 'ja'];

/**
* @param {string} string
Expand Down Expand Up @@ -39,14 +37,14 @@ const packages = [
{
product: 'material-ui',
paths: [
path.join(__dirname, '../../../packages/mui-lab/src'),
path.join(__dirname, '../../../packages/mui-material/src'),
path.join(__dirname, '../../../packages/mui-base/src'),
path.join(__dirname, '../../packages/mui-lab/src'),
path.join(__dirname, '../../packages/mui-material/src'),
path.join(__dirname, '../../packages/mui-base/src'),
],
},
{
product: 'base',
paths: [path.join(__dirname, '../../../packages/mui-base/src')],
paths: [path.join(__dirname, '../../packages/mui-base/src')],
},
];

Expand Down Expand Up @@ -75,6 +73,7 @@ packages.forEach((pkg) => {
*/
module.exports = async function demoLoader() {
const englishFilepath = this.resourcePath;
const config = this.getOptions();

const englishFilename = path.basename(englishFilepath, '.md');

Expand All @@ -94,7 +93,7 @@ module.exports = async function demoLoader() {
if (
filename.startsWith(englishFilename) &&
matchNotEnglishMarkdown !== null &&
LANGUAGES_IN_PROGRESS.indexOf(matchNotEnglishMarkdown[1]) !== -1
config.languagesInProgress.indexOf(matchNotEnglishMarkdown[1]) !== -1
) {
return {
filename,
Expand All @@ -121,7 +120,12 @@ module.exports = async function demoLoader() {
.replace(this.rootContext, '')
// win32 to posix
.replace(/\\/g, '/');
const { docs } = prepareMarkdown({ pageFilename, translations, componentPackageMapping });
const { docs } = prepareMarkdown({
pageFilename,
translations,
componentPackageMapping,
ignoreLanguagePages: config.ignoreLanguagePages,
});

const demos = {};
const importedModuleIDs = new Set();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { marked } = require('marked');
const kebabCase = require('lodash/kebabCase');
const textToHash = require('./textToHash');
const { LANGUAGES_IGNORE_PAGES } = require('../../src/modules/constants');
const prism = require('./prism');

const headerRegExp = /---[\r\n]([\s\S]*)[\r\n]---/;
Expand Down Expand Up @@ -174,9 +173,10 @@ const noSEOadvantage = [
* @param {Record<string, string>} context.headingHashes - WILL BE MUTATED
* @param {TableOfContentsEntry[]} context.toc - WILL BE MUTATED
* @param {string} context.userLanguage
* @param {function(string):boolean} context.ignoreLanguagePages
*/
function createRender(context) {
const { headingHashes, toc, userLanguage } = context;
const { headingHashes, toc, userLanguage, ignoreLanguagePages } = context;
const headingHashesFallbackTranslated = {};
let headingIndex = -1;

Expand Down Expand Up @@ -266,7 +266,7 @@ function createRender(context) {

checkUrlHealth(href, linkText, context);

if (userLanguage !== 'en' && href.indexOf('/') === 0 && !LANGUAGES_IGNORE_PAGES(href)) {
if (userLanguage !== 'en' && href.indexOf('/') === 0 && !ignoreLanguagePages(href)) {
finalHref = `/${userLanguage}${href}`;
}

Expand Down Expand Up @@ -371,9 +371,10 @@ 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 {function(string):boolean} config.ignoreLanguagePages
*/
function prepareMarkdown(config) {
const { pageFilename, translations, componentPackageMapping = {} } = config;
const { pageFilename, translations, componentPackageMapping = {}, ignoreLanguagePages } = config;

const demos = {};
/**
Expand Down Expand Up @@ -444,7 +445,13 @@ ${headers.components
}

const toc = [];
const render = createRender({ headingHashes, toc, userLanguage, location });
const render = createRender({
headingHashes,
toc,
userLanguage,
location,
ignoreLanguagePages,
});

const rendered = contents.map((content) => {
if (/^"(demo|component)": "(.*)"/.test(content)) {
Expand Down
1 change: 1 addition & 0 deletions packages/markdown/prism.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function highlight(code: string, language: string): string;
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import KeyboardArrowRightRounded from '@mui/icons-material/KeyboardArrowRightRou
import SearchIcon from '@mui/icons-material/Search';
import GlobalStyles from '@mui/material/GlobalStyles';
import { alpha, styled } from '@mui/material/styles';
import { LANGUAGES_SSR } from 'docs/src/modules/constants';
import { LANGUAGES_SSR } from 'docs/config';
import Link from 'docs/src/modules/components/Link';
import { useTranslate, useUserLanguage } from 'docs/src/modules/utils/i18n';
import useLazyCSS from 'docs/src/modules/utils/useLazyCSS';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import KeyboardArrowRightRounded from '@mui/icons-material/KeyboardArrowRightRou
import SearchIcon from '@mui/icons-material/Search';
import GlobalStyles from '@mui/material/GlobalStyles';
import { alpha, styled } from '@mui/material/styles';
import { LANGUAGES_SSR } from 'docs/src/modules/constants';
import { LANGUAGES_SSR } from 'docs/config';
import Link from 'docs/src/modules/components/Link';
import { useTranslate, useUserLanguage } from 'docs/src/modules/utils/i18n';
import useLazyCSS from 'docs/src/modules/utils/useLazyCSS';
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@mui/material/*": ["./packages/mui-material/src/*"],
"@mui/lab": ["./packages/mui-lab/src"],
"@mui/lab/*": ["./packages/mui-lab/src/*"],
"@mui/markdown": ["./docs/packages/markdown"],
"@mui/markdown": ["./packages/markdown"],
"@mui/styled-engine": ["./packages/mui-styled-engine/src"],
"@mui/styled-engine/*": ["./packages/mui-styled-engine/src/*"],
"@mui/styled-engine-sc": ["./packages/mui-styled-engine-sc/src"],
Expand Down