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

Discussion: Reenable link rewriting for colocated assets #1779

Closed

Conversation

timakro
Copy link
Contributor

@timakro timakro commented Feb 24, 2022

In #1582 I broke the asset colocation example from the documentation:
https://www.getzola.org/documentation/content/overview/#asset-colocation

The breakage was discovered by irimi1:
#1582 (comment)

When I wrote #1582 I was not aware of the old behavior: links to assets
used to be rewritten to absolute links, so that colocated assets could
be linked to from pagename/index.md simply as asset.xyz instead of
pagename/asset.xyz. I unintentionally changed this, so that currently
you have to write pagename/asset.xyz.

Read this commit as a partial revert of #1582. It restores the old
behavior as described above.

Caveats:

  1. This will break lots of sites, just like Support colocating subfolders #1582 unintentionally broke
    lots of sites (but no one seemed to notice).
  2. The old behavior is restored, including the quirk, that when linking
    from a page pagename.md to a section asset asset.xyz you need to
    write ../asset.xyz, even though both files are in the same folder
    on disk. You can make sense of it in the following way: the asset
    belongs to the section so that is one level up from the page.

@Irimi1

Please discuss this before merging. Some might prefer the current behavior over the old behavior.

In getzola#1582 I broke the asset colocation example from the documentation:
https://www.getzola.org/documentation/content/overview/#asset-colocation

The breakage was discovered by irimi1:
getzola#1582 (comment)

When I wrote getzola#1582 I was not aware of the old behavior: links to assets
used to be rewritten to absolute links, so that colocated assets could
be linked to from `pagename/index.md` simply as `asset.xyz` instead of
`pagename/asset.xyz`. I unintentionally changed this, so that currently
you have to write `pagename/asset.xyz`.

Read this commit as a partial revert of getzola#1582. It restores the old
behavior as described above.

Caveats:
1. This will break lots of sites, just like getzola#1582 unintentionally broke
   lots of sites (but no one seemed to notice).
2. The old behavior is restored, including the quirk, that when linking
   from a page `pagename.md` to a section asset `asset.xyz` you need to
   write `../asset.xyz`, even though both files are in the same folder
   on disk. You can make sense of it in the following way: the asset
   belongs to the section so that is one level up from the page.
@southerntofu
Copy link
Contributor

#1582 unintentionally broke lots of sites (but no one seemed to notice)

Hello i definitely noticed and had to work around it . Sorry i didn't catch it earlier that's because i don't have time to update my sites all of the time, but i'll soon setup an automated CI to ensure that my sites continue to build against zola next branch.

I personally believe rewriting those URLs is a good thing as it's consistent with the folder view. To be fair, path rules are a fairly complex topic and there's been long debates about how to make them more consistent. I personally haven't followed the conclusions of those debates (i'll get up to speed), but i've personally advocated for searching for simpler rules for path resolution which would support all the types of links we need: standard HTML links (relative/absolute with [text](link or /link)) as well as links from the zola site root ([text](@/.../page.md)) or even relative to the current page ([text](@asset_relative_to_markdown.png))

I'm not sure about the best syntax or the best choice of building blocks, but i'm sure there's currently a lot of corners we're cutting: for example how can i link to some resource from the site root from markdown? an absolute URL will only work with sites living on the webroot of the domain, so i had to wrap that into a shortcode calling get_url which is not the most intuitive.

@Keats
Copy link
Collaborator

Keats commented Feb 27, 2022

I personally haven't followed the conclusions of those debates (i'll get up to speed)

That's the status in the end: https://github.com/getzola/zola/blob/master/components/templates/src/global_fns/helpers.rs#L7 but for now it only applies in Tera functions, not in the markdown rendering.

@Keats
Copy link
Collaborator

Keats commented Mar 4, 2022

Any idea on how to solve this neatly @southerntofu ?

@@ -62,6 +70,11 @@ fn find_anchor(anchors: &[String], name: String, level: u16) -> String {
find_anchor(anchors, name, level + 1)
}

/// Colocated asset links refers to the files in the same directory.
fn is_colocated_asset_link(link: &str) -> bool {
!link.starts_with('/') && !link.starts_with('#') && !STARTS_WITH_SCHEMA_RE.is_match(link)

Choose a reason for hiding this comment

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

Does it make sense to consider ../ as well? Those assets also wouldn’t be colocated, but I don’t know if those links could occur while using zola in a reasonable way.

Copy link
Contributor Author

@timakro timakro Mar 6, 2022

Choose a reason for hiding this comment

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

For consistency we want to change links starting with ../ as well. Imagine you want to link to section/asset.png from section/page/index.md, then you would write ../asset.png. That would be rewritten to section/page/../asset.png, which is correct. Without link rewriting the link would be broken.

Choose a reason for hiding this comment

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

Okay, makes sense. What exactly is '/' for then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/ is for absolute links. We should probably prepend base_url, but not the page permalink.

Choose a reason for hiding this comment

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

Okay, thanks for explaining! :)

@manu-cyber
Copy link

The old behaviour makes more sense for me since when creating a page using pagename.md, a subdirectory pagename is created nonetheless. To me having to use a quirk for referencing parent section assets for pages created using somewhat of a “shorthand” does not way as heavy as not being able to use subpage assets at all. But then again, I might just be one of the very few people actually making use of this.

@Yaroslav-95
Copy link
Contributor

I just recently decided to try to fix the problem I had in #1705 and was about to make a PR when I stumbled upon this. In my use-case it also makes sense to rewrite the links as absolute, since I need them to be displayed in other parts of the webpage, e.g. as part of previews/summaries of posts. Unfortunately it was broken by #1582, like other use-cases of this functionality as it seems from this discussion.

@Keats
Copy link
Collaborator

Keats commented May 11, 2022

Ok I think this is the last big thing to solve for 0.16.

Does this patch satisfy everyone?

@Keats
Copy link
Collaborator

Keats commented Jun 9, 2022

ping @Irimi1 @timakro @southerntofu @Yaroslav-95 to confirm, not 100% sure what the emoji reactions mean :)

@Yaroslav-95
Copy link
Contributor

Yaroslav-95 commented Jun 9, 2022 via email

@manu-cyber
Copy link

ping @Irimi1 @timakro @southerntofu @Yaroslav-95 to confirm, not 100% sure what the emoji reactions mean :)

I’m also happy with that patch :)

Keats added a commit that referenced this pull request Jun 10, 2022
@Keats
Copy link
Collaborator

Keats commented Jun 10, 2022

This is added to the next branch.

@Keats Keats closed this Jun 10, 2022
@Keats
Copy link
Collaborator

Keats commented Sep 21, 2022

I think we forgot non-images while fixing that? See #1993 and 19b3376 for the fix.

@Keats
Copy link
Collaborator

Keats commented Oct 11, 2022

Just a quick ping @Irimi1 @timakro @Yaroslav-95 to make sure it works for you. I'm getting confused again with that :)

@Yaroslav-95
Copy link
Contributor

Yaroslav-95 commented Oct 11, 2022 via email

@manu-cyber
Copy link

Just a quick ping @Irimi1 @timakro @Yaroslav-95 to make sure it works for you. I'm getting confused again with that :)

Working for me, too :)

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

Successfully merging this pull request may close these issues.

5 participants