mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-19 00:04:51 +00:00
Refactored link macro
To handle external links correctly, and URI encode internal links
This commit is contained in:
parent
210f1bcd09
commit
e8a93fa56b
@ -10,6 +10,11 @@ title: js/macros/link.js
|
|||||||
var HTML = require("../HTML.js").HTML,
|
var HTML = require("../HTML.js").HTML,
|
||||||
utils = require("../Utils.js");
|
utils = require("../Utils.js");
|
||||||
|
|
||||||
|
var isLinkExternal = function(target) {
|
||||||
|
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/i;
|
||||||
|
return externalRegExp.test(target);
|
||||||
|
};
|
||||||
|
|
||||||
exports.macro = {
|
exports.macro = {
|
||||||
name: "link",
|
name: "link",
|
||||||
wrapperTag: "span",
|
wrapperTag: "span",
|
||||||
@ -19,6 +24,10 @@ exports.macro = {
|
|||||||
},
|
},
|
||||||
events: {
|
events: {
|
||||||
click: function(event,node,tiddler,store,params) {
|
click: function(event,node,tiddler,store,params) {
|
||||||
|
if(isLinkExternal(params.target)) {
|
||||||
|
event.target.setAttribute("target","_blank");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
var navEvent = document.createEvent("Event");
|
var navEvent = document.createEvent("Event");
|
||||||
navEvent.initEvent("tw-navigate",true,true);
|
navEvent.initEvent("tw-navigate",true,true);
|
||||||
navEvent.navigateTo = params.target;
|
navEvent.navigateTo = params.target;
|
||||||
@ -26,12 +35,26 @@ exports.macro = {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
render: function(type,tiddler,store,params,content) {
|
render: function(type,tiddler,store,params,content) {
|
||||||
|
var classes = ["tw-tiddlylink"],
|
||||||
|
target = params.target;
|
||||||
|
if(isLinkExternal(params.target)) {
|
||||||
|
classes.push("tw-tiddlylink-external");
|
||||||
|
} else {
|
||||||
|
classes.push("tw-tiddlylink-internal");
|
||||||
|
if(store.tiddlerExists(params.target)) {
|
||||||
|
classes.push("tw-tiddlylink-resolves");
|
||||||
|
} else {
|
||||||
|
classes.push("tw-tiddlylink-missing");
|
||||||
|
}
|
||||||
|
target = encodeURIComponent(target);
|
||||||
|
}
|
||||||
return HTML(HTML.elem(
|
return HTML(HTML.elem(
|
||||||
"a",{
|
"a",{
|
||||||
href: params.target,
|
href: target,
|
||||||
"class": store.adjustClassesForLink([],params.target)
|
"class": classes
|
||||||
},[
|
},[
|
||||||
HTML.raw(content)
|
HTML.raw(content)
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user