-
Notifications
You must be signed in to change notification settings - Fork 103
/
Post.vue
76 lines (68 loc) · 1.97 KB
/
Post.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<template>
<section class="post-view">
<div v-if="!content">loading..</div>
<h1 class="post-title">
{{ title }}
<time pubdate="pubdate" :datetime="this.date | formatDate" :title="this.date | formatDate" class="post-date">{{ this.date | timeago }}</time>
</h1>
<article v-if="content" v-html="htmlFromMarkdown"></article>
</section>
</template>
<script>
import Vue from 'vue'
import api from '../api'
import conf from '../conf.json'
import fm from 'front-matter'
import marked from '../utils/render.js'
export default {
name: 'postView',
data () {
return {
title: '',
date: null,
content: ''
}
},
computed: {
htmlFromMarkdown () {
return marked(this.content)
}
},
created () {
this.loadPost()
},
methods: {
loadPost () {
api.getDetail(this.$route.params.hash)
.then(text => {
// Parse front-matter
// https://github.com/jxson/front-matter#fmstring
const content = fm(text)
this.content = content.body
this.title = content.attributes.title
this.date = content.attributes.date
// Set window title
window.document.title = `${this.title} - ${conf.blogTitle}`
})
.catch(err => { console.error(err) })
},
newTab () {
Vue.nextTick(function () {
// Load the external link into new tab
const linksArray = Array.from(document.querySelectorAll('a'))
const currentHost = window.location.host
linksArray.forEach(el => {
if (el.href && el.host !== currentHost) {
el.target = '_blank'
// https://www.jitbit.com/alexblog/256-targetblank---the-most-underestimated-vulnerability-ever/
el.rel = 'noopener noreferrer'
}
})
})
}
},
watch: {
'htmlFromMarkdown': 'newTab'
}
}
</script>