Skip to content

Commit a9a8d4d

Browse files
committed
lychee: avoid problems between --base-url and relative paths
When a HTML file in a subdirectory references a file in the same subdirectory via a relative path (e.g. Pagefind Playground's `<link rel="stylesheet" href="./pagefind-playground.css" />` in `pagefind/playground/index.html`), lychee's `--base-url` option causes unwanted transformations: It pretends that that file is actually at the base URL. For example, running with `--base-url https://dscho.github.io/git-scm.com` will pretend that that CSS is at https://dscho.github.io/git-scm.com/pagefind-playground.css, skipping the `pagefind/playground/` altogether. This is a known issue, and lychee introduced a `--root-dir` option to accommodate. However, there remain problems, see e.g. lycheeverse/lychee#1718. In our concrete case, we cannot easily use `--root-dir` because when we deploy to forks, the absolute paths have a `/git-scm.com/` prefix yet that is not the suffix of the absolute path of the `public/` folder! Even though it is not easily used, it _is_ possible: by constructing a separate path and adding a symbolic link whose name _is_ `git-scm.com` and which points to the `public/` directory. Then the parent directory of that symlink can be used as `--root-dir`. There is still one caveat: lychee ignores symbolic links when populating the initial set of files from a given base directory. So let's pass the path to that symlink, with trailing slash, as the base directory parameter. That seems to work. This change is needed because we are about to enable Pagefind's shiny new Playground, which has an `index.html` file in `/pagefind/playground/` that references a `.css` in the same directory via a relative link (which would be mishandled when using `--base-url`). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 5b16dfd commit a9a8d4d

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

.github/actions/deploy-to-github-pages/action.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,33 @@ runs:
126126
-H "Content-Type: application/json" \
127127
-d '{ "purge_everything": true }'
128128
129-
- name: construct `--remap` options for lychee
129+
- name: construct `--remap` options, root-dir for lychee
130130
id: remap
131131
shell: bash
132132
run: |
133+
# Prepare the root-dir for use with the `--root-dir` option
134+
echo "root-dir-arg=$PWD/root-dir" >>$GITHUB_OUTPUT &&
135+
case "$base_url" in
136+
https://*/?*|http://*/?*)
137+
# The base URL is not at the top-level of the URL
138+
subdir="${base_url#http*://*/}"
139+
target="root-dir/${subdir%/}"
140+
source="$(echo "${subdir%/}" | sed 's/[^/]*/../g')/public"
141+
mkdir -p "${target%/*}" &&
142+
ln -s "$source" "$target" &&
143+
echo "root-dir=$target/" >>$GITHUB_OUTPUT
144+
;;
145+
https://*|http://*)
146+
# The base URL is at the top-level of the URL
147+
ln -s public root-dir &&
148+
echo 'root-dir=root-dir/' >>$GITHUB_OUTPUT
149+
;;
150+
*)
151+
echo "::error::Unexpected base_url '$base_url'" >&2
152+
echo exit 1
153+
;;
154+
esac || exit
155+
133156
echo "result=$(echo "$base_url" |
134157
sed 's|^\(.*\)\(/git-scm\.com\)$|^(\1)?\2(.*)|') file://$PWD/public\$2" \
135158
>>$GITHUB_OUTPUT
@@ -163,7 +186,7 @@ runs:
163186
args: >-
164187
--offline
165188
--fallback-extensions html
166-
--base '${{ env.base_url }}'
189+
--root-dir '${{ steps.remap.outputs.root-dir-arg }}'
167190
--remap '${{ steps.remap.outputs.result }}'
168191
${{ steps.remap.outputs.remap-dotdot }}
169192
${{ steps.remap.outputs.remap-git-scm }}
@@ -174,7 +197,7 @@ runs:
174197
--exclude file:///Pfad/zum/Repo.git/
175198
--exclude file:///chemin/du/d%C3%A9p%C3%B4t.git/
176199
--exclude file:///srv/git/project.git
177-
public/
200+
'${{ steps.remap.outputs.root-dir }}'
178201
output: lychee.md
179202
jobSummary: true
180203
fail: false

0 commit comments

Comments
 (0)