Skip to content

Commit cb0efae

Browse files
makishvili3y3
authored andcommitted
feat(anchors): Add rel=nofollow for anchor links
1 parent 1cc04dc commit cb0efae

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/transform/plugins/anchors/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function createLinkTokens(state: StateCore, id: string, setId = false) {
2020
open.attrSet('href', '#' + id);
2121
open.attrSet('class', 'yfm-anchor');
2222
open.attrSet('aria-hidden', 'true');
23+
open.attrSet('rel', 'nofollow');
2324
text.content = '';
2425

2526
return [open, text, close];

test/anchors.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ describe('Anchors', () => {
2424

2525
it('should add single anchor', () => {
2626
expect(transformYfm('## Test {#test1}\n' + '\n' + 'Content\n')).toBe(
27-
'<h2 id="test1"><a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
27+
'<h2 id="test1"><a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
2828
'<p>Content</p>\n',
2929
);
3030
});
3131

3232
it('should add multiple anchors', () => {
3333
expect(transformYfm('## Test {#test1} {#test2} {#test3}\n' + '\n' + 'Content\n')).toBe(
3434
'<h2 id="test1">' +
35-
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true"></a>' +
36-
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true"></a>' +
37-
'<a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
35+
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
36+
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
37+
'<a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
3838
'<p>Content</p>\n',
3939
);
4040
});
@@ -49,9 +49,9 @@ describe('Anchors', () => {
4949
'{% include [test](./mocks/include-anchor.md) %}\n',
5050
),
5151
).toBe(
52-
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
52+
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
5353
'<p>Content before include</p>\n' +
54-
'<h1 id="test1"><a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Title</h1>\n' +
54+
'<h1 id="test1"><a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Title</h1>\n' +
5555
'<p>Content</p>\n',
5656
);
5757
});
@@ -66,20 +66,20 @@ describe('Anchors', () => {
6666
'{% include [test](./mocks/include-multiple-anchors.md) %}\n',
6767
),
6868
).toBe(
69-
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
69+
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
7070
'<p>Content before include</p>\n' +
7171
'<h1 id="test1">' +
72-
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true"></a>' +
73-
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true"></a>' +
74-
'<a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Title</h1>\n' +
72+
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
73+
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
74+
'<a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Title</h1>\n' +
7575
'<p>Content</p>\n',
7676
);
7777
});
7878

7979
it('should be transliterated correctly', () => {
8080
expect(transformYfm('## Максимальный размер дисков \n' + '\n' + 'Content\n')).toBe(
8181
'<h2 id="maksimalnyj-razmer-diskov">' +
82-
'<a href="#maksimalnyj-razmer-diskov" class="yfm-anchor" aria-hidden="true"></a>' +
82+
'<a href="#maksimalnyj-razmer-diskov" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
8383
'Максимальный размер дисков' +
8484
'</h2>\n' +
8585
'<p>Content</p>\n',
@@ -89,7 +89,7 @@ describe('Anchors', () => {
8989
it('should be removed fences after transliteration', () => {
9090
expect(transformYfm('## `Test`\n' + '\n' + 'Content\n')).toBe(
9191
'<h2 id="test">' +
92-
'<a href="#test" class="yfm-anchor" aria-hidden="true"></a><code>Test</code>' +
92+
'<a href="#test" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a><code>Test</code>' +
9393
'</h2>\n' +
9494
'<p>Content</p>\n',
9595
);
@@ -106,7 +106,7 @@ describe('Anchors', () => {
106106
),
107107
).toBe(
108108
'<p>Content before include</p>\n' +
109-
'<h2 id="anchor"><a href="#anchor" class="yfm-anchor" aria-hidden="true"></a>Subtitle</h2>\n' +
109+
'<h2 id="anchor"><a href="#anchor" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Subtitle</h2>\n' +
110110
'<p>Subcontent</p>\n' +
111111
'<p>After include</p>\n',
112112
);

0 commit comments

Comments
 (0)