痛点

  • 文章内容过多之后,查阅不方便
  • maupassant主题默认的目录显示在正文

解决方式

以下方法来自于 https://wiki.hushhw.cn/posts/5dd904ed.html

1、_config.yml

在主题配置文件_config.yml中添加侧栏toc

1
2
3
4
5
6
7
8
9
widgets: 
- toc
- info
- search
- category
- tag
- recent_posts
- recent_comments
- links

2、base.pug

themes/maupassant/layout/base.pug
mac注意tab间隔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#layout.pure-g
.pure-u-1.pure-u-md-3-4: .content_container
block content
if page.toc != true
.pure-u-1.pure-u-md-1-4: #sidebar
each item in theme.widgets
!= partial('_widget/' + item + '.pug', null, {cache: !config.relative_link})
else
//- if theme.toc_on_small_screens
if theme.widgets_on_small_screens
.pure-u-1.pure-u-md-1-4: #sidebar-toc
div(id="sidebar-stoc" class="stoc-article")
strong(class="stoc-title")
i(class='fa')= ' Contents '
div(id="stoc" class='toc-nav')
!= toc(page.content, {list_number: theme.toc_number})
script(type='text/javascript', src=url_for(theme.js) + '/toc.js' + '?v=' + theme.version)
else
.pure-u-1-4.hidden_mid_and_down: #sidebar-toc
div(id="sidebar-stoc" class="stoc-article")
strong(class="stoc-title")
i(class='fa')= ' Contents '
div(id="stoc" class='toc-nav')
!= toc(page.content, {list_number: theme.toc_number})
script(type='text/javascript', src=url_for(theme.js) + '/toc.js' + '?v=' + theme.version)
.pure-u-1.pure-u-md-3-4
!= partial('_partial/footer.pug')

3、toc.js

themes/maupassant/source/js/toc.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var toc = document.getElementById('sidebar-stoc')
var HEADER_OFFSET = 200;
var toclink = document.getElementsByClassName('toc-link');
var headerlink = document.getElementsByClassName('headerlink');

if (toc != null) {
window.addEventListener("scroll", scrollcatelogHandler);
var tocPosition = toc.offsetTop;
function scrollcatelogHandler(e) {
var event = e || window.event,
target = event.target || event.srcElement;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > tocPosition -60) {
toc.classList.add("stoc-fixed");
} else {
toc.classList.remove("stoc-fixed");
}
for(var i=0; i<toclink.length; i++){
//console.log(i);
var currentHeaderTop = headerlink[i].offsetTop - HEADER_OFFSET,
nextHeaderTop = i+1 === toclink.length ? Infinity : headerlink[i+1].offsetTop - HEADER_OFFSET;
if(currentHeaderTop < scrollTop && scrollTop <= nextHeaderTop){
toclink[i].classList.add('active');
} else {
toclink[i].classList.remove('active');
}
}
}
}

4、style.scss

themes/maupassant/source/css/style.scss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// toc start
#sidebar-toc {
padding-left: 15px;
margin-top: 40px;
padding-bottom: 20px;
word-wrap: break-word;
}

.stoc-article{
position: absolute;
padding: 1em 1em 0 1em;
margin-left: 1em;
border-left: rgba(88,88,88,0.1) 1px solid;
}

.stoc-title{
font-size: 150%;
}

#sidebar-stoc {
width: inherit;
line-height: 1em;
font-size: 0.9em;
float: left;
overflow: auto;
}

.stoc-fixed{
position: fixed;
top: 60px;
margin-top: 0;
max-height: 81%;
overflow: hidden;
z-index: 1;
}

.toc-nav {
padding: 0.7em 0.7em 0 0;
}

#stoc {
line-height: 1em;
font-weight: 700;
.active {
color: #0085a1;
border-radius: 4px;
background-color: #F5F5F5;
}
.toc {
padding: 0;
margin: 0.5em;
line-height: 1.8em;
li {
list-style-type: none;
}
}
.toc-child {
margin-left: 1em;
padding-left: 0;
}
}
// toc end