Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mailto: handling of links #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function externalLinksPlugin(md, options) {
let externalClassName = (typeof options.externalClassName === "string" || options.externalClassName === null)
? options.externalClassName
: "external-link";
let mailtoClassName = (typeof options.mailtoClassName === "string" || options.mailtoClassName === null)
? options.mailtoClassName
: "mailto-link";
let internalClassName = (typeof options.internalClassName === "string" || options.internalClassName === null)
? options.internalClassName
: null;
Expand All @@ -18,9 +21,11 @@ function externalLinksPlugin(md, options) {
: [];

let externalTarget = options.externalTarget || "_self";
let mailtoTarget = options.mailtoTarget || "_blank";
let internalTarget = options.internalTarget || "_self";

let externalRel = options.externalRel || null;
let mailtoRel = options.mailtoRel || null;
let internalRel = options.internalRel || null;

if (externalClassName === null && internalClassName === null
Expand All @@ -39,8 +44,24 @@ function externalLinksPlugin(md, options) {
if (token.type === "link_open") {
let href = token.attrGet("href");
let internal = isInternalLink(href);
let mailto = isMailtoLink(href);

let newClasses, target, rel;

if (internal) {
newClasses = internalClassName;
target = internalTarget;
rel = internalRel;
} else if (mailto) {
newClasses = mailtoClassName;
target = mailtoTarget;
rel = mailtoRel;
} else {
newClasses = externalClassName;
target = externalTarget;
rel = externalRel;
}

let newClasses = internal ? internalClassName : externalClassName;
if (newClasses) {
let existingClasses = token.attrGet("class") || "";
if (existingClasses !== "") {
Expand All @@ -49,12 +70,10 @@ function externalLinksPlugin(md, options) {
token.attrSet("class", newClasses);
}

let target = internal ? internalTarget : externalTarget;
if (target !== "_self") {
token.attrSet("target", target);
}

let rel = internal ? internalRel : externalRel;
if (rel) {
let existingRel = token.attrGet("rel") || "";
if (existingRel !== "") {
Expand All @@ -69,6 +88,9 @@ function externalLinksPlugin(md, options) {
}

function isInternalLink(href) {
if (isMailtoLink(href)) {
return false;
}
let domain = getDomain(href);
return domain === null || internalDomains.indexOf(domain) !== -1;
}
Expand All @@ -82,6 +104,13 @@ function externalLinksPlugin(md, options) {
return null;
}

function isMailtoLink(href) {
// regex taken from https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript
let regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; // eslint-disable-line
let emailAddress = href.indexOf("mailto:") !== -1 ? href.split(":")[1] : href;
return regex.test(emailAddress);
}

md.core.ruler.push("external_links", externalLinks);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-it-external-links",
"version": "0.0.6",
"version": "0.0.7",
"description": "Plugin for markdown-it that adds CSS classes to links that fall outside of the specified internal domain(s).",
"repository": {
"type": "git",
Expand Down
14 changes: 14 additions & 0 deletions test/spec/plugin-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,18 @@ describe("markdown-it-external-links", function () {
.should.be.eql(expected);
});

given(
"mailto:[email protected]"
).it("adds custom class to external links", function (href) {
let source = `Text with [link](${href}).`;
let expected = `<p>Text with <a href="${href}" class="custom-mailto-class" target="_blank">link</a>.</p>`;

let markdownProcessor = markdownIt().use(externalLinks, {
mailtoClassName: "custom-mailto-class"
});

markdownProcessor.render(source)
.trim()
.should.be.eql(expected);
});
});