mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-08-20 14:45:59 +00:00
69 lines
1.3 KiB
JavaScript
69 lines
1.3 KiB
JavaScript
/*\
|
|
title: $:/core/modules/parsers/wikiparser/rules/prettylink.js
|
|
type: application/javascript
|
|
module-type: wikirule
|
|
|
|
Wiki text inline rule for pretty links. For example:
|
|
|
|
```
|
|
[[Introduction]]
|
|
|
|
[[Link description|TiddlerTitle]]
|
|
```
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
exports.name = "prettylink";
|
|
exports.types = {inline: true};
|
|
|
|
exports.init = function(parser) {
|
|
this.parser = parser;
|
|
// Regexp to match
|
|
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;
|
|
if(isLinkExternal(link)) {
|
|
return [{
|
|
type: "element",
|
|
tag: "a",
|
|
attributes: {
|
|
href: {type: "string", value: link},
|
|
"class": {type: "string", value: "tw-tiddlylink-external"},
|
|
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
|
|
}]
|
|
}];
|
|
}
|
|
};
|
|
|
|
})();
|