2012-12-15 17:35:35 +00:00
|
|
|
/*\
|
2012-12-20 16:41:06 +00:00
|
|
|
title: $:/core/modules/parsers/wikiparser/rules/prettylink.js
|
2012-12-15 17:35:35 +00:00
|
|
|
type: application/javascript
|
2012-12-20 16:02:03 +00:00
|
|
|
module-type: wikirule
|
2012-12-15 17:35:35 +00:00
|
|
|
|
2012-12-20 12:18:38 +00:00
|
|
|
Wiki text inline rule for pretty links. For example:
|
2012-12-15 17:35:35 +00:00
|
|
|
|
2012-12-22 23:09:44 +00:00
|
|
|
```
|
2012-12-15 17:35:35 +00:00
|
|
|
[[Introduction]]
|
|
|
|
|
|
|
|
[[Link description|TiddlerTitle]]
|
2012-12-22 23:09:44 +00:00
|
|
|
```
|
2012-12-15 17:35:35 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.name = "prettylink";
|
2012-12-20 16:02:03 +00:00
|
|
|
exports.types = {inline: true};
|
2012-12-15 17:35:35 +00:00
|
|
|
|
|
|
|
exports.init = function(parser) {
|
|
|
|
this.parser = parser;
|
|
|
|
// Regexp to match
|
|
|
|
this.matchRegExp = /\[\[(.*?)(?:\|(.*?))?\]\]/mg;
|
|
|
|
};
|
|
|
|
|
2013-10-24 10:54:54 +00:00
|
|
|
var isLinkExternal = function(to) {
|
2014-04-14 10:17:28 +00:00
|
|
|
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data|skype):[^\s<>{}\[\]`|'"\\^~]+(?:\/|\b)/i;
|
2013-10-24 10:54:54 +00:00
|
|
|
return externalRegExp.test(to);
|
|
|
|
};
|
|
|
|
|
2012-12-15 17:35:35 +00:00
|
|
|
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;
|
2013-10-24 10:54:54 +00:00
|
|
|
if(isLinkExternal(link)) {
|
|
|
|
return [{
|
|
|
|
type: "element",
|
|
|
|
tag: "a",
|
|
|
|
attributes: {
|
|
|
|
href: {type: "string", value: link},
|
2014-08-28 17:13:46 +00:00
|
|
|
"class": {type: "string", value: "tc-tiddlylink-external"},
|
2013-10-24 10:54:54 +00:00
|
|
|
target: {type: "string", value: "_blank"}
|
|
|
|
},
|
|
|
|
children: [{
|
|
|
|
type: "text", text: text
|
|
|
|
}]
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
return [{
|
2014-05-14 07:51:08 +00:00
|
|
|
type: "link",
|
2013-10-24 10:54:54 +00:00
|
|
|
attributes: {
|
|
|
|
to: {type: "string", value: link}
|
|
|
|
},
|
|
|
|
children: [{
|
|
|
|
type: "text", text: text
|
|
|
|
}]
|
|
|
|
}];
|
|
|
|
}
|
2012-12-15 17:35:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|