Skip to content

Commit 0fb1fc8

Browse files
committed
Enabled utf8 slugs
Prevents slug change when using only non-ascii chars Allows use of more localised urls. Closes #233
1 parent d3c7aad commit 0fb1fc8

File tree

8 files changed

+29
-15
lines changed

8 files changed

+29
-15
lines changed

app/Book.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class Book extends Entity
1313
public function getUrl($path = false)
1414
{
1515
if ($path !== false) {
16-
return baseUrl('/books/' . $this->slug . '/' . trim($path, '/'));
16+
return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
1717
}
18-
return baseUrl('/books/' . $this->slug);
18+
return baseUrl('/books/' . urlencode($this->slug));
1919
}
2020

2121
/*

app/Chapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function getUrl($path = false)
3232
{
3333
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
3434
if ($path !== false) {
35-
return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug . '/' . trim($path, '/'));
35+
return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
3636
}
37-
return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug);
37+
return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
3838
}
3939

4040
/**

app/Http/Controllers/ChapterController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ public function update(Request $request, $bookSlug, $chapterSlug)
115115
$book = $this->bookRepo->getBySlug($bookSlug);
116116
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
117117
$this->checkOwnablePermission('chapter-update', $chapter);
118+
if ($chapter->name !== $request->get('name')) {
119+
$chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $book->id, $chapter->id);
120+
}
118121
$chapter->fill($request->all());
119-
$chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
120122
$chapter->updated_by = user()->id;
121123
$chapter->save();
122124
Activity::add($chapter, 'chapter_update', $book->id);

app/Page.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public function getUrl($path = false)
7272
{
7373
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
7474
$midText = $this->draft ? '/draft/' : '/page/';
75-
$idComponent = $this->draft ? $this->id : $this->slug;
75+
$idComponent = $this->draft ? $this->id : urlencode($this->slug);
7676

7777
if ($path !== false) {
78-
return baseUrl('/books/' . $bookSlug . $midText . $idComponent . '/' . trim($path, '/'));
78+
return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
7979
}
8080

81-
return baseUrl('/books/' . $bookSlug . $midText . $idComponent);
81+
return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
8282
}
8383

8484
/**

app/Repos/BookRepo.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ public function createFromInput($input)
147147
*/
148148
public function updateFromInput(Book $book, $input)
149149
{
150+
if ($book->name !== $input['name']) {
151+
$book->slug = $this->findSuitableSlug($input['name'], $book->id);
152+
}
150153
$book->fill($input);
151-
$book->slug = $this->findSuitableSlug($book->name, $book->id);
152154
$book->updated_by = user()->id;
153155
$book->save();
154156
$this->permissionService->buildJointPermissionsForEntity($book);
@@ -208,8 +210,7 @@ public function doesSlugExist($slug, $currentId = false)
208210
*/
209211
public function findSuitableSlug($name, $currentId = false)
210212
{
211-
$slug = Str::slug($name);
212-
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
213+
$slug = $this->nameToSlug($name);
213214
while ($this->doesSlugExist($slug, $currentId)) {
214215
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
215216
}

app/Repos/ChapterRepo.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ public function doesSlugExist($slug, $bookId, $currentId = false)
150150
*/
151151
public function findSuitableSlug($name, $bookId, $currentId = false)
152152
{
153-
$slug = Str::slug($name);
154-
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
153+
$slug = $this->nameToSlug($name);
155154
while ($this->doesSlugExist($slug, $bookId, $currentId)) {
156155
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
157156
}

app/Repos/EntityRepo.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ public function buildJointPermissions(Collection $collection)
269269
$this->permissionService->buildJointPermissionsForEntities($collection);
270270
}
271271

272+
/**
273+
* Format a name as a url slug.
274+
* @param $name
275+
* @return string
276+
*/
277+
protected function nameToSlug($name)
278+
{
279+
$slug = str_replace(' ', '-', strtolower($name));
280+
$slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', $slug);
281+
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
282+
return $slug;
283+
}
284+
272285
}
273286

274287

app/Repos/PageRepo.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,7 @@ public function changePageParent(Page $page, Entity $parent)
614614
*/
615615
public function findSuitableSlug($name, $bookId, $currentId = false)
616616
{
617-
$slug = Str::slug($name);
618-
if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
617+
$slug = $this->nameToSlug($name);
619618
while ($this->doesSlugExist($slug, $bookId, $currentId)) {
620619
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
621620
}

0 commit comments

Comments
 (0)