Skip to content

Commit 2fb444f

Browse files
committed
refactor(tools): use deslugify, fix slug prefixing
1 parent f324fa5 commit 2fb444f

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

tools/pfe-tools/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ function getSlugsMap(rootDir: string) {
8686
return slugsConfigMap.get(rootDir)!;
8787
}
8888

89+
/**
90+
* Returns the prefixed custom element name for a given slug
91+
*/
8992
export function deslugify(slug: string, rootDir = process.cwd()): string {
9093
const { slugs, config } = getSlugsMap(rootDir);
91-
return slugs.get(slug) ?? `${config.tagPrefix}-${slug}`;
94+
const prefixedSlug = (slug.startsWith(`${config.tagPrefix}-`)) ? slug : `${config.tagPrefix}-${slug}`;
95+
return slugs.get(slug) ?? prefixedSlug;
9296
}

tools/pfe-tools/dev-server/config.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,6 @@ async function renderURL(context: Context, options: PfeDevServerInternalConfig):
8686
}
8787
}
8888

89-
function kebabCase(string: string) {
90-
return string
91-
.replace(/[^a-zA-Z0-9\s]/g, '') // Remove all characters not letters, numbers or spaces
92-
.replace(/[\s_]+/g, '-') // Replace spaces and underscores with -
93-
.replace(/-+/g, '-') // Replace multiple - with single -
94-
.replace(/^-+|-+$/g, '') // Remove leading and trailing -
95-
.toLowerCase();
96-
}
97-
98-
function convertAliases(aliases: Record<string, string>) {
99-
const keyedAliases = {} as Record<string, string>;
100-
for (const key in aliases) {
101-
if ({}.hasOwnProperty.call(aliases, key)) {
102-
const newKey = kebabCase(aliases[key]);
103-
keyedAliases[newKey] = key;
104-
}
105-
}
106-
return keyedAliases;
107-
}
108-
10989
/**
11090
* Generate HTML for each component by rendering a nunjucks template
11191
* Watch repository source files and reload the page when they change
@@ -117,15 +97,6 @@ function pfeDevServerPlugin(options: PfeDevServerInternalConfig): Plugin {
11797
const { elementsDir, tagPrefix, aliases } = options;
11898
const { componentSubpath } = options.site;
11999

120-
const keyedAliases = convertAliases(aliases);
121-
122-
const prefixTag = (tag: string) => {
123-
if (!tag.startsWith(tagPrefix)) {
124-
return `${tagPrefix}-${tag}`;
125-
}
126-
return tag;
127-
};
128-
129100
const router =
130101
new Router()
131102
.get(/\/pf-icon\/icons\/.*\.js$/, (ctx, next) => {
@@ -139,25 +110,22 @@ function pfeDevServerPlugin(options: PfeDevServerInternalConfig): Plugin {
139110
// Redirect `components/jazz-hands/*.js` to `components/pf-jazz-hands/*.ts`
140111
.get(`/${componentSubpath}/:element/:fileName.js`, async ctx => {
141112
const { element, fileName } = ctx.params;
142-
143-
const prefixedElement = keyedAliases[element] ?? prefixTag(element);
113+
const prefixedElement = deslugify(element);
144114

145115
ctx.redirect(`/${elementsDir}/${prefixedElement}/${fileName}.ts`);
146116
})
147117
// Redirect `elements/jazz-hands/*.js` to `elements/pf-jazz-hands/*.ts`
148118
.get(`/${elementsDir}/:element/:fileName.js`, async ctx => {
149119
const { element, fileName } = ctx.params;
150-
151-
const prefixedElement = keyedAliases[element] ?? prefixTag(element);
120+
const prefixedElement = deslugify(element);
152121

153122
ctx.redirect(`/${elementsDir}/${prefixedElement}/${fileName}.ts`);
154123
})
155124
// Redirect `components/pf-jazz-hands|jazz-hands/demo/*-lightdom.css` to `components/pf-jazz-hands/*-lightdom.css`
156125
// Redirect `components/jazz-hands/demo/*.js|css` to `components/pf-jazz-hands/demo/*.js|css`
157126
.get(`/${componentSubpath}/:element/demo/:demoSubDir?/:fileName.:ext`, async (ctx, next) => {
158127
const { element, fileName, ext } = ctx.params;
159-
160-
const prefixedElement = keyedAliases[element] ?? prefixTag(element);
128+
const prefixedElement = deslugify(element);
161129

162130
if (fileName.includes('-lightdom') && ext === 'css') {
163131
ctx.redirect(`/${elementsDir}/${prefixedElement}/${fileName}.${ext}`);
@@ -170,12 +138,14 @@ function pfeDevServerPlugin(options: PfeDevServerInternalConfig): Plugin {
170138
// Redirect `components/jazz-hands/*` to `components/pf-jazz-hands/*` for requests not previously handled
171139
.get(`/${componentSubpath}/:element/:splatPath*`, async (ctx, next) => {
172140
const { element, splatPath } = ctx.params;
141+
const prefixedElement = deslugify(element);
142+
173143
if (splatPath.includes('demo')) {
174144
/* if its the demo directory return */
175145
return next();
176146
}
177147
if (!element.includes(tagPrefix)) {
178-
ctx.redirect(`/${elementsDir}/${tagPrefix}-${element}/${splatPath}`);
148+
ctx.redirect(`/${elementsDir}/${prefixedElement}/${splatPath}`);
179149
} else {
180150
return next();
181151
}

0 commit comments

Comments
 (0)