Coder Social home page Coder Social logo

ashtaev / php-table-of-contents Goto Github PK

View Code? Open in Web Editor NEW
4.0 0.0 2.0 167 KB

A lightweight PHP library, for generating table of contents (TOC) in the style of Wikipedia.

License: MIT License

CSS 3.13% PHP 89.48% HTML 7.39%
table-of-contents toc contents php

php-table-of-contents's Introduction

PHP Table of Contents

Latest Stable Version Total Downloads Latest Unstable Version License

A lightweight PHP library, for generating table of contents in the style of Wikipedia. It automatically generates table of contents for your posts, pages and custom post types by parsing its contents for headers. By doing so, you can let your readers jump to a corresponding header by clicking on a link in the TOC. You can place this TOC anywhere in a post using the shortcodes. You don’t have to add any links or create any anchors for this. Built Wikipedia in mind, the table of contents by default appears before the first heading on a page. You also get various methods to configure your TOC and control how it is going to appear. Also, it sets no limit to the number of TOC you can create. You can go on and have as many TOC as you want.

Screenshots

They're rendered using the sample templates provided in the templates directory, which depend on Bootstrap 4. You can easily use your own custom HTML to render the table of contents instead.

Default template:


Installation

Install with composer:

composer require ashtaev/toc

Basic usage

Here's a quick example using the defaults:

<?php

require '../vendor/autoload.php';

use ashtaev\Toc;

$toc = new Toc($post);

?>
  
<html>
    <head>
        <!-- The default, built-in template supports the Bootstrap 4 TOC styles. -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <link rel="stylesheet" href="css/toc.css">
    </head>
    <body>
  
    <h1>Lorem ipsum</h1>

    <?php 
        // Example of rendering the table of contents with the built-in template.

        echo $toc->getPostWhithToc(); 
    ?>
    
    </body>
</html>

This will output the following:

<p>Lorem ipsum... </p>

<div class="border d-inline-block pr-4 pt-3 bg-light toc">
    <div class="ml-4 mb-2"><b>Content</b></div>
    <ol>
        <li><a href="#Lorem_ipsum" title="Lorem ipsum">Lorem ipsum</a></li>
        <li><a href="#Expedita_sint" title="Expedita sint">Expedita sint</a></li>
        <li><a href="#Maiores_quas" title="Maiores quas">Maiores quas</a>
            <ol>
                <li><a href="#Delectus_laudantium" title="Delectus laudantium">Delectus laudantium</a></li>
                <li><a href="#Eaque_quisquam?" title="Eaque quisquam?">Eaque quisquam?</a></li>
                <li><a href="#Deleniti_et" title="Deleniti et">Deleniti et</a>
                    <ol>
                        <li><a href="#Facilis_iusto!" title="Facilis iusto!">Facilis iusto!</a></li>
                        <li><a href="#Beatae_eveniet" title="Beatae eveniet">Beatae eveniet</a></li>
                    </ol>
                </li>
                <li><a href="#Beatae_fugit?" title="Beatae fugit?">Beatae fugit?</a></li>
            </ol>
        </li>
        <li><a href="#Fugit_in" title="Fugit in maxime">Fugit in maxime</a></li>
        <li><a href="#Odit_reprehenderit" title="Odit reprehenderit">Odit reprehenderit</a></li>
    </ol>
</div>

<h2><span id="Lorem_ipsum">"1. Lorem ipsum</span></h2>

<p>Lorem ipsum dolor...</p>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.