Skip to content

Commit

Permalink
[docs-infra] Throw on incorrect internal links (#37326)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored May 23, 2023
1 parent 1bc303c commit a99b40a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ Here is a collection of well-known icon libraries that you can use with Joy UI.
### Iconify

- [Browse icons](https://icon-sets.iconify.design/)
- [Installation—React](https://docs.iconify.design/icon-components/react/)
- [Installation—Web component](https://docs.iconify.design/iconify-icon/)
- [Figma plugin](https://docs.iconify.design/design/figma/)
- [Installation—React](https://iconify.design/docs/icon-components/react/)
- [Installation—Web component](https://iconify.design/docs/iconify-icon/)
- [Figma plugin](https://iconify.design/docs/design/figma/)

<iframe src="https://codesandbox.io/embed/joy-ui-iconify-r8fjrm?fontsize=12&hidenavigation=1&module=%2Fdemo.tsx&theme=dark"
style="width:100%; height:250px; border:0; border-radius: 12px; overflow:hidden;"
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/customization/theming/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const theme = createTheme({
```

:::warning
⚠️ `vars` is a private field for [CSS theme variables](material-ui/experimental-api/css-theme-variables/overview/). It will throw an error if you try to pass a value to it:
`vars` is a private field for [CSS theme variables](/material-ui/experimental-api/css-theme-variables/overview/). It will throw an error if you try to pass a value to it:

```jsx
createTheme({
Expand Down
4 changes: 2 additions & 2 deletions docs/src/modules/components/ApiPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ export default function ApiPage(props) {
: '/material-ui/customization/theme-components/#theme-style-overrides';
let slotGuideLink = '';
if (isJoyComponent) {
slotGuideLink = '/joy-ui/guides/overriding-component-structure';
slotGuideLink = '/joy-ui/guides/overriding-component-structure/';
} else if (isBaseComponent) {
slotGuideLink = '/base/guides/overriding-component-structure';
slotGuideLink = '/base/guides/overriding-component-structure/';
}

const {
Expand Down
4 changes: 2 additions & 2 deletions docs/src/modules/components/ComponentsApiContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ export default function ComponentsApiContent(props) {
: '/material-ui/customization/theme-components/#theme-style-overrides';
let slotGuideLink = '';
if (isJoyComponent) {
slotGuideLink = '/joy-ui/guides/overriding-component-structure';
slotGuideLink = '/joy-ui/guides/overriding-component-structure/';
} else if (isBaseComponent) {
slotGuideLink = '/base/guides/overriding-component-structure';
slotGuideLink = '/base/guides/overriding-component-structure/';
}

const source = filename
Expand Down
37 changes: 32 additions & 5 deletions packages/markdown/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ function escape(html, encode) {
}

function checkUrlHealth(href, linkText, context) {
// Skip links that are externals to MUI
if (!(href[0] === '/' || href.startsWith('https://mui.com/'))) {
const url = new URL(href, 'https://mui.com/');

// External links to MUI, ignore
if (url.host !== 'mui.com') {
return;
}

const url = new URL(href, 'https://mui.com/');

if (url.host === 'mui.com' && url.pathname[url.pathname.length - 1] !== '/') {
/**
* Break for links like:
* /material-ui/customization/theming
*
* It needs to be:
* /material-ui/customization/theming/
*/
if (url.pathname[url.pathname.length - 1] !== '/') {
throw new Error(
[
'Missing trailing slash. The following link:',
Expand All @@ -56,6 +63,26 @@ function checkUrlHealth(href, linkText, context) {
].join('\n'),
);
}

// Relative links
if (href[0] !== '#' && !(href.startsWith('https://') || href.startsWith('http://'))) {
/**
* Break for links like:
* material-ui/customization/theming/
*
* It needs to be:
* /material-ui/customization/theming/
*/
if (href[0] !== '/') {
throw new Error(
[
'Missing leading slash. The following link:',
`[${linkText}](${href}) in ${context.location} is missing a leading slash, please add it.`,
'',
].join('\n'),
);
}
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/markdown/parseMarkdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,23 @@ authors:
});
}).to.throw(/\[foo]\(\/foo\) in \/docs\/test\/index\.md is missing a trailing slash/);
});

it('should report missing leading splashes', () => {
const markdown = `
# Localization
<p class="description">Foo</p>
[bar](/bar/)
[foo](foo/)
`;

expect(() => {
prepareMarkdown({
...defaultParams,
translations: [{ filename: 'index.md', markdown, userLanguage: 'en' }],
});
}).to.throw(/\[foo]\(foo\/\) in \/docs\/test\/index\.md is missing a leading slash/);
});
});
});

0 comments on commit a99b40a

Please sign in to comment.