@@ -21,9 +21,10 @@ pub fn collapse_whitespace<'a>(text: &'a str) -> Cow<'a, str> {
2121 RE . replace_all ( text, " " )
2222}
2323
24- /// Convert the given string to a valid HTML element ID
24+ /// Convert the given string to a valid HTML element ID.
25+ /// The only restriction is that the ID must not contain any ASCII whitespace.
2526pub fn normalize_id ( content : & str ) -> String {
26- let mut ret = content
27+ content
2728 . chars ( )
2829 . filter_map ( |ch| {
2930 if ch. is_alphanumeric ( ) || ch == '_' || ch == '-' {
@@ -33,16 +34,7 @@ pub fn normalize_id(content: &str) -> String {
3334 } else {
3435 None
3536 }
36- } ) . collect :: < String > ( ) ;
37- // Ensure that the first character is [A-Za-z]
38- if ret
39- . chars ( )
40- . next ( )
41- . map_or ( false , |c| !c. is_ascii_alphabetic ( ) )
42- {
43- ret. insert ( 0 , 'a' ) ;
44- }
45- ret
37+ } ) . collect :: < String > ( )
4638}
4739
4840/// Generate an ID for use with anchors which is derived from a "normalised"
@@ -328,28 +320,51 @@ more text with spaces
328320
329321 #[ test]
330322 fn it_generates_anchors ( ) {
323+ assert_eq ! (
324+ id_from_content( "## Method-call expressions" ) ,
325+ "method-call-expressions"
326+ ) ;
327+ assert_eq ! (
328+ id_from_content( "## **Bold** title" ) ,
329+ "bold-title"
330+ ) ;
331+ assert_eq ! (
332+ id_from_content( "## `Code` title" ) ,
333+ "code-title"
334+ ) ;
335+ }
336+
337+ #[ test]
338+ fn it_generates_anchors_from_non_ascii_initial ( ) {
331339 assert_eq ! (
332340 id_from_content( "## `--passes`: add more rustdoc passes" ) ,
333- "a --passes-add-more-rustdoc-passes"
341+ "--passes-add-more-rustdoc-passes"
334342 ) ;
335343 assert_eq ! (
336- id_from_content( "## Method-call expressions" ) ,
337- "method-call-expressions"
344+ id_from_content( "## 中文標題 CJK title" ) ,
345+ "中文標題-cjk-title"
346+ ) ;
347+ assert_eq ! (
348+ id_from_content( "## Über" ) ,
349+ "Über"
338350 ) ;
339351 }
340352
341353 #[ test]
342354 fn it_normalizes_ids ( ) {
343355 assert_eq ! (
344356 normalize_id( "`--passes`: add more rustdoc passes" ) ,
345- "a --passes-add-more-rustdoc-passes"
357+ "--passes-add-more-rustdoc-passes"
346358 ) ;
347359 assert_eq ! (
348360 normalize_id( "Method-call 🐙 expressions \u{1f47c} " ) ,
349361 "method-call--expressions-"
350362 ) ;
351- assert_eq ! ( normalize_id( "_-_12345" ) , "a_-_12345" ) ;
352- assert_eq ! ( normalize_id( "12345" ) , "a12345" ) ;
363+ assert_eq ! ( normalize_id( "_-_12345" ) , "_-_12345" ) ;
364+ assert_eq ! ( normalize_id( "12345" ) , "12345" ) ;
365+ assert_eq ! ( normalize_id( "中文" ) , "中文" ) ;
366+ assert_eq ! ( normalize_id( "にほんご" ) , "にほんご" ) ;
367+ assert_eq ! ( normalize_id( "한국어" ) , "한국어" ) ;
353368 assert_eq ! ( normalize_id( "" ) , "" ) ;
354369 }
355370 }
0 commit comments