Teaser break handling. See #693

This commit is contained in:
danpros 2024-03-02 10:42:44 +07:00
commit 95f7afe950

View file

@ -678,7 +678,6 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0)
$word_count = str_word_count(strip_tags($post->body));
$post->readTime = ceil($word_count / 200);
$toc = explode('<!--toc-->', $post->body);
if (isset($toc['1'])) {
$post->body = insert_toc('subpage-' . $post->slug, $toc['0'], $toc['1']);
@ -1201,7 +1200,6 @@ function get_author($name)
}
$author->body = $author->about;
$author->title = $author->name;
$tmp[] = $author;
@ -2211,16 +2209,22 @@ function get_teaser($string, $url = null, $char = null)
} else {
return $string;
}
} else {
$readMore = explode('<!--more-->', $string);
if (isset($readMore['1'])) {
$string = shorten($readMore[0]);
return $string;
} else {
$string = shorten($string, $char);
return $string;
}
}
}
// Shorten the string
function shorten($string = null, $char = null)
{
if(empty($char) || empty($string)) {
if(empty($string)) {
return;
}
@ -2237,10 +2241,12 @@ function shorten($string = null, $char = null)
$string = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($dom->saveHTML($dom->documentElement), 'UTF-8'));
$string = preg_replace('/\s\s+/', ' ', strip_tags($string));
$string = ltrim(rtrim($string));
if (!empty($char)) {
if (strlen($string) > $char) {
$string = substr($string, 0, $char);
$string = substr($string, 0, strrpos($string, ' '));
}
}
return $string;
}