gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
74.71k stars 7.45k forks source link

Add .Markup page variable #8869

Closed helfper closed 5 months ago

helfper commented 3 years ago

Adding this issue for my PR #8191 to see if it finally gets either merged or closed, because it seems PRs without issues now are just ignored. (It seems there are not many fans of Linus Torvalds' "Talk is cheap, show me the code" around here :slightly_smiling_face:)

My use case for it is only include Asciidoctor-specific stylesheets in my theme for pages generated with Asciidoctor:

{{- if eq .Page.Markup "asciidocext" }}
<link href="{{"css/asciidoctor.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
<link href="{{"css/v4-shims.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
{{- end }}
jmooring commented 3 years ago

While I find your choice of words unfortunate, I am curious why this is insufficient:

{{ default .File.Ext .Params.markup }}
bep commented 3 years ago

@helfper thanks for the reminder, I will try to improve and try to close PRs like this without a corresponding accepted issue. I do, however don't see enough value to this proposal. But I'll leave it open for others to chime in.

Oh, and I'm pretty sure Linus Torvalds didn't refer to adding a simple getter to a struct.

jmooring commented 3 years ago

The Hugo forum (https://discourse.gohugo.io/) contains over 34,000 topics created over the past 7 years. I was able to find only 2 related topics, both resolved with a .File.Ext check.

But since there are a few different file extensions for the various markup formats, I find this pretty useful to eliminate the need to check for each one.

This is a trivial exercise with a partial.

I recommend closing this issue.

helfper commented 3 years ago

It can definitely be worked around, but it feels hacky for a theme to have to duplicate the logic in Hugo that determines the markup format, as well as having to keep track of any new file extensions that Hugo may decide to associate with a format in the future. (For example, currently .asciidoc is processed as Markdown in Hugo, so if that gets fixed, will have to add one more file extension below.) While there is already a canonical representation of it in Hugo with the markup variable.

Even just for Asciidoctor, it already looks quite cumbersome:

{{- $markup := .Params.markup }}
{{- if not $markup }}{{ with .File }}{{ $markup = .Ext }}{{ end }}{{ end }}
{{- if or (eq $markup "asciidocext") (or (eq $markup "adoc") (eq $markup "ad")) }}
It's AsciiDoc!
{{- end }}

Instead of:

{{- if eq .Markup "asciidocext" }}
It's AsciiDoc!
{{- end }}

Also, adding .Markup as page variable would be consistent with the already existing duality between many page variables and front matter fields (.Aliases/aliases, .Draft/draft, .Keywords/keywords, etc.).

nancym commented 3 years ago

Thanks for that code @braderhart! I've adapted it to this in some of my shortcodes:

{{ $isMd := false }}

{{ with .Page.File }}
  {{ $isMd = in (slice "blackfriday" "goldmark" "markdown" "md" "mdown" "mmark" "pandoc" "pdc") (default $.Page.Params.markup .Ext) }}
{{ end }}

{{ if $isMd }}
It's a Markdown flavor!
{{ end }}

I use this in my endnote-title and see-also shortcodes, both of which are called with {{% ... %}} (notice the %) and need to use, for example, one of the following for the section heading:

Which type of heading used depends on the markup language used in the calling page (for me that's either AsciiDoc, a Markdown flavor, or HTML). I would love to have what @helfper proposed. If this is implemented, please provide an identifier than means "any of the Markdown flavors Hugo supports" maybe "markdownish".

helfper commented 3 years ago

The default should be the other way around like @jmooring wrote: default .Ext $.Page.Params.markup. The markup field should take precedence. I find the pipeline version easier to read though and in case of a shortcode you may be able get away without the with as it's always in a file (I think?).

{{ $isMd := .Page.Params.markup | default .Page.File.Ext | in (slice "blackfriday" "goldmark" "markdown" "md" "mdown" "mmark" "pandoc" "pdc") }}

In my case I needed the with .File (or perhaps could just have added it to the if to simplify) because the partial seems to be used in a "synthetic" page without a source file and I was getting the famous warning about the zero file (.File.Ext on zero object. Wrap it in if or with: {{ with .File }}{{ .Ext }}{{ end }}). I wonder if it would be worth it to pass the metaProvider (or at least the kind and title) in the zero file constructor, so that it could be added to the log to make it easier to figure out which page caused it: https://github.com/gohugoio/hugo/blob/f55d2f43769053b80b419a690554e747dc5dcede/hugolib/page__new.go#L103-L105

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

github-actions[bot] commented 7 months ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

github-actions[bot] commented 4 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.