UPDATE: spis tresci

This commit is contained in:
Roman Pyrih
2025-06-23 13:33:29 +02:00
parent f19adaf268
commit 5ebae4b749

View File

@@ -2,26 +2,19 @@
namespace front\factory;
class Articles
{
static private function slugify($text) {
$text = strtolower($text);
$text = preg_replace('/[^\p{L}\p{Nd}]+/u', '-', $text);
$text = trim($text, '-');
return $text;
}
static public function generateTableOfContents($content) {
$result = '';
$currentLevel = [];
preg_match_all('/<(h[1-6])([^>]*)>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER);
// preg_match_all('/<(h[1-6])[^>]*>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER);
// preg_match_all('/<(h[1-6])([^>]*)>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER);
preg_match_all('/<(h[1-6])[^>]*>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER);
$firstLevel = true;
foreach ($matches as $match) {
$level = intval(substr($match[1], 1));
$headingText = strip_tags($match[2]);
$slug = self::slugify($headingText);
$slug = \S::seo( $headingText );
while ($level < count($currentLevel)) {
$result .= '</li></ol>';
@@ -46,16 +39,16 @@ class Articles
preg_match('/\sid="([^"]*)"/', $match[2], $idMatches);
$id = isset($idMatches[1]) ? $idMatches[1] : '';
$result .= sprintf(
'<a href="#%s">%s</a>',
urlencode(strtolower($id)),
$match[3]
);
// $result .= sprintf(
// '<a href="#%s">%s</a>',
// $slug,
// $headingText
// urlencode(strtolower($id)),
// $match[3]
// );
$result .= sprintf(
'<a href="#%s">%s</a>',
$slug,
$headingText
);
}
while (!empty($currentLevel)) {
@@ -75,10 +68,15 @@ class Articles
$level = $matches[1];
$attrs = $matches[2];
$content = $matches[3];
// usuniecie tagow
$cleanText = strip_tags($content);
$id_attr = 'id=';
$id_attr_pos = strpos($attrs, $id_attr);
if ($id_attr_pos === false) { // jeśli nie ma atrybutu id
$id = \S::seo( $content );
$id = \S::seo( $cleanText );
$attrs .= sprintf(' id="%s"', $id);
}