Skip to content

Commit 1b6ade5

Browse files
committed
Aside: Add support for icon attribute to markdown shorthand.
1 parent 353fcfc commit 1b6ade5

13 files changed

+171
-14
lines changed

docs/src/content/docs/components/asides.mdx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ Other content is also supported in asides.
4949
</Aside>
5050

5151
<Aside type="danger">Do not give your password to anyone.</Aside>
52-
53-
<Aside icon="heart">
54-
Aside with a custom icon. The icon must be part of starlight's icon list.
55-
</Aside>
5652
````
5753

5854
<Fragment slot="markdoc">
@@ -77,10 +73,6 @@ Other content is also supported in asides.
7773
{% aside type="danger" %}
7874
Do not give your password to anyone.
7975
{% /aside %}
80-
81-
{% aside icon="heart" %}
82-
Aside with a custom icon. The icon must be part of starlight's icon list.
83-
{% /aside %}
8476
````
8577

8678
</Fragment>
@@ -103,10 +95,6 @@ Aside with a custom icon. The icon must be part of starlight's icon list.
10395

10496
<Aside type="danger">Do not give your password to anyone.</Aside>
10597

106-
<Aside icon="heart">
107-
Aside with a custom icon. The icon must be part of starlight's icon list.
108-
</Aside>
109-
11098
</Fragment>
11199

112100
</Preview>
@@ -144,6 +132,36 @@ A warning aside *with* a custom title.
144132

145133
</Preview>
146134

135+
### Use custom icons
136+
137+
Override the default aside icons by using the [`icon`](#icon) attribute set to the name of [one of Starlight’s built-in icons](/reference/icons/#all-icons).
138+
139+
<Preview>
140+
141+
```mdx 'icon="starlight"'
142+
import { Aside } from '@astrojs/starlight/components';
143+
144+
<Aside type="tip" icon="starlight">
145+
A warning aside *with* a custom icon.
146+
</Aside>
147+
```
148+
149+
<Fragment slot="markdoc">
150+
151+
```markdoc 'icon="starlight"'
152+
{% aside type="tip" icon="starlight" %}
153+
A warning aside *with* a custom icon.
154+
{% /aside %}
155+
```
156+
157+
</Fragment>
158+
159+
<Aside slot="preview" type="tip" icon="starlight">
160+
A warning aside *with* a custom icon.
161+
</Aside>
162+
163+
</Preview>
164+
147165
## `<Aside>` Props
148166

149167
**Implementation:** [`Aside.astro`](https://github.com/withastro/starlight/blob/main/packages/starlight/user-components/Aside.astro)
@@ -168,3 +186,9 @@ The type of aside to display:
168186

169187
The title of the aside to display.
170188
If `title` is not set, the default title for the current aside `type` will be used.
189+
190+
### `icon`
191+
192+
**type:** `string`
193+
194+
An aside can include an `icon` attribute set to the name of [one of Starlight’s built-in icons](/reference/icons/#all-icons).

docs/src/content/docs/guides/authoring-content.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ Astro helps you build faster websites with [“Islands Architecture”](https://
151151
:::
152152
```
153153

154+
### Custom aside icons
155+
156+
You can specify a custom icon for the aside in curly brackets following the aside type/title, e.g. `:::tip[Did you know?]{icon="heart"}`.
157+
158+
:::tip{icon="heart"}
159+
Astro helps you build faster websites with [“Islands Architecture”](https://docs.astro.build/en/concepts/islands/).
160+
:::
161+
162+
```md
163+
:::tip{icon="heart"}
164+
Astro helps you build faster websites with [“Islands Architecture”](https://docs.astro.build/en/concepts/islands/).
165+
:::
166+
```
167+
154168
### More aside types
155169

156170
Caution and danger asides are helpful for drawing a user’s attention to details that may trip them up.

packages/starlight/__tests__/remark-rehype/asides.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,78 @@ Some text
126126
);
127127
});
128128

129+
const testCustomIcons = async (type: string) => {
130+
const res = await processor.render(`
131+
:::${type}{icon="heart"}
132+
Some text
133+
:::
134+
`);
135+
await expect(res.code).toMatchFileSnapshot(
136+
`./snapshots/generates-aside-${type}-custom-icon.html`
137+
);
138+
139+
await expect(async () =>
140+
processor.render(
141+
`
142+
:::${type}{icon="invalid-icon-name"}
143+
Some text
144+
:::
145+
`
146+
)
147+
).rejects.toThrowErrorMatchingInlineSnapshot(
148+
`
149+
"[AstroUserError]:
150+
Failed to parse Markdown file "undefined":
151+
Invalid aside icon
152+
Hint:
153+
An aside custom icon must be set to the name of one of Starlight\’s built-in icons, but received \`invalid-icon-name\`.
154+
155+
See https://starlight.astro.build/reference/icons/#all-icons for a list of available icons."
156+
`
157+
);
158+
};
159+
160+
describe('custom icons', () => {
161+
// We cannot use test.each here since toThrowErrorMatchingInlineSnapshot
162+
// cannot be used inside test.each.
163+
// See: https://github.com/vitest-dev/vitest/issues/3329
164+
test('note with custom icons', async () => testCustomIcons('note'));
165+
test('tip with custom icons', async () => testCustomIcons('tip'));
166+
test('caution with custom icons', async () => testCustomIcons('caution'));
167+
test('danger with custom icons', async () => testCustomIcons('danger'));
168+
169+
test('test icon with multiple paths inside the svg', async () => {
170+
const res = await processor.render(`
171+
:::note{icon="external"}
172+
Some text
173+
:::
174+
`);
175+
await expect(res.code).toMatchFileSnapshot(
176+
`./snapshots/generates-aside-note-multiple-path-custom-icon.html`
177+
);
178+
const pathCount = (res.code.match(/path/g) || []).length;
179+
// If we have two pairs of opening and closing tags of path,
180+
// we will have 4 occurences of that word.
181+
expect(pathCount).eq(4);
182+
});
183+
});
184+
185+
describe('custom labels with custom icons', () => {
186+
test.each(['note', 'tip', 'caution', 'danger'])('%s with custom label', async (type) => {
187+
const label = 'Custom Label';
188+
const res = await renderMarkdown(`
189+
:::${type}[${label}]{icon="heart"}
190+
Some text
191+
:::
192+
`);
193+
expect(res.code).includes(`aria-label="${label}"`);
194+
expect(res.code).includes(`</svg>${label}</p>`);
195+
await expect(res.code).toMatchFileSnapshot(
196+
`./snapshots/generates-aside-${type}-custom-label-and-icon.html`
197+
);
198+
});
199+
});
200+
129201
test('ignores unknown directive variants', async () => {
130202
const res = await renderMarkdown(`
131203
:::unknown
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Caution" class="starlight-aside starlight-aside--caution"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M20.16 5A6.29 6.29 0 0 0 12 4.36a6.27 6.27 0 0 0-8.16 9.48l6.21 6.22a2.78 2.78 0 0 0 3.9 0l6.21-6.22a6.27 6.27 0 0 0 0-8.84m-1.41 7.46-6.21 6.21a.76.76 0 0 1-1.08 0l-6.21-6.24a4.29 4.29 0 0 1 0-6 4.27 4.27 0 0 1 6 0 1 1 0 0 0 1.42 0 4.27 4.27 0 0 1 6 0 4.29 4.29 0 0 1 .08 6Z"></path></svg>Caution</p><div class="starlight-aside__content"><p>Some text</p></div></aside>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Custom Label" class="starlight-aside starlight-aside--caution"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M20.16 5A6.29 6.29 0 0 0 12 4.36a6.27 6.27 0 0 0-8.16 9.48l6.21 6.22a2.78 2.78 0 0 0 3.9 0l6.21-6.22a6.27 6.27 0 0 0 0-8.84m-1.41 7.46-6.21 6.21a.76.76 0 0 1-1.08 0l-6.21-6.24a4.29 4.29 0 0 1 0-6 4.27 4.27 0 0 1 6 0 1 1 0 0 0 1.42 0 4.27 4.27 0 0 1 6 0 4.29 4.29 0 0 1 .08 6Z"></path></svg>Custom Label</p><div class="starlight-aside__content"><p>Some text</p></div></aside>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Danger" class="starlight-aside starlight-aside--danger"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M20.16 5A6.29 6.29 0 0 0 12 4.36a6.27 6.27 0 0 0-8.16 9.48l6.21 6.22a2.78 2.78 0 0 0 3.9 0l6.21-6.22a6.27 6.27 0 0 0 0-8.84m-1.41 7.46-6.21 6.21a.76.76 0 0 1-1.08 0l-6.21-6.24a4.29 4.29 0 0 1 0-6 4.27 4.27 0 0 1 6 0 1 1 0 0 0 1.42 0 4.27 4.27 0 0 1 6 0 4.29 4.29 0 0 1 .08 6Z"></path></svg>Danger</p><div class="starlight-aside__content"><p>Some text</p></div></aside>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Custom Label" class="starlight-aside starlight-aside--danger"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M20.16 5A6.29 6.29 0 0 0 12 4.36a6.27 6.27 0 0 0-8.16 9.48l6.21 6.22a2.78 2.78 0 0 0 3.9 0l6.21-6.22a6.27 6.27 0 0 0 0-8.84m-1.41 7.46-6.21 6.21a.76.76 0 0 1-1.08 0l-6.21-6.24a4.29 4.29 0 0 1 0-6 4.27 4.27 0 0 1 6 0 1 1 0 0 0 1.42 0 4.27 4.27 0 0 1 6 0 4.29 4.29 0 0 1 .08 6Z"></path></svg>Custom Label</p><div class="starlight-aside__content"><p>Some text</p></div></aside>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Note" class="starlight-aside starlight-aside--note"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M20.16 5A6.29 6.29 0 0 0 12 4.36a6.27 6.27 0 0 0-8.16 9.48l6.21 6.22a2.78 2.78 0 0 0 3.9 0l6.21-6.22a6.27 6.27 0 0 0 0-8.84m-1.41 7.46-6.21 6.21a.76.76 0 0 1-1.08 0l-6.21-6.24a4.29 4.29 0 0 1 0-6 4.27 4.27 0 0 1 6 0 1 1 0 0 0 1.42 0 4.27 4.27 0 0 1 6 0 4.29 4.29 0 0 1 .08 6Z"></path></svg>Note</p><div class="starlight-aside__content"><p>Some text</p></div></aside>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Custom Label" class="starlight-aside starlight-aside--note"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M20.16 5A6.29 6.29 0 0 0 12 4.36a6.27 6.27 0 0 0-8.16 9.48l6.21 6.22a2.78 2.78 0 0 0 3.9 0l6.21-6.22a6.27 6.27 0 0 0 0-8.84m-1.41 7.46-6.21 6.21a.76.76 0 0 1-1.08 0l-6.21-6.24a4.29 4.29 0 0 1 0-6 4.27 4.27 0 0 1 6 0 1 1 0 0 0 1.42 0 4.27 4.27 0 0 1 6 0 4.29 4.29 0 0 1 .08 6Z"></path></svg>Custom Label</p><div class="starlight-aside__content"><p>Some text</p></div></aside>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<aside aria-label="Note" class="starlight-aside starlight-aside--note"><p class="starlight-aside__title" aria-hidden="true"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" class="starlight-aside__icon"><path d="M19.33 10.18a1 1 0 0 1-.77 0 1 1 0 0 1-.62-.93l.01-1.83-8.2 8.2a1 1 0 0 1-1.41-1.42l8.2-8.2H14.7a1 1 0 0 1 0-2h4.25a1 1 0 0 1 1 1v4.25a1 1 0 0 1-.62.93Z"></path><path d="M11 4a1 1 0 1 1 0 2H7a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1v-4a1 1 0 1 1 2 0v4a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3V7a3 3 0 0 1 3-3h4Z"></path></svg>Note</p><div class="starlight-aside__content"><p>Some text</p></div></aside>

0 commit comments

Comments
 (0)