Skip to content

Commit dc19369

Browse files
author
fengr06
committed
fix article detail click to go
1 parent 71c9ace commit dc19369

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

app/Http/Controllers/ArticleController.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,40 @@ public function show(Request $request, $id)
130130
$input = $comment ? $comment : $input;
131131
}
132132

133-
134133
preg_match_all('#<h2>(.*)</h2>#', $article->content_html, $outline);
135-
$outline = $outline[1];
136-
$preg = "#<h([0-9])>(.*?)</h([0-9])>#";
134+
$out = $outline[1];
135+
$outline = [];
136+
foreach ($out as $k => $v) {
137+
$outline[$k]['name'] = $v;
138+
$outline[$k]['id'] = $this->formatContent($v);
139+
}
140+
141+
$preg = "#<h([0-9])>(.*)</h([0-9])>#";
137142
$replace = '<h$1 id="$2">$2</h$1>';
138-
$content = preg_replace($preg, $replace, $article->content_html);
143+
$content = preg_replace_callback($preg, function ($matches) {
144+
$title = $matches[2];
145+
$title = $this->formatContent($title);
146+
$h2 = '<h' . $matches[1] . ' id= \'' . $title . '\'>' . $matches[2] . '</h' . $matches[1] . '>';
147+
return $h2;
148+
}, $article->content_html);
139149

140-
// $parse = new \Parsedown();
141-
// echo $parse->text($article->content_markdown);
142-
// $article->content_html = $parse->text($article->content_markdown);
143-
// exit;
144150
$article->content_html = $content;
145151
$tags = $article->tags;
146152
return view(env('BLOG_THEME') . '.articles.show', compact('article', 'comments', 'input', 'count', 'outline', 'tags', 'articleCount', 'catesCount', 'tagsCount'));
147153
}
148154

155+
156+
public function formatContent($content)
157+
{
158+
// Filter 英文标点符号
159+
$content = preg_replace("/[[:punct:]]/i", " ", $content);
160+
// Filter 中文标点符号
161+
mb_regex_encoding('utf-8');
162+
$char = "。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()";
163+
$content = mb_ereg_replace("[" . $char . "]", " ", $content, "UTF-8");
164+
// Filter 连续空格
165+
$content = preg_replace('# #', '', $content);
166+
return $content;
167+
}
168+
149169
}

resources/views/next/articles/show.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ class="vat"
329329
<div class="site-author motion-element" itemprop="author" itemscope=""
330330
itemtype="">
331331
<img class="site-author-image" itemprop="image" src="/next/images/avatar.jpg" alt="MrCoder">
332-
<p class="site-author-name" itemprop="name">FrCoder</p>
332+
<p class="site-author-name" itemprop="name">MrCoder</p>
333333
<p class="site-description motion-element"
334334
itemprop="description">@yield('title', setting('web_name', 'Laravel'))</p>
335335
</div>
@@ -383,8 +383,8 @@ class="vat"
383383
@if($outline)
384384
@foreach($outline as $k => $item)
385385
<li class="nav-item nav-level-1">
386-
<a class="nav-link" href="#{{$item}}">
387-
<span class="nav-number">{{$k+1}}.</span> <span class="nav-text">{{$item}}</span>
386+
<a class="nav-link" href="#{{$item['id']}}">
387+
<span class="nav-number">{{$k+1}}.</span> <span class="nav-text">{{$item['name']}}</span>
388388
</a>
389389
</li>
390390
@endforeach

0 commit comments

Comments
 (0)