Skip to content

Commit 8a09b60

Browse files
authored
Ignore <script> elements when processing <Steps> (#3726)
1 parent 9894df2 commit 8a09b60

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

.changeset/hip-experts-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/starlight': patch
3+
---
4+
5+
Fixes an issue using components containing scripts inside Starlight’s steps component in versions of Astro >= 5.16.9

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ test('component with multiple children throws an error', () => {
6262
`);
6363
});
6464

65+
// This tests a workaround added for https://github.com/withastro/astro/issues/15627
66+
// See rehype-steps.ts for more details.
67+
test('component with top-level script child is processed successfully', () => {
68+
const { html } = processSteps(
69+
'<script type="module" src="/test-script.js"></script><ol><li>List item</li></ol>'
70+
);
71+
expect(html).toMatchInlineSnapshot(
72+
`"<script type="module" src="/test-script.js"></script><ol role="list" class="sl-steps"><li>List item</li></ol>"`
73+
);
74+
});
75+
6576
test('applies `role="list"` to child list', () => {
6677
const { html } = processSteps('<ol><li>Step one</li></ol>');
6778
expect(html).toMatchInlineSnapshot(`"<ol role="list" class="sl-steps"><li>Step one</li></ol>"`);

packages/starlight/user-components/rehype-steps.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ const stepsProcessor = rehype()
1212
.data('settings', { fragment: true })
1313
.use(function steps() {
1414
return (tree: Root, vfile: VFile) => {
15-
const rootElements = tree.children.filter((item): item is Element => item.type === 'element');
15+
const rootElements = tree.children.filter(
16+
(item): item is Element =>
17+
item.type === 'element' &&
18+
// Since Astro 5.16.9, `<script>` elements from nested child elements can end up hoisted
19+
// into the `<Steps>` slot due to our use of `Astro.slots.render()`. We can safely ignore
20+
// these, so we filter them out here.
21+
// TODO: we may be able to remove this in the future if the upstream issue is fixed.
22+
// See: https://github.com/withastro/astro/issues/15627
23+
item.tagName !== 'script'
24+
);
1625
const [rootElement] = rootElements;
1726

1827
if (!rootElement) {

0 commit comments

Comments
 (0)