feat: Markdown 正文自动 TOC(锚点+折叠目录)+ 社交账号扩充国内平台(B站/小红书/抖音/视频号/公众号/其他)+ 侧边栏社交展示 + JSON-LD sameAs 全平台聚合 + 首页 h1(SEO 语义)
This commit is contained in:
@@ -32,9 +32,14 @@ class PostController extends Controller
|
||||
->orderBy('created_at', (int) blog_setting('comment_order', 1) === 1 ? 'asc' : 'desc')
|
||||
->paginate(20);
|
||||
|
||||
$contentHtml = $this->renderer->render($post);
|
||||
$rendered = $this->renderer->renderWithToc($post);
|
||||
|
||||
return theme_view('show', compact('post', 'comments', 'contentHtml'));
|
||||
return theme_view('show', [
|
||||
'post' => $post,
|
||||
'comments' => $comments,
|
||||
'contentHtml' => $rendered['html'],
|
||||
'toc' => $rendered['toc'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function ensureReadable(Post $post): void
|
||||
|
||||
@@ -46,6 +46,43 @@ class PostContentRenderer
|
||||
return app(\App\Blog\Support\PluginManager::class)->applyFilters('post.rendered', $html, $post);
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染并提取目录(TOC)。
|
||||
*
|
||||
* @return array{html: string, toc: array<int, array{id: string, text: string, level: int}>}
|
||||
*/
|
||||
public function renderWithToc(Post $post): array
|
||||
{
|
||||
$html = $this->render($post);
|
||||
|
||||
$toc = [];
|
||||
$index = 0;
|
||||
|
||||
$html = preg_replace_callback(
|
||||
'/<h([2-6])([^>]*)>(.*?)<\/h\1>/is',
|
||||
function (array $m) use (&$toc, &$index): string {
|
||||
$level = (int) $m[1];
|
||||
$attrs = $m[2];
|
||||
$text = trim(strip_tags($m[3]));
|
||||
$id = 'toc-'.(++$index);
|
||||
|
||||
$toc[] = ['id' => $id, 'text' => $text, 'level' => $level];
|
||||
|
||||
// 已存在 id 则保留原 id,否则注入锚点
|
||||
if (preg_match('/id="([^"]+)"/', $attrs, $idMatch)) {
|
||||
$toc[array_key_last($toc)]['id'] = $idMatch[1];
|
||||
|
||||
return $m[0];
|
||||
}
|
||||
|
||||
return '<h'.$level.$attrs.' id="'.$id.'">'.$m[3].'</h'.$level.'>';
|
||||
},
|
||||
$html
|
||||
);
|
||||
|
||||
return ['html' => $html, 'toc' => $toc];
|
||||
}
|
||||
|
||||
public function toHtml(string $markdown): string
|
||||
{
|
||||
return $this->converter->convert($markdown)->getContent();
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Blog\Support;
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
/**
|
||||
* 社交账号聚合:后台「社交账号」Tab 配置 -> 前端展示 + SEO sameAs。
|
||||
*/
|
||||
class SocialLinks
|
||||
{
|
||||
public const PLATFORMS = [
|
||||
'social_weibo' => '微博',
|
||||
'social_zhihu' => '知乎',
|
||||
'social_bilibili' => '哔哩哔哩',
|
||||
'social_xiaohongshu' => '小红书',
|
||||
'social_douyin' => '抖音',
|
||||
'social_wechat_channel' => '视频号',
|
||||
'social_wechat_public' => '微信公众号',
|
||||
'social_github' => 'GitHub',
|
||||
'social_twitter' => 'Twitter / X',
|
||||
'social_facebook' => 'Facebook',
|
||||
'social_email' => '邮箱',
|
||||
];
|
||||
|
||||
/**
|
||||
* 全部社交账号:['platform' => 平台名, 'value' => URL/名称, 'url' => 是否为外链]。
|
||||
*/
|
||||
public static function all(): array
|
||||
{
|
||||
$items = [];
|
||||
|
||||
foreach (self::PLATFORMS as $key => $platform) {
|
||||
$value = trim((string) Setting::get($key, ''));
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
'platform' => $platform,
|
||||
'value' => $value,
|
||||
'url' => filter_var($value, FILTER_VALIDATE_URL) ? $value : null,
|
||||
];
|
||||
}
|
||||
|
||||
// 其他平台(KeyValue)
|
||||
$other = Setting::get('social_other', []);
|
||||
$other = is_array($other) ? $other : (json_decode((string) $other, true) ?: []);
|
||||
foreach ($other as $platform => $value) {
|
||||
if (trim((string) $value) === '') {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
'platform' => (string) $platform,
|
||||
'value' => (string) $value,
|
||||
'url' => filter_var((string) $value, FILTER_VALIDATE_URL) ? (string) $value : null,
|
||||
];
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* 可进 JSON-LD sameAs 的 URL 列表。
|
||||
*/
|
||||
public static function sameAs(): array
|
||||
{
|
||||
return array_values(array_unique(array_filter(array_map(
|
||||
fn (array $item) => $item['url'],
|
||||
self::all()
|
||||
))));
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,8 @@ class SidebarComposer
|
||||
];
|
||||
}));
|
||||
|
||||
$view->with('socialLinks', \App\Blog\Support\SocialLinks::all());
|
||||
|
||||
$view->with('siteName', blog_setting('site_name', config('blog.name')));
|
||||
$view->with('siteDescription', blog_setting('site_description', config('blog.description')));
|
||||
$view->with('siteIcp', blog_setting('site_icp', config('blog.icp')));
|
||||
|
||||
Reference in New Issue
Block a user