Skip to content

Commit 69e7f5b

Browse files
authored
feat(tools): site.subpath config (#2462)
1 parent 0895f50 commit 69e7f5b

File tree

8 files changed

+40
-17
lines changed

8 files changed

+40
-17
lines changed

.changeset/tools-site-subpath.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/pfe-tools": minor
3+
---
4+
Added `site.componentSubpath` config to `.pfe.config.json`, representing the
5+
site subpath for component pages and demos. Default is `'components'`.

.pfe.config.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"renderTitleInOverview": false,
3-
"site": {
4-
"renderTitleInOverview": false
5-
}
2+
"renderTitleInOverview": false
63
}

elements/custom-elements-manifest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { pfeCustomElementsManifestConfig } from '@patternfly/pfe-tools/custom-elements-manifest/config.js';
2+
import { fileURLToPath } from 'node:url'
3+
import { dirname } from 'node:path'
24

5+
// HACK: cem runs from `./elements` in a monorepo
6+
let rootDir = dirname(dirname(fileURLToPath(import.meta.url)))
37
export default pfeCustomElementsManifestConfig({
8+
rootDir,
49
globs: [
510
'./*/pf-*.ts',
611
'./*/Base*.ts'

tools/pfe-tools/11ty/plugins/custom-elements-manifest.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ module.exports = function configFunction(eleventyConfig, pluginOpts = {}) {
7070
// Netlify tends to turn html files into directories with index.html,
7171
// but 11ty already did that, so let's delete the html file.
7272
eleventyConfig.on('eleventy.after', async function({ runMode, dir }) {
73+
const { getPfeConfig } = await import('../../config.js');
74+
const options = { ...getPfeConfig(), ...pluginOpts };
7375
if (runMode === 'build') {
74-
const files = await glob(`${dir.output}/components/*/demo/*`);
76+
const files = await glob(`${dir.output}/${options.site.componentSubpath}/*/demo/*`);
7577
const htmls = files.filter(x => x.endsWith('.html') && !x.endsWith('/index.html'));
7678
for (const file of htmls) {
7779
const dir = file.replace(/\.html$/, '');

tools/pfe-tools/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ interface SiteOptions {
1313
stylesheets?: string[];
1414
/** Title for main page of the demo */
1515
title?: string;
16+
/** Site subpath for components. e.g. 'elements'. default: 'components' */
17+
componentSubpath?: string;
1618
}
1719

1820
export interface PfeConfig {
@@ -37,7 +39,8 @@ const SITE_DEFAULTS: Required<SiteOptions> = {
3739
favicon: '/docs/images/logo/pfe-icon-blue.svg',
3840
logoUrl: '/docs/images/pfe-logo-inverse-white.svg',
3941
stylesheets: [],
40-
title: 'PatternFly Elements'
42+
title: 'PatternFly Elements',
43+
componentSubpath: 'components',
4144
};
4245

4346
const DEFAULT_CONFIG: PfeConfig = {
@@ -70,7 +73,8 @@ export function getPfeConfig(rootDir = process.cwd()): Required<PfeConfig> {
7073

7174
const slugsConfigMap = new Map<string, { config: PfeConfig; slugs: Map<string, string> }>();
7275
const reverseSlugifyObject = ([k, v]: [string, string]): [string, string] =>
73-
[slugify(v).toLowerCase(), k];
76+
[slugify(v, { lower: true }), k];
77+
7478
function getSlugsMap(rootDir: string) {
7579
if (!slugsConfigMap.get(rootDir)) {
7680
const config = getPfeConfig(rootDir);

tools/pfe-tools/custom-elements-manifest/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import { getPfeConfig, type PfeConfig } from '../config.js';
1414

1515
import Chalk from 'chalk';
1616

17-
type Options = Config & Pick<PfeConfig, 'sourceControlURLPrefix' | 'demoURLPrefix'>;
17+
type Options = Config & Pick<PfeConfig, 'sourceControlURLPrefix' | 'demoURLPrefix'> & { rootDir?: string };
1818

1919
/**
2020
* PFE Default custom-elements-manifest analyzer config
2121
* @deprecated
2222
*/
2323
export function pfeCustomElementsManifestConfig(options?: Options): Config {
2424
console.log(`${Chalk.yellow(`pfeCustomElementsManifestConfig is ${Chalk.bold('deprecated')}`)}`);
25-
const config = getPfeConfig();
25+
const config = getPfeConfig(options?.rootDir);
2626
const { demoURLPrefix, sourceControlURLPrefix, dev } = { ...config, ...options ?? {} };
2727
return {
2828
globs: options?.globs ?? ['src/**/*.ts'],
@@ -43,7 +43,7 @@ export function pfeCustomElementsManifestConfig(options?: Options): Config {
4343
deprecatedDescriptionInlineTagPlugin(),
4444
dedentDescriptionsPlugin(),
4545
summaryPlugin(),
46-
demosPlugin({ demoURLPrefix, sourceControlURLPrefix }),
46+
demosPlugin({ ...options, demoURLPrefix, sourceControlURLPrefix }),
4747
ecmaPrivateClassMembersPlugin(),
4848
versionStaticFieldPlugin(),
4949

tools/pfe-tools/custom-elements-manifest/demos.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import slugify from 'slugify';
2727
* `/elements/pf-jazz-hands/pf-jazz-hands.js`
2828
*/
2929
export function demosPlugin(options?: PfeConfig): Plugin {
30-
const config = { ...getPfeConfig(), ...options };
30+
const fileOptions = getPfeConfig(options?.rootDir);
31+
const config = { ...fileOptions, ...options };
32+
const subpath = config.site.componentSubpath ?? 'components';
3133
const { rootDir, demoURLPrefix, sourceControlURLPrefix } = config;
3234
return {
3335
name: 'demos-plugin',
@@ -37,7 +39,7 @@ export function demosPlugin(options?: PfeConfig): Plugin {
3739

3840
for (const moduleDoc of customElementsManifest.modules) {
3941
const primaryElementName = moduleDoc.path.split(sep).find(x => x !== 'elements') ?? '';
40-
let demoPath = join(rootDir, primaryElementName, 'demo');
42+
let demoPath = join(rootDir, 'elements', primaryElementName, 'demo');
4143

4244
if (!existsSync(demoPath)) {
4345
demoPath = join('elements', primaryElementName, 'demo');
@@ -52,19 +54,19 @@ export function demosPlugin(options?: PfeConfig): Plugin {
5254
const { tagName } = decl;
5355
for (const demo of allDemos) {
5456
const demoName = demo.replace(/\.html$/, '');
55-
const slug = slugify(alias).toLowerCase();
57+
const slug = slugify(alias, { strict: true, lower: true });
5658
const href = new URL(`elements/${primaryElementName}/demo/${demo}/`, sourceControlURLPrefix || '/').href.replace(/\/$/, '');
5759
if (demoName === tagName && demoName === primaryElementName) {
5860
// case: elements/pf-jazz-hands/demo/pf-jazz-hands.html
59-
const { href: url } = new URL(`/components/${slug}/demo/`, demoURLPrefix || '/');
61+
const { href: url } = new URL(`/${subpath}/${slug}/demo/`, demoURLPrefix || '/');
6062
decl.demos.push({ url, source: { href } });
6163
} else if (allTagNames.includes(demoName) && demoName === tagName) {
6264
// case: elements/pf-jazz-hands/demo/pf-jazz-shimmy.html
63-
const { href: url } = new URL(`/components/${slug}/demo/${demoName}/`, demoURLPrefix || '/');
65+
const { href: url } = new URL(`/${subpath}/${slug}/demo/${demoName}/`, demoURLPrefix || '/');
6466
decl.demos.push({ url, source: { href } });
6567
} else if (tagName === primaryElementName && !allTagNames.includes(demoName)) {
6668
// case: elements/pf-jazz-hands/demo/ack.html
67-
const { href: url } = new URL(`/components/${slug}/demo/${demoName}/`, demoURLPrefix || '/');
69+
const { href: url } = new URL(`/${subpath}/${slug}/demo/${demoName}/`, demoURLPrefix || '/');
6870
decl.demos.push({ url, source: { href } });
6971
}
7072
}

tools/pfe-tools/custom-elements-manifest/lib/Manifest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,15 @@ export class Manifest {
267267
const { prettyTag } = Manifest;
268268
return this.getDemos(tagName).map(demo => {
269269
const permalink = demo.url.replace(options.demoURLPrefix, '/');
270-
let [, slug = ''] = permalink.match(/\/components\/(.*)\/demo/) ?? [];
270+
271+
/**
272+
* `/components/`
273+
* capture group 1:
274+
* > **ANY** (_>= 0x_)
275+
* `/demo`
276+
*/
277+
const DEMO_PATH_RE = new RegExp(`/${options.site.componentSubpath}/(.*)/demo`);
278+
let [, slug = ''] = permalink.match(DEMO_PATH_RE) ?? [];
271279
// strict removes all special characters from slug
272280
slug = slugify(slug, { strict: true, lower: true });
273281
const primaryElementName = deslugify(slug, options.rootDir);

0 commit comments

Comments
 (0)