# Rich text

A visual editor for formatted content, with a **Source** mode for the raw Markdown or HTML. Both modes edit the same value, and both save through the form.

Add this field under `fields` in `.pages.yml`:

```yaml
- name: body
  type: rich-text
```

Content is saved as Markdown by default. As the `body` field of a Markdown file, it's the text below the front matter. As any other field, it's a string:

```yaml
intro: 'Welcome to **our** site.'
```

## Options

| Option       | Description                                                                                                                                                               |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `format`     | `markdown` (default) or `html`.                                                                                                                                           |
| `switcher`   | `false` to hide the **Editor**/**Source** switch.                                                                                                                         |
| `media`      | The [media library](/configuration/media/) for inserted images. Defaults to the first. `false` turns off picking and uploading; editors can still insert an image by URL. |
| `path`       | Folder to open first when inserting an image, as a repository path inside the library's `input`.                                                                          |
| `extensions` | Allowed image extensions. Images in rich text only ever accept image types.                                                                                               |
| `rename`     | Rename uploads: `false`, `safe` (or `true`) or `random`. Overrides the library's setting.                                                                                 |
| `snippets`   | Reusable content editors insert with `/`. See [snippets](#snippets).                                                                                                      |
| `html`       | Which HTML elements to keep, unwrap or drop. See [HTML elements](#html-elements).                                                                                         |
| `preserve`   | Template syntax to keep as written during visual edits. See [template syntax](#template-syntax).                                                                          |

## MDX and components

Markdown comments and component tags such as `<Callout />` are shown as source blocks and kept as written. Edit complex MDX, such as expressions or object props, in **Source** mode.

## GitHub alerts

Markdown fields recognise GitHub alerts. Start a quote with `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` on its own line:

```markdown
> [!TIP]
> Save your changes before switching branches.
```

Use **Source** to set the alert type. Its body stays editable in the visual editor. For a literal marker inside a quote, write `> \[!NOTE\]` in Source. Astro doesn't render alerts on its own, so the site needs Markdown support for them.

## Template syntax

If a rich-text field contains template tags, list them in `preserve` so the visual editor keeps them intact. Give the literal opening and closing strings. For example, for Liquid output expressions such as `{{ page.title }}` and control tags such as `{% if page.featured %}`:

```yaml
- name: body
  type: rich-text
  options:
    preserve:
      - { start: '{{', end: '}}' }
      - { start: '{%', end: '%}' }
```

The prose between tags stays editable; switch to **Source** to edit the tags themselves. The longest opening string matches first, and a pair ends at its first closing string, even inside quotes. Use **Source** for nested expressions, raw blocks or templates that contain HTML.

For Hugo shortcodes such as `{{< embed … >}}` and `{{% note %}}`, use the `hugo-shortcodes` preset instead, which also handles quoted arguments:

```yaml
options:
  preserve:
    - hugo-shortcodes
```

## Snippets

Snippets give editors ready-made content, such as a callout, to insert by typing `/` and searching for its label:

```yaml
- name: body
  type: rich-text
  options:
    html:
      keep: [div.callout]
    snippets:
      - label: Callout
        source: |
          <div class="callout">
            <p>Your message.</p>
          </div>
```

| Snippet key | Description                                                                          |
| ----------- | ------------------------------------------------------------------------------------ |
| `label`     | Required. Name shown in the `/` menu.                                                |
| `source`    | Required. The content to insert. Use a YAML block scalar (`\|`) to keep line breaks. |
| `format`    | `markdown` or `html`, if the source isn't in the field's format.                     |

HTML wrappers in a snippet must also be listed in `html.keep`, or they're removed when inserted.

## HTML elements

The visual editor understands standard formatting: headings, lists, links, images, tables and so on. Other HTML, such as a `<div class="callout">`, is shown as a block edited as source. `html` changes that per element, using CSS selectors:

```yaml
options:
  html:
    keep: [div.callout, details, summary]
    strip: [span.legacy]
    drop: [.internal-note]
    attributes: [data-kind]
```

| Key          | Effect                                                                                                                                                                |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `keep`       | Keep the element and make its content editable. Works for `div`, `section`, `article`, `aside`, `header`, `footer`, `details`, `summary` and definition lists (`dl`). |
| `strip`      | Remove the element but keep its text.                                                                                                                                 |
| `drop`       | Remove the element and its content.                                                                                                                                   |
| `attributes` | Extra attributes to keep on kept elements, such as `data-kind`. Classes, IDs and standard attributes are kept already.                                                |

`drop` takes precedence over `strip`, and `strip` over `keep`. Rules apply when content is opened in the visual editor; switching to Source and back doesn't change stored content. Scripts, event handlers, inline styles, iframes and form controls are always removed and can't be allowed.

With `format: markdown`, ordinary content stays Markdown and kept elements are saved as HTML inside it.

Simple SVG icons display in the editor; scripts and external resources inside them are removed.
