-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: custom component name should be constrained explicitely
- Loading branch information
1 parent
9191a57
commit d8ffa7f
Showing
2 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters