aboutsummaryrefslogtreecommitdiff
path: root/packages/client/src/components/RepositoryTreeBlob.vue
blob: 4637a84aa504a6bfc7b85889a606eb45f0f457b6 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
	<table cellspacing="0px" v-if="!is_markdown">
		<tbody>
			<tr v-for="(line, index) in content_lines" :key="index">
				<td :line="index + 1" />
				<td>
					<code v-html="line" />
				</td>
			</tr>
		</tbody>
	</table>
	<span
		v-else v-html="content_lines"
		class="text-wrap" />
</template>

<script>
import { ref } from "vue";
import hljs from "highlight.js";
import hljs_languages from "../util/hljs-languages";
import path from "path";
import marked from "@/lib/marked.min.js";

export default {
	name: "RepositoryTreeBlob",
	props: {
		repository: {
			type: String,
			required: true
		},
		path: {
			type: String,
			required: true
		},
		content: {
			type: String,
			required: true
		}
	},
	watch: {
		content() {
			this.initHighlightedContent();
		}
	},
	mounted() {
		this.initHighlightedContent();
	},
	setup(props) {
		const content_lines = ref([]);
		const is_markdown = ref(false);

		const initHighlightedContent = async() => {
			if(path.extname(props.path) === ".md") {
				const markdown = document.createElement("html");
				markdown.innerHTML = marked(props.content);

				const checkboxes = markdown.querySelectorAll("ul > li > input[type=\"checkbox\"]");
				checkboxes.forEach((checkbox) => {
					checkbox.parentElement.parentElement.classList.add("checkbox-list");
				});

				const codeblocks = markdown.querySelectorAll("code");
				codeblocks.forEach((codeblock) => {
					codeblock.classList.add("markdown-codeblock");
				});

				content_lines.value = markdown.innerHTML;
				is_markdown.value = true;
				return;
			}

			const language = hljs_languages.find((lang) => lang.extensions.some((extension) => path.extname(props.path) === extension));
			const highlighted = language ? hljs.highlight(props.content, { language: language.name }) : hljs.highlightAuto(props.content);

			content_lines.value = highlighted.value.split("\n");
		};

		return { content_lines, is_markdown, initHighlightedContent };
	}
};
</script>

<style lang="scss">
@import "~highlight.js/scss/srcery.scss";
@import "../scss/fonts";

ul {
	padding-left: 30px;
}

.checkbox-list {
	list-style-type: none;
	padding-left: 5px;
}

code {
	display: inline-block;
	white-space: pre-wrap;
	word-wrap: anywhere;
	background-color: lighten(#000000, 8%);
	font-family: $font-primary;
}

.markdown-codeblock {
	padding-right: 10px;
	padding-left: 5px;
	padding-top: 2px;
	padding-bottom: 5px;
}

p {
	width: 95ch;
}

</style>

<style lang="scss" scoped>
table {
	table-layout: fixed;
	background-color: lighten(#000000, 8%);
	padding-top: 5px;
	padding-bottom: 5px;
	width: 98%;
}

td:nth-child(1) {
	width: max-content;
	padding-right: 20px;
	font-weight: 300;
}

[line]::before {
	content: attr(line);
}
</style>