Skip to content

Commit

Permalink
[core] Move the internal packages from docs/packages (mui#35305)
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Dudak <[email protected]>
Co-authored-by: Marija Najdova <[email protected]>
  • Loading branch information
michaldudak and mnajdova authored Dec 2, 2022
1 parent 258ab9f commit ca15855
Show file tree
Hide file tree
Showing 34 changed files with 92 additions and 56 deletions.
7 changes: 6 additions & 1 deletion docs/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
// eslint-disable-next-line import/prefer-default-export
export const LANGUAGES: string[];

export const LANGUAGES_SSR: string[];

export const LANGUAGES_IN_PROGRESS: string[];

export const LANGUAGES_IGNORE_PAGES: (pathname: string) => boolean;
31 changes: 27 additions & 4 deletions docs/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +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 = {
/**
* Valid languages to server-side render in production
*/
LANGUAGES: ['en', 'zh', 'pt'],
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
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/featureToggle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// need to use commonjs export so that docs/packages/markdown can use
// need to use commonjs export so that @mui/markdown can use
module.exports = {
enable_website_banner: false,
enable_toc_banner: true,
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
26 changes: 0 additions & 26 deletions docs/src/modules/constants.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
const { LANGUAGES } = require('../../config');

const CODE_VARIANTS = {
JS: 'JS',
TS: 'TS',
};

// 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 @@ -19,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 @@ -239,7 +239,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.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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[]>;
Expand Down
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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

0 comments on commit ca15855

Please sign in to comment.