camoo/toc

Generates table of Contents from Text

Installs: 24

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:project

1.0 2020-03-06 21:12 UTC

This package is auto-updated.

Last update: 2024-11-07 07:37:57 UTC


README

Build Status

Feature

This package parses a Html content with H-tags and generate Table of Contents with anchors in your content.

Installation:

  composer require camoo/toc

Usage

        $toc = new TableOfContents('<h1>Html Content</h1>');
		// get only table of contents part
        $onlyTableOfContents = $toc->getTableOfContents();

		// get full content with table of contents included
        $fullContent = $toc->getContent();

Basic Example:

$htmlContent = <<<END
    <h1>This is a header tag h1</h1>
    <p>Lorum ipsum doler sit amet</p>
    <h2>This is a header tag h2</h2>
    <p>Foo Bar</p>
    <h3>This is a header tag h3</h3>
END;

$toc = new TableOfContents($htmlContent);

$fullHtml  = $toc->getContent();


echo $fullHtml;

This produces the following output:

<h1 id="toc-header">Table of Contents</h1>
<hr />
<div>
    <ul>
        <li>
            <a href="#toc-0">This is a header tag h1</a>
            <ul>
                <li>
                    <a href="#toc-1">This is a header tag h2</a>
                    <ul>
                        <li><a href="#toc-2">This is a header tag h3</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>
<hr />
<h1><a href="#" name="toc-0"></a>This is a header tag h1</h1>
<p>Lorum ipsum doler sit amet</p>
<h2><a href="#" name="toc-1"></a>This is a header tag h2</h2>
<p>Foo Bar</p>
<h3><a href="#" name="toc-2"></a>This is a header tag h3</h3>

pecifying Heading Levels to Include

You can choose to include only specific h1...h6 heading levels in your TOC. By default the script is parsing h1-h6. To do this, pass the optional parameter $depth to the constructor

$htmlContent = '<h1>Tester</h1><b>Bold</b><h2>Sub-Test</h2><a href="#">link</a><h3>3rd Level</h3><h4>4rd level</h4>';

// Get TOC using h1-h3
$toc = new TableOfContents($htmlContent,3);