File tree Expand file tree Collapse file tree
rules/valid-heading-anchor Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -66,6 +66,12 @@ const invalid: InvalidTestCase[] = [
6666 output : '# 中文标题 {#foo_bar123}' ,
6767 errors : [ { messageId : 'invalidHeadingAnchor' } ] ,
6868 } ,
69+ {
70+ description : 'should collapse multiple spaces in anchor fix' ,
71+ code : '# 中文标题 {#Chinese Title}' ,
72+ output : '# 中文标题 {#chinese-title}' ,
73+ errors : [ { messageId : 'missingAnchor' } ] ,
74+ } ,
6975]
7076
7177run ( {
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ export function hasChinese(str: string): boolean {
6161 */
6262export function normalizeAnchor ( anchor : string ) : string {
6363 const lowerCaseAnchor = anchor . toLowerCase ( )
64- const completeHyphen = lowerCaseAnchor . replace ( / [ \s . ] / g, '-' )
64+ const completeHyphen = lowerCaseAnchor . replace ( / [ \s . ] + / g, '-' )
6565 const keepLegalCharacters = completeHyphen . replace ( / [ ^ a - z 0 - 9 _ - ] / g, '' )
6666 return keepLegalCharacters . replace ( / ^ - + | - + $ / g, '' )
6767}
Original file line number Diff line number Diff line change @@ -85,6 +85,11 @@ describe('normalizeAnchor', () => {
8585 expect ( normalizeAnchor ( '中文-title' ) ) . toBe ( 'title' )
8686 } )
8787
88+ it ( 'should collapse repeated spaces into a single hyphen' , ( ) => {
89+ expect ( normalizeAnchor ( 'Your first test' ) ) . toBe ( 'your-first-test' )
90+ expect ( normalizeAnchor ( 'Your first test' ) ) . toBe ( 'your-first-test' )
91+ } )
92+
8893 it ( 'should preserve digits and hyphens' , ( ) => {
8994 expect ( normalizeAnchor ( 'intro-2' ) ) . toBe ( 'intro-2' )
9095 expect ( normalizeAnchor ( 'API-Reference_v2' ) ) . toBe ( 'api-reference_v2' )
You can’t perform that action at this time.
0 commit comments