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: handle build.format links in sidebar #1023

Merged
merged 49 commits into from
Nov 17, 2023
Merged

fix: handle build.format links in sidebar #1023

merged 49 commits into from
Nov 17, 2023

Conversation

kevinzunigacuellar
Copy link
Sponsor Member

@kevinzunigacuellar kevinzunigacuellar commented Nov 2, 2023

What kind of changes does this PR include?

  • Changes to Starlight code

Description

  • Closes Setting Astro build.format config breaks sidebar links #180

  • Implemented formatPath function to adjust path formats according to project configurations.

  • Defined conventions for formatting paths:

    • For build.format: "file", the path will conclude with a ".html" extension. For instance, "/reference/" will transform into "/reference.html".
    • For build.format: "directory", the path will include a trailing slash. For instance, "/reference.html" will change to "/reference/".

Copy link

changeset-bot bot commented Nov 2, 2023

🦋 Changeset detected

Latest commit: 596d889

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Nov 2, 2023

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 18b614c
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/65497dc6d95bfa0008d6e75e
😎 Deploy Preview https://deploy-preview-1023--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added 📚 docs Documentation website changes 🌟 core Changes to Starlight’s main package labels Nov 2, 2023
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Nov 2, 2023

size-limit report 📦

Path Size
/index.html 5.12 KB (0%)
/_astro/*.js 19.27 KB (0%)
/_astro/*.css 9.71 KB (0%)

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kevinzunigacuellar! I see build.format: 'file' in the docs to test, but looks like the sidebar in the deploy preview does not use .html. Any idea why?

packages/starlight/__tests__/test-config.ts Outdated Show resolved Hide resolved
packages/starlight/integrations/virtual-user-config.ts Outdated Show resolved Hide resolved
packages/starlight/virtual.d.ts Outdated Show resolved Hide resolved
@kevinzunigacuellar
Copy link
Sponsor Member Author

I see build.format: 'file' in the docs to test, but looks like the sidebar in the deploy preview does not use .html. Any idea why?

Checking the build output it looks that all the href in the sidebar have a .html append to the end. I think it's one of netlify features to strip the .html from the deployed files (Pretty URL’s)

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
@delucis
Copy link
Member

delucis commented Nov 2, 2023

Checking the build output it looks that all the href in the sidebar have a .html append to the end. I think it's one of netlify features to strip the .html from the deployed files (Pretty URL’s)

Ahhhhh! Thank you Netlify 🖖

@kevinzunigacuellar
Copy link
Sponsor Member Author

kevinzunigacuellar commented Nov 2, 2023

I had to make a few assertions for AstroConfig['build'] because Partial only makes the top level properties optional and it expects a fully defined build property to be passed to the user config plugin.

On second thought, we could create a default fully defined build config and override it with opts.

I am open to suggestions

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning up that test config helper. I left a suggestion to avoid the type assertions and then one question.

packages/starlight/integrations/virtual-user-config.ts Outdated Show resolved Hide resolved
packages/starlight/__tests__/test-config.ts Outdated Show resolved Hide resolved
packages/starlight/__tests__/test-config.ts Outdated Show resolved Hide resolved
packages/starlight/utils/navigation.ts Outdated Show resolved Hide resolved
@kevinzunigacuellar
Copy link
Sponsor Member Author

I made a function called formatPathFromConfig that formats any given path using the astro config options (base and build.format). I am not sure where to put it for now its just on navigation utils. And I wrote a few tests with different edge cases. What do you think?

@delucis
Copy link
Member

delucis commented Nov 3, 2023

Nice work!

I am not sure where to put it for now its just on navigation utils.

Might make sense to move it to its own file. We do have path.ts and base.ts, but those both avoid relying on the virtual imports.

The other option would be to refactor it slightly to take the required config as an argument and put it together in path.ts:

/** Add `.html` extension to a path. */
export function ensureHtmlExtension(path: string) {
	if (path.endsWith('.html')) return ensureLeadingSlash(path);
	path = stripLeadingAndTrailingSlashes(path);
	return path ? ensureLeadingSlash(path) + '.html' : '/index.html';
}

export function formatPath(href: string, format: 'file' | 'directory') {
	// atach base
	href = format === 'file' ? fileWithBase(href) : pathWithBase(href);
	// add html extension
	href = format === 'file' ? ensureHtmlExtension(href) : stripExtension(href);
	return href;
}

Then users could import it and pass the format:

import project from 'virtual:starlight/project-context';
import { formatPath } from './path';

formatPath(path, project.build.format);

@kevinzunigacuellar
Copy link
Sponsor Member Author

kevinzunigacuellar commented Nov 3, 2023

I've created a new file for the format function and added trailingSlash as an option, given its relevance to the project configuration.

There's some ambiguity regarding the expected behavior for trailingSlash: "ignore". Should we establish a convention similar to the existing one for adding trailing slashes to links? How might this convention apply when the build format is set to "file," or should we disregard it entirely?

In my view, adopting a convention would simplify the comparison of values for "isCurrent" in the sidebar. The convention I propose is as follows:

  • format: "directory", trailingSlash: "ignore": Retain the trailing slash (similar to the existing setup).

    • "/reference" becomes "/reference/"
    • "/reference.html" becomes "/reference/"
  • format: "file", trailingSlash: "ignore": Omit the trailing slash.

    • "/reference" becomes "/reference.html"
    • "/reference/" becomes "/reference.html"

Establishing this convention would enhance consistency and aid in comparing values for "isCurrent" in the sidebar, offering a clearer and more predictable structure for the project. What do you think? @delucis @HiDeoo

PS: When reviewing the test file for formatPath, kindly disregard the tests related to the trailing slash set to "ignore". I accidentally duplicated the expected values from other tests 😅. The rest of the test sets should be accurate and reliable.

@delucis
Copy link
Member

delucis commented Nov 16, 2023

Hey @kevinzunigacuellar! Turned out this was fiddlier than I expected, so I spent some time getting everything polished up.

Quick summary of the changes I made:

  • Wire up a project’s trailingSlash option with the virtual module and apply it when formatting paths.
  • Move around some of the logic about when slashes are added/removed to ensure that with trailingSlash: 'ignore' (the Astro default) we don’t modify trailing slashes on user-specified links.
  • Make sure we never add a trailing slash when build.format is set to file — I’m pretty sure /example.html should never be /example.html/.
  • Fixed checks for whether a sidebar link was for the current page by always formatting the comparison links with a formatter that sets trailingSlash: 'always' so they will be consistent.
  • Made the formatPath() helper easier to use in Starlight code by exporting it already configured with the project settings. Formatters with different settings can be created with the createPathFormatter() factory method.

Would love a quick look if you have time and thanks for the patience waiting for me on this one!

@delucis delucis added the 🌟 minor Change that triggers a minor release label Nov 16, 2023
Copy link
Sponsor Member Author

@kevinzunigacuellar kevinzunigacuellar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @delucis for the review and changes. Yes, this PR started very simple but it got very complex very quickly 😅. I left a couple comments, feel free to disregard the nit comments btw.

Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
@kevinzunigacuellar
Copy link
Sponsor Member Author

Lost Glasses, Troublesome Moment! ✅

@delucis delucis merged commit a3b80f7 into main Nov 17, 2023
6 checks passed
@delucis delucis deleted the kev-build-format branch November 17, 2023 18:30
@astrobot-houston astrobot-houston mentioned this pull request Nov 17, 2023
@delucis delucis mentioned this pull request Nov 22, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package 📚 docs Documentation website changes 🌟 minor Change that triggers a minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting Astro build.format config breaks sidebar links
3 participants