Skip to content

Commit a521b1a

Browse files
authored
chore: add index.html listing page to charts dev pages (#10009)
1 parent 366c287 commit a521b1a

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

dev/charts/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Dev pages</title>
7+
<script type="module" src="../common.js"></script>
8+
</head>
9+
<body>
10+
<h1>Charts development pages</h1>
11+
12+
<ul id="listing"></ul>
13+
</body>
14+
</html>

dev/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
65
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
76
<title>Dev pages</title>
87
<script type="module" src="./common.js"></script>

dev/rollup.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ export default {
1212
nodeResolve(),
1313
html({
1414
flattenOutput: false, // Preserve "charts" folder
15-
transformHtml: [(html) => appendStyles(html), (html) => generateListing(html)],
15+
transformHtml: [
16+
(html) => appendStyles(html),
17+
(html, { htmlFileName }) => {
18+
const folderPath = htmlFileName === 'charts/index.html' ? '/charts' : '';
19+
return generateListing(html, `.${folderPath}`);
20+
},
21+
],
1622
transformAsset: [
1723
async (content, filePath) => {
1824
if (filePath.endsWith('.css')) {

wds-utils.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ export function appendStyles(html) {
2424
export function generateListing(html, dir, path) {
2525
if (html.includes('<ul id="listing">')) {
2626
// Add <base> to make index.html work when opening
27-
// http://localhost:8000/dev without trailing slash
28-
if (path && path.endsWith('/dev')) {
29-
html = html.replace('<head>', '<head>\n<base href="dev/">');
27+
// http://localhost:8000/dev or http://localhost:8000/dev/charts without trailing slash
28+
const match = /\/(?<section>dev|charts)$/u.exec(path);
29+
if (match) {
30+
html = html.replace('<head>', `<head>\n<base href="${match.groups.section}/">`);
3031
}
3132

3233
const listing = `
3334
<ul id="listing">
3435
${fs
3536
.readdirSync(dir || '.')
3637
.filter((file) => file !== 'index.html')
37-
.filter((file) => file.endsWith('.html'))
38-
.map((file) => `<li><a href="${file}">${file}</a></li>`)
38+
.filter((file) => file.endsWith('.html') || file === 'charts')
39+
.map(
40+
(file) => `<li>
41+
<a href="${file}${file.endsWith('.html') ? '' : '/'}">${file}</a>
42+
</li>`,
43+
)
3944
.join('')}
4045
</ul>`;
4146

web-dev-server.config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ export default {
7676
body = appendStyles(body);
7777

7878
// Index page listing
79-
if (['/dev/index.html', '/dev', '/dev/'].includes(context.path)) {
80-
body = generateListing(body, './dev', context.path);
79+
const devPageMatch = /^\/dev(?:\/(?<section>charts))?(?:\/index\.html|\/)?$/u.exec(context.path);
80+
81+
if (devPageMatch) {
82+
// If a group was captured, then it's in the /dev/charts path,
83+
// otherwise it should list the pages under the /dev folder
84+
const dir = devPageMatch.groups.section ? './dev/charts' : './dev';
85+
body = generateListing(body, dir, context.path);
8186
}
8287

8388
return { body };

0 commit comments

Comments
 (0)