Collections with subdirectories and URL encoding spaces

Hello! I’m trying to accomplish the task of linking to files within a collection from a post. However, I’m having an issue that relates to the collection having subdirectories, and those subdirectories having spaces in their names.

Here’s a simplified example of how my site source is layed out:

├── 404.html
├── about.md
├── _config.yml
├── Gemfile
├── Gemfile.lock
├── index.md
├── _labs
│   └── Lab 01 Introduction
│       └── PS01 source.html
├── _posts
│   └── 2020-01-01-test_post.md

The post 2020-01-01-test_post.md contains the following markdown:

---
layout: post
title: "Introduction"
author: "Will Hopper"
---

[Link]({{ site.baseurl }}{% link _labs/Lab 01 Introduction/PS01 source.html %})

So far so good, and running jekyll serve succeeds just fine, meaning it can find the linked file at build time. The issue arises later, with the way jekyll has handled the spaces in the Lab 01 Introduction directory. Here is the contents of the _site folder after building:

└── _site
    ├── 404.html
    ├── about
    │   └── index.html
    ├── assets
    │   ├── main.css
    │   └── minima-social-icons.svg
    ├── feed.xml
    ├── index.html
    ├── labs
    │   └── Lab%2001%20Introduction
    │       └── PS01 source.html
    └── posts
        └── test_post.html

As you can see, the spaces in the filename have been replaced with the URL encoded escapes, %20. As a result, the link in the post 404’s.

I tried replacing the “raw” name of the folder with the URL encoded output, i.e., I tried {% link _labs/Lab%2001%20Introduction/PS01 source.html %} but now the site fails to build with Liquid Exception: Could not find document.

In case it’s important, this HTML file is effectively a static file (i.e., it has no front matter).

A curious complement to this, Jekyll seems to leave spaces in the filenames alone? Any ideas on what I could change to get this working correctly, without removing all the spaces?

Thanks in advance for the help!

Have you tried {% link "_labs/Lab 01 Introduction/PS01 source.html" %} ?

Unfortunately it doesn’t help, I get the following error:

Liquid Exception: Could not find document '"_labs/Lab 01 Introduction/PS01 source.html"' in tag 'link'.

From the presence of both the double and single quotes in the error, it looks like the double quotes are being treated as part of the file name.

In general, to me it looks like the problem is with the URL encoding of file names on disk, not URL encoding in the link generated.

I have no idea how that is happening, but it shouldnt be. I’ve never seen files stored on disk pre-encoded like that. URLencoding, by the nature of its name, is for URLs, not file storage.

Spaces arent great for URLs, but they are a thing sometimes, and work perfectly fine.

It should have the spaces in the site folder, and the browser should know how to convert the %20 in the url to request the name with spaces.

If the disk path really IS actually /labs/Lab%2001%20Introduction/PS01 source.html your url will need to encode the % as %25, so the url to that page would be /labs/Lab%252001%2520Introduction/PS01%20source.html

I dont know how that would interact with the {% link %} tag, I’ve never actually used it.