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

normalize links #1037

Merged
merged 12 commits into from Mar 12, 2024
Merged

normalize links #1037

merged 12 commits into from Mar 12, 2024

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Mar 11, 2024

This introduces a cleanUrls option (boolean, defaults to true) that indicates whether links to pages should end with ".html" (except for directories, where the convention is that the file dir/index.html is served with the dir/ path).

This option is subsumed in the definition of config.md.normalizeLinks, which is applied to all links generated by Markdown text and now also by links generated in the sidebar, in the pager, and in the search results. (It is not applied to links that are written directly in HTML.)

closes #92

based on #1034

This could also help re-implement a normalization of the pager (#689 #1022)

@Fil
Copy link
Contributor Author

Fil commented Mar 11, 2024

observable deploy tested by manually deploying with this option on.

Base automatically changed from fil/config-md to main March 11, 2024 23:07
src/markdown.ts Outdated
export function mdparser({markdownIt}: {markdownIt?: (md: MarkdownIt) => MarkdownIt} = {}): MarkdownIt {
function cleanPath(href: string, clean: boolean): string {
if (href.startsWith("./") || href.startsWith("../")) {
const g = href.match(/(?<dir>[^#?]*\/)((?<file>[^/?#]*?)([.]html)?)(?<q>[?#].*)?$/)?.groups;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should use URL to parse, and dirname, basename, extname? Regular expressions…

src/preview.ts Outdated
return;
path = path.slice(0, -".html".length);
pathname = pathname.slice(0, -".html".length);
if (config.md.normalizeLink("./hello") === "./hello") {
Copy link
Member

Choose a reason for hiding this comment

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

This seems a little icky. Wouldn’t it be better to check the value of config.md.normalizeLink(path) instead, and then redirect to the normalized value? Otherwise we’re essentially duplicating the logic of normalizeLink and assuming its behavior. Or if we want to assume its behavior, then we should expose config.cleanUrls instead of trying to invert it by calling md.normalizeLink.

src/search.ts Outdated
@@ -81,7 +81,8 @@ export async function searchIndex(config: Config, effects = defaultEffects): Pro
options: {
fields: indexOptions.fields,
storeFields: indexOptions.storeFields
}
},
cleanUrls: config.md.normalizeLink("./hello") === "./hello"
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above. If we expect md.normalizeLink to be overridden and have some other behavior, then we need to pass the normalized links to minisearch so that it renders normalized links on the client. On the other hand, if we only want to support our more limited definition of cleanUrls, then we shouldn’t test the behavior of md.normalizeLink; we should explicitly track the setting in the config.

I guess it’d be cool to respect the implementation of md.normalizeLink, if it’s not too much work. Perhaps we could invoke it up above when computing the id rather than having the client do the normalization and repeating the logic?

@mbostock
Copy link
Member

I moved all the normalization to the server-side and got rid of the ./hello test. I also changed the preview server to respect the normalizeLink implementation, which simplified things quite a bit! 👍

@Fil Fil merged commit 106c0f5 into main Mar 12, 2024
4 checks passed
@Fil Fil deleted the fil/normalize-links branch March 12, 2024 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants