Skip to content

Commit 2ebbc7d

Browse files
committed
fix: demo urls
1 parent b67ec7f commit 2ebbc7d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

elements/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
},
7575
"scripts": {
7676
"prepublishOnly": "npm run build",
77-
"analyze": "cem generate",
77+
"analyze": "cem generate && node ../scripts/cem-demo-index.ts",
7878
"build": "wireit",
7979
"test": "wtr --files './test/*.spec.ts' --config ../../web-test-runner.config.js"
8080
},

scripts/cem-demo-index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
import { writeFile } from 'node:fs/promises';
3+
import type * as M from 'custom-elements-manifest/schema';
4+
5+
import manifest from '../elements/custom-elements.json' with { type: 'json' };
6+
7+
function isCustomElementDeclaration(decl: M.Declaration): decl is M.CustomElementDeclaration {
8+
return !!(decl as M.CustomElementDeclaration).customElement;
9+
}
10+
11+
const RE = /^https:\/\/patternflyelements\.com\/components\/(?<slug>[\w-]+)\//;
12+
13+
const copy = structuredClone(manifest) as M.Package;
14+
15+
const getSlug = (demo: M.Demo) =>
16+
demo.url.match(RE)?.groups?.slug ?? '';
17+
18+
const isMainDemo = (demo: M.Demo): boolean =>
19+
demo.url.endsWith(`${getSlug(demo)}/`)
20+
|| demo.url.endsWith('demo/');
21+
22+
// replace all canonical demos with /
23+
// e.g.
24+
// from: https://patternflyelements.com/components/button/demo/button/
25+
// to: https://patternflyelements.com/components/button/demo/
26+
// This is a stopgap. the ideal would be to either generate the canonical demo from the cem, aka knobs
27+
// or to include it in the jsdoc in an @example tag
28+
// or to rearrange the elements/*/demo/*.html file structure
29+
await writeFile('custom-elements.json', JSON.stringify({
30+
...manifest,
31+
modules: copy.modules.map(module => ({
32+
...module,
33+
declarations: module.declarations?.map(decl => ({
34+
...decl,
35+
demos:
36+
!isCustomElementDeclaration(decl) ? decl
37+
: decl.demos?.map(demo => isMainDemo(demo) ? ({
38+
...demo,
39+
url: `https://patternflyelements.com/components/${getSlug(demo)}/demo/`,
40+
}) : demo)
41+
.sort((a, b) => isMainDemo(a) ? -1 : isMainDemo(b) ? 1 : 0),
42+
})),
43+
})),
44+
}, null, 2), 'utf-8');

0 commit comments

Comments
 (0)