Skip to content

Commit

Permalink
(frontend) fixes #91; append small random suffix to slug in creation
Browse files Browse the repository at this point in the history
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
enchant97 committed Sep 9, 2023
1 parent 14edfe4 commit 899c4cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions frontend/src/components/modals/new_book.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, createSignal } from 'solid-js';
import BaseModal from './base';
import { Book, CreateBook, User } from '../../core/types';
import { createStore } from 'solid-js/store';
import { toSlug } from '../../core/helpers';
import { toSlug, toSlugWithSuffix } from '../../core/helpers';
import { useApi } from '../../contexts/ApiProvider';
import { useNavigate } from '@solidjs/router';
import { ApiError } from '../../core/api';
Expand Down Expand Up @@ -40,7 +40,7 @@ const NewBookModal: Component<NewBookModalProps> = (props) => {
<input
oninput={(ev) => setForm({
name: ev.currentTarget.value,
slug: toSlug(ev.currentTarget.value)
slug: toSlugWithSuffix(ev.currentTarget.value)
})}
value={form.name}
class="input input-bordered w-full"
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/modals/new_note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, createSignal } from 'solid-js';
import BaseModal from './base';
import { Book, CreateNote, Note, User } from '../../core/types';
import { createStore } from 'solid-js/store';
import { toSlug } from '../../core/helpers';
import { toSlug, toSlugWithSuffix } from '../../core/helpers';
import { useApi } from '../../contexts/ApiProvider';
import { useNavigate } from '@solidjs/router';
import { ApiError } from '../../core/api';
Expand Down Expand Up @@ -41,7 +41,7 @@ const NewNoteModal: Component<NewNoteModalProps> = (props) => {
<input
oninput={(ev) => setForm({
name: ev.currentTarget.value,
slug: toSlug(ev.currentTarget.value)
slug: toSlugWithSuffix(ev.currentTarget.value)
})}
value={form.name}
class="input input-bordered w-full"
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/core/helpers.ts
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
}

0 comments on commit 899c4cd

Please sign in to comment.