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

hugo: Fix anchor links #468

Closed

Conversation

JorindeUsMedia
Copy link
Collaborator

@JorindeUsMedia JorindeUsMedia commented Jul 19, 2024

  • Add example of anchor link using ref to the example/ref page (this is not
    mandatory to use this way but it can be used like this)
  • Add working example to Example - Text on how to use custom-id's.
  • Fix smoothscroll of anchor links that start with the path of the current
    page (instead of starting with #)
  • Change markdownify to $.Page.RenderString so anchor links get rendered
    correctly (recommended by hugo for more complicated markdown which uses the
    Page context such as anchor links, see highlighted block on https://gohugo.io/functions/transform/markdownify/)
    . Only tabs still uses markdownify because for some reason RenderString does
    not work here because it's a partial inside a shortcode.
  • Change links in example/columns to '/' instead of a non-existing anchor link

To test:
Go to /examples and test if all the shortcodes are still working as expected.

Closes cue-lang/cue#2971

Copy link

netlify bot commented Jul 19, 2024

👷 Deploy request for cue pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit d58c3aa

Copy link
Collaborator

@jpluscplusm jpluscplusm left a comment

Choose a reason for hiding this comment

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

@JorindeUsMedia I don't think this PR leaves us in a state where either cue-lang/cue#2971 is fixed; or where we understand what we have to do to avoid it; and where we have on-page examples of things working "correctly" to review and cross-check in the future.

cue-lang/cue#2971 is solely concerned with links to fragments on the same page, when placed inside columns or info/warning/caution blocks. If this issue is demonstrating a workaround for that problem, please can we place some examples on page that show it being fixed (with the use of relref?).

If this issue isn't demonstrating that, then I'm not sure of its purpose, and I don't think it should close cue-lang/cue#2971.

@@ -23,3 +24,20 @@ The result will be just like a link. But the difference is that with ref/relref

[Buttons example]({{< relref "buttons/index.md" >}})

Relref can also be used for anchor links:
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I'm interpretting this change correctly, please add a note that same-page links to a fragment "must" be invoked via ref/relref, if they're to work consistently.

Relref can also be used for anchor links:

```
[Anchor links example]({{<ref "#anchor-header" >}})
Copy link
Collaborator

Choose a reason for hiding this comment

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

This example renders oddly, negating the point that I think you're trying to make (that relref must be used, and not just bare fragments inside a Markdown link):

image

@JorindeUsMedia
Copy link
Collaborator Author

@jpluscplusm It seems Hugo adds the baseUrl in front of all links: also anchor links.
See https://discourse.gohugo.io/t/link-with-anchor-has-leading-slash/30391
They suggest to use relref instead. I did some more googling but could not find a solution for hugo/goldmark not adding the baseUrl to anchor links.

@JorindeUsMedia
Copy link
Collaborator Author

@jpluscplusm what do I need to do in order to get this pr closed? Mention that is must be used on the examples page?

@jpluscplusm
Copy link
Collaborator

@jpluscplusm what do I need to do in order to get this pr closed? Mention that is must be used on the examples page?

I think an explicit mention on the example page that linking to an anchor on the same page requires relref, with an example of it being used successfully inside the set of shortcodes that are mentioned in the linked issue: columns and info, at a minimum.

@JorindeUsMedia
Copy link
Collaborator Author

JorindeUsMedia commented Aug 16, 2024

Hm it seems even with using ref instead of regular links there still seems to be an issue with Hugo (or Goldmark) adding the baseUrl in front of the anchor links when used inside of shortcodes.. I'll have to dive into the issue again.

@JorindeUsMedia
Copy link
Collaborator Author

JorindeUsMedia commented Aug 16, 2024

I think I found it:

https://gohugo.io/functions/transform/markdownify/ >

Although the markdownify function honors Markdown render hooks when rendering Markdown to HTML, use the RenderString method instead of markdownify if a render hook accesses .Page context. See issue #9692 for details.

gohugoio/hugo#9692

I think we need to use

{{- $.Page.RenderString .Inner -}}
instead of

{{- .Inner | markdownify -}}

I will check in which components we use markdownify and change it. I will create a new pr for that

@jpluscplusm
Copy link
Collaborator

Hey @JorindeUsMedia - I've just hit this issue when trying to link to a same-page fragment from inside a caution block. Is there a workaround that works with the current site, other than hardcoding a link to the absolute path of the current page?

@JorindeUsMedia JorindeUsMedia changed the title hugo: Add example of anchor link usage hugo: Fix anchor links Sep 3, 2024
@JorindeUsMedia
Copy link
Collaborator Author

Hey @JorindeUsMedia - I've just hit this issue when trying to link to a same-page fragment from inside a caution block. Is there a workaround that works with the current site, other than hardcoding a link to the absolute path of the current page?

I've found a solution: see my last comment. I also implemented it but didn't have time to test it yet. I've just updated the pr

@jpluscplusm
Copy link
Collaborator

I can see changes to several different elements that we use throughout the site.

I'm just writing this as a marker that I've seen this PR, but need to find a bit of time to sit down and compare some before-and-after uses of the elements that have been updated :-)

cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this pull request Nov 4, 2024
A manual preview of PR #486
(cue-lang/cuelang.org#468) as the Netlify
previews are currently unhappy.

Change-Id: I31682a0bccc87ff44347bfa9612c6d0ad54622c5
Signed-off-by: Jorinde Reijnierse <[email protected]>
Dispatch-Trailer: {"type":"trybot","CL":1203530,"patchset":1,"ref":"refs/changes/30/1203530/1","targetBranch":"master"}
@jpluscplusm
Copy link
Collaborator

I've started CI manually, here, as a change we made recently has made the Netlify preview bot unhappy. This is a just a temporary work around until we fix the bot.

Copy link
Collaborator

@jpluscplusm jpluscplusm left a comment

Choose a reason for hiding this comment

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

Looks generally good - just a few questions/etc.

I'd like to grab a brief chat to understand the core of what's changing here, so that I can make sure we're marking up anchor links correctly.

content/examples/shortcodes/columns/en.md Outdated Show resolved Hide resolved
content/examples/basic/text/en.md Outdated Show resolved Hide resolved
content/examples/basic/text/en.md Show resolved Hide resolved
content/examples/shortcodes/columns/en.md Outdated Show resolved Hide resolved
content/examples/shortcodes/ref-relref/en.md Show resolved Hide resolved
hugo/layouts/shortcodes/step.html Show resolved Hide resolved
hugo/layouts/shortcodes/table.html Show resolved Hide resolved
hugo/layouts/shortcodes/warning.html Show resolved Hide resolved
content/examples/shortcodes/ref-relref/en.md Outdated Show resolved Hide resolved
content/examples/shortcodes/spotlight/en.md Show resolved Hide resolved
- Add example of anchor link using ref to the example/ref page (this is not
mandatory to use this way but it can be used like this)
- Add working example to Example - Text on how to use custom-id's.
- Fix smoothscroll of anchor links that start with the path of the current
page (instead of starting with #)
- Change markdownify to $.Page.RenderString so anchor links get rendered
correctly (recommended by hugo for more complicated markdown which uses the
Page context such as anchor links, see highlighted block on https://gohugo.io/functions/transform/markdownify/)
. Only tabs still uses markdownify because for some reason RenderString does
not work here because it's a partial inside a shortcode.
- Change links in example/columns to '/' instead of a non-existing anchor link

To test:
Go to /examples and test if all the shortcodes are still working as expected.

Closes cue-lang/cue#2971

Signed-off-by: Jorinde Reijnierse <[email protected]>
@jpluscplusm
Copy link
Collaborator

Preview available at https://cl-1203530-2--cue-cls.netlify.app/.

@JorindeUsMedia I've resolved all the threads I feel able to - please could you resolve the rest, and then I'll know it's good for final review? Thank you!

@JorindeUsMedia
Copy link
Collaborator Author

@JorindeUsMedia I've resolved all the threads I feel able to - please could you resolve the rest, and then I'll know it's good for final review? Thank you!

All resolved :)

Copy link
Collaborator

@jpluscplusm jpluscplusm left a comment

Choose a reason for hiding this comment

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

LGTM!

@jpluscplusm
Copy link
Collaborator

Imported as https://cuelang.org/cl/1203530.

@cueckoo cueckoo closed this in 3416954 Nov 12, 2024
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