lutdev / table-contents by alutskevich

Table of contents generation
27
5
3
Package Data
Maintainer Username: alutskevich
Maintainer Contact: karantin91@gmail.com (Andrii Lutskevich)
Package Create Date: 2017-03-26
Package Last Update: 2024-06-05
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-15 03:04:25
Package Statistics
Total Downloads: 27
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 5
Total Watchers: 3
Total Forks: 2
Total Open Issues: 5

License GitHub version Build Status

About

Package for generating table of contents for the text.

For example, you have next text:

<h1>Header first level</h1>
Some text here
<h2>Header second level</h2>
Some text here
<h4>Header fourth level</h4>
Some text here
<h3>Header third level</h3>
Some text here

Table of contents will be:

 - Header first level
    - Header second level
            - Header fourth level
         - Header third level

Table of contents support how strict headers nesting and free nesting, i.e. it can be: 1-2-4-3-6-2-4-1-3-7 or 1-2-3-2-4-1-2-3-4-3-5-2-3.

Table of contents work with only <h*> tags 1-10 levels (h1 - h10).

Getting Started

Requirements

PHP 7.1

Installation

  1. You can install the package using the composer.
"lutdev/table-contents": "^2.1"

Or install it by running this command in your project root:

composer require lutdev/table-contents
  1. PHP class
use Lutdev\TOC\TableContents

or you can download package and include it via require

Usage

$tableContents = new TableContents();
//return string table contents
$tableContents->tableContents($text);

Table of contents have next structure:

"0": {
    "title": "Title",
    "link": "link"
},
"1": {
    "title": "Title",
    "link": "link"
},
"2": {
    "title": "Title",
    "link": "link"
    "subItems": {
        "3": {
            "title": "Title",
            "link": "link"
            "subItems": {
                "1": {
                    "subItems": {
                        "2": {
                            "subItems": {
                                "4": {
                                    "title": "Title",
                                    "link": "link"
                                },
                            }
                        }
                    }
                }
            }
        }
    }
}
  • Keys (0,1,2,3,4) - header index number. Keys (1,2 subItems) - intermediate empty headers;
  • title - title of the content item;
  • link - anchor link. If text have few same headers links of them will be <link>-<number> (for example: link-1, link-2, link-3).
//Add ID attribute to the headers. Need for anchors.
$tableContents->headerLinks($text)

Feedback

Thank you! If you have proposition or find error/bug write me, please.

License

MIT