aboutsummaryrefslogtreecommitdiff
path: root/packages/client/src/components/RepositoryTreeBlob.vue
blob: 60f050603d6fed4828e1d31fd4669d3ed058d2d4 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<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_blob"
		id="markdown-blob" />
</template>

<script lang="ts">
import { defineComponent, Ref, ref } from "vue";
import hljs from "highlight.js";
import hljs_languages from "../util/hljs-languages";
import marked from "marked";

export default defineComponent({
	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<string[]> = ref([]);
		const content_blob: Ref<string> = ref("");
		const is_markdown: Ref<boolean> = ref(false);

		const initHighlightedContent = async() => {
			const path_extension = /(?:\.([^.]+))?$/;

			const path_ext = path_extension.exec(props.path);
			let ext = "";

			if(path_ext) {
				ext = `.${path_ext[1]}`;
			}

			if(ext === ".md") {
				const markdown = document.createElement("html");
				markdown.innerHTML = marked(props.content);

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

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

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

			const language = hljs_languages.find((lang) => lang.extensions.some((extension) => ext === 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, content_blob, is_markdown, initHighlightedContent };
	}
});
</script>

<style lang="scss">
@use "../scss/colors";
@use "../scss/fonts";

@import "~highlight.js/scss/srcery.scss";

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: fonts.$primary;
}

#markdown-blob {
	word-wrap: anywhere;
	a {
		color: colors.$primary-light;
		&:hover {
			text-decoration: underline;
		}
	}
}

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

p {
	max-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>