Add link anchor to every title in markdown document

pull/408/head
afc163 9 years ago
parent 44cb11a2bd
commit f469541433

@ -610,6 +610,22 @@ footer ul li > a {
margin: 0; 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 { #api ~ ul > li > p > strong ~ code {
background: #fff; background: #fff;
color: #CC7300; color: #CC7300;

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

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

@ -172,6 +172,13 @@ module.exports = function(nico) {
result.push(p); result.push(p);
}); });
return result; 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;
} }
}; };

Loading…
Cancel
Save