fix(console): url recognition

This commit is contained in:
surunzi
2020-01-03 11:39:43 +08:00
parent 1937c0d33e
commit c6b7a4668c
3 changed files with 124 additions and 98 deletions

View File

@@ -1239,10 +1239,6 @@ Escape special chars to be used as literals in RegExp constructors.
escapeRegExp('[licia]'); // -> '\\[licia\\]'
```
## evalCss
Eval css.
## extend
Copy all of the properties in the source objects over to the destination object.
@@ -1271,6 +1267,20 @@ Like extend, but only copies own properties over to the destination object.
extendOwn({name: 'RedHood'}, {age: 24}); // -> {name: 'RedHood', age: 24}
```
## extractUrls
Extract urls from plain text.
|Name |Type |Desc |
|------|------|---------------|
|str |string|Text to extract|
|return|array |Url list |
```javascript
const str = '[Official site: http://eustia.liriliri.io](http://eustia.liriliri.io)';
extractUrls(str); // -> ['http://eustia.liriliri.io']
```
## fileSize
Turn bytes into human readable file size.
@@ -1837,6 +1847,24 @@ Get the last element of array.
last([1, 2]); // -> 2
```
## linkify
Hyperlink urls in a string.
|Name |Type |Desc |
|-----------|--------|-------------------------|
|str |string |String to hyperlink |
|[hyperlink]|function|Function to hyperlink url|
|return |string |Result string |
```javascript
const str = 'Official site: http://eustia.liriliri.io'
linkify(str); // -> 'Official site: <a href="http://eustia.liriliri.io">http://eustia.liriliri.io</a>'
linkify(str, function (url) {
return '<a href="' + url + '" target="_blank">' + url + '</a>';
});
```
## loadJs
Inject script tag into page with given src value.
@@ -2532,23 +2560,6 @@ trim('_abc_', '_'); // -> 'abc'
trim('_abc_', ['a', 'c', '_']); // -> 'b'
```
## tryIt
Run function in a try catch.
|Name|Type |Desc |
|----|--------|---------------------|
|fn |function|Function to try catch|
|[cb]|function|Callback |
```javascript
tryIt(function () {
// Do something that might cause an error.
}, function (err, result) {
if (err) console.log(err);
});
```
## type
Determine the internal JavaScript [[Class]] of an object.

View File

@@ -34,7 +34,8 @@ import {
$,
Emitter,
stringifyAll,
nextTick
nextTick,
linkify
} from '../lib/util'
import evalCss from '../lib/evalCss'
@@ -305,13 +306,18 @@ export default class Log extends Emitter {
break
}
if (type !== 'error') msg = recognizeUrl(msg)
msg = render({ msg, type, icon, id, displayHeader, time, from, group })
if (!this._needSrc() || !Log.lazyEvaluation) {
delete this.args
}
// Only linkify for simple types
if (type !== 'error' && !this.args) {
msg = linkify(msg, url => {
return `<a href="${url}" target="_blank">${url}</a>`
})
}
msg = render({ msg, type, icon, id, displayHeader, time, from, group })
this._$el.addClass('eruda-log-container').html(msg)
this._$content = this._$el.find('.eruda-log-content')
this._content = this._$content.get(0)
@@ -556,11 +562,6 @@ function formatEl(val) {
)}</pre>`
}
const regUrl = /((?:https?|ftp):\/\/[-A-Z0-9+\u0026\u2019@#/%?=()~_|!:,.;]*[-A-Z0-9+\u0026@#/%=~()_|])/gi
const recognizeUrl = str =>
str.replace(regUrl, '<a href="$1" target="_blank">$1</a>')
function getFrom() {
const e = new Error()
let ret = ''

View File

@@ -1321,33 +1321,6 @@ export var escapeRegExp = _.escapeRegExp = (function (exports) {
return exports;
})({});
/* ------------------------------ evalCss ------------------------------ */
export var evalCss = _.evalCss = (function (exports) {
/* Load css into page.
*
* |Name|Type |Desc |
* |----|------|--------|
* |css |string|Css code|
*/
/* example
* evalCss('body{background:#08c}');
*/
/* typescript
* export declare function evalCss(css: string): void;
*/
exports = function(css) {
var style = document.createElement('style');
style.textContent = css;
style.type = 'text/css';
document.head.appendChild(style);
};
return exports;
})({});
/* ------------------------------ fileSize ------------------------------ */
export var fileSize = _.fileSize = (function (exports) {
@@ -7197,6 +7170,88 @@ export var trim = _.trim = (function (exports) {
return exports;
})({});
/* ------------------------------ extractUrls ------------------------------ */
export var extractUrls = _.extractUrls = (function (exports) {
/* Extract urls from plain text.
*
* |Name |Type |Desc |
* |------|------|---------------|
* |str |string|Text to extract|
* |return|array |Url list |
*/
/* example
* const str = '[Official site: http://eustia.liriliri.io](http://eustia.liriliri.io)';
* extractUrls(str); // -> ['http://eustia.liriliri.io']
*/
/* typescript
* export declare function extractUrls(str: string): string[];
*/
/* dependencies
* unique trim map toArr
*/
exports = function(str) {
var urlList = toArr(str.match(regUrl));
return unique(
map(urlList, function(url) {
return trim(url);
})
);
};
var regUrl = /((https?)|(ftp)):\/\/[\w.]+[^ \f\n\r\t\v"\\<>[\]\u2100-\uFFFF(),]*/gi;
return exports;
})({});
/* ------------------------------ linkify ------------------------------ */
export var linkify = _.linkify = (function (exports) {
/* Hyperlink urls in a string.
*
* |Name |Type |Desc |
* |-----------|--------|-------------------------|
* |str |string |String to hyperlink |
* |[hyperlink]|function|Function to hyperlink url|
* |return |string |Result string |
*/
/* example
* const str = 'Official site: http://eustia.liriliri.io'
* linkify(str); // -> 'Official site: <a href="http://eustia.liriliri.io">http://eustia.liriliri.io</a>'
* linkify(str, function (url) {
* return '<a href="' + url + '" target="_blank">' + url + '</a>';
* });
*/
/* typescript
* export declare function linkify(str: string, hyperlink?: Function): string;
*/
/* dependencies
* extractUrls each escapeRegExp
*/
exports = function(str, hyperlink) {
hyperlink = hyperlink || defHyperlink;
var urlList = extractUrls(str);
each(urlList, function(url) {
str = str.replace(new RegExp(escapeRegExp(url), 'g'), hyperlink);
});
return str;
};
function defHyperlink(url) {
return '<a href="' + url + '">' + url + '</a>';
}
return exports;
})({});
/* ------------------------------ query ------------------------------ */
export var query = _.query = (function (exports) {
@@ -8253,47 +8308,6 @@ export var throttle = _.throttle = (function (exports) {
return exports;
})({});
/* ------------------------------ tryIt ------------------------------ */
export var tryIt = _.tryIt = (function (exports) {
/* Run function in a try catch.
*
* |Name|Type |Desc |
* |----|--------|---------------------|
* |fn |function|Function to try catch|
* |[cb]|function|Callback |
*/
/* example
* tryIt(function () {
* // Do something that might cause an error.
* }, function (err, result) {
* if (err) console.log(err);
* });
*/
/* typescript
* export declare function tryIt(fn: Function, cb?: Function): void;
*/
/* dependencies
* noop
*/
exports = function(fn, cb) {
cb = cb || noop;
try {
cb(null, fn());
} catch (e) {
cb(e);
return;
}
};
return exports;
})({});
/* ------------------------------ uncaught ------------------------------ */
export var uncaught = _.uncaught = (function (exports) {