gohugoio / hugo

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

{{ .Summary }} includes unwanted text in certain circumstances #1503

Open madranet opened 8 years ago

madranet commented 8 years ago

When using {{ .Summary }} within a template, traditional markdown image tags [and their associated title and alt attributes] are ignored when the summary is created. So in a post beginning with:

![Beam me up Scotty!](/images/kirk.jpg "The Captain")

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

The summary is correctly rendered as:

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

However, if the {{% figure %}} shortcode is used to add images:

{{% figure src="/images/kirk.jpg" alt="Beam me up Scotty!" title="The Captain" %}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

Then the title attribute is also included in the generated summary:

The Captain Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

This is also the case when caption is used:

{{% figure src="/images/kirk.jpg" alt="Beam me up Scotty!" caption="The Captain" %}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

also produces:

The Captain Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

Interestingly, when both title and caption are used, only the content of the caption attribute is included in the generated summary.

Also, if a post begins with a list, this is included in the generated summary as well:

* Blah Blah
* Waffle Waffle
* Drone Drone

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

Generates the following summary:

Blah Blah Waffle Waffle Drone Drone Lorem ipsum dolor sit amet, consectetur adipiscing elit.

The same happens when a post begins with ## mardown headers ##. The content of these is also included in the generated summary.

All of the above make summaries generated by {{ .Summary }} quite messy, when posts do not start with a paragraph of text.

SUGGESTION: {{ .Summary }} should begin summarising from the first encountered <p> tag and should strip out any content between non text-markup tags.

mattwoolnough commented 8 years ago

+1. Code blocks turn out quite poorly also. Not formatted in any way. Strip the code blocks out as suggested, or just stop the summary at the start of the code.

erichiller commented 8 years ago

+1 Agreed- this seems the perfect solutions -- @mattwoolnough "Strip the code blocks out as suggested, or just stop the summary at the start of the code."

qw3ry commented 7 years ago

I have a similar issue: My page is starting with a header: # some title and this header is included in .Summary, but only if I use the <!--more--> somewhere in the text.

This problem makes the "<!--more-->-feature" quite useless for me as the resulting web page looks awful.

madranet commented 7 years ago

Disappointed to see this still hasn't been addressed as of version 0,18

$ hugo version
Hugo Static Site Generator v0.18.1 BuildDate: 2016-12-29T17:12:41Z

2017-01-03_14-05-19

ghost commented 7 years ago

I for one appreciate the simplicity of the automatic summary split. Its constraints have moved me to improve my layouts by refactoring items from the content more aptly expressed in front matter.

stale[bot] commented 6 years 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.

kleinschmidt commented 5 years ago

This is still an issue: including a fenced code block in the summary is, IMHO, lunacy. Perhaps an option to only include the first <p> would help?

ghost commented 4 years ago

Is this issue not a point of concern anymore? It's been more than 4 years since this issue was opened. The summary looks messed up no matter which theme I use if I start my blog post with a title or other elements other than <p>.

jtooker commented 4 years ago

I agree with @pointfourone. The "automatic" summary works well at removing images and tables, but it leaves headers in. If it ignored headers too (or if there was a site configuration variable to omit them), I think it would work quite well for me. Without it, I'm forced to copy the first paragraph or so into my front matter.

madranet commented 4 years ago

I agree with @pointfourone. The "automatic" summary works well at removing images

Summary doesn't remove images in all circumstances. See my original issue posting at the top of the thread.

khayyamsaleem commented 4 years ago

Could there be a solution where you could use something like a "summary-ignore" shortcode to mark off content that you want to exclude from a summary?

khayyamsaleem commented 4 years ago

envisioning something like:

{{% summary-ignore %}}
{{% figure src="/images/kirk.jpg" alt="Beam me up Scotty!" caption="The Captain" %}}
{{% /summary-ignore %}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]
musm commented 4 years ago

Encountering the same problem here

AngelCrawford commented 4 years ago

Same Problem. Hoped I could do a workaround with the "replaceRE" Function and Regex, but it didn't worked >.<

AngelCrawford commented 3 years ago

As a workaround, I now create my own Summary-Split with a comment.

{{ $summarySplits := split .Content "<!--start-summary-->" }}
{{ if ge (len $summarySplits) 2 }}
    <!-- If content has the comment "start-summary", start there -->
    {{ index $summarySplits 1 | plainify | truncate 270 }}
{{ else }}
    {{ .Summary| markdownify | truncate 270 " [...]" }}
{{ end }}

In my index.md, where the content is at, I add the comment at my first <p>-Tag.

willfaught commented 1 year ago

I'm seeing shortcode syntax appearing in .Summary:

Basic Code: {{< paige/vimeo "644036051" >}} Result: Video parameter Code: {{< paige/vimeo video="644036051" >}} Result: All parameters Code:
frankenstein91 commented 3 months ago

envisioning something like:

{{% summary-ignore %}}
{{% figure src="/images/kirk.jpg" alt="Beam me up Scotty!" caption="The Captain" %}}
{{% /summary-ignore %}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit...[etc]

would love it