Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
hjonin committed Feb 7, 2024
2 parents 153f845 + 081ccda commit f199e66
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _includes/components/alert.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if not alert %}{% set alert = params %}{% endif %}
<div class="fr-alert {% if alert.type %}fr-alert--{{ alert.type }}{% else %}fr-alert--info{% endif %} {% if not alert.title %}fr-alert--sm{% endif %}">
{% if alert.title %}
<h3 class="fr-alert__title">{{ alert.title | safe }}</h3>
{% endif %}
{% if alert.description %}{{ alert.description | safe }}{% endif %}
</div>
68 changes: 68 additions & 0 deletions content/fr/blog/posts/alerte.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: Alerte
description: Comment intégrer une alerte dans une page du site ?
date: git Last Modified
tags:
- DSFR
- composant
---

Ce composant peut être inclus dans un fichier Nunjucks `.njk` ou Markdown `.md`.

## Utilisation

### Exemple d'utilisation dans un fichier Markdown `.md`

```md
:::info Test d'alerte
Contenu **Mardown**
:::
```

### Exemple d'utilisation dans un fichier Nunjucks `.njk`

```njk
{% raw %}
{% from "components/component.njk" import component with context %}
{{ component("alert", {
type: "info",
title: "Test d'alerte",
description: "<p>Le contenu de l'alerte</p>"
}) }}
{% endraw %}
```

**Notes :**

Le composant alerte n'inclut pas de bouton de fermeture.

Le bloc ne porte pas l'attribut `role="alert"` car il n’apparait pas dynamiquement en cours de navigation.

Les types possibles sont `info`, `warning`, `error`, `success`. En `njk` si le type est omis, le type `info` sera appliqué.

## Rendu

:::info Titre de l'information
Contenu de l'alerte
:::

{% from "components/component.njk" import component with context %}
{{ component("alert", {
type: "warning",
title: "Titre de l'avertissement",
description: "<p>Le contenu de l'alerte</p>"
}) }}

:::success
Contenu de l'alerte seule
:::

{% from "components/component.njk" import component with context %}
{{ component("alert", {
type: "error",
title: "Titre d'une erreur simple"
}) }}

<br>

[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}
4 changes: 4 additions & 0 deletions eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ module.exports = function (eleventyConfig) {
mdLib.use(markdownItContainer, 'quote', customMarkdownContainers.quote(mdLib));
});

eleventyConfig.amendLibrary("md", mdLib => {
mdLib.use(markdownItContainer, 'alert', customMarkdownContainers.alert(mdLib));
});

// Automatically strip all leading or trailing whitespace
// to prevent Markdown lib from rendering with wrapping into paragraphs
// instead of using Nunjucks special syntax. Learn more:
Expand Down
31 changes: 31 additions & 0 deletions markdown-custom-containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,36 @@ module.exports = {
}
}
}
},
alert: md => {
const re = /^(info|warning|error|success)(\s+.*)?$/;
return {
validate: (params) => {
return params.trim().match(re);
},

render: (tokens, idx) => {
const params = tokens[idx].info.trim().match(re);
const type = params?.[1];
const title = md.utils.escapeHtml(params?.[2]) || '';

if (tokens[idx].nesting === 1) {
title_elem = '';
small_class = 'fr-alert--sm';
if (title !== '') {
title_elem = `<h3 class="fr-alert__title">${title}</h3>`;
small_class = "";
}
// opening tag
return `
<div class="fr-alert fr-alert--${type} ${small_class}">
${title_elem}
`;
} else {
// closing tag
return '</div>\n';
}
}
}
}
}

0 comments on commit f199e66

Please sign in to comment.