mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-09-18 02:09:43 +00:00
632970cd86
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
54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
/*\
|
|
title: $:/core/modules/parsers/wikiparser/rules/extlink.js
|
|
type: application/javascript
|
|
module-type: wikirule
|
|
|
|
Wiki text inline rule for external links. For example:
|
|
|
|
```
|
|
An external link: http://www.tiddlywiki.com/
|
|
|
|
A suppressed external link: ~http://www.tiddlyspace.com/
|
|
```
|
|
|
|
External links can be suppressed by preceding them with `~`.
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
exports.name = "extlink";
|
|
exports.types = {inline: true};
|
|
|
|
exports.init = function(parser) {
|
|
this.parser = parser;
|
|
// Regexp to match
|
|
this.matchRegExp = /~?(?:file|http|https|mailto|ftp|irc|news|data|skype):[^\s'"<>]+(?:\/|\b)/mg;
|
|
};
|
|
|
|
exports.parse = function() {
|
|
// Move past the match
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
|
// Create the link unless it is suppressed
|
|
if(this.match[0].substr(0,1) === "~") {
|
|
return [{type: "text", text: this.match[0].substr(1)}];
|
|
} else {
|
|
return [{
|
|
type: "element",
|
|
tag: "a",
|
|
attributes: {
|
|
href: {type: "string", value: this.match[0]},
|
|
target: {type: "string", value: "_blank"}
|
|
},
|
|
children: [{
|
|
type: "text", text: this.match[0]
|
|
}]
|
|
}];
|
|
}
|
|
};
|
|
|
|
})();
|