Skip to content

Commit

Permalink
feat(anchors): Add rel=nofollow for anchor links
Browse files Browse the repository at this point in the history
  • Loading branch information
makishvili authored and 3y3 committed Jul 5, 2023
1 parent 1cc04dc commit cb0efae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/transform/plugins/anchors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function createLinkTokens(state: StateCore, id: string, setId = false) {
open.attrSet('href', '#' + id);
open.attrSet('class', 'yfm-anchor');
open.attrSet('aria-hidden', 'true');
open.attrSet('rel', 'nofollow');
text.content = '';

return [open, text, close];
Expand Down
26 changes: 13 additions & 13 deletions test/anchors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ describe('Anchors', () => {

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

it('should add multiple anchors', () => {
expect(transformYfm('## Test {#test1} {#test2} {#test3}\n' + '\n' + 'Content\n')).toBe(
'<h2 id="test1">' +
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true"></a>' +
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true"></a>' +
'<a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
'<a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
'<p>Content</p>\n',
);
});
Expand All @@ -49,9 +49,9 @@ describe('Anchors', () => {
'{% include [test](./mocks/include-anchor.md) %}\n',
),
).toBe(
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
'<p>Content before include</p>\n' +
'<h1 id="test1"><a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Title</h1>\n' +
'<h1 id="test1"><a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Title</h1>\n' +
'<p>Content</p>\n',
);
});
Expand All @@ -66,20 +66,20 @@ describe('Anchors', () => {
'{% include [test](./mocks/include-multiple-anchors.md) %}\n',
),
).toBe(
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true"></a>Test</h2>\n' +
'<h2 id="test0"><a href="#test0" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Test</h2>\n' +
'<p>Content before include</p>\n' +
'<h1 id="test1">' +
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true"></a>' +
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true"></a>' +
'<a href="#test1" class="yfm-anchor" aria-hidden="true"></a>Title</h1>\n' +
'<a id="test3" href="#test3" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
'<a id="test2" href="#test2" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
'<a href="#test1" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Title</h1>\n' +
'<p>Content</p>\n',
);
});

it('should be transliterated correctly', () => {
expect(transformYfm('## Максимальный размер дисков \n' + '\n' + 'Content\n')).toBe(
'<h2 id="maksimalnyj-razmer-diskov">' +
'<a href="#maksimalnyj-razmer-diskov" class="yfm-anchor" aria-hidden="true"></a>' +
'<a href="#maksimalnyj-razmer-diskov" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>' +
'Максимальный размер дисков' +
'</h2>\n' +
'<p>Content</p>\n',
Expand All @@ -89,7 +89,7 @@ describe('Anchors', () => {
it('should be removed fences after transliteration', () => {
expect(transformYfm('## `Test`\n' + '\n' + 'Content\n')).toBe(
'<h2 id="test">' +
'<a href="#test" class="yfm-anchor" aria-hidden="true"></a><code>Test</code>' +
'<a href="#test" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a><code>Test</code>' +
'</h2>\n' +
'<p>Content</p>\n',
);
Expand All @@ -106,7 +106,7 @@ describe('Anchors', () => {
),
).toBe(
'<p>Content before include</p>\n' +
'<h2 id="anchor"><a href="#anchor" class="yfm-anchor" aria-hidden="true"></a>Subtitle</h2>\n' +
'<h2 id="anchor"><a href="#anchor" class="yfm-anchor" aria-hidden="true" rel="nofollow"></a>Subtitle</h2>\n' +
'<p>Subcontent</p>\n' +
'<p>After include</p>\n',
);
Expand Down

0 comments on commit cb0efae

Please sign in to comment.