picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.82k stars 616 forks source link

Table of contents with TWIG? #473

Closed iagovar closed 5 years ago

iagovar commented 5 years ago

Is it possible to hierarchically list h1's and h2's in posts with twig? Like the Table of Contents plugin in wordpress.

PhrozenByte commented 5 years ago

If you want to create a TOC automatically, you'll have to write a plugin to extend Parsedown (the Markdown parser Pico uses).

Besides that you can use the ### This is a headline {#this-is-a-headline} Markdown syntax in your content files to add anchors to headlines. You can then add a list of all anchors and their respective title to the YAML header of your content file and use this information to create a TOC using Twig. See "Table of contents" on http://picocms.org/cookbook/

iagovar commented 5 years ago

If you want to create a TOC automatically, you'll have to write a plugin to extend Parsedown (the Markdown parser Pico uses).

Besides that you can use the ### This is a headline {#this-is-a-headline} Markdown syntax in your content files to add anchors to headlines. You can then add a list of all anchors and their respective title to the YAML header of your content file and use this information to create a TOC using Twig. See "Table of contents" on http://picocms.org/cookbook/

Ok, thanks!

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:

marcus-at-localhost commented 4 years ago

Just for the record:

Table of Content in Pages

https://github.com/caseyamcl/toc

# GIS, Geolocation

[TOC]

Links to location related stuff.

## Linklist

- bla

## More

Plugin

<?php

class TableOfContent extends AbstractPicoPlugin
{
    const API_VERSION = 2;

    protected $enabled = true;

    public function onContentParsed(&$content)
    {

        $markupFixer  = new TOC\MarkupFixer();
        $tocGenerator = new TOC\TocGenerator();

        $content = $markupFixer->fix($content);
        $toc = '<div class="toc">' . $tocGenerator->getHtmlMenu($content, 1,2) . '</div>';

        if(strpos($content,'[TOC]') !== false)
        {
            $content = str_replace(['<p>[TOC]</p>','[TOC]'], $toc, $content);
        }
    }
}