@@ -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}
0 commit comments