quartz icon indicating copy to clipboard operation
quartz copied to clipboard

Add ability to override entire path via front matter

Open anthops opened this issue 8 months ago • 8 comments

Is your feature request related to a problem? Please describe. My quartz is starting to have quite a few nested folders. My notes will always have unique names so I was hoping for an option to override the entire path so that something like:

https://anthops.github.io/anthopedia/Knowledge/Tools-and-Platforms/Operating-Systems/Linux/Ubuntu/Sound-Issues/Headset-Stuck-on-same-Volume---Ubuntu

would become https://anthops.github.io/anthopedia/headset-volume-stuck-ubuntu

I know I can sort of achieve this with permalinks and aliases. However, I'd like it to not redirect and instead override.

Describe the solution you'd like Hugo currently has the ability to override the path via the font matter like so:

---
title: Example
url: my-example
---

resulting in a URL like https://example.org/my-example. If possible I would like this to be a feature

Describe alternatives you've considered Tried using aliases and permalink but it would redirect instead of replace

Additional context https://gohugo.io/content-management/urls/#url

anthops avatar Apr 10 '25 10:04 anthops

Hey, I noticed you have a rather unique build setup. It seems you use a dockerfile.

You could consider adding a step in the dockerfile to execute a bash script to recursively copy any file under content/ or subdirectories to content/. Something like this:

#!/bin/bash

# Script to copy all files from immediate and nested subdirectories
# into the current directory.

echo "Starting file copy process..."

# find .       : Start searching from the current directory (.).
# -mindepth 1 : Only consider items at least one level deep (i.e., in subdirs).
# -type f     : Find only regular files (not directories, links, etc.).
# -exec cp -v : Execute the 'cp' command for each file found.
#   -v        : Verbose - print the name of each file copied.
#   -n        : No-clobber - do not overwrite an existing file. Remove this
#               if you *want* to overwrite files with the same name.
#   {}        : Placeholder for the found file path.
#   .         : The destination directory (the current directory).
#   \;        : Required terminator for the -exec command.

find . -mindepth 1 -type f -exec cp -vn {} . \;

echo "-------------------------------------"
echo "File copy process finished."
echo "Files were copied without overwriting existing ones (due to -n flag)."
echo "If you saw 'not overwritten' messages, files with the same name already existed here."

exit 0

Given that your file names are unique and you're using shortest as link resolution, this shouldn't break links between pages.

saberzero1 avatar Apr 10 '25 11:04 saberzero1

Thank you for the response! Yeah I guess my build setup is a little unique. I found it to be easier for updating versions and applying customisations haha

Regarding your suggestion, it technically works but I'd like to keep the folder structure while also overriding the url path

anthops avatar Apr 10 '25 12:04 anthops

Thank you for the response! Yeah I guess my build setup is a little unique. I found it to be easier for updating versions and applying customisations haha

Regarding your suggestion, it technically works but I'd like to keep the folder structure while also overriding the url path

I'm not sure if this would work, but you can try setting the permalink to the filepath relative to the content/ directory. (so content/dir/subdir/hello.md, would have permalink: dir/subdir/hello.)

In combination with the script I provided, you would have redirect files at the expected locations and the actual files at the site root. (essentially swapping them.)

I am not sure if permalinks are added to the Explorer component. If they are, we just need to filter any non-index file at the explorer root level to accomplish what you want. If they are not added to the Explorer, let me know, I'll see if I can point to a fix for that.

saberzero1 avatar Apr 10 '25 13:04 saberzero1

Another option might be using the History API, but I'm unsure if it only rewrites history or also the current URL. I'm also unsure how well it interacts with Quartz' SPA implementation.

See live demo and source code.

saberzero1 avatar Apr 10 '25 13:04 saberzero1

Gave the swapped permalink a go but no luck. It definitely permalinks but doesn't swap the explorer elements.

anthops avatar Apr 10 '25 14:04 anthops

Gave the swapped permalink a go but no luck. It definitely permalinks but doesn't swap the explorer elements.

Then I suppose you could alter how the ContentIndex works by adding your frontmatter permalink to it and then modifying the Explorer component to construct its file tree using that instead of slug.

saberzero1 avatar Apr 10 '25 14:04 saberzero1

Slowly getting to a working solution (You can sus it out at https://anthops.github.io/anthopedia/).

Just need to make sure that it works with every component, e.g. still need to get it to work with RecentNotes. A few other things aren't working yet either (e.g. the search). Then need to handle url override collisions and add some tests.

I'll make a PR once I'm done (I'm an ops guy that barely touches frontend-dev so hopefully my typescript is up to scratch 😅)

anthops avatar Apr 21 '25 13:04 anthops

Slowly getting to a working solution (You can sus it out at https://anthops.github.io/anthopedia/).

Just need to make sure that it works with every component, e.g. still need to get it to work with RecentNotes. A few other things aren't working yet either (e.g. the search). Then need to handle url override collisions and add some tests.

I'll make a PR once I'm done (I'm an ops guy that barely touches frontend-dev so hopefully my typescript is up to scratch 😅)

Just a quick thought. You seem to replace any call to file.data.slug with a check for urlOverride. Wouldn't it be easier to do the check in the file.data.slug definition? That way, you have to only make a single change.

Perhaps that is a too simplified approach, just a thought.

saberzero1 avatar Apr 21 '25 19:04 saberzero1