@@ -5,6 +5,7 @@ import {headingInfo} from '../../utils';
5
5
import { CUSTOM_ID_REGEXP , CUSTOM_ID_EXCEPTION } from './constants' ;
6
6
import StateCore from 'markdown-it/lib/rules_core/state_core' ;
7
7
import Token from 'markdown-it/lib/token' ;
8
+ import { escapeHtml } from 'markdown-it/lib/common/utils' ;
8
9
import { MarkdownItPluginCb } from '../typings' ;
9
10
10
11
const slugify : ( str : string , opts : { } ) => string = require ( 'slugify' ) ;
@@ -21,14 +22,11 @@ function createLinkTokens(state: StateCore, id: string, title: string, setId = f
21
22
open . attrSet ( 'aria-hidden' , 'true' ) ;
22
23
23
24
// SEO: render invisible heading title because link must have text content.
24
- const spanOpen = new state . Token ( 'span_open' , 'span' , 1 ) ;
25
- const spanText = new state . Token ( 'text' , '' , 0 ) ;
26
- const spanClose = new state . Token ( 'span_close' , 'span' , - 1 ) ;
27
- spanOpen . attrSet ( 'class' , 'visually-hidden' ) ;
28
- spanText . content = title ;
29
- spanText . meta = { hidden : true } ;
30
-
31
- return [ open , spanOpen , spanText , spanClose , close ] ;
25
+ const hiddenDesc = new state . Token ( 'anchor_hidden_desc' , '' , 0 ) ;
26
+
27
+ hiddenDesc . content = title ;
28
+
29
+ return [ open , hiddenDesc , close ] ;
32
30
}
33
31
34
32
const getCustomIds = ( content : string ) => {
@@ -93,6 +91,7 @@ const index: MarkdownItPluginCb<Options> = (
93
91
94
92
if ( isHeading ) {
95
93
const { title, level} = headingInfo ( tokens , i ) ;
94
+
96
95
const inlineToken = tokens [ i + 1 ] ;
97
96
let id = token . attrGet ( 'id' ) ;
98
97
let ghId : string ;
@@ -158,6 +157,10 @@ const index: MarkdownItPluginCb<Options> = (
158
157
md . core . ruler . push ( 'anchors' , plugin ) ;
159
158
}
160
159
}
160
+
161
+ md . renderer . rules . anchor_hidden_desc = function ( tokens , index ) {
162
+ return '<span class="visually-hidden">' + escapeHtml ( tokens [ index ] . content ) + '</span>' ;
163
+ } ;
161
164
} ;
162
165
163
166
export = index ;
0 commit comments