Skip to content

Commit e9ab5e8

Browse files
committed
Better counters that fix forward refs using js on client
1 parent 2afe228 commit e9ab5e8

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/components/numbering/Counter.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ incrementCounter(type, name)
77
const counterText = getCounterText(type, name)
88
---
99

10-
<span id={counterId}>{counterText}</span>
10+
<span id={counterId} class={`counter--${type}`} data-type={type} data-name={name} data-text={counterText}>{counterText}</span>
1111

src/components/numbering/CountersTools.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,27 @@ export function resetCounters() {
3232

3333
export function resetCounter(type: string) {
3434
delete counters[type];
35-
}
35+
}
36+
37+
38+
39+
////
40+
41+
export const jsSetupCounteurs = function(type: string) {
42+
const texts = {} as Record<string, string>;
43+
document.querySelectorAll(`.counter--${type}`).forEach(el => {
44+
const label = `${el.getAttribute('data-type')}:${el.getAttribute('data-name')}`;
45+
texts[label] = el.getAttribute('data-text') || '';
46+
});
47+
document.querySelectorAll(`.counter-ref--${type}`).forEach(el => {
48+
const label = `${el.getAttribute('data-type')}:${el.getAttribute('data-name')}`;
49+
if (texts[label]) {
50+
el.textContent = texts[label];
51+
el.setAttribute('data-text', texts[label]);
52+
}
53+
});
54+
}
55+
56+
export const jsOnLoadSetupCounters = function(type: string) {
57+
return `window.addEventListener('load', () => { (${jsSetupCounteurs.toString()})('${type}') })`;
58+
}

src/components/numbering/Ref.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ const counterId = getCounterId(type, name);
66
const counterText = getCounterText(type, name);
77
---
88

9-
<a href={'#' + counterId}>{counterText}</a>
9+
<a href={'#' + counterId} class={`counter-ref--${type}`} data-type={type} data-name={name}>{counterText}</a>
1010

src/layouts/LessonLayout.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import FormattedDate from '../components/FormattedDate.astro';
77
import Header from '../components/Header.astro';
88
99
import '../styles/diffusion.css';
10-
import { resetCounters } from '../components/numbering/CountersTools';
10+
import { resetCounters, jsOnLoadSetupCounters } from '../components/numbering/CountersTools';
1111
1212
type Props = CollectionEntry<'blog'>['data'];
1313
1414
const { title, description, pubDate, updatedDate, heroImage } = Astro.props;
1515
1616
resetCounters();
17+
const onload = jsOnLoadSetupCounters('fig');
1718
---
1819

1920
<html lang="en">
@@ -63,6 +64,7 @@ resetCounters();
6364
<body>
6465
<Header />
6566
<main>
67+
<script set:html={onload}></script>
6668
<article>
6769
<div class="hero-image">
6870
{heroImage && <Image width={1020} height={510} src={heroImage} alt="" />}

0 commit comments

Comments
 (0)