Add link anchor to every title in markdown document

This commit is contained in:
afc163 2015-10-19 17:11:03 +08:00
parent 44cb11a2bd
commit f469541433
4 changed files with 25 additions and 2 deletions

View File

@ -610,6 +610,22 @@ footer ul li > a {
margin: 0;
}
.markdown .anchor {
opacity: 0;
transition: opacity 0.3s ease;
margin-left: 8px;
}
.markdown h1:hover .anchor,
.markdown h2:hover .anchor,
.markdown h3:hover .anchor,
.markdown h4:hover .anchor,
.markdown h5:hover .anchor,
.markdown h6:hover .anchor {
opacity: 1;
display: inline-block;
}
#api ~ ul > li > p > strong ~ code {
background: #fff;
color: #CC7300;

View File

@ -12,7 +12,7 @@
</a>
{%- endif %}
</h1>
{{ post.html }}
{{ post.html|add_anchor }}
</article>
{%- if post.meta.template === 'component' %}
{%- include "demos.html" %}

View File

@ -18,7 +18,7 @@
<section class="main-container">
<article class="markdown">
<h1>{{ post.title }} {{ post.meta.chinese }}</h1>
{{ post.html }}
{{ post.html|add_anchor }}
</article>
</section>
{% endblock %}

View File

@ -172,6 +172,13 @@ module.exports = function(nico) {
result.push(p);
});
return result;
},
add_anchor: function(content) {
for (var i = 1; i <= 6; i++) {
var reg = new RegExp('(<h' + i + '\\sid="(.*?)">.*?)(<\/h' + i + '>)', 'g');
content = content.replace(reg, '$1<a href="#$2" class="anchor">#</a> $3');
}
return content;
}
};