최근 검색
최근 검색 없음

Lily Svetnik
가입한 날짜: 2021년 4월 16일
·
마지막 활동: 2021년 10월 27일
팔로잉
0
팔로워
0
총 활동 수
3
투표 수
0
가입 플랜
1
활동 개요
배지
문서
게시물
커뮤니티 댓글
문서 댓글
활동 개요
님의 최근 활동 Lily Svetnik
Lily Svetnik님이 에 댓글을 입력함
Hi Jessie!
I was using the latest code that was posted here. Apparently, it wasn't working mostly because of syntax errors and other minor issues.
We've corrected and slightly modified the script, it's working perfectly fine now and I'm loving it!
Here's my version if anyone else is looking for a solution:
/*** Table of contents code ***/
var $headers = $('.article-body:first h1');
if ($headers.length == 0) $headers = $('.article-body:first h2');
if ($headers.length > 0) {
var $toc = $('
var $firstUl = $('
- ');
var $currentUl = $firstUl;
var previous_level = 1;
var $arrayUl = [];
$firstUl.appendTo($toc);
$('#table-of-contents').length > 0 ? $toc.appendTo('#table-of-contents') : $toc.prependTo('.article-body:first');
// start with first H1
insertHeading($headers[0]);
}
function insertHeading(heading) {
var $heading = $(heading);
// what level heading are we on?
var current_level = headingLevel(heading);
// if it's an H1, add it to the original list
if (current_level === 1) {
newLi($heading, $firstUl);
$currentUl = $firstUl;
$arrayUl = [];
$arrayUl.push($firstUl);
}
// if it's the same as the one before it, add it to the current list
else if (current_level === previous_level) {
newLi($heading, $currentUl);
}
// if it's one level higher than the one before it... time to make a new nested list
else if (current_level > previous_level) {
nestUl();
$arrayUl.push($currentUl);
newLi($heading, $currentUl);
}
else if (current_level for (i = 0; i < (previous_level-current_level); i++) {
$arrayUl.pop();
}
$currentUl = $arrayUl[$arrayUl.length-1];
newLi($heading, $currentUl);
}
previous_level = current_level;
var $nextHeading = $heading.nextAll("h1, h2, h3, h4, h5, h6").first()[0];
// if there's any headings left... run this again
if ($nextHeading) insertHeading($nextHeading);
}
// adds a new UL to the current UL
function nestUl() {
var $newUl = $('
- ');
$newUl.appendTo($currentUl);
$currentUl = $newUl;
}
// returns a numerical value for each heading
function headingLevel(heading) {
switch (heading.nodeName) {
case 'H1':
return 1;
break;
case 'H2':
return 2;
break;
case 'H3':
return 3;
break;
case 'H4':
return 4;
break;
case 'H5':
return 5;
break;
case 'H6':
return 6;
break;
default:
return 0;
}
}
// inserts a new line to the current list
function newLi(heading, $list) {
var $heading = $(heading);
if ($heading.text().replace(/\s/g, '') == '') return null;
var $wrapper = $('
//var $link = $('').prop('href', '#' + $heading.prop('id'));
var $anchorname = $heading[0].outerText.replace (/\s/g,'');
var $link = $('').prop('href', '#' + $anchorname);
$link.html($heading.text());
$link.appendTo($wrapper);
$wrapper.appendTo($list);
var place_in_parent = $list.children('li').length;
$heading.html("" + $link.find('.index').text() + ' ' + $heading.text());
}
});
댓글 보기 · 2018년 6월 25일에 게시됨 · Lily Svetnik
0
팔로워
0
투표 수
0
댓글
Lily Svetnik님이 에 댓글을 입력함
Hi, can someone please post a working version of this script? I've tested all of them from original to the last one and unfortunately nothing happens at all with my articles :(
Thank you in advance
댓글 보기 · 2018년 6월 20일에 게시됨 · Lily Svetnik
0
팔로워
0
투표 수
0
댓글