Doc: add anchor

pull/827/head
qingwei.li 2016-11-04 16:59:59 +08:00
parent fad0fd320c
commit 9282417d58
3 changed files with 60 additions and 2 deletions

View File

@ -2,6 +2,7 @@ var cooking = require('cooking');
var config = require('./config'); var config = require('./config');
var md = require('markdown-it')(); var md = require('markdown-it')();
var striptags = require('./strip-tags'); var striptags = require('./strip-tags');
var slugify = require('transliteration').slugify;
function convert(str) { function convert(str) {
str = str.replace(/(&#x)(\w{4});/gi, function($0) { str = str.replace(/(&#x)(\w{4});/gi, function($0) {
@ -37,6 +38,12 @@ cooking.add('loader.md', {
cooking.add('vueMarkdown', { cooking.add('vueMarkdown', {
use: [ use: [
[require('markdown-it-anchor'), {
level: 2,
slugify: slugify,
permalink: true,
permalinkBefore: true
}],
[require('markdown-it-container'), 'demo', { [require('markdown-it-container'), 'demo', {
validate: function(params) { validate: function(params) {
return params.trim().match(/^demo\s*(.*)$/); return params.trim().match(/^demo\s*(.*)$/);

View File

@ -71,6 +71,21 @@
h2, h3, h4, h5 { h2, h3, h4, h5 {
font-weight: normal; font-weight: normal;
color: #1f2f3d; color: #1f2f3d;
&:hover a {
opacity: .4;
}
a {
float: left;
margin-left: -20px;
opacity: 0;
cursor: pointer;
&:hover {
opacity: .4;
}
}
} }
p { p {
font-size: 14px; font-size: 14px;
@ -101,10 +116,44 @@
<script> <script>
export default { export default {
name: 'app', 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() { created() {
window.addEventListener('hashchange', () => { window.addEventListener('hashchange', () => {
document.body.scrollTop = 0; if (location.href.match(/#/g).length < 2) {
document.documentElement.scrollTop = 0; document.documentElement.scrollTop = document.body.scrollTop = 0;
this.renderAnchorHref();
} else {
this.goAnchor();
}
}); });
} }
}; };

View File

@ -93,6 +93,7 @@
"lerna": "2.0.0-beta.18", "lerna": "2.0.0-beta.18",
"lolex": "^1.5.1", "lolex": "^1.5.1",
"markdown-it": "^6.1.1", "markdown-it": "^6.1.1",
"markdown-it-anchor": "^2.5.0",
"markdown-it-container": "^2.0.0", "markdown-it-container": "^2.0.0",
"mocha": "^3.1.1", "mocha": "^3.1.1",
"phantomjs-prebuilt": "^2.1.13", "phantomjs-prebuilt": "^2.1.13",
@ -104,6 +105,7 @@
"sinon-chai": "^2.8.0", "sinon-chai": "^2.8.0",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",
"theaterjs": "^3.0.0", "theaterjs": "^3.0.0",
"transliteration": "^1.1.11",
"uppercamelcase": "^1.1.0", "uppercamelcase": "^1.1.0",
"url-loader": "^0.5.7", "url-loader": "^0.5.7",
"vue": "^2.0.3", "vue": "^2.0.3",