Skip to content

Commit c4a7411

Browse files
fix: HTML example code in showcases (#4987)
* fix(showcase): clean up names of showcase examples * fix(showcase): remove spread operator for example code * chore(showcase): remove console log --------- Co-authored-by: Maximilian Franzke <[email protected]>
1 parent 34210c8 commit c4a7411

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

showcases/patternhub/scripts/generate-example-jsx.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import FS from 'node:fs';
22
import {
3+
generateExampleKey,
34
getCodeByFramework,
45
getComponentName,
56
transformToUpperComponentName
@@ -35,12 +36,13 @@ const generateExampleJSX = () => {
3536
true,
3637
variant.children
3738
);
38-
examples.push(
39-
`"${componentName}${variant.name}${
40-
example.name
41-
// eslint-disable-next-line unicorn/no-unnecessary-slice-end
42-
}":renderToString(${code.slice(0, code.length)})`
39+
40+
const exampleKey = generateExampleKey(
41+
componentName,
42+
variant.name,
43+
example.name
4344
);
45+
examples.push(`"${exampleKey}":renderToString(${[code]})`);
4446
}
4547
}
4648
}

showcases/patternhub/scripts/get-code-files.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import FS from 'node:fs';
33
import prettier from 'prettier';
44
import { allExamples } from './generated/index.jsx';
5-
import { getCodeByFramework } from './utils.js';
5+
import { generateExampleKey, getCodeByFramework } from './utils.js';
66

77
const sharedPath = '../shared';
88
const reactPath = '../react-showcase/src/components';
@@ -66,10 +66,12 @@ const getExamplesAsMDX = async (componentName, variant) => {
6666
if (example.code && example.code[framework]) {
6767
exampleCode = example.code[framework];
6868
} else if (framework === 'html') {
69-
exampleCode =
70-
allExamples[
71-
`${componentName}${variant.name}${example.name}`
72-
];
69+
const exampleKey = generateExampleKey(
70+
componentName,
71+
variant.name,
72+
example.name
73+
);
74+
exampleCode = allExamples[exampleKey];
7375
} else {
7476
exampleCode = getCodeByFramework(
7577
componentName,

showcases/patternhub/scripts/utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,29 @@ export const getColorVariants = () => [
219219
'informational-transparent-semi'
220220
];
221221

222+
/**
223+
* Clean names by removing spaces and special characters to create valid JavaScript property names
224+
* @param {string} name - The name to clean
225+
* @returns {string} - Cleaned name with only word characters
226+
*/
227+
export const cleanupName = (name) => {
228+
if (!name) return '';
229+
return name.replaceAll(/\s+/g, '').replaceAll(/\W/g, '');
230+
};
231+
232+
/**
233+
* Generate a consistent key for allExamples object
234+
* @param {string} componentName - Component name
235+
* @param {string} variantName - Variant name (will be cleaned)
236+
* @param {string} exampleName - Example name (will be cleaned)
237+
* @returns {string} - Clean key for allExamples
238+
*/
239+
export const generateExampleKey = (componentName, variantName, exampleName) => {
240+
const cleanVariantName = cleanupName(variantName);
241+
const cleanExampleName = cleanupName(exampleName);
242+
return `${componentName}${cleanVariantName}${cleanExampleName}`;
243+
};
244+
222245
export const transformToUpperComponentName = (componentName) =>
223246
componentName
224247
? componentName

0 commit comments

Comments
 (0)