Giter Site home page Giter Site logo

table-contents's Introduction

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

table-contents's People

Contributors

lutdev avatar mimshe avatar

Watchers

James Cloos avatar

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.