Skip to content

Commit

Permalink
added table of content [SEO]
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Sep 18, 2024
1 parent 267213c commit 935828d
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 3 deletions.
95 changes: 95 additions & 0 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1271,3 +1271,98 @@ function sendingSMS($text, $number, $args)
return true;

}

/**
* table of content generator
* @param $html
* @return array
*/
function generateTOC($html) {
// Load HTML into a DOMDocument for parsing
$doc = new DOMDocument();
@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));

$toc = '';
$tocItems = [];
$lastH2 = '';
$lastH3 = '';
$idCounter = 0;

// Fetch all headings in the document
$headings = $doc->getElementsByTagName('*');

foreach ($headings as $heading) {
if (in_array($heading->nodeName, ['h2', 'h3'])) {
// Generate a unique ID for each heading
$id = generateHeadingID($heading->nodeValue, $idCounter);
$idCounter++;
$heading->setAttribute('id', $id);

if ($heading->nodeName === 'h2') {
$tocItems[] = [
'title' => $heading->nodeValue,
'id' => $id,
'children' => []
];
$lastH2 = $heading->nodeValue; // Update last H2 title
$lastH3 = ''; // Reset last H3
} elseif ($heading->nodeName === 'h3') {
if ($lastH2) {
// Create a new child entry for the last H2
$tocItems[count($tocItems) - 1]['children'][] = [
'title' => $heading->nodeValue,
'id' => $id,
];
$lastH3 = $heading->nodeValue; // Update last H3 title
}
}
}
}

// Create the TOC HTML
$toc .= buildTOC($tocItems);

// Return the modified HTML and the TOC
return [$toc, $doc->saveHTML()];
}

/**
* generate heading ID for table of content
* @param $text
* @param $counter
* @return string
*/
function generateHeadingID($text, $counter) {
// Convert to lowercase and replace non-alphanumeric characters with dashes
$id = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $text));

// Remove leading and trailing dashes
$id = trim($id, '-');

// Ensure the ID is not empty
if (empty($id)) {
$id = 'heading';
}

// Add the counter to ensure uniqueness
$id .= '-' . $counter;

return $id;
}

// The buildTOC function remains unchanged
function buildTOC($items) {
$html = '<ul>';
foreach ($items as $item) {
$html .= '<li>';
$html .= '<a href="#' . $item['id'] . '">' . $item['title'] . '</a>';

if (!empty($item['children'])) {
$html .= buildTOC($item['children']);
}

$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
1 change: 1 addition & 0 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function save($post, $request)
$post->group_id = $request->input('group_id');
$post->user_id = auth()->id();
$post->is_pinned = $request->has('is_pin');
$post->table_of_contents = $request->has('table_of_contents');
$post->icon = $request->input('icon');

if ($post->hash == null) {
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,14 @@ public function tagsList()
return implode(',', $this->tags()->pluck('name')->toArray());
}
}

public function tableOfContents(){
list($toc, $modifiedHtml) = generateTOC($this->body);
return $toc;
}

public function bodyContent(){
list($toc, $modifiedHtml) = generateTOC($this->body);
return $modifiedHtml;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "GPL-3.0-or-later",
"require": {
"php": "^8.2",
"ext-dom": "*",
"carlos-meneses/laravel-mpdf": "^2.1",
"chillerlan/php-qrcode": "^5.0",
"dpsoft/mellat": "^1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function up(): void
$table->unsignedInteger('like')->default(0);
$table->unsignedInteger('dislike')->default(0);
$table->string('icon', 128)->nullable();
$table->boolean('table_of_contents')->default(0);
$table->softDeletes();
$table->timestamps();

Expand Down
1 change: 1 addition & 0 deletions resources/lang/fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
"Successfully Invoices": "",
"Summary": "خلاصه",
"System notification": "پیام سیستم",
"Table of contents": "فهرست عناوین",
"Tag": "برچسب",
"Tags": "برچسب‌ها",
"Tags list": "فهرست برچسب‌ها",
Expand Down
12 changes: 11 additions & 1 deletion resources/views/admin/posts/post-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,23 @@ class="form-control @error('title') is-invalid @enderror"
placeholder="{{__('Title')}}" value="{{old('title',$item->title??null)}}"/>
</div>
</div>
<div class="col-md-12 mt-3">
<div class="col-md-9 mt-3">
<label for="slug">
{{__('Slug')}}
</label>
<input name="slug" type="text" class="form-control @error('slug') is-invalid @enderror"
placeholder="{{__('Slug')}}" value="{{old('slug',$item->slug??null)}}"/>
</div>
<div class="col-3 mt-4">
<div class="form-group mt-4">
<div class="form-check form-switch">
<input class="form-check-input" name="table_of_contents" @if (old('table_of_contents',$item->table_of_contents??0) != 0)
checked
@endif type="checkbox" id="table_of_contents">
<label class="form-check-label" for="table_of_contents">{{__('Table of contents')}}</label>
</div>
</div>
</div>
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="subtitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@
@endif
</div>
<hr>
{!! $post->body !!}
@if($post->table_of_contents)
{!! $post->tableOfContents() !!}
{!! $post->bodyContent() !!}
@else

{!! $post->body !!}
@endif

</div>

</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
@endif
</div>
<hr>
{!! $post->body !!}
@if($post->table_of_contents)
{!! $post->tableOfContents() !!}
{!! $post->bodyContent() !!}
@else

{!! $post->body !!}
@endif

</div>
</div>
Expand Down

0 comments on commit 935828d

Please sign in to comment.