Commit 7a8180d6 authored by SkyAo's avatar SkyAo Committed by GitHub

Merge pull request #827 from QingWei-Li/feat/doc-anchor

Doc: add anchor
parents f67104f2 9282417d
......@@ -2,6 +2,7 @@ var cooking = require('cooking');
var config = require('./config');
var md = require('markdown-it')();
var striptags = require('./strip-tags');
var slugify = require('transliteration').slugify;
function convert(str) {
str = str.replace(/(&#x)(\w{4});/gi, function($0) {
......@@ -37,6 +38,12 @@ cooking.add('loader.md', {
cooking.add('vueMarkdown', {
use: [
[require('markdown-it-anchor'), {
level: 2,
slugify: slugify,
permalink: true,
permalinkBefore: true
}],
[require('markdown-it-container'), 'demo', {
validate: function(params) {
return params.trim().match(/^demo\s*(.*)$/);
......
......@@ -71,6 +71,21 @@
h2, h3, h4, h5 {
font-weight: normal;
color: #1f2f3d;
&:hover a {
opacity: .4;
}
a {
float: left;
margin-left: -20px;
opacity: 0;
cursor: pointer;
&:hover {
opacity: .4;
}
}
}
p {
font-size: 14px;
......@@ -101,10 +116,44 @@
<script>
export default {
name: 'app',
methods: {
renderAnchorHref() {
const anchors = document.querySelectorAll('h2 a,h3 a');
const basePath = location.href.split('#').splice(0, 2).join('#');
[].slice.call(anchors).forEach(a => {
const href = a.getAttribute('href');
a.href = basePath + href;
});
},
goAnchor() {
if (location.href.match(/#/g).length > 1) {
const auchor = location.href.match(/#[^#]+$/g);
if (!auchor || auchor.length !== 1) return;
const elm = document.querySelector(auchor[0]);
if (!elm) return;
setTimeout(_ => {
document.documentElement.scrollTop = document.body.scrollTop = elm.offsetTop;
}, 50);
}
}
},
mounted() {
this.renderAnchorHref();
this.goAnchor();
},
created() {
window.addEventListener('hashchange', () => {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
if (location.href.match(/#/g).length < 2) {
document.documentElement.scrollTop = document.body.scrollTop = 0;
this.renderAnchorHref();
} else {
this.goAnchor();
}
});
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment