Merge pull request #1975 from flarum/fl/194-better-slugs

Use Laravel's slugger for basic transliteration
This commit is contained in:
Franz Liedke 2020-01-31 13:32:55 +01:00 committed by GitHub
commit c26c06faf6
4 changed files with 6 additions and 33 deletions

View File

@ -16,6 +16,10 @@ export function truncate(string, length, start = 0) {
* Create a slug out of the given string. Non-alphanumeric characters are * Create a slug out of the given string. Non-alphanumeric characters are
* converted to hyphens. * converted to hyphens.
* *
* NOTE: This method does not use the comparably sophisticated transliteration
* mechanism that is employed in the backend. Therefore, it should only be used
* to *suggest* slugs that can be overridden by the user.
*
* @param {String} string * @param {String} string
* @return {String} * @return {String}
*/ */

View File

@ -7,9 +7,9 @@
* LICENSE file that was distributed with this source code. * LICENSE file that was distributed with this source code.
*/ */
use Flarum\Util\Str;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Str;
return [ return [
'up' => function (Builder $schema) { 'up' => function (Builder $schema) {

View File

@ -23,7 +23,7 @@ use Flarum\Notification\Notification;
use Flarum\Post\MergeableInterface; use Flarum\Post\MergeableInterface;
use Flarum\Post\Post; use Flarum\Post\Post;
use Flarum\User\User; use Flarum\User\User;
use Flarum\Util\Str; use Illuminate\Support\Str;
/** /**
* @property int $id * @property int $id

View File

@ -1,31 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Util;
class Str
{
/**
* Create a slug out of the given string.
*
* Non-alphanumeric characters are converted to hyphens.
*
* @param string $str
* @return string
*/
public static function slug($str)
{
$str = strtolower($str);
$str = preg_replace('/[^a-z0-9]/i', '-', $str);
$str = preg_replace('/-+/', '-', $str);
$str = preg_replace('/-$|^-/', '', $str);
return $str;
}
}