UPDATE: spis tresci
This commit is contained in:
@@ -2,26 +2,19 @@
|
|||||||
namespace front\factory;
|
namespace front\factory;
|
||||||
class Articles
|
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) {
|
static public function generateTableOfContents($content) {
|
||||||
$result = '';
|
$result = '';
|
||||||
$currentLevel = [];
|
$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;
|
$firstLevel = true;
|
||||||
|
|
||||||
foreach ($matches as $match) {
|
foreach ($matches as $match) {
|
||||||
$level = intval(substr($match[1], 1));
|
$level = intval(substr($match[1], 1));
|
||||||
$headingText = strip_tags($match[2]);
|
$headingText = strip_tags($match[2]);
|
||||||
|
|
||||||
$slug = self::slugify($headingText);
|
$slug = \S::seo( $headingText );
|
||||||
|
|
||||||
while ($level < count($currentLevel)) {
|
while ($level < count($currentLevel)) {
|
||||||
$result .= '</li></ol>';
|
$result .= '</li></ol>';
|
||||||
@@ -46,16 +39,16 @@ class Articles
|
|||||||
preg_match('/\sid="([^"]*)"/', $match[2], $idMatches);
|
preg_match('/\sid="([^"]*)"/', $match[2], $idMatches);
|
||||||
$id = isset($idMatches[1]) ? $idMatches[1] : '';
|
$id = isset($idMatches[1]) ? $idMatches[1] : '';
|
||||||
|
|
||||||
$result .= sprintf(
|
|
||||||
'<a href="#%s">%s</a>',
|
|
||||||
urlencode(strtolower($id)),
|
|
||||||
$match[3]
|
|
||||||
);
|
|
||||||
// $result .= sprintf(
|
// $result .= sprintf(
|
||||||
// '<a href="#%s">%s</a>',
|
// '<a href="#%s">%s</a>',
|
||||||
// $slug,
|
// urlencode(strtolower($id)),
|
||||||
// $headingText
|
// $match[3]
|
||||||
// );
|
// );
|
||||||
|
$result .= sprintf(
|
||||||
|
'<a href="#%s">%s</a>',
|
||||||
|
$slug,
|
||||||
|
$headingText
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!empty($currentLevel)) {
|
while (!empty($currentLevel)) {
|
||||||
@@ -75,10 +68,15 @@ class Articles
|
|||||||
$level = $matches[1];
|
$level = $matches[1];
|
||||||
$attrs = $matches[2];
|
$attrs = $matches[2];
|
||||||
$content = $matches[3];
|
$content = $matches[3];
|
||||||
|
|
||||||
|
// usuniecie tagow
|
||||||
|
$cleanText = strip_tags($content);
|
||||||
|
|
||||||
$id_attr = 'id=';
|
$id_attr = 'id=';
|
||||||
$id_attr_pos = strpos($attrs, $id_attr);
|
$id_attr_pos = strpos($attrs, $id_attr);
|
||||||
|
|
||||||
if ($id_attr_pos === false) { // jeśli nie ma atrybutu id
|
if ($id_attr_pos === false) { // jeśli nie ma atrybutu id
|
||||||
$id = \S::seo( $content );
|
$id = \S::seo( $cleanText );
|
||||||
$attrs .= sprintf(' id="%s"', $id);
|
$attrs .= sprintf(' id="%s"', $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user