Skip to content

Commit 8f9cf1f

Browse files
[docs-infra] Polish reportBrokenLinks.js to support Base UI
1 parent 3226205 commit 8f9cf1f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

docs/scripts/reportBrokenLinks.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ function getLinksAndAnchors(fileName) {
9090
};
9191
}
9292

93+
const markdownImportRegExp = /'(.*)\?(muiMarkdown|@mui\/markdown)'/g;
94+
9395
const getMdFilesImported = (jsPageFile) => {
9496
// For each JS file extract the markdown rendered if it exists
9597
const fileContent = fse.readFileSync(jsPageFile, 'utf8');
@@ -99,27 +101,31 @@ const getMdFilesImported = (jsPageFile) => {
99101
* - 'docs/data/advanced-components/overview.md?muiMarkdown';
100102
* - './index.md?muiMarkdown';
101103
*/
102-
const importPaths = fileContent.match(/'.*\?muiMarkdown'/g);
104+
const importPaths = fileContent.match(markdownImportRegExp);
103105

104106
if (importPaths === null) {
105107
return [];
106108
}
107109
return importPaths.map((importPath) => {
108-
let cleanImportPath = importPath.slice(1, importPath.length - "?muiMarkdown'".length);
110+
let cleanImportPath = importPath.replace(markdownImportRegExp, '$1');
109111
if (cleanImportPath.startsWith('.')) {
110112
cleanImportPath = path.join(path.dirname(jsPageFile), cleanImportPath);
111-
} else if (cleanImportPath.startsWith('docs/')) {
112-
cleanImportPath = path.join(
113-
jsPageFile.slice(0, jsPageFile.indexOf('docs/')),
114-
cleanImportPath,
115-
);
116-
} else if (cleanImportPath.startsWith('docsx/')) {
113+
} else {
114+
/**
115+
* convert /Users/oliviertassinari/base-ui/docs/pages/base-ui/react-switch/index.js
116+
* and docs-base/data/base/components/switch/switch.md
117+
* into /Users/oliviertassinari/base-ui/docs/data/base/components/switch/switch.md
118+
*/
119+
const cleanImportPathArray = cleanImportPath.split('/');
120+
// Assume that the first folder is /docs or an alias that starts with /docs
121+
cleanImportPathArray.shift();
122+
123+
// Truncate jsPageFile at /docs/ and append cleanImportPath
117124
cleanImportPath = path.join(
118-
jsPageFile.slice(0, jsPageFile.indexOf('docs/')),
119-
cleanImportPath.replace('docsx', 'docs'),
125+
jsPageFile.slice(0, jsPageFile.indexOf('/docs/')),
126+
'docs',
127+
cleanImportPathArray.join('/'),
120128
);
121-
} else {
122-
console.error(`unable to deal with import path: ${cleanImportPath}`);
123129
}
124130

125131
return cleanImportPath;

0 commit comments

Comments
 (0)