From 8af5a60ab14f4dae7f5a5e4ee535ae927273368b Mon Sep 17 00:00:00 2001 From: HiDeoo Date: Thu, 20 Jun 2024 20:08:51 +0200 Subject: Refactor `` counter (#2041) Co-authored-by: Chris Swithinbank --- .changeset/strong-apes-burn.md | 5 +++++ .../starlight/__tests__/remark-rehype/rehype-steps.test.ts | 13 ++++++++++++- packages/starlight/user-components/Steps.astro | 4 +++- packages/starlight/user-components/rehype-steps.ts | 8 ++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 .changeset/strong-apes-burn.md diff --git a/.changeset/strong-apes-burn.md b/.changeset/strong-apes-burn.md new file mode 100644 index 00000000..1536b4cd --- /dev/null +++ b/.changeset/strong-apes-burn.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Fixes `` numbering bug caused by Chrome v126 CSS counter rewrite diff --git a/packages/starlight/__tests__/remark-rehype/rehype-steps.test.ts b/packages/starlight/__tests__/remark-rehype/rehype-steps.test.ts index 36f4082c..273788e6 100644 --- a/packages/starlight/__tests__/remark-rehype/rehype-steps.test.ts +++ b/packages/starlight/__tests__/remark-rehype/rehype-steps.test.ts @@ -70,7 +70,7 @@ test('applies `role="list"` to child list', () => { test('does not interfere with other attributes on the child list', () => { const { html } = processSteps('
  1. Step one
'); expect(html).toMatchInlineSnapshot( - `"
  1. Step one
"` + `"
  1. Step one
"` ); }); @@ -87,3 +87,14 @@ test('applies class name and preserves existing classes on a child list', () => `"
  1. Step one
"` ); }); + +test('applies custom property if start attribute is used', () => { + const start = 10; + const { html } = processSteps(`
  1. Step one
`); + expect(html).toContain(`style="--sl-steps-start: ${start - 1}"`); +}); + +test('custom property for start count does not interfere with custom styles', () => { + const { html } = processSteps(`
  1. Step one
`); + expect(html).toMatchInlineSnapshot(`"
  1. Step one
"`); +}); diff --git a/packages/starlight/user-components/Steps.astro b/packages/starlight/user-components/Steps.astro index ffd65a35..66562555 100644 --- a/packages/starlight/user-components/Steps.astro +++ b/packages/starlight/user-components/Steps.astro @@ -13,10 +13,12 @@ const { html } = processSteps(content); --bullet-margin: 0.375rem; list-style: none; + counter-reset: steps-counter var(--sl-steps-start, 0); padding-inline-start: 0; } .sl-steps > li { + counter-increment: steps-counter; position: relative; padding-inline-start: calc(var(--bullet-size) + 1rem); /* HACK: Keeps any `margin-bottom` inside the `
  • `’s padding box to avoid gaps in the hairline border. */ @@ -31,7 +33,7 @@ const { html } = processSteps(content); /* Custom list marker element. */ .sl-steps > li::before { - content: counter(list-item); + content: counter(steps-counter); position: absolute; top: 0; inset-inline-start: 0; diff --git a/packages/starlight/user-components/rehype-steps.ts b/packages/starlight/user-components/rehype-steps.ts index ff9c9593..184df00e 100644 --- a/packages/starlight/user-components/rehype-steps.ts +++ b/packages/starlight/user-components/rehype-steps.ts @@ -44,6 +44,14 @@ const stepsProcessor = rehype() } else { rootElement.properties.className.push('sl-steps'); } + + // Add the `start` attribute as a CSS custom property so we can use it as the starting index + // of the steps custom counter. + if (typeof rootElement.properties.start === 'number') { + const styles = [`--sl-steps-start: ${rootElement.properties.start - 1}`]; + if (rootElement.properties.style) styles.push(String(rootElement.properties.style)); + rootElement.properties.style = styles.join(';'); + } }; }); -- cgit