Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where .standAloneFiles doesn't work with section indexes #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

atomicbird
Copy link

This PR fixes a bug in the current code where using .standAloneFiles produces broken links on generated section pages.

The bug this addresses

You can reproduce the bug as follows:

  1. Create a new site:
mkdir testsite
cd testsite
publish new
  1. Edit main.swift so that the publishing code looks like this:
try Testsite().publish(using: [
    .addMarkdownFiles(),
    .generateHTML(withTheme: .foundation, fileMode: .standAloneFiles)
])
  1. Build the site. Serve it using either publish run or python3 -m http.server 8000

  2. Navigate to http://localhost:8000/posts/ and click on the "My first post" link.

The link is broken. It points to http://localhost:8000/posts/first-post, but since HTML generation used the .standAloneFiles mode, there's no index.html at that path.

At first this seems the problem is that the foundation theme doesn't support .standAloneFiles, which would be fine. However it's impossible for a theme to discover the file mode, so it's impossible to fix this at the theme level. No theme can find the correct path in this case.

The fix

I'm addressing this by moving file mode to Website so that it will be more widely available:

  • Website now includes var fileMode: HTMLFileMode, and an extension defaults this to .foldersAndIndexFiles.

  • In generateHTML(...), the fileMode argument now defaults to nil instead of .foldersAndIndexFiles. The HTMLGenerator instance will use fileMode if it's provided, which means existing code will still work. If file mode is not provided, it uses the site's value described above.

  • Item has a new func linkPath(for site:Site) -> String that provides the correct destination for the item based on the site's file mode.

  • The foundation theme has been modified to use this new function on Item, to demonstrate how a theme can make use of this functionality.

Unit tests have also been updated.

The effect of all of this is that in the reproduction steps above

  • If . foldersAndIndexFiles is used, the link to "My first post" is still http://localhost:8000/posts/first-post
  • If .standAloneFiles is used, the link is now http://localhost:8000/posts/first-post.html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant