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,19 +24,37 @@ exports.macro = {
|
|||||||
},
|
},
|
||||||
events: {
|
events: {
|
||||||
click: function(event,node,tiddler,store,params) {
|
click: function(event,node,tiddler,store,params) {
|
||||||
var navEvent = document.createEvent("Event");
|
if(isLinkExternal(params.target)) {
|
||||||
navEvent.initEvent("tw-navigate",true,true);
|
event.target.setAttribute("target","_blank");
|
||||||
navEvent.navigateTo = params.target;
|
return true;
|
||||||
node.dispatchEvent(navEvent);
|
} else {
|
||||||
event.preventDefault();
|
var navEvent = document.createEvent("Event");
|
||||||
return false;
|
navEvent.initEvent("tw-navigate",true,true);
|
||||||
|
navEvent.navigateTo = params.target;
|
||||||
|
node.dispatchEvent(navEvent);
|
||||||
|
event.preventDefault();
|
||||||
|
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