public function getToc(ContentInterface $md_content, OutputFormatInterface $formatter, array $attributes = null)
    {
        $cfg_toc_max_level      = $this->getConfigOrDefault('toc_max_level');
        $cfg_toc_title          = $this->getConfigOrDefault('toc_title');
        $cfg_toc_title_level    = $this->getConfigOrDefault('toc_title_level');
        $cfg_toc_id             = $this->getConfigOrDefault('toc_id');
        $cfg_toc_class          = $this->getConfigOrDefault('toc_class');
        $cfg_toc_item_title     = $this->getConfigOrDefault('toc_item_title');

        $menu                   = $md_content->getMenu();
        $content                = $list_content = '';
        $max_level              = isset($attributes['max_level']) ? $attributes['max_level'] : $cfg_toc_max_level;

        if (!empty($menu) && count($menu) > 1) {
            $depth = 0;
            $current_level = null;

            foreach ($menu as $item_id=>$menu_item) {
                $_item_id = TemplateHelper::getSafeIdString($item_id);

                if (isset($max_level) && $menu_item['level']>$max_level) {
                    continue;
                }

                $diff = $menu_item['level']-(is_null($current_level) ? $menu_item['level'] : $current_level);
                if ($diff > 0) {
                    $list_content .= str_repeat('<ul><li>', $diff);
                } elseif ($diff < 0) {
                    $list_content .= str_repeat('</li></ul></li>', -$diff);
                    $list_content .= '<li>';
                } else {
                    if (!is_null($current_level)) $list_content .= '</li>';
                    $list_content .= '<li>';
                }

                $depth += $diff;
                $list_content .= $formatter->buildTag('link', $menu_item['text'], array(
                    'href'  => '#'.$_item_id,
                    'title' => isset($attributes['toc_item_title']) ? $attributes['toc_item_title'] : $cfg_toc_item_title,
                ));
                $current_level = $menu_item['level'];
            }

            if ($depth!=0) {
                $list_content .= str_repeat('</ul></li>', $depth);
            }

            $content .= $formatter->buildTag('title', $cfg_toc_title, array(
                'level'     => isset($attributes['toc_title_level']) ? $attributes['toc_title_level'] : $cfg_toc_title_level,
                'id'        => isset($attributes['toc_id']) ? $attributes['toc_id'] : $cfg_toc_id,
                'no-addon'  => true
            ));

            $content .= $formatter->buildTag('unordered_list', $list_content, array(
                'class'     => isset($attributes['class']) ? $attributes['class'] : $cfg_toc_class,
            ));

        }
        return $content;
    }