dd/app/Models/Bird.php
2026-06-14 15:46:45 +08:00

43 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use League\CommonMark\GithubFlavoredMarkdownConverter;
class Bird extends Model
{
// 显式指定表名
protected $table = 'birds';
// 批量赋值白名单
protected $fillable = ['title', 'markdown_content'];
/**
* 工业级设计:只读的虚拟属性 html_content
* 自动转换并过滤 XSS
*/
protected function htmlContent(): Attribute
{
return Attribute::make(
get: function () {
// 匈牙利命名法s前缀表示字符串
$sMarkdown = $this->attributes['markdown_content'] ?? '';
if (empty($sMarkdown)) {
return '';
}
// 实例化大厂标准的 GitHub Markdown 转换器
// 谐音发音:看沃特尔 (Con-vert-er)
$oConverter = new GithubFlavoredMarkdownConverter([
'html_input' => 'strip', // 安全核心:剥离所有原生 HTML 标签,彻底防御 XSS
'allow_unsafe_links' => false, // 禁用 javascript: 等不安全协议链接
]);
return $oConverter->convert($sMarkdown)->getContent();
}
);
}
}