Skip to content

Commit b3e4ccf

Browse files
authored
A better alert shortcode more markdown friendly (#941)
1 parent 433ddb6 commit b3e4ccf

File tree

8 files changed

+155
-52
lines changed

8 files changed

+155
-52
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ For the full list of changes, see the [0.x.y] release notes.
2626

2727
**Breaking changes**:
2828

29+
- **Shortcodes**:
30+
- The alert shortcode has been reworked to address [#906] and [#939]. It can
31+
now be used with Markdown content, and it can contain calls to other
32+
shortcodes. For details, see [Shortcode helpers > alert][] ([#941])
2933
- ...
3034

35+
[#941]: https://github.com/google/docsy/pull/941
36+
[Shortcode helpers > alert]:
37+
https://www.docsy.dev/docs/adding-content/shortcodes/#alert
38+
3139
**New**:
3240

3341
**Other changes**:

assets/scss/_alerts.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
color: inherit;
66
border-radius: 0;
77

8+
:last-child {
9+
margin-bottom: 0;
10+
}
11+
812
@each $color, $value in $theme-colors {
913
&-#{$color} {
1014
& .alert-heading {
1115
color: $value;
1216
}
13-
1417
border-style: solid;
1518
border-color: $value;
1619
border-width: 0 0 0 4px;

layouts/_shortcodes/alert.html

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
{{ $_hugo_config := `{ "version": 1 }` }}
2-
{{ $color := .Get "color" | default "primary" }}
1+
{{/*
2+
3+
@param {string} [title=""] HTML rendered as a Bootstrap alert heading.
4+
@param {string} [color="primary"] Suffix used for the Bootstrap alert class
5+
`alert-{color}`.
6+
7+
For details, see https://www.docsy.dev/docs/adding-content/shortcodes/#alert.
8+
9+
*/ -}}
10+
11+
{{ $color := .Get "color" | default "primary" -}}
12+
313
<div class="alert alert-{{ $color }}" role="alert">
4-
{{ with .Get "title" }}<h4 class="alert-heading">{{ . | safeHTML }}</h4>{{ end }}
5-
{{ if eq .Page.File.Ext "md" }}
6-
{{ .Inner | markdownify }}
7-
{{ else }}
8-
{{ .Inner | htmlUnescape | safeHTML }}
9-
{{ end }}
14+
{{- with .Get "title" -}}
15+
<div class="h4 alert-heading" role="heading">
16+
{{- . | safeHTML -}}
17+
</div>
18+
{{- end }}
19+
20+
{{/* Do NOT remove the blank line above. It ends the previous HTML block and
21+
ensures that any Markdown in .Inner will be treated as Markdown. For details,
22+
see https://spec.commonmark.org/0.31.2/#html-blocks, 7. */ -}}
23+
24+
{{ .Inner }}
25+
26+
{{- /* Do NOT remove this space trimming comment. It ensures that any Markdown
27+
that _follows_ this shortcode remains correctly indented, in contexts where this
28+
is necessary. */ -}}
29+
1030
</div>

userguide/content/en/docs/adding-content/content.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
title: Adding Content
33
weight: 1
4-
description: >
5-
Add different types of content to your Docsy site.
4+
description: Add different types of content to your Docsy site.
5+
cSpell:ignore: goldmark Riona MacNamara
66
---
77

88
So you've got a new Hugo website with Docsy, now it's time to add some content!
@@ -311,9 +311,7 @@ By default you create pages in a Docsy site as simple
311311
[Goldmark](https://github.com/yuin/goldmark/) is the only Markdown parser
312312
supported by Hugo.
313313

314-
<div class="alert alert-primary" role="alert">
315-
316-
<h4 class="alert-heading">Tip</h4>
314+
{{% alert title="Tip" %}}
317315

318316
If you've been using versions of Hugo before 0.60 that use
319317
[`BlackFriday`](https://github.com/russross/blackfriday) as its Markdown parser,
@@ -351,7 +349,7 @@ markup:
351349
{{< /tabpane >}}
352350
<!-- prettier-ignore-end -->
353351

354-
</div>
352+
{{% /alert %}}
355353

356354
In addition to your marked-up text, you can also use Hugo and Docsy's
357355
[shortcodes](/docs/adding-content/shortcodes): reusable chunks of HTML that you
@@ -852,9 +850,7 @@ disableKinds: [RSS]
852850
{{< /tabpane >}}
853851
<!-- prettier-ignore-end -->
854852

855-
<div class="alert alert-info" role="alert">
856-
857-
<h4 class="alert-heading">Note</h4>
853+
{{% alert title=Note color=info %}}
858854

859855
If you have enabled our [print feature](/docs/adding-content/print/) or
860856
otherwise specified section-level output formats in
@@ -892,7 +888,8 @@ outputs:
892888
{{< /tab >}}
893889
{{< /tabpane >}}
894890
<!-- prettier-ignore-end -->
895-
</div>
891+
892+
{{% /alert %}}
896893

897894
## Sitemap
898895

userguide/content/en/docs/adding-content/feedback.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ configuration file. For details, see [Configure Google Analytics][].
3535

3636
{{% alert title="Deprecation note and warning" color="warning" %}}
3737

38-
<!-- Remove this warning once the Hugo docs have been updated to include it. -->
38+
<!-- Remove this warning once the Hugo docs have been updated to include it. -->
3939

4040
While you can configure your project's analytics ID by setting either the
4141
top-level `googleAnalytics` config parameter or `services.googleAnalytics.id`,

userguide/content/en/docs/adding-content/shortcodes/index.md

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ title: Docsy Shortcodes
33
linkTitle: Shortcodes
44
date: 2017-01-05
55
weight: 5
6-
description: >
7-
Use Docsy's Hugo shortcodes to quickly build site pages.
6+
description: Use Docsy's Hugo shortcodes to quickly build site pages.
87
resources:
98
- src: '**spruce*.jpg'
109
params:
1110
byline: '*Photo*: Bjørn Erik Pedersen / CC-BY-SA'
11+
params:
12+
message: Some _message_.
13+
cSpell:ignore: imgproc pageinfo Bjørn Pedersen
1214
---
1315

1416
Rather than writing all your site pages from scratch, Hugo lets you define and
@@ -172,22 +174,74 @@ section. It's meant to be used in combination with the other blocks shortcodes.
172174

173175
### alert
174176

175-
The **alert** shortcode creates an alert block that can be used to display
176-
notices or warnings.
177+
Use the **alert** shortcode to display notices and warnings. The shortcode
178+
renders a [Bootstrap alert component][bs-alert]. It can be used with Markdown
179+
content and contain other shortcodes. For example:
177180

178-
```go-html-template
179-
{{%/* alert title="Warning" color="warning" */%}}
180-
This is a warning.
181-
{{%/* /alert */%}}
181+
```go-template
182+
{{%/* alert title="Welcome" */%}} **Hello**, world! {{%/* /alert */%}}
182183
```
183184

184-
Renders to:
185+
Renders as:
185186

186-
{{% alert title="Warning" color="warning" %}} This is a warning. {{% /alert %}}
187+
{{% alert title="Welcome" %}} **Hello**, world! {{% /alert %}}
187188

188-
| Parameter | Default | Description |
189-
| --------- | ------- | ------------------------------------------------------------- |
190-
| color | primary | One of the theme colors, eg `primary`, `info`, `warning` etc. |
189+
Parameters:
190+
191+
- `title` (optional): Use this to specify a title for your alert. The title
192+
renders as a Bootstrap [alert heading][bs-alert], at heading level 4, using
193+
the `h4` Bootstrap class over a `<div>` element. This prevents the title from
194+
appearing in a page's table of contents.
195+
- `color` (optional): Use this parameter to specify one of Bootstrap's
196+
predefined [alert variants][bs-alert], each of which has their own color.
197+
These include `primary`, `info`, and `warning`.
198+
199+
#### Alerts and indentation
200+
201+
When the `alert` shortcode is used in a Markdown context that requires
202+
indentation, such as a list, then the alert _content_ (whether specified as
203+
text/Markdown or a shortcode) must be indented accordingly. For example:
204+
205+
```go-template
206+
- The following note is part of this list item:
207+
{{%/* alert title="Celebrate!" color=success */%}}
208+
This alert content is properly rendered.
209+
210+
> {{%/* param message */%}}
211+
{{%/* /alert */%}}
212+
The first list item continues.
213+
214+
- **Don't put content on the same line** as the opening tag, it breaks rendering:
215+
{{%/* alert title="Misformed alert!" color=warning */%}} **This content appears outside of
216+
the list!** {{%/* /alert */%}}
217+
```
218+
219+
This renders as:
220+
221+
{{< comment >}}
222+
223+
<!--
224+
IMPORTANT: the following lone opening div tag prevents the mis-formatted alert example below from breaking all the rest of the page. DO NOT remove it.
225+
-->
226+
227+
{{< /comment >}}
228+
229+
<div class="td-max-width-on-larger-screens">
230+
231+
<!-- prettier-ignore -->
232+
- The following note is part of this list item:
233+
{{% alert title="Celebrate!" color=success %}}
234+
This alert content is properly rendered.
235+
236+
> {{% param message %}}
237+
{{% /alert %}}
238+
The first list item continues.
239+
240+
- **Don't put content on the same line** as the opening tag, it breaks rendering:
241+
{{% alert title="Misformed alert!" color=warning %}} **This alert content appears outside of
242+
the list!** {{% /alert %}}
243+
244+
[bs-alert]: https://getbootstrap.com/docs/5.3/components/alerts/
191245

192246
### pageinfo
193247

@@ -196,7 +250,7 @@ information for a page: for example, letting users know that the page contains
196250
placeholder content, that the content is deprecated, or that it documents a beta
197251
feature.
198252

199-
```go-html-template
253+
```go-template
200254
{{%/* pageinfo color="info" */%}}
201255
This is placeholder content.
202256
{{%/* /pageinfo */%}}
@@ -220,7 +274,7 @@ The **imgproc** shortcode finds an image in the current
220274
[Page Bundle](/docs/adding-content/content/#page-bundles) and scales it given a
221275
set of processing instructions.
222276

223-
```go-html-template
277+
```go-template
224278
{{%/* imgproc spruce Fill "400x450" */%}}
225279
Norway Spruce *Picea abies* shoot with foliage buds.
226280
{{%/* /imgproc */%}}

userguide/content/en/docs/get-started/other-options.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,12 @@ your project's root directory:
181181
echo 'theme: docsy' >> hugo.yaml
182182
```
183183
184-
{{% alert title="Tip" %}}As of Hugo 0.110.0, the default config base
184+
{{% alert title="Tip" %}}
185+
186+
As of Hugo 0.110.0, the default config base
185187
filename was changed to `hugo.*` from `config.*`. If you are using hugo
186188
0.110 or above, consider renaming your `config.*` to `hugo.*`.
189+
187190
{{% /alert %}}
188191
189192
3. Get Docsy dependencies:
@@ -270,15 +273,21 @@ You can use Docsy as an NPM module as follows:
270273
npm install --save-dev google/docsy#semver:{{% param version %}} --omit=peer
271274
```
272275

273-
{{% alert title="Hugo-module compatibility" color="warning" %}} Installing
276+
{{% alert title="Hugo-module compatibility" color="warning" %}}
277+
278+
Installing
274279
Docsy using NPM creates an empty `github.com` sibling folder. For details,
275-
see [Docsy NPM install side-effect](#docsy-npm-install-side-effect). {{%
276-
/alert %}}
280+
see [Docsy NPM install side-effect](#docsy-npm-install-side-effect).
281+
282+
{{% /alert %}}
277283

278-
{{% alert title="Hugo install tip" color="info" %}} You can install Docsy's
284+
{{% alert title="Hugo install tip" %}}
285+
286+
You can install Docsy's
279287
officially supported version of [Hugo using NPM](#hugo-extended-npm) at the
280-
same time as Docsy. Just omit the `--omit` flag from the command above. {{%
281-
/alert %}}
288+
same time as Docsy. Just omit the `--omit` flag from the command above.
289+
290+
{{% /alert %}}
282291
283292
3. Build or serve your new site using the usual Hugo commands, specifying the
284293
path to the Docsy theme files. For example, build your site as follows:
@@ -291,21 +300,21 @@ You can use Docsy as an NPM module as follows:
291300
292301
{{% alert title="Error: failed to load modules" color="warning" %}}
293302
294-
If Hugo reports the following error when building your site ([#2116]):
303+
If Hugo reports the following error when building your site ([#2116]):
295304
296-
```
297-
Error: failed to load modules: module "github.com/FortAwesome/Font-Awesome" not found in ".../myproject/node_modules/github.com/FortAwesome/Font-Awesome" ...
298-
```
305+
```
306+
Error: failed to load modules: module "github.com/FortAwesome/Font-Awesome" not found in ".../myproject/node_modules/github.com/FortAwesome/Font-Awesome" ...
307+
```
299308
300-
Then run the following command and try again:
309+
Then run the following command and try again:
301310
302-
```sh
303-
npm rebuild
304-
```
311+
```sh
312+
npm rebuild
313+
```
305314
306-
[#2116]: https://github.com/google/docsy/issues/2116
315+
[#2116]: https://github.com/google/docsy/issues/2116
307316
308-
{{% /alert %}}
317+
{{% /alert %}}
309318
310319
As an alternative to specifying a `themesDir`, on some platforms, you can
311320
instead create a symbolic link to the Docsy theme directory as follows (Linux

userguide/static/refcache.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@
411411
"StatusCode": 206,
412412
"LastSeen": "2024-11-06T12:03:46.341081-05:00"
413413
},
414+
"https://getbootstrap.com/docs/5.3/components/alerts/": {
415+
"StatusCode": 206,
416+
"LastSeen": "2025-06-10T21:33:38.654894-04:00"
417+
},
414418
"https://getbootstrap.com/docs/5.3/content/tables/": {
415419
"StatusCode": 206,
416420
"LastSeen": "2025-05-16T09:20:45.464085-04:00"
@@ -1119,6 +1123,10 @@
11191123
"StatusCode": 206,
11201124
"LastSeen": "2025-05-22T19:21:14.104646-04:00"
11211125
},
1126+
"https://github.com/google/docsy/pull/941": {
1127+
"StatusCode": 206,
1128+
"LastSeen": "2025-06-10T18:59:25.465236-04:00"
1129+
},
11221130
"https://github.com/google/docsy/pulls": {
11231131
"StatusCode": 200,
11241132
"LastSeen": "2024-11-06T12:03:15.642661-05:00"
@@ -2355,6 +2363,10 @@
23552363
"StatusCode": 206,
23562364
"LastSeen": "2024-11-06T12:06:43.680938-05:00"
23572365
},
2366+
"https://www.docsy.dev/docs/adding-content/shortcodes/#alert": {
2367+
"StatusCode": 206,
2368+
"LastSeen": "2025-06-10T21:43:28.870112-04:00"
2369+
},
23582370
"https://www.docsy.dev/docs/adding-content/shortcodes/#blocksfeature": {
23592371
"StatusCode": 206,
23602372
"LastSeen": "2024-11-06T12:05:17.284993-05:00"

0 commit comments

Comments
 (0)