1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 23:03:15 +00:00

Implement explicit external links

This commit is contained in:
Jermolene 2014-05-06 20:05:51 +01:00
parent abe0ce28b9
commit 854b739a35
4 changed files with 146 additions and 6 deletions

View File

@ -0,0 +1,112 @@
/*\
title: $:/core/modules/parsers/wikiparser/rules/prettyextlink.js
type: application/javascript
module-type: wikirule
Wiki text inline rule for external links. For example:
```
[ext[http://tiddlywiki.com/fractalveg.jpg]]
[ext[Tooltip|http://tiddlywiki.com/fractalveg.jpg]]
```
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "prettyextlink";
exports.types = {inline: true};
exports.init = function(parser) {
this.parser = parser;
};
exports.findNextMatch = function(startPos) {
// Find the next tag
this.nextLink = this.findNextLink(this.parser.source,startPos);
return this.nextLink ? this.nextLink.start : undefined;
};
exports.parse = function() {
// Move past the match
this.parser.pos = this.nextLink.end;
return [this.nextLink];
};
/*
Find the next link from the current position
*/
exports.findNextLink = function(source,pos) {
// A regexp for finding candidate links
var reLookahead = /(\[ext\[)/g;
// Find the next candidate
reLookahead.lastIndex = pos;
var match = reLookahead.exec(source);
while(match) {
// Try to parse the candidate as a link
var link = this.parseLink(source,match.index);
// Return success
if(link) {
return link;
}
// Look for the next match
reLookahead.lastIndex = match.index + 1;
match = reLookahead.exec(source);
}
// Failed
return null;
};
/*
Look for an link at the specified position. Returns null if not found, otherwise returns {type: "element", tag: "a", attributes: [], isSelfClosing:, start:, end:,}
*/
exports.parseLink = function(source,pos) {
var token,
textNode = {
type: "text"
},
node = {
type: "element",
tag: "a",
start: pos,
attributes: {},
children: [textNode]
};
// Skip whitespace
pos = $tw.utils.skipWhiteSpace(source,pos);
// Look for the `[ext[`
token = $tw.utils.parseTokenString(source,pos,"[ext[");
if(!token) {
return null;
}
pos = token.end;
// Look ahead for the terminating `]]`
var closePos = source.indexOf("]]",pos);
if(closePos === -1) {
return null;
}
// Look for a `|` separating the tooltip
var splitPos = source.indexOf("|",pos);
if(splitPos === -1 || splitPos > closePos) {
splitPos = null;
}
// Pull out the tooltip and URL
var tooltip, URL;
if(splitPos) {
URL = source.substring(splitPos + 1,closePos).trim();
textNode.text = source.substring(pos,splitPos).trim();
} else {
URL = source.substring(pos,closePos).trim();
textNode.text = URL;
}
node.attributes.href = {type: "string", value: URL};
node.attributes.target = {type: "string", value: "_blank"};
// Update the end position
node.end = closePos + 2;
return node;
};
})();

View File

@ -21,6 +21,7 @@ See [[Notes for upgrading to 5.0.11-beta]] for more details of these changes:
* Many performance optimisations, particularly for filter operations
* Added support for PermaLinks
* Added support for WikiLinks in Markdown via `[link text](#TiddlerTitle)`
* Added support for explicit external [[Linking in WikiText]] (eg `[ext[tooltip|url]]`)
* [[Replaced|https://github.com/Jermolene/TiddlyWiki5/issues/580]] hamburger menu icon with double chevron icon
* [[Enhance|https://github.com/Jermolene/TiddlyWiki5/commit/552657fc584dbb36754d3fcabca2cdef7e916ec9]] plain text parsing to use the CodeBlockWidget, and hence use syntax highlighting if the plugin is installed. This gives us syntax highlighting for JavaScript shadow tiddlers, amongst other things
* Improvements to Chinese, Japanese and French translations

View File

@ -23,7 +23,7 @@ The following additional features are planned or under consideration for impleme
** Tiddler object format (to provide true polymorphism of field values)
* Perfecting WikiText
** Global macros
** ~~`[img[url]]` for remote image embedding~~, and `[ext[url]]` for explicit external links
** ~~`[img[url]]` for remote image embedding, and `[ext[url]]` for explicit external links~~
** Further ~WikiText features
* Productivity features
** Import wizard allowing individual tiddlers to be selected for import

View File

@ -1,15 +1,42 @@
created: 20131205155230596
modified: 20131205155813974
modified: 20130506185813974
tags: wikitext
title: Linking in WikiText
type: text/vnd.tiddlywiki
A key capability of WikiText is the ability to make links to other tiddlers or to external websites. There are several ways of doing this:
* To link to a tiddler by title: `[[Tiddler Title]]`
* To link to a tiddler and specify the text of the link: `[[Displayed Link Title|Tiddler Title]]`
* For tiddler titles that match the CamelCase rules, just typing the title will automatically create a link
* To link to an external website, type the full URL of the site: `http://tiddlywiki.com/` or `[[TW5|http://tiddlywiki.com/]]`
Link to a tiddler by title:
```
[[Tiddler Title]]
```
To link to a tiddler and specify the text of the link:
```
[[Displayed Link Title|Tiddler Title]]
```
For tiddler titles that match the CamelCase rules, just typing the title will automatically create a link.
To link to an external website, type the full URL of the site:
```
http://tiddlywiki.com/
[[TW5|http://tiddlywiki.com/]]
```
For this syntax to work, the URL has to be recognisable as an URL, including a protocol such as `http://` or `file://`. You can force an external link with the extended syntax:
```
[ext[tiddlywiki.com]]
[ext[caption for link|tiddlywiki.com]]
[ext[Donate|bitcoin:1aabbdd....?amount=0.001]]
```
You can suppress a link from being recognised by preceding it with `~`. For example: