Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to import list w/o validate #134

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 90 additions & 79 deletions dist/browser/anchorme.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,81 +61,6 @@
dictionary.closingParenthesis;
dictionary.openingParenthesis;

var transform_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.transform = void 0;
function applyOption(string, props, option) {
// conditional
if (typeof option === "function") {
return option(string, props);
}
// all
else {
return option;
}
}
function transform(input, options) {
var protocol = "";
var truncation = Infinity;
var attributes = {};
var truncateFromTheMiddle = false;
// special transformation
if (options && options.specialTransform) {
for (var index = 0; index < options.specialTransform.length; index++) {
var transformer = options.specialTransform[index];
if (transformer.test.test(input.string)) {
return transformer.transform(input.string, input);
}
}
}
// exclude
if (options && options.exclude) {
if (applyOption(input.string, input, options.exclude))
{ return input.string; }
}
// protocol
if (options && options.protocol) {
protocol = applyOption(input.string, input, options.protocol);
}
if (input.protocol) {
protocol = "";
}
else if (!protocol) {
protocol = input.isEmail
? "mailto:"
: input.isFile
? "file:///"
: "http://";
}
// truncation
if (options && options.truncate) {
truncation = applyOption(input.string, input, options.truncate);
}
if (options && options.middleTruncation) {
truncateFromTheMiddle = applyOption(input.string, input, options.middleTruncation);
}
// attributes
if (options && options.attributes) {
attributes = applyOption(input.string, input, options.attributes);
}
return "<a ".concat(Object.keys(attributes)
.map(function (key) {
return attributes[key] === true ? key : "".concat(key, "=\"").concat(attributes[key], "\" ");
})
.join(" "), "href=\"").concat(protocol).concat(input.string, "\">").concat(input.string.length > truncation
? truncateFromTheMiddle
? input.string.substring(0, Math.floor(truncation / 2)) +
"…" +
input.string.substring(input.string.length - Math.ceil(truncation / 2), input.string.length)
: input.string.substring(0, truncation) + "…"
: input.string, "</a>");
}
exports.transform = transform;
});

unwrapExports(transform_1);
transform_1.transform;

var regex = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.iidxes = exports.urlRegex = exports.fileRegex = exports.emailRegex = exports.ipRegex = exports.finalRegex = exports.final = exports.file = exports.url = exports.email = void 0;
Expand Down Expand Up @@ -220,9 +145,9 @@
utils.maximumAttrLength;
utils.checkParenthesis;

var node = createCommonjsModule(function (module, exports) {
var list_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

exports.list = void 0;



Expand Down Expand Up @@ -349,6 +274,92 @@
}
return found;
};
exports.list = list;
});

unwrapExports(list_1);
list_1.list;

var transform_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.transform = void 0;
function applyOption(string, props, option) {
// conditional
if (typeof option === "function") {
return option(string, props);
}
// all
else {
return option;
}
}
function transform(input, options) {
var protocol = "";
var truncation = Infinity;
var attributes = {};
var truncateFromTheMiddle = false;
// special transformation
if (options && options.specialTransform) {
for (var index = 0; index < options.specialTransform.length; index++) {
var transformer = options.specialTransform[index];
if (transformer.test.test(input.string)) {
return transformer.transform(input.string, input);
}
}
}
// exclude
if (options && options.exclude) {
if (applyOption(input.string, input, options.exclude))
{ return input.string; }
}
// protocol
if (options && options.protocol) {
protocol = applyOption(input.string, input, options.protocol);
}
if (input.protocol) {
protocol = "";
}
else if (!protocol) {
protocol = input.isEmail
? "mailto:"
: input.isFile
? "file:///"
: "http://";
}
// truncation
if (options && options.truncate) {
truncation = applyOption(input.string, input, options.truncate);
}
if (options && options.middleTruncation) {
truncateFromTheMiddle = applyOption(input.string, input, options.middleTruncation);
}
// attributes
if (options && options.attributes) {
attributes = applyOption(input.string, input, options.attributes);
}
return "<a ".concat(Object.keys(attributes)
.map(function (key) {
return attributes[key] === true ? key : "".concat(key, "=\"").concat(attributes[key], "\" ");
})
.join(" "), "href=\"").concat(protocol).concat(input.string, "\">").concat(input.string.length > truncation
? truncateFromTheMiddle
? input.string.substring(0, Math.floor(truncation / 2)) +
"…" +
input.string.substring(input.string.length - Math.ceil(truncation / 2), input.string.length)
: input.string.substring(0, truncation) + "…"
: input.string, "</a>");
}
exports.transform = transform;
});

unwrapExports(transform_1);
transform_1.transform;

var node = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });



var anchorme = function (arg) {
var _a = typeof arg === "string"
? { input: arg, options: undefined, extensions: undefined }
Expand All @@ -359,7 +370,7 @@
input = input.replace(extension.test, extension.transform);
}
}
var found = list(input, (options || {}).skipHTML);
var found = (0, list_1.list)(input, (options || {}).skipHTML);
var newStr = "";
// the following code isn't very intuitive nor human readable
// but faster than others
Expand All @@ -377,7 +388,7 @@
}
return newStr ? newStr : input;
};
anchorme.list = list;
anchorme.list = list_1.list;
anchorme.validate = {
ip: function (input) { return regex.ipRegex.test(input); },
email: function (input) { return regex.emailRegex.test(input); },
Expand Down
2 changes: 1 addition & 1 deletion dist/browser/anchorme.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="mocha" />
import { transform } from "./transform";
import { ListingProps, Options } from "./types";
import { Options } from "./types";
declare const anchorme: {
(arg: string | {
input: string;
Expand All @@ -10,7 +10,7 @@ declare const anchorme: {
transform: (string: string) => string;
}[] | undefined;
}): string;
list: (input: string, skipHTML?: boolean) => ListingProps[];
list: (input: string, skipHTML?: boolean) => import("./types").ListingProps[];
validate: {
ip: (input: string) => boolean;
email: (input: string) => boolean;
Expand Down
130 changes: 3 additions & 127 deletions dist/node/index.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var dictionary_1 = require("./dictionary");
var list_1 = require("./list");
var transform_1 = require("./transform");
var regex_1 = require("./regex");
var utils_1 = require("./utils");
var dictionary_2 = require("./dictionary");
var TLDsRgex = new RegExp("^(".concat(dictionary_2.TLDs, ")$"), 'i');
var list = function (input, skipHTML) {
if (skipHTML === void 0) { skipHTML = true; }
var found = [];
var result = null;
var _loop_1 = function () {
var start = result.index;
var end = start + result[0].length;
var string = result[0];
var protocol = result[regex_1.iidxes.url.protocol[0]] ||
result[regex_1.iidxes.url.protocol[1]] ||
result[regex_1.iidxes.url.protocol[2]];
// ### Parenthesis problem
/**
As we're using the \b to tokenize the URL, sometimes the parenthesis are part of the URL
and sometimes they are actually the last part, this makes the tokenization stops just
before them.
To fix this, we calculate how many parenthesis are open and how many are closed inside
the URL and based on the number we should be able to know whether the aforementioned
parenthesis character is part of the URL or not
*/
if (dictionary_1.closingParenthesis.indexOf(input.charAt(end)) > -1) {
dictionary_1.parenthesis.forEach(function (str) {
var opening = str.charAt(0);
var closing = str.charAt(1);
if ((0, utils_1.checkParenthesis)(opening, closing, string, input.charAt(end))) {
string = string + input.charAt(end);
end++;
}
});
}
if (skipHTML) {
// ### HTML problem 1
/**
checking whether the token is already inside an HTML element by seeing if it's
preceded by an HTML attribute that would hold a url (e.g. src, cite ...etc)
e.g. <a href="ab.com">ab.com</a>
*/
if (['""', "''", "()"].indexOf(input.charAt(start - 1) + input.charAt(end)) !== -1) {
if ((0, utils_1.isInsideAttribute)(input.substring(start - utils_1.maximumAttrLength - 15, start))) {
return "continue";
}
}
// ### HTML problem 2
/**
Checking whether the token is the content of an actual anchor
e.g. <a href="https://something.com">click to go to something.com and have fun</a>
*/
if (input.substring(end, input.length).indexOf("</a>") > -1 &&
input.substring(0, start).indexOf("<a") > -1 &&
(0, utils_1.isInsideAnchorTag)(string, input, end)) {
return "continue";
}
}
// filter out URLs that doesn't have a vaild TLD
var tld = result[regex_1.iidxes.url.TLD[0]] || result[regex_1.iidxes.url.TLD[1]];
if (tld && (!protocol) && (!result[regex_1.iidxes.email.protocol]) && (!tld.startsWith("xn--") && !TLDsRgex.test(tld))) {
return "continue";
}
if (result[regex_1.iidxes.isURL]) {
var host = result[regex_1.iidxes.url.host[0]] || result[regex_1.iidxes.url.host[1]] || result[regex_1.iidxes.url.host[2]];
var path = (string.match(/(?:[^\/:]|])((?:\/[^?#\s]+)+)/) || [])[1];
var query = (string.match(/(?:\?)([^#]+)\b/) || [])[1];
var fragment = (string.match(/(?:#)(.+)\b/) || [])[1];
var ipv6 = host === undefined ? (string.match(/\/\/\[((?:(?:[a-f\d:]+:+)+[a-f\d]+))\]/) || [])[1] : undefined;
found.push({
start: start,
end: end,
string: string,
isURL: true,
protocol: protocol,
port: result[regex_1.iidxes.url.port],
ipv4: result[regex_1.iidxes.url.ipv4],
ipv6: ipv6,
host: ipv6 ? '[' + ipv6 + ']' : host,
confirmedByProtocol: !!protocol,
path: path || undefined,
query: query,
fragment: fragment,
reason: "url",
});
}
else if (result[regex_1.iidxes.isFile]) {
var filePath = string.substr(8);
found.push({
start: start,
end: end,
string: string,
isFile: true,
protocol: result[regex_1.iidxes.file.protocol],
filename: result[regex_1.iidxes.file.fileName],
filePath: filePath,
fileDirectory: filePath.substr(0, filePath.length - result[regex_1.iidxes.file.fileName].length),
reason: "file",
});
}
else if (result[regex_1.iidxes.isEmail]) {
found.push({
start: start,
end: end,
string: string,
isEmail: true,
local: result[regex_1.iidxes.email.local],
protocol: result[regex_1.iidxes.email.protocol],
host: result[regex_1.iidxes.email.host],
reason: "email",
});
}
else {
found.push({
start: start,
end: end,
string: string,
reason: "unknown",
});
}
};
while ((result = regex_1.finalRegex.exec(input)) !== null) {
_loop_1();
}
return found;
};
var anchorme = function (arg) {
var _a = typeof arg === "string"
? { input: arg, options: undefined, extensions: undefined }
Expand All @@ -137,7 +13,7 @@ var anchorme = function (arg) {
input = input.replace(extension.test, extension.transform);
}
}
var found = list(input, (options || {}).skipHTML);
var found = (0, list_1.list)(input, (options || {}).skipHTML);
var newStr = "";
// the following code isn't very intuitive nor human readable
// but faster than others
Expand All @@ -155,7 +31,7 @@ var anchorme = function (arg) {
}
return newStr ? newStr : input;
};
anchorme.list = list;
anchorme.list = list_1.list;
anchorme.validate = {
ip: function (input) { return regex_1.ipRegex.test(input); },
email: function (input) { return regex_1.emailRegex.test(input); },
Expand Down
2 changes: 2 additions & 0 deletions dist/node/list.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { ListingProps } from "./types";
export declare const list: (input: string, skipHTML?: boolean) => ListingProps[];
Loading