Skip to content

Commit

Permalink
feat: add header anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Mar 2, 2019
1 parent 0276fdd commit f598f1d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ a {
.docute-footer {
padding-top: 60px;
}

.header-anchor {
float: left;
line-height: 1;
margin-left: -20px;
padding-right: 4px;
opacity: 0;

&:hover {
opacity: 1;
}

& .anchor-icon {
vertical-align: middle;
}
}

.markdown-header:focus,
.markdown-header:hover {
outline: none;
& .header-anchor {
opacity: 1;
}
}
6 changes: 5 additions & 1 deletion src/utils/markedRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export default hooks => {
}

const tag = `h${level}`
return `<${tag} id="${slug}">${text}</${tag}>`
return `<${tag} class="markdown-header" id="${slug}">
<router-link class="header-anchor" :to="{hash:'${slug}'}">
<svg class="anchor-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>
</router-link>
${text}</${tag}>`
}

// Disable template interpolation in code
Expand Down
5 changes: 4 additions & 1 deletion src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export default {
watch: {
'$route.hash'() {
this.jumpToHash()
this.$nextTick(() => {
this.jumpToHash()
})
},
pageTitle(newValue) {
const {path} = this.$route
Expand Down Expand Up @@ -132,6 +134,7 @@ export default {
if (el) {
const header = document.querySelector('.Header')
jump(el, {
a11y: true,
duration: 0,
offset: -(header.clientHeight + 30)
})
Expand Down

0 comments on commit f598f1d

Please sign in to comment.