Skip to content

Commit

Permalink
[Fixed] TOC bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
idawnlight committed Jan 20, 2018
1 parent fac17d1 commit 99f8c59
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
8 changes: 4 additions & 4 deletions footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ function() {

<?php if (!empty($this->options->searchis) && $this->options->searchis == '1'): ?>
<!-- Local Search (beta) -->
<script>
<script type="text/ls-javascript" id="search-local-js-script">
var searchFunc=function(c,a,b){$.ajax({url:c,dataType:"xml",success:function(e){var d=$("entry",e).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get();var g=document.getElementById(a);var f=document.getElementById(b);g.addEventListener("input",function(){var i='<ul class="search-result-list">';var h=this.value.trim().toLowerCase().split(/[\s\-]+/);f.innerHTML="";if(this.value.trim().length<=0){return}d.forEach(function(o){var n=true;var s=[];var t=o.title.trim().toLowerCase();var m=o.content.trim().replace(/<[^>]+>/g,"").toLowerCase();var j=o.url;var u=-1;var q=-1;var p=-1;if(t!==""&&m!==""){h.forEach(function(w,x){u=t.indexOf(w);q=m.indexOf(w);if(u<0&&q<0){n=false}else{if(q<0){q=0}if(x===0){p=q}}})}if(n){i+='<li><a href="'+j+'" class="search-result-title" target="_blank">'+t;var r=o.content.trim().replace(/<[^>]+>/g,"");if(p>=0){var k=p-6;var l=p+6;if(k<0){k=0}if(k===0){l=10}if(l>r.length){l=r.length}var v=r.substr(k,l);h.forEach(function(w){var x=new RegExp(w,"gi");v=v.replace(x,'<em class="search-keyword">'+w+"</em>")});i+='<p class="search-result">'+v+"...</p></a>"}}});f.innerHTML=i})}})};
</script>
<script type="text/ls-javascript" id="search-input-script">
var inputArea = document.querySelector('#search');
var getSearchFile = function() {
var path = '<?php $this->options->LocalsearchURL() ?>';
Expand All @@ -236,9 +239,6 @@ function() {
}
}
</script>
<script>
var searchFunc=function(c,a,b){$.ajax({url:c,dataType:"xml",success:function(e){var d=$("entry",e).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get();var g=document.getElementById(a);var f=document.getElementById(b);g.addEventListener("input",function(){var i='<ul class="search-result-list">';var h=this.value.trim().toLowerCase().split(/[\s\-]+/);f.innerHTML="";if(this.value.trim().length<=0){return}d.forEach(function(o){var n=true;var s=[];var t=o.title.trim().toLowerCase();var m=o.content.trim().replace(/<[^>]+>/g,"").toLowerCase();var j=o.url;var u=-1;var q=-1;var p=-1;if(t!==""&&m!==""){h.forEach(function(w,x){u=t.indexOf(w);q=m.indexOf(w);if(u<0&&q<0){n=false}else{if(q<0){q=0}if(x===0){p=q}}})}if(n){i+='<li><a href="'+j+'" class="search-result-title" target="_blank">'+t;var r=o.content.trim().replace(/<[^>]+>/g,"");if(p>=0){var k=p-6;var l=p+6;if(k<0){k=0}if(k===0){l=10}if(l>r.length){l=r.length}var v=r.substr(k,l);h.forEach(function(w){var x=new RegExp(w,"gi");v=v.replace(x,'<em class="search-keyword">'+w+"</em>")});i+='<p class="search-result">'+v+"...</p></a>"}}});f.innerHTML=i})}})};
</script>
<?php endif; ?>

<script>
Expand Down
19 changes: 12 additions & 7 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ function pangu($html_source)
* @param string post content
* @return string content
* TODO: 更改实现方式
* TODO: 平滑跳转
*/
function toc($content)
{
Expand All @@ -221,35 +220,41 @@ function toc($content)
}
if ($i > 0 && $flag == 2) {
$i2 = 0;
$toc .= "</li></ol></li>";
$toc .= "</ol></li>";
}
$flag = 1;
$toc .= '<li class="post-toc-item post-toc-level-1">';
}
if (strtolower(substr($c, 0, 4)) == '<h2>' && $i !== 0 && $flag == 1) {
if (strtolower(substr($c, 0, 4)) == '<h2>' && $i !== 0) {
if ($i > 0 && $flag == 1) {
$toc .= '<ol class="post-toc-child">';
$toc .= '<li class="post-toc-item post-toc-level-2">';
}
$i2++;
$flag = 2;
}
$toc .= '<a class="post-toc-link" href="#' . filter_var(substr($c, 4, strlen($c) - 4 - 5), FILTER_SANITIZE_ENCODED) . '"><span class="post-toc-number">';
if ($flag == 2) $toc .= '<li class="post-toc-item post-toc-level-2">';
$toc .= '<a class="post-toc-link" href="#' . substr($c, 4, strlen($c) - 4 - 5) . '"><span class="post-toc-number">';
if ($flag == 1) $toc .= $i;
if ($flag == 2) $toc .= $i . "." . $i2;
$toc .= '.</span><span class="post-toc-text">' . substr($c, 4, strlen($c) - 4 - 5) . '</span></a>';
if ($flag == 2) $toc .= '</li>';

$result .= strtolower(substr($c, 0, 3)) . ' id="' . filter_var(substr($c, 4, strlen($c) - 4 - 5), FILTER_SANITIZE_ENCODED) . '">' . substr($c, 4, strlen($c) - 4);
$result .= strtolower(substr($c, 0, 3)) . ' id="' . substr($c, 4, strlen($c) - 4 - 5) . '">' . substr($c, 4, strlen($c) - 4);
}

if ($i > 0 && $flag == 1) {
$toc .= '</li>';
}
if ($i > 0 && $flag == 2) {
$toc .= "</li></ol></li>";
$toc .= "</ol></li>";
}
$toc .= '</ol><!--</nopangu>-->';

if ($i == 0) {
echo '&nbsp;此文章暂无目录';
return $content;
}

echo $toc;
return $result;
}
4 changes: 0 additions & 4 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@
text-decoration: underline;
}

.mdl-menu__container {
position: fixed
}

</style>


Expand Down

0 comments on commit 99f8c59

Please sign in to comment.