-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(frontend) fixes #91; append small random suffix to slug in creation
this should ensure that notebook and note slug names have a lower chance of conflicting, with similar named ones e.g. `my-note` -> `my-note-abc12`.
- Loading branch information
Showing
3 changed files
with
15 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
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
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 |
---|---|---|
@@ -1,3 +1,14 @@ | ||
const SLUG_SUFFIX_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789" | ||
const SLUG_SUFFIX_LENGTH = 5; | ||
|
||
export function toSlug(v: string): string { | ||
return v.toLowerCase().replaceAll(" ", "-").replaceAll(/[^a-z0-9-]/g, "") | ||
} | ||
|
||
export function toSlugWithSuffix(v: string, suffixLength = SLUG_SUFFIX_LENGTH): string { | ||
let suffix = "-"; | ||
for (let i = 0; i < suffixLength; i++) { | ||
suffix += SLUG_SUFFIX_CHARS[Math.floor(Math.random() * SLUG_SUFFIX_CHARS.length)] | ||
} | ||
return toSlug(v) + suffix | ||
} |