Skip to content

Commit c1029ae

Browse files
committed
Aside: Add support for icon attribute to markdown shorthand.
1 parent 9d6d5c8 commit c1029ae

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

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

+14
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[Did you know?]{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[Did you know?]{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

+23
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ Some text
126126
);
127127
});
128128

129+
describe('custom icons', () => {
130+
test.each(['note', 'tip', 'caution', 'danger'])('%s with custom label', async (type) => {
131+
const res = await processor.render(`
132+
:::${type}{icon="heart"}
133+
Some text
134+
:::
135+
`);
136+
expect(res.code).includes(
137+
'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'
138+
);
139+
140+
expect(async () =>
141+
processor.render(`
142+
:::${type}{icon="invalid-icon-name"}
143+
Some text
144+
:::
145+
`)
146+
).rejects.toThrowError(
147+
'Failed to parse Markdown file "undefined":\nIcon name should be part of Starlight\'s icon list.'
148+
);
149+
});
150+
});
151+
129152
test('ignores unknown directive variants', async () => {
130153
const res = await renderMarkdown(`
131154
:::unknown

packages/starlight/integrations/asides.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import remarkDirective from 'remark-directive';
1515
import type { Plugin, Transformer } from 'unified';
1616
import { visit } from 'unist-util-visit';
1717
import type { HookParameters, StarlightConfig } from '../types';
18+
import { Icons } from '../components/Icons';
19+
import { fromHtml } from 'hast-util-from-html';
20+
import type { Element } from 'hast';
21+
import { AstroError } from 'astro/errors';
1822

1923
interface AsidesOptions {
2024
starlightConfig: Pick<StarlightConfig, 'defaultLocale' | 'locales'>;
@@ -159,6 +163,7 @@ function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
159163
return;
160164
}
161165
const variant = node.name;
166+
const attributes = node.attributes;
162167
if (!isAsideVariant(variant)) return;
163168

164169
// remark-directive converts a container’s “label” to a paragraph added as the head of its
@@ -180,6 +185,19 @@ function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
180185
node.children.splice(0, 1);
181186
}
182187

188+
let iconPath = iconPaths[variant];
189+
if (attributes) {
190+
if (attributes['icon']) {
191+
const iconName = attributes['icon'] as keyof typeof Icons;
192+
if (!Object.keys(Icons).includes(iconName)) {
193+
throw new AstroError("Icon name should be part of Starlight's icon list.");
194+
}
195+
const { properties } = fromHtml(Icons[iconName], { fragment: true })
196+
.children[0] as Element;
197+
iconPath = [s('path', properties)];
198+
}
199+
}
200+
183201
const aside = h(
184202
'aside',
185203
{
@@ -197,7 +215,7 @@ function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
197215
fill: 'currentColor',
198216
class: 'starlight-aside__icon',
199217
},
200-
iconPaths[variant]
218+
iconPath
201219
),
202220
...titleNode,
203221
]),

0 commit comments

Comments
 (0)