forked from typeorm/typeorm.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdownReader.js
80 lines (72 loc) · 2.3 KB
/
MarkdownReader.js
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
77
78
79
80
const MarkdownReader = {
template: `<div v-html="html"></div>`,
props: ["file", "hash"],
data: function() {
return {
html: "",
document: "readme"
}
},
watch: {
'file': function(file) {
this.setDocument();
this.loadFile(file)
.then(() => {
this.scrollToHash(this.hash);
});
},
'hash': function(hash) {
this.scrollToHash(hash);
}
},
created: function () {
this.setDocument();
this.loadFile(this.file)
.then(() => {
this.scrollToHash(this.hash);
});
},
methods: {
scrollToHash: function(hash) {
if (!hash) {
window.scrollTo(0, 0);
return;
}
const hashElement = document.getElementById(hash.substr(1));
if (!hashElement){
window.scrollTo(0, 0);
return;
}
hashElement.scrollIntoView();
},
setDocument: function () {
if(this.$route.params.document) {
this.document = this.$route.params.document;
}
else {
this.document = "readme";
}
},
loadFile: function(file) {
return fetch(file)
.then((response) => {
return response.text();
})
.then((body) => {
showdown.extension('header-anchors', () => {
var ancTpl = '$1<a id="user-content-$3" class="anchor" href="#$3" aria-hidden="true">#</a> $4';
return [{
type: "html",
regex: /(<h([2-4]) id="([^"]+?)">)(.*<\/h\2>)/g,
replace: ancTpl
}];
});
const converter = new showdown.Converter({ extensions: ['header-anchors'] });
converter.setFlavor('github');
converter.setOption('simpleLineBreaks', false);
this.html = converter.makeHtml(body);
this.$nextTick(() => Prism.highlightAll());
});
}
}
};