Typecho无插件实现目录

对网上的版本进行了个小修改~

代码
function createCatalog($obj) {
$obj = preg_replace_callback('/(.*?)<\/h\1>/i', function ($obj) {
global $catalog;
global $catalog_count;
$catalog_count++;
$catalog[] = array('text' => trim(strip_tags($obj[3])), 'depth' => $obj[1], 'count' => $catalog_count);
return ''.$obj[3].'';
}, $obj);
return $obj;
}

function getCatalog() {
global $catalog;
$index = '';
if ($catalog) {
$index = '

    ' . "\n"; $prev_depth = ''; $to_depth = 0; foreach ($catalog as $catalog_item) { $catalog_depth = $catalog_item['depth']; if ($prev_depth) { if ($catalog_depth == $prev_depth) { $index .= '' . "\n"; } elseif ($catalog_depth > $prev_depth) { $to_depth++; $index .= '
      ' . "\n"; } else { $to_depth2 = ($to_depth > ($prev_depth - $catalog_depth)) ? ($prev_depth - $catalog_depth) : $to_depth; if ($to_depth2) { for ($i = 0; $i < $to_depth2; $i++) { $index .= '' . "\n" . '
    ' . "\n"; $to_depth--; } } $index .= ''; } } $index .= '
  • ' . $catalog_item['text'] . '';
    $prev_depth = $catalog_item['depth'];
    }
    for ($i = 0; $i <= $to_depth; $i++) {
    $index .= '
  • ' . "\n" . '

' . "\n";
}

}
echo $index;
}
然后Post.php中进行如下修改

$content = preg_replace('#(.*?)#','$2',$this->content);
echo $content;
?>
我自己在使用这个版本的时候添加锚点出现了bug,所以就进行了这些修改。

发表新评论