fix(tags): use crc32-fallback slugger for Chinese tag names (HasSlug was blanking slugs)
This commit is contained in:
@@ -344,8 +344,8 @@ class SablogImport extends Command
|
|||||||
// 必须用 findFromString(内部按 locale 查询 JSON 键),否则每次导入都重复建标签
|
// 必须用 findFromString(内部按 locale 查询 JSON 键),否则每次导入都重复建标签
|
||||||
$tag = Tag::findFromString($tagName, 'post');
|
$tag = Tag::findFromString($tagName, 'post');
|
||||||
if (! $tag) {
|
if (! $tag) {
|
||||||
|
// slug 由 spatie/tags 的 HasSlug 钩子用 tags.slugger(=SlugGenerator::slug)自动生成
|
||||||
$tag = new Tag(['name' => $tagName, 'type' => 'post']);
|
$tag = new Tag(['name' => $tagName, 'type' => 'post']);
|
||||||
$tag->slug = SlugGenerator::make($tagName, 'tags', 'slug', fallback: 'tag-'.crc32($tagName));
|
|
||||||
$tag->save();
|
$tag->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,16 @@ use Illuminate\Support\Str;
|
|||||||
|
|
||||||
class SlugGenerator
|
class SlugGenerator
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* spatie/tags 的 tags.slugger 回调:拉丁字符用 Str::slug,纯中文名回退 crc32 形式。
|
||||||
|
*/
|
||||||
|
public static function slug(string $string): string
|
||||||
|
{
|
||||||
|
$slug = Str::slug($string, '-', 'en');
|
||||||
|
|
||||||
|
return $slug === '' ? 'tag-'.crc32($string) : $slug;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成唯一 slug。中文标题自动回退为 $fallback 或 crc32 形式。
|
* 生成唯一 slug。中文标题自动回退为 $fallback 或 crc32 形式。
|
||||||
*/
|
*/
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ return [
|
|||||||
* The given function generates a URL friendly "slug" from the tag name property before saving it.
|
* The given function generates a URL friendly "slug" from the tag name property before saving it.
|
||||||
* Defaults to Str::slug (https://laravel.com/docs/master/helpers#method-str-slug)
|
* Defaults to Str::slug (https://laravel.com/docs/master/helpers#method-str-slug)
|
||||||
*/
|
*/
|
||||||
'slugger' => null,
|
'slugger' => '\App\Support\SlugGenerator::slug',
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The fully qualified class name of the tag model.
|
* The fully qualified class name of the tag model.
|
||||||
|
|||||||
Reference in New Issue
Block a user