Skip to content

Commit

Permalink
fix: custom component name should be constrained explicitely
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienArcellier committed Sep 2, 2024
1 parent 9191a57 commit d8ffa7f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
34 changes: 34 additions & 0 deletions src/ui/src/custom_components/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Declare custom components
*
* @param customComponents list of components in key-value form
*/
export function declareCustomComponents(
customComponents: Record<string, unknown>,
) {
const validCustomComponents = {};
Object.entries(customComponents).forEach(([key, customComponent]) => {
const isValid = checkComponentKey(key);
if (isValid) {
validCustomComponents[key] = customComponent;
}
});

return validCustomComponents;
}

/**
* Checks that the key contains only alphanumeric characters and underscores without capital letters
*
* @see https://github.com/writer/writer-framework/issues/517
*/
function checkComponentKey(key: string): boolean {
const isValidKey = /^[a-z0-9_]+$/.test(key);
if (!isValidKey) {
console.warn(
`custom component '${key}' is ignored. A custom component in 'index.ts' should be declared using only alphanumeric lowercase and _.`,
);
}

return isValidKey;
}
8 changes: 4 additions & 4 deletions src/ui/src/custom_components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import BubbleMessage from "./BubbleMessage.vue";
import BubbleMessageAdvanced from "./BubbleMessageAdvanced.vue";

import { declareCustomComponents } from "./core";
// Export an object with the ids and the templates as default

export default {
export default declareCustomComponents({
bubblemessage: BubbleMessage,
bubblemessageadvanced: BubbleMessageAdvanced,
};
bubbleMessageadvanced: BubbleMessageAdvanced,
});

0 comments on commit d8ffa7f

Please sign in to comment.