Skip to content

Commit 6611151

Browse files
3y3k03y3
3y3k0
authored andcommitted
fix: link breaks anchored header since version
Fixes #253
1 parent 532a8cd commit 6611151

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/transform/headings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Heading} from './typings';
44
function getTitle(token: Token) {
55
return (
66
token.children?.reduce((acc, tok) => {
7-
if ((tok.type === 'text' && !tok.meta?.hidden) || tok.type === 'code_inline') {
7+
if (tok.type === 'text' || tok.type === 'code_inline') {
88
return acc + tok.content;
99
}
1010

src/transform/plugins/anchors/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {headingInfo} from '../../utils';
55
import {CUSTOM_ID_REGEXP, CUSTOM_ID_EXCEPTION} from './constants';
66
import StateCore from 'markdown-it/lib/rules_core/state_core';
77
import Token from 'markdown-it/lib/token';
8+
import {escapeHtml} from 'markdown-it/lib/common/utils';
89
import {MarkdownItPluginCb} from '../typings';
910

1011
const slugify: (str: string, opts: {}) => string = require('slugify');
@@ -21,14 +22,11 @@ function createLinkTokens(state: StateCore, id: string, title: string, setId = f
2122
open.attrSet('aria-hidden', 'true');
2223

2324
// 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];
3230
}
3331

3432
const getCustomIds = (content: string) => {
@@ -93,6 +91,7 @@ const index: MarkdownItPluginCb<Options> = (
9391

9492
if (isHeading) {
9593
const {title, level} = headingInfo(tokens, i);
94+
9695
const inlineToken = tokens[i + 1];
9796
let id = token.attrGet('id');
9897
let ghId: string;
@@ -158,6 +157,10 @@ const index: MarkdownItPluginCb<Options> = (
158157
md.core.ruler.push('anchors', plugin);
159158
}
160159
}
160+
161+
md.renderer.rules.anchor_hidden_desc = function (tokens, index) {
162+
return '<span class="visually-hidden">' + escapeHtml(tokens[index].content) + '</span>';
163+
};
161164
};
162165

163166
export = index;

0 commit comments

Comments
 (0)