Skip to content

Commit 993766b

Browse files
committed
improve preview
1 parent cae83aa commit 993766b

3 files changed

Lines changed: 39 additions & 37 deletions

File tree

preview/preview.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,52 @@ func GetHtmlPreview(dir string, name string) template.HTML {
3030
if err != nil {
3131
return template.HTML("Cannot preview this file")
3232
}
33-
if isText {
34-
if path.Ext(name) == ".md" {
35-
md, err := renderMarkdown(filePath)
36-
if err != nil {
37-
return template.HTML("Cannot preview this file")
38-
}
39-
return template.HTML(md)
40-
} else if path.Ext(filePath) == ".svg" {
41-
return template.HTML("<img width=\"100%\" height=\"100%\" src=\"" + filePath + "\">")
42-
} else {
43-
lexer := lexers.Match(name)
44-
if lexer == nil {
45-
txt, err := renderText(filePath)
46-
if err != nil {
47-
return template.HTML("Cannot preview this file")
48-
}
49-
return template.HTML(txt)
50-
}
51-
code, err := renderCode(filePath)
52-
if err != nil {
53-
return template.HTML("Cannot preview this file")
54-
}
55-
return template.HTML(code)
33+
ext := path.Ext(filePath)
34+
if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".webp" || ext == ".jpeg" || ext == "svg" {
35+
return template.HTML("<img width=\"100%\" height=\"100%\" src=\"" + filePath + "\">")
36+
} else if ext == ".zip" {
37+
htmlZip, err := renderZip(filePath)
38+
if err != nil {
39+
return template.HTML("Cannot preview this file")
5640
}
57-
} else {
58-
ext := path.Ext(filePath)
59-
if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".webp" || ext == ".jpeg" {
60-
return template.HTML("<img width=\"100%\" height=\"100%\" src=\"" + filePath + "\">")
61-
} else if ext == ".zip" {
62-
htmlZip, err := renderZip(filePath)
41+
return template.HTML(htmlZip)
42+
} else if ext == ".pdf" {
43+
encodedImage, err := renderPdf(filePath)
44+
if err != nil {
45+
log.Println("ERROR:", err)
46+
return template.HTML("Cannot preview this file")
47+
}
48+
return template.HTML("<img width=\"100%\" height=\"100%\" src=\"" + encodedImage + "\">")
49+
} else if ext == ".md" {
50+
md, err := renderMarkdown(filePath)
51+
if err != nil {
52+
return template.HTML("Cannot preview this file")
53+
}
54+
return template.HTML(md)
55+
} else if isText {
56+
lexer := lexers.Match(name)
57+
if lexer == nil {
58+
txt, err := renderText(filePath)
6359
if err != nil {
6460
return template.HTML("Cannot preview this file")
6561
}
66-
return template.HTML(htmlZip)
67-
} else if ext == ".pdf" {
68-
encodedImage, err := renderPdf(filePath)
62+
return template.HTML(txt)
63+
}
64+
code, err := renderCode(filePath)
65+
if err != nil {
66+
return template.HTML("Cannot preview this file")
67+
}
68+
if code == "" {
69+
txt, err := renderText(filePath)
6970
if err != nil {
70-
log.Println("ERROR:", err)
7171
return template.HTML("Cannot preview this file")
7272
}
73-
return template.HTML("<img src=\"" + encodedImage + "\">")
73+
return template.HTML(txt)
7474
}
75+
return template.HTML(code)
76+
} else {
77+
return template.HTML("Cannot preview this file")
7578
}
76-
return template.HTML("Cannot preview this file")
7779
}
7880

7981
func renderPdf(filePath string) (string, error) {

static/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ div#drop-zone {
150150
div.preview-content {
151151
margin-left: 5ch;
152152
padding: 10px;
153-
max-width: 50vw;
153+
max-width: 45vw;
154154
min-width: 30vw;
155155
height: 70vh;
156156
border-width: 1px;

templates/files.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{{define "content"}}
1010
<form class="index-page" method="post" action="/action">
1111
<div class="search">
12-
<input type="text" name="search" id="search_input" placeholder="Fuzzy search">
12+
<input type="text" name="search" id="search_input" placeholder="Search">
1313
<button type="submit" name="action" value="search">
1414
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="50px" height="50px"><path d="M 21 3 C 11.621094 3 4 10.621094 4 20 C 4 29.378906 11.621094 37 21 37 C 24.710938 37 28.140625 35.804688 30.9375 33.78125 L 44.09375 46.90625 L 46.90625 44.09375 L 33.90625 31.0625 C 36.460938 28.085938 38 24.222656 38 20 C 38 10.621094 30.378906 3 21 3 Z M 21 5 C 29.296875 5 36 11.703125 36 20 C 36 28.296875 29.296875 35 21 35 C 12.703125 35 6 28.296875 6 20 C 6 11.703125 12.703125 5 21 5 Z"/></svg>
1515
</button>

0 commit comments

Comments
 (0)