Skip to content

Commit 8f1ba71

Browse files
committed
scripts: harden slidev docker helpers
Populate titles in the generated template, allow selecting host ports, and expose NODE_OPTIONS to bump the Playwright container heap. Also ignore crash dumps and temporary vite config files created while rendering. Signed-off-by: Piotr Król <[email protected]>
1 parent 953b649 commit 8f1ba71

File tree

3 files changed

+92
-32
lines changed

3 files changed

+92
-32
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ venv
1111
output
1212
tmp.*
1313
slides
14+
core
15+
vite.config.ts

scripts/ci/gen_slides.sh

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ set -e
66
gen_slides() {
77
local day
88
local escaped_file
9+
local escaped_title
910
local filename
1011
local input_file
12+
local input_file_abs
13+
local input_file_rel
14+
local node_max_old_space
1115
local range
1216
local output_file
17+
local title
1318

1419
# Extract slide information from YAML using yq with pipe as a separator
1520
IFS=$'\n' # Set Internal Field Separator to newline for reading line by line
@@ -18,25 +23,46 @@ gen_slides() {
1823
IFS='|' read -r input_file range output_file <<< "$entry"
1924

2025
# Derive other necessary variables from the input_file
21-
filename=$(basename "$input_file")
26+
if ! input_file_abs=$(realpath "$input_file"); then
27+
error_exit "Could not resolve path for $input_file"
28+
fi
29+
30+
if [ ! -f "$input_file_abs" ]; then
31+
error_exit "$input_file doesn't exist"
32+
fi
33+
34+
input_file_rel=$(realpath --relative-to "$PWD/slidev-template" "$input_file_abs")
35+
if [ -z "$input_file_rel" ]; then
36+
error_exit "Couldn't compute relative path for $input_file_abs"
37+
fi
38+
39+
filename=$(basename "$input_file_abs")
2240
filename=${filename%.*}
2341
day=${filename:0:1}
42+
title=$(awk 'BEGIN{in_frontmatter=0} \
43+
/^---[\r\n]*$/ { if (in_frontmatter) exit; in_frontmatter=1; next } \
44+
in_frontmatter && /^title:[[:space:]]*/ { sub(/^title:[[:space:]]*/,"",$0); print; exit }' "$input_file_abs")
45+
if [ -z "$title" ]; then
46+
title="3mdeb Presentation"
47+
fi
2448
copyright=${COPYRIGHT:-3mdeb Sp. z o.o. Licensed under the CC BY-SA 4.0}
2549
copyright=${copyright//\"/}
2650

2751
# Escape strings for sed
28-
escaped_file=$(printf 'slides/%s\n' "$input_file" | sed -e 's/[\/&$]/\\&/g')
52+
escaped_file=$(printf '%s\n' "$input_file_rel" | sed -e 's/[\/&$]/\\&/g')
53+
escaped_title=$(printf '%s\n' "$title" | sed -e 's/[\/&$]/\\&/g')
2954

3055
# Create temporary markdown file using the slide template
31-
sed -e "s/<SRC>/$escaped_file/g" -e "s/<DAY>/$day/g" -e "s/<COPYRIGHT>/$copyright/g" \
56+
sed -e "s/<SRC>/$escaped_file/g" -e "s/<DAY>/$day/g" -e "s/<COPYRIGHT>/$copyright/g" -e "s/<TITLE>/$escaped_title/g" \
3257
slides-template.md >slidev-template/slides.md
3358

3459
cat slidev-template/slides.md
3560

61+
node_max_old_space=${SLIDEV_NODE_MAX_OLD_SPACE:-4096}
3662
if [ -n "$USE_DOCKER" ]; then
3763
docker run -it --rm --user $(id -u):$(id -g) \
3864
-v "$PWD:/repo" \
39-
-p 8000:8000 \
65+
-e NODE_OPTIONS=--max-old-space-size="$node_max_old_space" \
4066
mcr.microsoft.com/playwright:v1.53.2-noble \
4167
bash -c "
4268
cd /repo/slidev-template && npm run export slides.md -- \
@@ -57,7 +83,6 @@ check_dependencies() {
5783
if [ -n "$USE_DOCKER" ]; then
5884
docker run -it --rm --user $(id -u):$(id -g) \
5985
-v "$PWD:/repo" \
60-
-p 8000:8000 \
6186
mcr.microsoft.com/playwright:v1.53.2-noble \
6287
bash -c "cd /repo/slidev-template && npm install"
6388
else
@@ -135,11 +160,5 @@ if ! check_dependencies; then
135160
error_exit "Missing dependencies"
136161
fi
137162

138-
if [ -L "slidev-template/slides" ]; then
139-
unlink slidev-template/slides
140-
fi
141-
142-
ln -sr . slidev-template/slides
143-
144163
# Call the function with the provided YAML file
145164
gen_slides "$1"

scripts/render-slides.sh

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,82 @@
44
render_slides() {
55
local day
66
local escaped_file
7+
local escaped_title
78
local filename
8-
local input_file
9+
local input_arg
10+
local input_file_abs
11+
local input_file_rel
12+
local node_max_old_space
13+
local slidev_port
14+
local title
15+
16+
# Remove any surrounding quotes from the argument to avoid breaking realpath
17+
input_arg=${1//\"/}
18+
19+
if ! input_file_abs=$(realpath "$input_arg"); then
20+
error_exit "Could not resolve path for $input_arg"
21+
fi
22+
23+
if [ ! -f "$input_file_abs" ]; then
24+
error_exit "$input_arg doesn't exist"
25+
fi
26+
27+
title=$(awk 'BEGIN{in_frontmatter=0} \
28+
/^---[\r\n]*$/ { if (in_frontmatter) exit; in_frontmatter=1; next } \
29+
in_frontmatter && /^title:[[:space:]]*/ { sub(/^title:[[:space:]]*/,"",$0); print; exit }' "$input_file_abs")
30+
if [ -z "$title" ]; then
31+
title="3mdeb Presentation"
32+
fi
33+
34+
input_file_rel=$(realpath --relative-to "$PWD/slidev-template" "$input_file_abs")
35+
if [ -z "$input_file_rel" ]; then
36+
error_exit "Couldn't compute relative path for $input_file_abs"
37+
fi
938

10-
# Remove any surrounding quotes from the variables
11-
input_file=${1//\"/}
12-
input_file="slides/$(realpath --relative-to slidev-template/slides/ "$input_file")"
1339
# Derive other necessary variables from the input_file
14-
filename=$(basename "$input_file")
40+
filename=$(basename "$input_file_abs")
1541
filename=${filename%.*}
1642
day=${filename:0:1}
1743
copyright=${COPYRIGHT:-3mdeb Sp. z o.o. Licensed under the CC BY-SA 4.0}
1844
copyright=${copyright//\"/}
1945

2046
# Escape strings for sed
21-
escaped_file=$(printf '%s\n' "$input_file" | sed -e 's/[\/&$]/\\&/g')
47+
escaped_file=$(printf '%s\n' "$input_file_rel" | sed -e 's/[\/&$]/\\&/g')
48+
escaped_title=$(printf '%s\n' "$title" | sed -e 's/[\/&$]/\\&/g')
2249

2350
# Create temporary markdown file using the slide template
24-
sed -e "s/<SRC>/$escaped_file/g" -e "s/<DAY>/$day/g" -e "s/<COPYRIGHT>/$copyright/g" \
51+
sed -e "s/<SRC>/$escaped_file/g" -e "s/<DAY>/$day/g" -e "s/<COPYRIGHT>/$copyright/g" -e "s/<TITLE>/$escaped_title/g" \
2552
slides-template.md >slidev-template/slides.md
2653
cp slidev-template/vite.config.ts .
2754

55+
slidev_port=${SLIDEV_PORT:-8000}
56+
node_max_old_space=${SLIDEV_NODE_MAX_OLD_SPACE:-4096}
57+
58+
if command -v python3 >/dev/null 2>&1; then
59+
if ! python3 - "$slidev_port" <<'PY'
60+
import socket
61+
import sys
62+
63+
port = int(sys.argv[1])
64+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
66+
try:
67+
s.bind(("0.0.0.0", port))
68+
except OSError:
69+
sys.exit(1)
70+
finally:
71+
s.close()
72+
PY
73+
then
74+
print_error "Port $slidev_port is already in use. Set SLIDEV_PORT to a free port."
75+
exit 1
76+
fi
77+
fi
78+
2879
docker run -it --rm --user $(id -u):$(id -g) \
2980
-v "$PWD:/repo" \
30-
-p 8000:8000 \
81+
-p "$slidev_port":8000 \
82+
-e NODE_OPTIONS=--max-old-space-size="$node_max_old_space" \
3183
mcr.microsoft.com/playwright:v1.53.2-noble \
3284
bash -c "cd /repo/slidev-template && npm run dev slides.md -- -o false -p 8000 --remote --force"
3385

@@ -39,7 +91,6 @@ render_slides() {
3991
check_dependencies() {
4092
docker run -it --rm --user $(id -u):$(id -g) \
4193
-v "$PWD:/repo" \
42-
-p 8000:8000 \
4394
mcr.microsoft.com/playwright:v1.53.2-noble \
4495
bash -c "cd /repo/slidev-template && npm install"
4596
return $?
@@ -101,21 +152,9 @@ if [ $# -ne 1 ]; then
101152
print_usage_error "Script accepts 1 positional arguments, got $#"
102153
fi
103154

104-
if [ ! -f "$1" ]; then
105-
error_exit "$1 doesn't exist"
106-
fi
107-
108155
if ! check_dependencies; then
109156
error_exit "Missing dependencies"
110157
fi
111158

112-
if [ -L "slidev-template/slides" ]; then
113-
unlink slidev-template/slides
114-
fi
115-
116-
if ! ln -sr . slidev-template/slides; then
117-
error_exit "Couldn't create slidev-template/slides symlink"
118-
fi
119-
120159
# Call the function with the provided YAML file
121160
render_slides "$1"

0 commit comments

Comments
 (0)