Get external links working again

We no longer use the `<$link>` widget for external links. Instead the
parser generates `<a>` elements. This makes things simpler, but does
mean that the `target=_blank` behaviour is baked into the parser.
Probably we should introduce a new `<$extlink>` widget that generates
an `<a>` element with a configurable `target` attribute
This commit is contained in:
Jeremy Ruston
2013-10-24 11:54:54 +01:00
parent d3df2c5860
commit 632970cd86
4 changed files with 44 additions and 24 deletions
@@ -26,7 +26,7 @@ exports.types = {inline: true};
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /~?(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"<>]+(?:\/|\b)/mg;
this.matchRegExp = /~?(?:file|http|https|mailto|ftp|irc|news|data|skype):[^\s'"<>]+(?:\/|\b)/mg;
};
exports.parse = function() {
@@ -38,9 +38,10 @@ exports.parse = function() {
} else {
return [{
type: "element",
tag: "$link",
tag: "a",
attributes: {
to: {type: "string", value: this.match[0]}
href: {type: "string", value: this.match[0]},
target: {type: "string", value: "_blank"}
},
children: [{
type: "text", text: this.match[0]
@@ -27,22 +27,41 @@ exports.init = function(parser) {
this.matchRegExp = /\[\[(.*?)(?:\|(.*?))?\]\]/mg;
};
var isLinkExternal = function(to) {
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data|skype):[^\s'"]+(?:\/|\b)/i;
return externalRegExp.test(to);
};
exports.parse = function() {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Process the link
var text = this.match[1],
link = this.match[2] || text;
return [{
type: "element",
tag: "$link",
attributes: {
to: {type: "string", value: link}
},
children: [{
type: "text", text: text
}]
}];
if(isLinkExternal(link)) {
return [{
type: "element",
tag: "a",
attributes: {
href: {type: "string", value: link},
target: {type: "string", value: "_blank"}
},
children: [{
type: "text", text: text
}]
}];
} else {
return [{
type: "element",
tag: "$link",
attributes: {
to: {type: "string", value: link}
},
children: [{
type: "text", text: text
}]
}];
}
};
})();