blob: 1f9510559df77e526a129e81d86507e2723903f7 (
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
159
160
161
162
163
164
165
166
|
#!/bin/sh
# mdview - simple markdown viewer
# Usage: mdview [--light|--dark|--auto] [--live] <file.md>
theme="auto"
live_mode=0
filename=""
# lang="en" - maybe support language tags later
for arg in "$@"; do
case "$arg" in
--light) theme="light" ;;
--dark) theme="dark" ;;
--auto) theme="auto" ;;
--live|-w) live_mode=1 ;;
--help|-h)
echo "Usage: mdview [--light|--dark|--auto] [--live] <file.md>"
echo " --light Light theme"
echo " --dark Dark theme"
echo " --auto Follow system (default)"
echo " --live Watch file for changes"
exit 0
;;
*) filename="$arg" ;;
esac
done
if [ -z "$filename" ]; then
echo "Usage: mdview [--light|--dark|--auto] [--live] <file.md>" >&2
exit 1
fi
# Resolve the base directory for relative paths
if [ "$filename" = "-" ]; then
base_dir="file://$(pwd)/"
else
base_dir="file://$(cd "$(dirname "$filename")" && pwd)/"
fi
case "$theme" in
light)
css_file="$HOME/.config/mdview/github-markdown-light.css"
page_background="#ffffff"
page_scheme="light"
;;
dark)
css_file="$HOME/.config/mdview/github-markdown-dark.css"
page_background="#0d1117"
page_scheme="dark"
;;
auto)
css_file="$HOME/.config/mdview/github-markdown.css"
page_background="Canvas"
page_scheme="light dark"
;;
esac
html() {
if [ "$filename" = "-" ]; then
body=$(cat | cmark-gfm --width 80 --unsafe)
else
body=$(cmark-gfm --width 80 --unsafe "$filename")
fi
cat <<EOF
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<base href="$base_dir">
<style>
:root {
color-scheme: $page_scheme;
}
html,
body {
min-height: 100%;
}
body {
margin: 0;
background-color: $page_background;
}
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
min-height: 100vh;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
</head>
<body>
<article class="markdown-body">
$body
</article>
</body>
</html>
EOF
}
if [ "$live_mode" -eq 1 ]; then
if [ "$filename" = "-" ]; then
echo "mdview: --live cannot be used with standard input" >&2
exit 1
fi
tmp=$(mktemp /tmp/mdview_live_XXXXXX.html)
surf_pid=""
cleanup() {
[ -n "$surf_pid" ] && kill "$surf_pid" 2>/dev/null
rm -f "$tmp"
}
render() {
html > "$tmp" || return 1
if [ -n "$surf_pid" ]; then
kill -HUP "$surf_pid" 2>/dev/null || return 1
fi
}
trap cleanup EXIT INT TERM
render || exit 1
echo "Live preview: $filename (theme: $theme)"
echo "Press Ctrl+C to stop"
# -d disables surf's disk cache.
surf -d -C "$css_file" "$tmp" &
surf_pid=$!
old_sum=$(cksum < "$filename") || exit 1
while kill -0 "$surf_pid" 2>/dev/null; do
sleep 1
new_sum=$(cksum < "$filename") || continue
[ "$new_sum" = "$old_sum" ] && continue
old_sum=$new_sum
if render; then
echo "Updated: $filename"
fi
done
else
tmp=$(mktemp /tmp/mdview_XXXXXX.html)
html > "$tmp"
surf -C "$css_file" "$tmp"
rm -f "$tmp"
fi
|