This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMarkdownReader.js
More file actions
93 lines (84 loc) · 3.04 KB
/
MarkdownReader.js
File metadata and controls
93 lines (84 loc) · 3.04 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
const MarkdownReader = {
template: `<section v-html="html"></section>`,
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) {
const fragmentElement = document.getElementById(hash.substring("#".length));
if (fragmentElement) {
fragmentElement.scrollIntoView();
return;
}
}
window.scrollTo(0, 0);
},
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
}];
});
showdown.extension('links-replacer', () => {
return [{
type: "html",
regex: /<a href="#(.*)">/g,
replace: "<a href='/" + (this.document ? this.document + "#" : "") + "$1'>"
}];
});
showdown.extension('other-page-links-replacer', () => {
return [{
type: "html",
regex: /<a href="(?!https?\:)\.?\/?(docs\/)?(.*?)\.md\#?(.*?)">/g,
replace: "<a href='/$2#$3'>"
}];
});
const converter = new showdown.Converter({ metadata: true, extensions: ['header-anchors', 'links-replacer', 'other-page-links-replacer'] });
converter.setFlavor('github');
converter.setOption('simpleLineBreaks', false);
this.html = converter.makeHtml(body);
this.$nextTick(() => Prism.highlightAll());
});
}
}
};