mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-09-19 00:00:43 +00:00
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:
@@ -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
|
||||
}]
|
||||
}];
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user