Skip to content

Commit b83de12

Browse files
committed
simplify sed operations
1 parent 72306a7 commit b83de12

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

Dockerfile

+20-14
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ COPY . .
1010
# Hugo base url
1111
ARG HUGO_BASEURL="https://tvhsfrc.org"
1212

13-
RUN HUGO_BASEURL=$HUGO_BASEURL hugo --environment production
14-
15-
RUN find public -type f -name "*.html" -exec sed -i "s|src=.*/img/dragons.png|src=\"/img/dragons.png|g" {} \;
16-
RUN find public -type f -name "*.html" -exec sed -i "s|src=.*/img/logo.png|src=\"/img/logo.png|g" {} \;
17-
RUN find public -type f -name "*.html" -exec sed -i "s|class=\"logo-link\" href=\".*\"|class=\"logo-link\" href=\"$HUGO_BASEURL\"|g" {} \;
18-
RUN find public -type f -name "*.html" -exec sed -i "s|href=.*/support|href=\"$HUGO_BASEURL/support|g" {} \;
19-
20-
RUN find public -type f -name "*.html" -exec sed -i "s|src=\"/img|src=\"$HUGO_BASEURL/img|g" {} \;
21-
RUN find public -type f -name "*.html" -exec sed -i "s|href=\"/css|href=\"$HUGO_BASEURL/css|g" {} \;
22-
RUN find public -type f -name "*.html" -exec sed -i "s|src=\"/js|src=\"$HUGO_BASEURL/js|g" {} \;
23-
RUN find public -type f -name "*.html" -exec sed -i "s|href=\"/fonts|href=\"$HUGO_BASEURL/fonts|g" {} \;
24-
25-
RUN find public -type f -name "*.css" -exec sed -i "s|url(/img|url($HUGO_BASEURL/img|g" {} \;
26-
RUN find public -type f -name "*.css" -exec sed -i "s|url(/fonts|url($HUGO_BASEURL/fonts|g" {} \;
13+
RUN hugo --environment production
14+
15+
# Remove the entire BaseURL from all hrefs, srcs, and urls.
16+
RUN find public -type f \( -name "*.html" -o -name "*.css" \) -print -exec sed -i "s|$HUGO_BASEURL||g" {} \;
17+
# Extract the _path_ from the Base URL, `https://example.com/some/path` -> `/some/path`
18+
RUN HUGO_BASEURL_PATH=$(echo $HUGO_BASEURL | sed -n 's|https://[^/]*\(.*\)|\1|p') && \
19+
echo $HUGO_BASEURL_PATH && \
20+
find public -type f \( -name "*.html" -o -name "*.css" \) -print -exec sed -i "s|$HUGO_BASEURL_PATH||g" {} \;
21+
# Extract the scheme and domain from the Base URL, `https://example.com/path/` -> `https://example.com`
22+
RUN HUGO_BASEURL_SCHEME_DOMAIN=$(echo $HUGO_BASEURL | sed -n 's|\(https://[^/]*\).*|\1|p') && \
23+
echo $HUGO_BASEURL_SCHEME_DOMAIN && \
24+
find public -type f \( -name "*.html" -o -name "*.css" \) -print -exec sed -i "s|$HUGO_BASEURL_SCHEME_DOMAIN||g" {} \;
25+
26+
# Add back the BaseURL to all hrefs, srcs, and urls
27+
RUN find public -type f \( -name "*.html" -o -name "*.css" \) -exec sed -i "s|href=\"/|href=\"$HUGO_BASEURL/|g" {} \;
28+
RUN find public -type f \( -name "*.html" -o -name "*.css" \) -exec sed -i "s|src=\"/|src=\"$HUGO_BASEURL/|g" {} \;
29+
RUN find public -type f \( -name "*.html" -o -name "*.css" \) -exec sed -i "s|url(\"/|url(\"$HUGO_BASEURL/|g" {} \;
30+
31+
# Remove integrity attributes from script and link tags
32+
RUN find public -type f -name "*.html" -exec sed -i "s| integrity=\"[^\"]*\"||g" {} \;
2733

2834
FROM nginx:alpine
2935

0 commit comments

Comments
 (0)