tent-markdown
Version:
Tent dialect for markdown lib
438 lines (394 loc) • 23.4 kB
JavaScript
/*
Adapted from https://github.com/twitter/twitter-text-js
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License below, or at:
http://www.apache.org/licenses/LICENSE-2.0
*/
(function(expose) {
"use strict";
var twttr = { txt: { regexen: {} } };
// Builds a RegExp
function regexSupplant(regex, flags) {
flags = flags || "";
if (typeof regex !== "string") {
if (regex.global && flags.indexOf("g") < 0) {
flags += "g";
}
if (regex.ignoreCase && flags.indexOf("i") < 0) {
flags += "i";
}
if (regex.multiline && flags.indexOf("m") < 0) {
flags += "m";
}
regex = regex.source;
}
return new RegExp(regex.replace(/#\{(\w+)\}/g, function(match, name) {
var newRegex = twttr.txt.regexen[name] || "";
if (typeof newRegex !== "string") {
newRegex = newRegex.source;
}
return newRegex;
}), flags);
}
twttr.txt.regexSupplant = regexSupplant;
// simple string interpolation
function stringSupplant(str, values) {
return str.replace(/#\{(\w+)\}/g, function(match, name) {
return values[name] || "";
});
}
var fromCode = String.fromCharCode;
var INVALID_CHARS = [
fromCode(0xFFFE),
fromCode(0xFEFF), // BOM
fromCode(0xFFFF) // Special
];
twttr.txt.regexen.invalid_chars_group = regexSupplant(INVALID_CHARS.join(""));
twttr.txt.stringSupplant = stringSupplant;
twttr.txt.stringSupplant = stringSupplant;
var UNICODE_SPACES = [
fromCode(0x0020), // White_Space # Zs SPACE
fromCode(0x0085), // White_Space # Cc <control-0085>
fromCode(0x00A0), // White_Space # Zs NO-BREAK SPACE
fromCode(0x1680), // White_Space # Zs OGHAM SPACE MARK
fromCode(0x180E), // White_Space # Zs MONGOLIAN VOWEL SEPARATOR
fromCode(0x2028), // White_Space # Zl LINE SEPARATOR
fromCode(0x2029), // White_Space # Zp PARAGRAPH SEPARATOR
fromCode(0x202F), // White_Space # Zs NARROW NO-BREAK SPACE
fromCode(0x205F), // White_Space # Zs MEDIUM MATHEMATICAL SPACE
fromCode(0x3000) // White_Space # Zs IDEOGRAPHIC SPACE
];
twttr.txt.regexen.spaces_group = regexSupplant(UNICODE_SPACES.join(""));
twttr.txt.regexen.spaces = regexSupplant("[" + UNICODE_SPACES.join("") + "]");
twttr.txt.regexen.invalid_chars_group = regexSupplant(INVALID_CHARS.join(""));
twttr.txt.regexen.punct = /\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$/;
function addCharsToCharClass(charClass, start, end) {
var s = String.fromCharCode(start);
if (end !== start) {
s += "-" + String.fromCharCode(end);
}
charClass.push(s);
return charClass;
}
twttr.txt.addCharsToCharClass = addCharsToCharClass;
var nonLatinHashtagChars = [];
// Cyrillic
addCharsToCharClass(nonLatinHashtagChars, 0x0400, 0x04ff); // Cyrillic
addCharsToCharClass(nonLatinHashtagChars, 0x0500, 0x0527); // Cyrillic Supplement
addCharsToCharClass(nonLatinHashtagChars, 0x2de0, 0x2dff); // Cyrillic Extended A
addCharsToCharClass(nonLatinHashtagChars, 0xa640, 0xa69f); // Cyrillic Extended B
// Hebrew
addCharsToCharClass(nonLatinHashtagChars, 0x0591, 0x05bf); // Hebrew
addCharsToCharClass(nonLatinHashtagChars, 0x05c1, 0x05c2);
addCharsToCharClass(nonLatinHashtagChars, 0x05c4, 0x05c5);
addCharsToCharClass(nonLatinHashtagChars, 0x05c7, 0x05c7);
addCharsToCharClass(nonLatinHashtagChars, 0x05d0, 0x05ea);
addCharsToCharClass(nonLatinHashtagChars, 0x05f0, 0x05f4);
addCharsToCharClass(nonLatinHashtagChars, 0xfb12, 0xfb28); // Hebrew Presentation Forms
addCharsToCharClass(nonLatinHashtagChars, 0xfb2a, 0xfb36);
addCharsToCharClass(nonLatinHashtagChars, 0xfb38, 0xfb3c);
addCharsToCharClass(nonLatinHashtagChars, 0xfb3e, 0xfb3e);
addCharsToCharClass(nonLatinHashtagChars, 0xfb40, 0xfb41);
addCharsToCharClass(nonLatinHashtagChars, 0xfb43, 0xfb44);
addCharsToCharClass(nonLatinHashtagChars, 0xfb46, 0xfb4f);
// Arabic
addCharsToCharClass(nonLatinHashtagChars, 0x0610, 0x061a); // Arabic
addCharsToCharClass(nonLatinHashtagChars, 0x0620, 0x065f);
addCharsToCharClass(nonLatinHashtagChars, 0x066e, 0x06d3);
addCharsToCharClass(nonLatinHashtagChars, 0x06d5, 0x06dc);
addCharsToCharClass(nonLatinHashtagChars, 0x06de, 0x06e8);
addCharsToCharClass(nonLatinHashtagChars, 0x06ea, 0x06ef);
addCharsToCharClass(nonLatinHashtagChars, 0x06fa, 0x06fc);
addCharsToCharClass(nonLatinHashtagChars, 0x06ff, 0x06ff);
addCharsToCharClass(nonLatinHashtagChars, 0x0750, 0x077f); // Arabic Supplement
addCharsToCharClass(nonLatinHashtagChars, 0x08a0, 0x08a0); // Arabic Extended A
addCharsToCharClass(nonLatinHashtagChars, 0x08a2, 0x08ac);
addCharsToCharClass(nonLatinHashtagChars, 0x08e4, 0x08fe);
addCharsToCharClass(nonLatinHashtagChars, 0xfb50, 0xfbb1); // Arabic Pres. Forms A
addCharsToCharClass(nonLatinHashtagChars, 0xfbd3, 0xfd3d);
addCharsToCharClass(nonLatinHashtagChars, 0xfd50, 0xfd8f);
addCharsToCharClass(nonLatinHashtagChars, 0xfd92, 0xfdc7);
addCharsToCharClass(nonLatinHashtagChars, 0xfdf0, 0xfdfb);
addCharsToCharClass(nonLatinHashtagChars, 0xfe70, 0xfe74); // Arabic Pres. Forms B
addCharsToCharClass(nonLatinHashtagChars, 0xfe76, 0xfefc);
addCharsToCharClass(nonLatinHashtagChars, 0x200c, 0x200c); // Zero-Width Non-Joiner
// Thai
addCharsToCharClass(nonLatinHashtagChars, 0x0e01, 0x0e3a);
addCharsToCharClass(nonLatinHashtagChars, 0x0e40, 0x0e4e);
// Hangul (Korean)
addCharsToCharClass(nonLatinHashtagChars, 0x1100, 0x11ff); // Hangul Jamo
addCharsToCharClass(nonLatinHashtagChars, 0x3130, 0x3185); // Hangul Compatibility Jamo
addCharsToCharClass(nonLatinHashtagChars, 0xA960, 0xA97F); // Hangul Jamo Extended-A
addCharsToCharClass(nonLatinHashtagChars, 0xAC00, 0xD7AF); // Hangul Syllables
addCharsToCharClass(nonLatinHashtagChars, 0xD7B0, 0xD7FF); // Hangul Jamo Extended-B
addCharsToCharClass(nonLatinHashtagChars, 0xFFA1, 0xFFDC); // half-width Hangul
// Japanese and Chinese
addCharsToCharClass(nonLatinHashtagChars, 0x30A1, 0x30FA); // Katakana (full-width)
addCharsToCharClass(nonLatinHashtagChars, 0x30FC, 0x30FE); // Katakana Chouon and iteration marks (full-width)
addCharsToCharClass(nonLatinHashtagChars, 0xFF66, 0xFF9F); // Katakana (half-width)
addCharsToCharClass(nonLatinHashtagChars, 0xFF70, 0xFF70); // Katakana Chouon (half-width)
addCharsToCharClass(nonLatinHashtagChars, 0xFF10, 0xFF19); // \
addCharsToCharClass(nonLatinHashtagChars, 0xFF21, 0xFF3A); // - Latin (full-width)
addCharsToCharClass(nonLatinHashtagChars, 0xFF41, 0xFF5A); // /
addCharsToCharClass(nonLatinHashtagChars, 0x3041, 0x3096); // Hiragana
addCharsToCharClass(nonLatinHashtagChars, 0x3099, 0x309E); // Hiragana voicing and iteration mark
addCharsToCharClass(nonLatinHashtagChars, 0x3400, 0x4DBF); // Kanji (CJK Extension A)
addCharsToCharClass(nonLatinHashtagChars, 0x4E00, 0x9FFF); // Kanji (Unified)
// -- Disabled as it breaks the Regex.
//addCharsToCharClass(nonLatinHashtagChars, 0x20000, 0x2A6DF); // Kanji (CJK Extension B)
addCharsToCharClass(nonLatinHashtagChars, 0x2A700, 0x2B73F); // Kanji (CJK Extension C)
addCharsToCharClass(nonLatinHashtagChars, 0x2B740, 0x2B81F); // Kanji (CJK Extension D)
addCharsToCharClass(nonLatinHashtagChars, 0x2F800, 0x2FA1F); // Kanji (CJK supplement)
addCharsToCharClass(nonLatinHashtagChars, 0x3003, 0x3003); // Kanji iteration mark
addCharsToCharClass(nonLatinHashtagChars, 0x3005, 0x3005); // Kanji iteration mark
addCharsToCharClass(nonLatinHashtagChars, 0x303B, 0x303B); // Han iteration mark
twttr.txt.regexen.nonLatinHashtagChars = regexSupplant(nonLatinHashtagChars.join(""));
var latinAccentChars = [];
// Latin accented characters (subtracted 0xD7 from the range, it's a confusable multiplication sign. Looks like "x")
addCharsToCharClass(latinAccentChars, 0x00c0, 0x00d6);
addCharsToCharClass(latinAccentChars, 0x00d8, 0x00f6);
addCharsToCharClass(latinAccentChars, 0x00f8, 0x00ff);
// Latin Extended A and B
addCharsToCharClass(latinAccentChars, 0x0100, 0x024f);
// assorted IPA Extensions
addCharsToCharClass(latinAccentChars, 0x0253, 0x0254);
addCharsToCharClass(latinAccentChars, 0x0256, 0x0257);
addCharsToCharClass(latinAccentChars, 0x0259, 0x0259);
addCharsToCharClass(latinAccentChars, 0x025b, 0x025b);
addCharsToCharClass(latinAccentChars, 0x0263, 0x0263);
addCharsToCharClass(latinAccentChars, 0x0268, 0x0268);
addCharsToCharClass(latinAccentChars, 0x026f, 0x026f);
addCharsToCharClass(latinAccentChars, 0x0272, 0x0272);
addCharsToCharClass(latinAccentChars, 0x0289, 0x0289);
addCharsToCharClass(latinAccentChars, 0x028b, 0x028b);
// Okina for Hawaiian (it *is* a letter character)
addCharsToCharClass(latinAccentChars, 0x02bb, 0x02bb);
// Combining diacritics
addCharsToCharClass(latinAccentChars, 0x0300, 0x036f);
// Latin Extended Additional
addCharsToCharClass(latinAccentChars, 0x1e00, 0x1eff);
twttr.txt.regexen.latinAccentChars = regexSupplant(latinAccentChars.join(""));
// A hashtag must contain characters, numbers and underscores, but not all numbers.
twttr.txt.regexen.hashSigns = /[##]/;
twttr.txt.regexen.hashtagAlpha = regexSupplant(/[a-z_#{latinAccentChars}#{nonLatinHashtagChars}]/i);
twttr.txt.regexen.hashtagAlphaNumeric = regexSupplant(/[a-z0-9_#{latinAccentChars}#{nonLatinHashtagChars}]/i);
twttr.txt.regexen.endHashtagMatch = regexSupplant(/^(?:#{hashSigns}|:\/\/)/);
twttr.txt.regexen.hashtagBoundary = regexSupplant(/(?:^|$|[^&a-z0-9_#{latinAccentChars}#{nonLatinHashtagChars}])/);
twttr.txt.regexen.validHashtag = regexSupplant(/(#{hashtagBoundary})(#{hashSigns})(#{hashtagAlphaNumeric}*#{hashtagAlpha}#{hashtagAlphaNumeric}*)/gi);
// URL related regex collection
twttr.txt.regexen.validUrlPrecedingChars = regexSupplant(/(?:[^A-Za-z0-9@@$###{invalid_chars_group}]|^)/);
twttr.txt.regexen.invalidUrlWithoutProtocolPrecedingChars = /[-_.\/]$/;
twttr.txt.regexen.invalidDomainChars = stringSupplant("#{punct}#{spaces_group}#{invalid_chars_group}", twttr.txt.regexen);
twttr.txt.regexen.validDomainChars = regexSupplant(/[^#{invalidDomainChars}]/);
twttr.txt.regexen.validSubdomain = regexSupplant(/(?:(?:#{validDomainChars}(?:[_-]|#{validDomainChars})*)?#{validDomainChars}\.)/);
twttr.txt.regexen.validDomainName = regexSupplant(/(?:(?:#{validDomainChars}(?:-|#{validDomainChars})*)?#{validDomainChars}\.)/);
twttr.txt.regexen.validGTLD = regexSupplant(new RegExp(
'(?:(?:' +
'abb|abbott|abogado|academy|accenture|accountant|accountants|active|actor|ads|adult|aeg|aero|afl|' +
'agency|aig|airforce|allfinanz|alsace|amsterdam|android|apartments|aquarelle|archi|army|arpa|' +
'asia|associates|attorney|auction|audio|auto|autos|axa|azure|band|bank|bar|barclaycard|barclays|' +
'bargains|bauhaus|bayern|bbc|bbva|beer|berlin|best|bharti|bible|bid|bike|bing|bingo|bio|biz|' +
'black|blackfriday|bloomberg|blue|bmw|bnl|bnpparibas|boats|bond|boo|boutique|bradesco|' +
'bridgestone|broker|brother|brussels|budapest|build|builders|business|buzz|bzh|cab|cafe|cal|' +
'camera|camp|cancerresearch|canon|capetown|capital|caravan|cards|care|career|careers|cars|' +
'cartier|casa|cash|casino|cat|catering|cba|cbn|center|ceo|cern|cfa|cfd|channel|chat|cheap|chloe|' +
'christmas|chrome|church|cisco|citic|city|claims|cleaning|click|clinic|clothing|cloud|club|coach|' +
'codes|coffee|college|cologne|com|commbank|community|company|computer|condos|construction|' +
'consulting|contractors|cooking|cool|coop|corsica|country|coupons|courses|credit|creditcard|' +
'cricket|crown|crs|cruises|cuisinella|cymru|cyou|dabur|dad|dance|date|dating|datsun|day|dclk|' +
'deals|degree|delivery|democrat|dental|dentist|desi|design|dev|diamonds|diet|digital|direct|' +
'directory|discount|dnp|docs|dog|doha|domains|doosan|download|drive|durban|dvag|earth|eat|edu|' +
'education|email|emerck|energy|engineer|engineering|enterprises|epson|equipment|erni|esq|estate|' +
'eurovision|eus|events|everbank|exchange|expert|exposed|express|fail|faith|fan|fans|farm|fashion|' +
'feedback|film|finance|financial|firmdale|fish|fishing|fit|fitness|flights|florist|flowers|' +
'flsmidth|fly|foo|football|forex|forsale|forum|foundation|frl|frogans|fund|furniture|futbol|fyi|' +
'gal|gallery|garden|gbiz|gdn|gent|genting|ggee|gift|gifts|gives|glass|gle|global|globo|gmail|gmo|' +
'gmx|gold|goldpoint|golf|goo|goog|google|gop|gov|graphics|gratis|green|gripe|guge|guide|guitars|' +
'guru|hamburg|hangout|haus|healthcare|help|here|hermes|hiphop|hitachi|hiv|hockey|holdings|' +
'holiday|homedepot|homes|honda|horse|host|hosting|hoteles|hotmail|house|how|ibm|icbc|icu|ifm|' +
'immo|immobilien|industries|infiniti|info|ing|ink|institute|insure|int|international|investments|' +
'irish|iwc|java|jcb|jetzt|jewelry|jlc|jll|jobs|joburg|juegos|kaufen|kddi|kim|kitchen|kiwi|koeln|' +
'komatsu|krd|kred|kyoto|lacaixa|land|lasalle|lat|latrobe|law|lawyer|lds|lease|leclerc|legal|lgbt|' +
'liaison|lidl|life|lighting|limited|limo|link|loan|loans|lol|london|lotte|lotto|love|ltda|lupin|' +
'luxe|luxury|madrid|maif|maison|management|mango|market|marketing|markets|marriott|mba|media|' +
'meet|melbourne|meme|memorial|men|menu|miami|microsoft|mil|mini|mma|mobi|moda|moe|monash|money|' +
'montblanc|mormon|mortgage|moscow|motorcycles|mov|movie|movistar|mtn|mtpc|museum|nadex|nagoya|' +
'name|navy|nec|net|netbank|network|neustar|new|news|nexus|ngo|nhk|nico|ninja|nissan|nra|nrw|ntt|' +
'nyc|office|okinawa|omega|one|ong|onl|online|ooo|oracle|org|organic|osaka|otsuka|ovh|page|' +
'panerai|paris|partners|parts|party|pharmacy|philips|photo|photography|photos|physio|piaget|pics|' +
'pictet|pictures|pink|pizza|place|play|plumbing|plus|pohl|poker|porn|post|praxi|press|pro|prod|' +
'productions|prof|properties|property|pub|qpon|quebec|racing|realtor|realty|recipes|red|redstone|' +
'rehab|reise|reisen|reit|ren|rent|rentals|repair|report|republican|rest|restaurant|review|' +
'reviews|rich|ricoh|rio|rip|rocks|rodeo|rsvp|ruhr|run|ryukyu|saarland|sale|samsung|sandvik|' +
'sandvikcoromant|sap|sarl|saxo|sca|scb|schmidt|scholarships|school|schule|schwarz|science|scor|' +
'scot|seat|sener|services|sew|sex|sexy|shiksha|shoes|show|shriram|singles|site|ski|sky|skype|' +
'sncf|soccer|social|software|sohu|solar|solutions|sony|soy|space|spiegel|spreadbetting|starhub|' +
'statoil|study|style|sucks|supplies|supply|support|surf|surgery|suzuki|swatch|swiss|sydney|' +
'systems|taipei|tatar|tattoo|tax|taxi|team|tech|technology|tel|telefonica|temasek|tennis|thd|' +
'theater|tickets|tienda|tips|tires|tirol|today|tokyo|tools|top|toray|toshiba|tours|town|toys|' +
'trade|trading|training|travel|trust|tui|university|uno|uol|vacations|vegas|ventures|' +
'vermögensberater|vermögensberatung|versicherung|vet|viajes|video|villas|vision|vista|vistaprint|' +
'vlaanderen|vodka|vote|voting|voto|voyage|wales|walter|wang|watch|webcam|website|wed|wedding|' +
'weir|whoswho|wien|wiki|williamhill|win|windows|wme|work|works|world|wtc|wtf|xbox|xerox|xin|xxx|' +
'xyz|yachts|yandex|yodobashi|yoga|yokohama|youtube|zip|zone|zuerich|дети|москва|онлайн|орг|рус|' +
'сайт|بازار|شبكة|موقع|संगठन|みんな|グーグル|世界|中信|中文网|企业|佛山|信息|健康|八卦|公司|公益|商城|商店|商标|在线|娱乐|工行|广东|慈善|我爱你|' +
'手机|政务|政府|时尚|机构|淡马锡|游戏|移动|组织机构|网址|网店|网络|谷歌|集团|飞利浦|餐厅|삼성|onion' +
')(?=[^0-9a-zA-Z@]|$))'));
twttr.txt.regexen.validCCTLD = regexSupplant(new RegExp(
'(?:(?:' +
'ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bl|bm|bn|bo|bq|' +
'br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|' +
'ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|' +
'gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|' +
'la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mf|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|' +
'my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|' +
'rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|' +
'tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw|ελ|' +
'бел|мкд|мон|рф|срб|укр|қаз|հայ|الاردن|الجزائر|السعودية|المغرب|امارات|ایران|بھارت|تونس|سودان|' +
'سورية|عراق|عمان|فلسطين|قطر|مصر|مليسيا|پاکستان|भारत|বাংলা|ভারত|ਭਾਰਤ|ભારત|இந்தியா|இலங்கை|' +
'சிங்கப்பூர்|భారత్|ලංකා|ไทย|გე|中国|中國|台湾|台灣|新加坡|澳門|香港|한국' +
')(?=[^0-9a-zA-Z@]|$))'));
twttr.txt.regexen.validPunycode = regexSupplant(/(?:xn--[0-9a-z]+)/);
twttr.txt.regexen.validDomain = regexSupplant(/(?:#{validSubdomain}*#{validDomainName}(?:#{validGTLD}|#{validCCTLD}|#{validPunycode}))/);
twttr.txt.regexen.validAsciiDomain = regexSupplant(/(?:(?:[\-a-z0-9#{latinAccentChars}]+)\.)+(?:#{validGTLD}|#{validCCTLD}|#{validPunycode})/gi);
twttr.txt.regexen.invalidShortDomain = regexSupplant(/^#{validDomainName}#{validCCTLD}$/i);
twttr.txt.regexen.validPortNumber = regexSupplant(/[0-9]+/);
twttr.txt.regexen.validGeneralUrlPathChars = regexSupplant(/[a-z0-9!\*';:=\+,\.\$\/%#\[\]\-_~@|&#{latinAccentChars}]/i);
// Allow URL paths to contain up to two nested levels of balanced parens
// 1. Used in Wikipedia URLs like /Primer_(film)
// 2. Used in IIS sessions like /S(dfd346)/
// 3. Used in Rdio URLs like /track/We_Up_(Album_Version_(Edited))/
twttr.txt.regexen.validUrlBalancedParens = regexSupplant(
'\\(' +
'(?:' +
'#{validGeneralUrlPathChars}+' +
'|' +
// allow one nested level of balanced parentheses
'(?:' +
'#{validGeneralUrlPathChars}*' +
'\\(' +
'#{validGeneralUrlPathChars}+' +
'\\)' +
'#{validGeneralUrlPathChars}*' +
')' +
')' +
'\\)', 'i');
// Valid end-of-path chracters (so /foo. does not gobble the period).
// 1. Allow =&# for empty URL parameters and other URL-join artifacts
twttr.txt.regexen.validUrlPathEndingChars = regexSupplant(/[\+\-a-z0-9=_#\/#{latinAccentChars}]|(?:#{validUrlBalancedParens})/i);
// Allow @ in a url, but only in the middle. Catch things like http://example.com/@user/
twttr.txt.regexen.validUrlPath = regexSupplant('(?:' +
'(?:' +
'#{validGeneralUrlPathChars}*' +
'(?:#{validUrlBalancedParens}#{validGeneralUrlPathChars}*)*' +
'#{validUrlPathEndingChars}'+
')|(?:@#{validGeneralUrlPathChars}+\/)'+
')', 'i');
twttr.txt.regexen.validUrlQueryChars = /[a-z0-9!?\*'@\(\);:&=\+\$\/%#\[\]\-_\.,~|]/i;
twttr.txt.regexen.validUrlQueryEndingChars = /[a-z0-9_&=#\/]/i;
twttr.txt.regexen.extractUrl = regexSupplant(
'(' + // $1 total match
'(#{validUrlPrecedingChars})' + // $2 Preceeding chracter
'(' + // $3 URL
'(https?:\\/\\/)?' + // $4 Protocol (optional)
'(#{validDomain})' + // $5 Domain(s)
'(?::(#{validPortNumber}))?' + // $6 Port number (optional)
'(\\/#{validUrlPath}*)?' + // $7 URL Path
'(\\?#{validUrlQueryChars}*#{validUrlQueryEndingChars})?' + // $8 Query String
')' +
')', 'gi');
twttr.txt.regexen.validTcoUrl = /^https?:\/\/t\.co\/[a-z0-9]+/i;
twttr.extractHashtagsWithIndices = function(text) {
if (!text || !text.match(twttr.txt.regexen.hashSigns)) {
return [];
}
var tags = [];
text.replace(twttr.txt.regexen.validHashtag, function(match, before, hash, hashText, offset, chunk) {
var after = chunk.slice(offset + match.length);
if (after.match(twttr.txt.regexen.endHashtagMatch)) {
return;
}
var startPosition = offset + before.length;
var endPosition = startPosition + hashText.length + 1;
tags.push({
hashtag: hashText,
indices: [startPosition, endPosition]
});
});
return tags;
};
twttr.extractUrlsWithIndices = function(text, options) {
if (!options) {
options = {extractUrlsWithoutProtocol: true};
}
if (!text || (options.extractUrlsWithoutProtocol ? !text.match(/\./) : !text.match(/:/))) {
return [];
}
var urls = [];
var replacerFn = function(asciiDomain) {
var asciiStartPosition = domain.indexOf(asciiDomain, asciiEndPosition);
asciiEndPosition = asciiStartPosition + asciiDomain.length;
lastUrl = {
url: asciiDomain,
indices: [startPosition + asciiStartPosition, startPosition + asciiEndPosition]
};
if (!before.match(/^[\^]$/)) {
lastUrlInvalidMatch = asciiDomain.match(twttr.txt.regexen.invalidShortDomain);
}
if (!lastUrlInvalidMatch) {
urls.push(lastUrl);
}
};
while (twttr.txt.regexen.extractUrl.exec(text)) {
var before = RegExp.$2, url = RegExp.$3, protocol = RegExp.$4, domain = RegExp.$5, path = RegExp.$7;
var endPosition = twttr.txt.regexen.extractUrl.lastIndex,
startPosition = endPosition - url.length;
// if protocol is missing and domain contains non-ASCII characters,
// extract ASCII-only domains.
if (!protocol) {
if (!options.extractUrlsWithoutProtocol || before.match(twttr.txt.regexen.invalidUrlWithoutProtocolPrecedingChars)) {
continue;
}
var lastUrl = null,
lastUrlInvalidMatch = false,
asciiEndPosition = 0;
domain.replace(twttr.txt.regexen.validAsciiDomain, replacerFn);
// no ASCII-only domain found. Skip the entire URL.
if (lastUrl === null || lastUrl === undefined) {
continue;
}
// lastUrl only contains domain. Need to add path and query if they exist.
if (path) {
if (lastUrlInvalidMatch) {
urls.push(lastUrl);
}
lastUrl.url = url.replace(domain, lastUrl.url);
lastUrl.indices[1] = endPosition;
}
} else {
// In the case of t.co URLs, don't allow additional path characters.
if (url.match(twttr.txt.regexen.validTcoUrl)) {
url = RegExp.lastMatch;
endPosition = startPosition + url.length;
}
urls.push({
url: url,
indices: [startPosition, endPosition]
});
}
}
return urls;
};
expose.extractUrlsWithIndices = twttr.extractUrlsWithIndices;
expose.extractHashtagsWithIndices = twttr.extractHashtagsWithIndices;
})((function() {
"use strict";
if (typeof exports === "undefined") {
window.twttr = {};
return window.twttr;
} else {
return exports;
}
})());