Hacks & Customizations
These hacks are unsupported customizations meant as unofficial workarounds.
They can cause instability, introduce issues and may conflict with future updates. Apply at your own risk!

Page Export Contents List

  • Author: @ssddanbrown
  • Created: 16th Mar 2023
  • Updated: 16th Mar 2023
  • Last Tested On: v23.02.1

This hack uses the visual theme system to customize the page export template file, used for both PDF and HTML exports, to add a simple linked “Contents” list to the top of the file, generated from the headers within the document.

Considerations

  • The “Page Contents” header is hardcoded in English.
  • This contents list will not show page numbers for the PDF export.
  • The contents will show when headers are used in page content.
  • This hack uses internal an “PageContent” content class, and it’s methods, which will likely change in future BookStack versions.

Code

exports/page.blade.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@extends('layouts.export')

@section('title', $page->name)

@section('content')
    <div dir="auto">

        <h1 class="break-text" id="bkmrk-page-title">{{$page->name}}</h1>
        @php
            $toc = (new \BookStack\Entities\Tools\PageContent($page))->getNavigation($page->html);
        @endphp

        @if(!empty($toc))
            <hr>
            <div style="color: #888;"><strong>Page Contents</strong></div>
            <ul>
                @foreach($toc as $tocEntry)
                    <li style="margin-left: {{ ($tocEntry['level'] - 1) * 16  }}px;"><a href="{{ $tocEntry['link'] }}">{{ $tocEntry['text'] }}</a></li>
                @endforeach
            </ul>
            <hr>
        @endif

        <div style="clear:left;"></div>

        {!! $page->renderedHTML ?? $page->html !!}
    </div>

    <hr>

    <div class="text-muted text-small">
        @include('exports.parts.meta', ['entity' => $page])
    </div>
@endsection

Request an Update

Hack not working on the latest version of BookStack?
You can request this hack to be updated & tested for a small one-time fee.
This helps keeps these hacks updated & maintained in a sustainable manner.


Latest Hacks