This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rel=nofollow to links if the
nofollow
option is set (#426)
BREAKING CHANGE: links now default to having rel=nofollow set
- Loading branch information
Showing
11 changed files
with
222 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Set rel=nofollow on all links if we have set `nofollow` in the options. | ||
|
||
module.exports = function (md, options) { | ||
var defaultRender = md.renderer.rules.link_open || function (tokens, idx, options, env, self) { | ||
return self.renderToken(tokens, idx, options) | ||
} | ||
|
||
md.renderer.rules.link_open = function handleLink (tokens, idx, options, env, self) { | ||
tokens[idx].attrPush(['rel', 'nofollow']) | ||
return defaultRender(tokens, idx, options, env, self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
/* globals describe, it */ | ||
|
||
var assert = require('assert') | ||
var marky = require('..') | ||
var cheerio = require('cheerio') | ||
|
||
describe('nofollow plugin', function () { | ||
it('adds rel=nofollow attributes to links', function () { | ||
var rendered = cheerio.load(marky('[link text](https://example.com/spam)')) | ||
assert.equal(rendered('a').attr('rel'), 'nofollow') | ||
}) | ||
|
||
it('respects the option to turn off nofollow', function () { | ||
var rendered = cheerio.load(marky('[link text](https://example.com/spam)', { nofollow: false })) | ||
assert.equal(rendered('a').attr('rel'), undefined) | ||
}) | ||
|
||
it('adds rel=nofollow attributes to html links', function () { | ||
var rendered = cheerio.load(marky('<a href=https://example.com/spam>link text</a>')) | ||
assert.equal(rendered('a').attr('rel'), 'nofollow') | ||
}) | ||
|
||
it('respects the option to turn off nofollow for html', function () { | ||
var rendered = cheerio.load(marky('<a href=https://example.com/spam>link text</a>', { nofollow: false })) | ||
assert.equal(rendered('a').attr('rel'), undefined) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.