Debian Codegraph @

docs/content/en/content-management/summaries.md

raw
1 ---
2 title: Content summaries
3 linkTitle: Summaries
4 description: Create and render content summaries.
5 categories: []
6 keywords: []
7 aliases: [/content/summaries/,/content-management/content-summaries/]
8 ---
9
10 <!-- Do not remove the manual summary divider below. -->
11 <!-- If you do, you will break its first literal usage on this page. -->
12
13 <!--more-->
14
15 You can define a summary manually, in front matter, or automatically. A manual summary takes precedence over a front matter summary, and a front matter summary takes precedence over an automatic summary.
16
17 Review the [comparison table](#comparison) below to understand the characteristics of each summary type.
18
19 ## Manual summary
20
21 Use a `<!--more-->` divider to indicate the end of the summary. Hugo will not render the summary divider itself.
22
23 ```md {file="content/example.md"}
24 +++
25 title: 'Example'
26 date: 2024-05-26T09:10:33-07:00
27 +++
28
29 This is the first paragraph.
30
31 <!--more-->
32
33 This is the second paragraph.
34 ```
35
36 > [!NOTE]
37 > Place the summary divider on its own line. Do not place it inline with other content.
38
39 Correct placement:
40
41 ```md {file="content/example.md"}
42 ---
43 title: 'Example'
44 ---
45
46 This is an example of **strong text** in a sentence. This is another sentence.
47
48 <!--more-->
49
50 This is another paragraph.
51 ```
52
53 Incorrect placement:
54
55 ```md {file="content/example.md"}
56 ---
57 title: 'Example'
58 ---
59
60 This is an example of **strong text** <!--more--> in a sentence. This is another sentence.
61
62 This is another paragraph.
63 ```
64
65 When using the Emacs Org Mode [content format][], use a `# more` divider to indicate the end of the summary.
66
67 ## Front matter summary
68
69 Use front matter to define a summary independent of content.
70
71 ```md {file="content/example.md"}
72 +++
73 title: 'Example'
74 date: 2024-05-26T09:10:33-07:00
75 summary: 'This summary is independent of the content.'
76 +++
77
78 This is the first paragraph.
79
80 This is the second paragraph.
81 ```
82
83 ## Automatic summary
84
85 If you do not define the summary manually or in front matter, Hugo automatically defines the summary based on the [`summaryLength`][] in your project configuration.
86
87 ```md {file="content/example.md"}
88 +++
89 title: 'Example'
90 date: 2024-05-26T09:10:33-07:00
91 +++
92
93 This is the first paragraph.
94
95 This is the second paragraph.
96
97 This is the third paragraph.
98 ```
99
100 For example, with a `summaryLength` of 7, the automatic summary will be:
101
102 ```html
103 <p>This is the first paragraph.</p>
104 <p>This is the second paragraph.</p>
105 ```
106
107 > [!WARNING]
108 > Automatic `.Summary` may cut block tags (e.g., `blockquote`) in the middle when `summaryLength` is reached, causing the browser to recover the end tag (the end tag will be inserted before the parent's end tag), resulting in unexpected rendering behavior. To avoid this, wrap `.Summary` in a `<div>`; alternatively, wrap it together with the heading tag using `<section>`. You can avoid this entirely by using a manual summary. See issue [#14044][] for details.
109
110 ## Comparison
111
112 Each summary type has different characteristics:
113
114 Type|Precedence|Renders markdown|Renders shortcodes|Wraps single lines with `<p>`
115 :--|:-:|:-:|:-:|:-:
116 Manual|1|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:
117 Front&nbsp;matter|2|:heavy_check_mark:|:x:|:x:
118 Automatic|3|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:
119
120 ## Rendering
121
122 Render the summary in a template by calling the [`Summary`][] method on a `Page` object.
123
124 ```go-html-template
125 {{ range site.RegularPages }}
126 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
127 <div class="summary">
128 {{ .Summary }}
129 {{ if .Truncated }}
130 <a href="{{ .RelPermalink }}">More ...</a>
131 {{ end }}
132 </div>
133 {{ end }}
134 ```
135
136 ## Alternative
137
138 Instead of calling the `Summary` method on a `Page` object, use the [`strings.Truncate`][] function for granular control of the summary length. For example:
139
140 ```go-html-template
141 {{ range site.RegularPages }}
142 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
143 <div class="summary">
144 {{ .Content | strings.Truncate 42 }}
145 </div>
146 {{ end }}
147 ```
148
149 [#14044]: https://github.com/gohugoio/hugo/issues/14044
150 [`Summary`]: /methods/page/summary/
151 [`strings.Truncate`]: /functions/strings/truncate/
152 [`summaryLength`]: /configuration/all/#summarylength
153 [content format]: /content-management/formats/