File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 1
1
use std:: borrow:: Cow ;
2
+ use std:: collections:: HashSet ;
2
3
3
4
use lol_html:: html_content:: ContentType ;
4
5
use lol_html:: { element, text, HtmlRewriter , Settings } ;
@@ -18,8 +19,33 @@ pub fn post_process_html<T: PageLike>(
18
19
sidebar : bool ,
19
20
) -> Result < String , DocError > {
20
21
let mut output = vec ! [ ] ;
22
+ let mut ids = HashSet :: new ( ) ;
21
23
22
24
let mut element_content_handlers = vec ! [
25
+ element!( "*[id]" , |el| {
26
+ if let Some ( id) = el. get_attribute( "id" ) {
27
+ if !ids. contains( id. as_str( ) ) {
28
+ let ( prefix, mut count) = if let Some ( ( prefix, counter) ) = id. rsplit_once( '_' ) {
29
+ if counter. chars( ) . all( |c| c. is_ascii_digit( ) ) {
30
+ let count = counter. parse:: <i64 >( ) . unwrap_or_default( ) + 1 ;
31
+ ( prefix, count)
32
+ } else {
33
+ ( id. as_str( ) , 2 )
34
+ }
35
+ } else {
36
+ ( id. as_str( ) , 2 )
37
+ } ;
38
+ let mut id = format!( "{prefix}_{count}" ) ;
39
+ while !ids. insert( id) && count < 666 {
40
+ count += 1 ;
41
+ id = format!( "{prefix}_{count}" ) ;
42
+ }
43
+ } else {
44
+ ids. insert( id) ;
45
+ }
46
+ }
47
+ Ok ( ( ) )
48
+ } ) ,
23
49
element!( "img:not([loading])" , |el| {
24
50
el. set_attribute( "loading" , "lazy" ) ?;
25
51
Ok ( ( ) )
Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ impl Anchorizer {
123
123
let anchor = if uniq == 0 {
124
124
Cow :: from ( & id)
125
125
} else {
126
- Cow :: from ( format ! ( "{}_{}" , id, uniq) )
126
+ Cow :: from ( format ! ( "{}_{}" , id, uniq + 1 ) )
127
127
} ;
128
128
129
129
if !self . 0 . contains ( & * anchor) {
You can’t perform that action at this time.
0 commit comments