Skip to content

Commit 6a9cda8

Browse files
authored
Merge pull request #14 from slafayIGN/alert-component
Composant alerte
2 parents 8c1f51b + aa19151 commit 6a9cda8

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

_includes/components/alert.njk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if not alert %}{% set alert = params %}{% endif %}
2+
<div class="fr-alert {% if alert.type %}fr-alert--{{ alert.type }}{% else %}fr-alert--info{% endif %} {% if not alert.title %}fr-alert--sm{% endif %}">
3+
{% if alert.title %}
4+
<h3 class="fr-alert__title">{{ alert.title | safe }}</h3>
5+
{% endif %}
6+
{% if alert.description %}{{ alert.description | safe }}{% endif %}
7+
</div>

content/fr/blog/posts/alerte.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: Alerte
3+
description: Comment intégrer une alerte dans une page du site ?
4+
date: git Last Modified
5+
tags:
6+
- DSFR
7+
- composant
8+
---
9+
10+
Ce composant peut être inclus dans un fichier Nunjucks `.njk` ou Markdown `.md`.
11+
12+
## Utilisation
13+
14+
### Exemple d'utilisation dans un fichier Markdown `.md`
15+
16+
```md
17+
:::info Test d'alerte
18+
Contenu **Mardown**
19+
:::
20+
```
21+
22+
### Exemple d'utilisation dans un fichier Nunjucks `.njk`
23+
24+
```njk
25+
{% raw %}
26+
{% from "components/component.njk" import component with context %}
27+
{{ component("alert", {
28+
type: "info",
29+
title: "Test d'alerte",
30+
description: "<p>Le contenu de l'alerte</p>"
31+
}) }}
32+
{% endraw %}
33+
```
34+
35+
**Notes :**
36+
37+
Le composant alerte n'inclut pas de bouton de fermeture.
38+
39+
Le bloc ne porte pas l'attribut `role="alert"` car il n’apparait pas dynamiquement en cours de navigation.
40+
41+
Les types possibles sont `info`, `warning`, `error`, `success`. En `njk` si le type est omis, le type `info` sera appliqué.
42+
43+
## Rendu
44+
45+
:::info Titre de l'information
46+
Contenu de l'alerte
47+
:::
48+
49+
{% from "components/component.njk" import component with context %}
50+
{{ component("alert", {
51+
type: "warning",
52+
title: "Titre de l'avertissement",
53+
description: "<p>Le contenu de l'alerte</p>"
54+
}) }}
55+
56+
:::success
57+
Contenu de l'alerte seule
58+
:::
59+
60+
{% from "components/component.njk" import component with context %}
61+
{{ component("alert", {
62+
type: "error",
63+
title: "Titre d'une erreur simple"
64+
}) }}
65+
66+
<br>
67+
68+
[Voir aussi la page du composant sur le site du DSFR](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/alerte){.fr-link .fr-fi-arrow-right-line .fr-link--icon-right}

content/fr/blog/posts/md-cheatsheet.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ La syntaxe utilisée dans les fichiers Markdown `.md` du site suit la spécifica
1010

1111
[Voir un rappel des principaux éléments](https://commonmark.org/help/){.fr-link .fr-fi-arrow-right-line .fr-link--icon-right}
1212

13-
**Deux nouveaux éléments** ont été ajoutés à cette syntaxe et sont disponibles dans eleventy-dsfr.
13+
**De nouveaux éléments** ont été ajoutés à cette syntaxe et sont disponibles dans eleventy-dsfr.
14+
15+
## L'alerte
16+
17+
```md
18+
:::info Test d'alerte
19+
Contenu **Mardown**
20+
:::
21+
```
22+
23+
[Voir aussi](/fr/blog/alerte/#exemple-d-utilisation-dans-un-fichier-markdown-md){.fr-link .fr-fi-arrow-right-line .fr-link--icon-right}
1424

1525
## La citation
1626

eleventy.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ module.exports = function (eleventyConfig) {
165165
mdLib.use(markdownItContainer, 'quote', customMarkdownContainers.quote(mdLib));
166166
});
167167

168+
eleventyConfig.amendLibrary("md", mdLib => {
169+
mdLib.use(markdownItContainer, 'alert', customMarkdownContainers.alert(mdLib));
170+
});
171+
168172
// Automatically strip all leading or trailing whitespace
169173
// to prevent Markdown lib from rendering with wrapping into paragraphs
170174
// instead of using Nunjucks special syntax. Learn more:

markdown-custom-containers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,36 @@ module.exports = {
5757
}
5858
}
5959
}
60+
},
61+
alert: md => {
62+
const re = /^(info|warning|error|success)(\s+.*)?$/;
63+
return {
64+
validate: (params) => {
65+
return params.trim().match(re);
66+
},
67+
68+
render: (tokens, idx) => {
69+
const params = tokens[idx].info.trim().match(re);
70+
const type = params?.[1];
71+
const title = md.utils.escapeHtml(params?.[2]) || '';
72+
73+
if (tokens[idx].nesting === 1) {
74+
title_elem = '';
75+
small_class = 'fr-alert--sm';
76+
if (title !== '') {
77+
title_elem = `<h3 class="fr-alert__title">${title}</h3>`;
78+
small_class = "";
79+
}
80+
// opening tag
81+
return `
82+
<div class="fr-alert fr-alert--${type} ${small_class}">
83+
${title_elem}
84+
`;
85+
} else {
86+
// closing tag
87+
return '</div>\n';
88+
}
89+
}
90+
}
6091
}
6192
}

0 commit comments

Comments
 (0)