YenYuHsuan / hexo-theme-beantech

:sparkles: Ported theme of Hux Blog by Kaijun, Modified by YuHsuan :sparkles:
http://beantech.org
Apache License 2.0
441 stars 105 forks source link

Toc feature seems not working #19

Closed trannamquan closed 5 years ago

trannamquan commented 6 years ago

image It does not scroll to the clicked header. It returns #undefined. I saw some issues which is similar to mine but they were written in Chinese (or sth like that), I can not understand what their meanings. So it is nice if you can clarify me again. Thank so much

HeXingye commented 6 years ago

我刚刚看了一下,我的toc功能也用不了

wakakalu commented 6 years ago

Reference to the answer of @fawks96 in issue #11:

Actually this is an issue caused by the package hexo-toc. Something has been updated so bad situation happened. @YenYuHsuan don't have this problem maybe because you are using the old version of this. Here is the solution I got from a kindly friend of mine, now I share with you. Hope it will solve your problem:

// Preliminary inspection After simple inspection, it is found that the following markdown statement ## title will be parsed as follows in html in the old version of hexo-toc <h2 id="title">title</h2>
while in the new version, hexo-toc will parse it as <h2><span id="title">title</span></h2>

It causes that when toc is generating, the id property of element h2 is undefined so that the anchor is undefined. The above problem seems caused by the change of package hexo-toc exactly in this commit:https://github.com/bubkoo/hexo-toc/blob/7c9624c2d3fb7d13ce973cb8de4dc10ff865c089/lib/filter.js#L28

However, it is just a preliminary inspection. It can't be sure that the hexo-toc update causes this problem. It's just deemed that this change made some code incompatible.

// Solution At the moment, the solution is to open node_modules/hexo-toc/lib/filter.js under your project root directory and modify line 28 to 31 as follows

$title.attr('id', id);
// $title.children('a').remove();
// $title.html( '<span id="' + id + '">' + $title.html() + '</span>' );
// $title.removeAttr('id');

i.e., to revert this commit and reserve the line 28 only, with others (line 29 to 31) annotated. Then, the toc will be generated correctly.

// Attention Note that the files generated from markdown will be cached, thus you should create a new document to test whether the problem is solved.

This answer helped me solve this problem, hope it will suit you, too ; ) @trannamquan @HeXingye

weite122 commented 5 years ago

Reference to the answer of @fawks96 in issue #11:

Actually this is an issue caused by the package hexo-toc. Something has been updated so bad situation happened. @YenYuHsuan don't have this problem maybe because you are using the old version of this. Here is the solution I got from a kindly friend of mine, now I share with you. Hope it will solve your problem:

// Preliminary inspection After simple inspection, it is found that the following markdown statement ## title will be parsed as follows in html in the old version of hexo-toc <h2 id="title">title</h2> while in the new version, hexo-toc will parse it as <h2><span id="title">title</span></h2>

It causes that when toc is generating, the id property of element h2 is undefined so that the anchor is undefined. The above problem seems caused by the change of package hexo-toc exactly in this commit:https://github.com/bubkoo/hexo-toc/blob/7c9624c2d3fb7d13ce973cb8de4dc10ff865c089/lib/filter.js#L28

However, it is just a preliminary inspection. It can't be sure that the hexo-toc update causes this problem. It's just deemed that this change made some code incompatible.

// Solution At the moment, the solution is to open node_modules/hexo-toc/lib/filter.js under your project root directory and modify line 28 to 31 as follows

$title.attr('id', id);
// $title.children('a').remove();
// $title.html( '<span id="' + id + '">' + $title.html() + '</span>' );
// $title.removeAttr('id');

i.e., to revert this commit and reserve the line 28 only, with others (line 29 to 31) annotated. Then, the toc will be generated correctly.

// Attention Note that the files generated from markdown will be cached, thus you should create a new document to test whether the problem is solved.

This answer helped me solve this problem, hope it will suit you, too ; ) @trannamquan @HeXingye

Thank you very much,it's helpful

adamSellers commented 5 years ago

Has anyone verified that this works? I am trying to get it working this morning, but this doesn't solve the issue. updated latest version of hexo-toc etc, to no avail..