counters js
This commit is contained in:
parent
5b80e66e60
commit
4a95558237
5 changed files with 154 additions and 85 deletions
94
js/counters.js
Normal file
94
js/counters.js
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import { Handler } from '/csspageweaver/lib/paged.esm.js';
|
||||
|
||||
export default class counters extends Handler {
|
||||
constructor(chunker, polisher, caller) {
|
||||
super(chunker, polisher, caller);
|
||||
}
|
||||
|
||||
// Convertir en upper-alpha (A, B, C...)
|
||||
toUpperAlpha(num) {
|
||||
return String.fromCharCode(64 + num);
|
||||
}
|
||||
|
||||
// Convertir en lower-alpha (a, b, c...)
|
||||
toLowerAlpha(num) {
|
||||
return String.fromCharCode(96 + num);
|
||||
}
|
||||
|
||||
// Convertir en upper-roman (I, II, III...)
|
||||
toUpperRoman(num) {
|
||||
const romanNumerals = [
|
||||
['M', 1000], ['CM', 900], ['D', 500], ['CD', 400],
|
||||
['C', 100], ['XC', 90], ['L', 50], ['XL', 40],
|
||||
['X', 10], ['IX', 9], ['V', 5], ['IV', 4], ['I', 1]
|
||||
];
|
||||
let result = '';
|
||||
for (const [letter, value] of romanNumerals) {
|
||||
while (num >= value) {
|
||||
result += letter;
|
||||
num -= value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
beforeParsed(content) {
|
||||
const sectionContent = content.querySelector('#section__content');
|
||||
if (!sectionContent) return;
|
||||
|
||||
// Compteurs
|
||||
let h1Count = 0;
|
||||
let h2Count = 0;
|
||||
let h3Count = 0;
|
||||
let h4Count = 0;
|
||||
let h5Count = 0;
|
||||
let h6Count = 0;
|
||||
|
||||
// Parcourir tous les éléments dans l'ordre du document
|
||||
const allElements = sectionContent.querySelectorAll('h1, h2, h3, h4, h5, h6');
|
||||
|
||||
allElements.forEach(el => {
|
||||
const tagName = el.tagName.toLowerCase();
|
||||
|
||||
switch (tagName) {
|
||||
case 'h1':
|
||||
h1Count++;
|
||||
h2Count = 0; // reset h2
|
||||
el.setAttribute('data-counter', this.toUpperAlpha(h1Count));
|
||||
break;
|
||||
|
||||
case 'h2':
|
||||
h2Count++;
|
||||
h3Count = 0; // reset h3
|
||||
el.setAttribute('data-counter', this.toUpperRoman(h2Count));
|
||||
break;
|
||||
|
||||
case 'h3':
|
||||
h3Count++;
|
||||
h4Count = 0; // reset h4
|
||||
el.setAttribute('data-counter', h3Count.toString());
|
||||
break;
|
||||
|
||||
case 'h4':
|
||||
h4Count++;
|
||||
h5Count = 0; // reset h5
|
||||
el.setAttribute('data-counter', this.toLowerAlpha(h4Count));
|
||||
break;
|
||||
|
||||
case 'h5':
|
||||
h5Count++;
|
||||
h6Count = 0; // reset h6
|
||||
const letter = this.toLowerAlpha(h5Count);
|
||||
el.setAttribute('data-counter', letter + letter);
|
||||
break;
|
||||
|
||||
case 'h6':
|
||||
h6Count++;
|
||||
el.setAttribute('data-counter', '(' + h6Count + ')');
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue