index.cjs.js 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540
  1. 'use strict';
  2. var mdurl = require('mdurl');
  3. var ucmicro = require('uc.micro');
  4. var entities = require('entities');
  5. var LinkifyIt = require('linkify-it');
  6. var punycode = require('punycode.js');
  7. function _interopNamespaceDefault(e) {
  8. var n = Object.create(null);
  9. if (e) {
  10. Object.keys(e).forEach(function (k) {
  11. if (k !== 'default') {
  12. var d = Object.getOwnPropertyDescriptor(e, k);
  13. Object.defineProperty(n, k, d.get ? d : {
  14. enumerable: true,
  15. get: function () { return e[k]; }
  16. });
  17. }
  18. });
  19. }
  20. n.default = e;
  21. return Object.freeze(n);
  22. }
  23. var mdurl__namespace = /*#__PURE__*/_interopNamespaceDefault(mdurl);
  24. var ucmicro__namespace = /*#__PURE__*/_interopNamespaceDefault(ucmicro);
  25. // Utilities
  26. //
  27. function _class(obj) {
  28. return Object.prototype.toString.call(obj);
  29. }
  30. function isString(obj) {
  31. return _class(obj) === '[object String]';
  32. }
  33. const _hasOwnProperty = Object.prototype.hasOwnProperty;
  34. function has(object, key) {
  35. return _hasOwnProperty.call(object, key);
  36. }
  37. // Merge objects
  38. //
  39. function assign(obj /* from1, from2, from3, ... */) {
  40. const sources = Array.prototype.slice.call(arguments, 1);
  41. sources.forEach(function (source) {
  42. if (!source) {
  43. return;
  44. }
  45. if (typeof source !== 'object') {
  46. throw new TypeError(source + 'must be object');
  47. }
  48. Object.keys(source).forEach(function (key) {
  49. obj[key] = source[key];
  50. });
  51. });
  52. return obj;
  53. }
  54. // Remove element from array and put another array at those position.
  55. // Useful for some operations with tokens
  56. function arrayReplaceAt(src, pos, newElements) {
  57. return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1));
  58. }
  59. function isValidEntityCode(c) {
  60. /* eslint no-bitwise:0 */
  61. // broken sequence
  62. if (c >= 0xD800 && c <= 0xDFFF) {
  63. return false;
  64. }
  65. // never used
  66. if (c >= 0xFDD0 && c <= 0xFDEF) {
  67. return false;
  68. }
  69. if ((c & 0xFFFF) === 0xFFFF || (c & 0xFFFF) === 0xFFFE) {
  70. return false;
  71. }
  72. // control codes
  73. if (c >= 0x00 && c <= 0x08) {
  74. return false;
  75. }
  76. if (c === 0x0B) {
  77. return false;
  78. }
  79. if (c >= 0x0E && c <= 0x1F) {
  80. return false;
  81. }
  82. if (c >= 0x7F && c <= 0x9F) {
  83. return false;
  84. }
  85. // out of range
  86. if (c > 0x10FFFF) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. function fromCodePoint(c) {
  92. /* eslint no-bitwise:0 */
  93. if (c > 0xffff) {
  94. c -= 0x10000;
  95. const surrogate1 = 0xd800 + (c >> 10);
  96. const surrogate2 = 0xdc00 + (c & 0x3ff);
  97. return String.fromCharCode(surrogate1, surrogate2);
  98. }
  99. return String.fromCharCode(c);
  100. }
  101. const UNESCAPE_MD_RE = /\\([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])/g;
  102. const ENTITY_RE = /&([a-z#][a-z0-9]{1,31});/gi;
  103. const UNESCAPE_ALL_RE = new RegExp(UNESCAPE_MD_RE.source + '|' + ENTITY_RE.source, 'gi');
  104. const DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))$/i;
  105. function replaceEntityPattern(match, name) {
  106. if (name.charCodeAt(0) === 0x23 /* # */ && DIGITAL_ENTITY_TEST_RE.test(name)) {
  107. const code = name[1].toLowerCase() === 'x' ? parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
  108. if (isValidEntityCode(code)) {
  109. return fromCodePoint(code);
  110. }
  111. return match;
  112. }
  113. const decoded = entities.decodeHTML(match);
  114. if (decoded !== match) {
  115. return decoded;
  116. }
  117. return match;
  118. }
  119. /* function replaceEntities(str) {
  120. if (str.indexOf('&') < 0) { return str; }
  121. return str.replace(ENTITY_RE, replaceEntityPattern);
  122. } */
  123. function unescapeMd(str) {
  124. if (str.indexOf('\\') < 0) {
  125. return str;
  126. }
  127. return str.replace(UNESCAPE_MD_RE, '$1');
  128. }
  129. function unescapeAll(str) {
  130. if (str.indexOf('\\') < 0 && str.indexOf('&') < 0) {
  131. return str;
  132. }
  133. return str.replace(UNESCAPE_ALL_RE, function (match, escaped, entity) {
  134. if (escaped) {
  135. return escaped;
  136. }
  137. return replaceEntityPattern(match, entity);
  138. });
  139. }
  140. const HTML_ESCAPE_TEST_RE = /[&<>"]/;
  141. const HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
  142. const HTML_REPLACEMENTS = {
  143. '&': '&amp;',
  144. '<': '&lt;',
  145. '>': '&gt;',
  146. '"': '&quot;'
  147. };
  148. function replaceUnsafeChar(ch) {
  149. return HTML_REPLACEMENTS[ch];
  150. }
  151. function escapeHtml(str) {
  152. if (HTML_ESCAPE_TEST_RE.test(str)) {
  153. return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar);
  154. }
  155. return str;
  156. }
  157. const REGEXP_ESCAPE_RE = /[.?*+^$[\]\\(){}|-]/g;
  158. function escapeRE(str) {
  159. return str.replace(REGEXP_ESCAPE_RE, '\\$&');
  160. }
  161. function isSpace(code) {
  162. switch (code) {
  163. case 0x09:
  164. case 0x20:
  165. return true;
  166. }
  167. return false;
  168. }
  169. // Zs (unicode class) || [\t\f\v\r\n]
  170. function isWhiteSpace(code) {
  171. if (code >= 0x2000 && code <= 0x200A) {
  172. return true;
  173. }
  174. switch (code) {
  175. case 0x09: // \t
  176. case 0x0A: // \n
  177. case 0x0B: // \v
  178. case 0x0C: // \f
  179. case 0x0D: // \r
  180. case 0x20:
  181. case 0xA0:
  182. case 0x1680:
  183. case 0x202F:
  184. case 0x205F:
  185. case 0x3000:
  186. return true;
  187. }
  188. return false;
  189. }
  190. /* eslint-disable max-len */
  191. // Currently without astral characters support.
  192. function isPunctChar(ch) {
  193. return ucmicro__namespace.P.test(ch) || ucmicro__namespace.S.test(ch);
  194. }
  195. // Markdown ASCII punctuation characters.
  196. //
  197. // !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, or ~
  198. // http://spec.commonmark.org/0.15/#ascii-punctuation-character
  199. //
  200. // Don't confuse with unicode punctuation !!! It lacks some chars in ascii range.
  201. //
  202. function isMdAsciiPunct(ch) {
  203. switch (ch) {
  204. case 0x21 /* ! */:
  205. case 0x22 /* " */:
  206. case 0x23 /* # */:
  207. case 0x24 /* $ */:
  208. case 0x25 /* % */:
  209. case 0x26 /* & */:
  210. case 0x27 /* ' */:
  211. case 0x28 /* ( */:
  212. case 0x29 /* ) */:
  213. case 0x2A /* * */:
  214. case 0x2B /* + */:
  215. case 0x2C /* , */:
  216. case 0x2D /* - */:
  217. case 0x2E /* . */:
  218. case 0x2F /* / */:
  219. case 0x3A /* : */:
  220. case 0x3B /* ; */:
  221. case 0x3C /* < */:
  222. case 0x3D /* = */:
  223. case 0x3E /* > */:
  224. case 0x3F /* ? */:
  225. case 0x40 /* @ */:
  226. case 0x5B /* [ */:
  227. case 0x5C /* \ */:
  228. case 0x5D /* ] */:
  229. case 0x5E /* ^ */:
  230. case 0x5F /* _ */:
  231. case 0x60 /* ` */:
  232. case 0x7B /* { */:
  233. case 0x7C /* | */:
  234. case 0x7D /* } */:
  235. case 0x7E /* ~ */:
  236. return true;
  237. default:
  238. return false;
  239. }
  240. }
  241. // Hepler to unify [reference labels].
  242. //
  243. function normalizeReference(str) {
  244. // Trim and collapse whitespace
  245. //
  246. str = str.trim().replace(/\s+/g, ' ');
  247. // In node v10 'ẞ'.toLowerCase() === 'Ṿ', which is presumed to be a bug
  248. // fixed in v12 (couldn't find any details).
  249. //
  250. // So treat this one as a special case
  251. // (remove this when node v10 is no longer supported).
  252. //
  253. if ('ẞ'.toLowerCase() === 'Ṿ') {
  254. str = str.replace(/ẞ/g, 'ß');
  255. }
  256. // .toLowerCase().toUpperCase() should get rid of all differences
  257. // between letter variants.
  258. //
  259. // Simple .toLowerCase() doesn't normalize 125 code points correctly,
  260. // and .toUpperCase doesn't normalize 6 of them (list of exceptions:
  261. // İ, ϴ, ẞ, Ω, K, Å - those are already uppercased, but have differently
  262. // uppercased versions).
  263. //
  264. // Here's an example showing how it happens. Lets take greek letter omega:
  265. // uppercase U+0398 (Θ), U+03f4 (ϴ) and lowercase U+03b8 (θ), U+03d1 (ϑ)
  266. //
  267. // Unicode entries:
  268. // 0398;GREEK CAPITAL LETTER THETA;Lu;0;L;;;;;N;;;;03B8;
  269. // 03B8;GREEK SMALL LETTER THETA;Ll;0;L;;;;;N;;;0398;;0398
  270. // 03D1;GREEK THETA SYMBOL;Ll;0;L;<compat> 03B8;;;;N;GREEK SMALL LETTER SCRIPT THETA;;0398;;0398
  271. // 03F4;GREEK CAPITAL THETA SYMBOL;Lu;0;L;<compat> 0398;;;;N;;;;03B8;
  272. //
  273. // Case-insensitive comparison should treat all of them as equivalent.
  274. //
  275. // But .toLowerCase() doesn't change ϑ (it's already lowercase),
  276. // and .toUpperCase() doesn't change ϴ (already uppercase).
  277. //
  278. // Applying first lower then upper case normalizes any character:
  279. // '\u0398\u03f4\u03b8\u03d1'.toLowerCase().toUpperCase() === '\u0398\u0398\u0398\u0398'
  280. //
  281. // Note: this is equivalent to unicode case folding; unicode normalization
  282. // is a different step that is not required here.
  283. //
  284. // Final result should be uppercased, because it's later stored in an object
  285. // (this avoid a conflict with Object.prototype members,
  286. // most notably, `__proto__`)
  287. //
  288. return str.toLowerCase().toUpperCase();
  289. }
  290. // Re-export libraries commonly used in both markdown-it and its plugins,
  291. // so plugins won't have to depend on them explicitly, which reduces their
  292. // bundled size (e.g. a browser build).
  293. //
  294. const lib = {
  295. mdurl: mdurl__namespace,
  296. ucmicro: ucmicro__namespace
  297. };
  298. var utils = /*#__PURE__*/Object.freeze({
  299. __proto__: null,
  300. arrayReplaceAt: arrayReplaceAt,
  301. assign: assign,
  302. escapeHtml: escapeHtml,
  303. escapeRE: escapeRE,
  304. fromCodePoint: fromCodePoint,
  305. has: has,
  306. isMdAsciiPunct: isMdAsciiPunct,
  307. isPunctChar: isPunctChar,
  308. isSpace: isSpace,
  309. isString: isString,
  310. isValidEntityCode: isValidEntityCode,
  311. isWhiteSpace: isWhiteSpace,
  312. lib: lib,
  313. normalizeReference: normalizeReference,
  314. unescapeAll: unescapeAll,
  315. unescapeMd: unescapeMd
  316. });
  317. // Parse link label
  318. //
  319. // this function assumes that first character ("[") already matches;
  320. // returns the end of the label
  321. //
  322. function parseLinkLabel(state, start, disableNested) {
  323. let level, found, marker, prevPos;
  324. const max = state.posMax;
  325. const oldPos = state.pos;
  326. state.pos = start + 1;
  327. level = 1;
  328. while (state.pos < max) {
  329. marker = state.src.charCodeAt(state.pos);
  330. if (marker === 0x5D /* ] */) {
  331. level--;
  332. if (level === 0) {
  333. found = true;
  334. break;
  335. }
  336. }
  337. prevPos = state.pos;
  338. state.md.inline.skipToken(state);
  339. if (marker === 0x5B /* [ */) {
  340. if (prevPos === state.pos - 1) {
  341. // increase level if we find text `[`, which is not a part of any token
  342. level++;
  343. } else if (disableNested) {
  344. state.pos = oldPos;
  345. return -1;
  346. }
  347. }
  348. }
  349. let labelEnd = -1;
  350. if (found) {
  351. labelEnd = state.pos;
  352. }
  353. // restore old state
  354. state.pos = oldPos;
  355. return labelEnd;
  356. }
  357. // Parse link destination
  358. //
  359. function parseLinkDestination(str, start, max) {
  360. let code;
  361. let pos = start;
  362. const result = {
  363. ok: false,
  364. pos: 0,
  365. str: ''
  366. };
  367. if (str.charCodeAt(pos) === 0x3C /* < */) {
  368. pos++;
  369. while (pos < max) {
  370. code = str.charCodeAt(pos);
  371. if (code === 0x0A /* \n */) {
  372. return result;
  373. }
  374. if (code === 0x3C /* < */) {
  375. return result;
  376. }
  377. if (code === 0x3E /* > */) {
  378. result.pos = pos + 1;
  379. result.str = unescapeAll(str.slice(start + 1, pos));
  380. result.ok = true;
  381. return result;
  382. }
  383. if (code === 0x5C /* \ */ && pos + 1 < max) {
  384. pos += 2;
  385. continue;
  386. }
  387. pos++;
  388. }
  389. // no closing '>'
  390. return result;
  391. }
  392. // this should be ... } else { ... branch
  393. let level = 0;
  394. while (pos < max) {
  395. code = str.charCodeAt(pos);
  396. if (code === 0x20) {
  397. break;
  398. }
  399. // ascii control characters
  400. if (code < 0x20 || code === 0x7F) {
  401. break;
  402. }
  403. if (code === 0x5C /* \ */ && pos + 1 < max) {
  404. if (str.charCodeAt(pos + 1) === 0x20) {
  405. break;
  406. }
  407. pos += 2;
  408. continue;
  409. }
  410. if (code === 0x28 /* ( */) {
  411. level++;
  412. if (level > 32) {
  413. return result;
  414. }
  415. }
  416. if (code === 0x29 /* ) */) {
  417. if (level === 0) {
  418. break;
  419. }
  420. level--;
  421. }
  422. pos++;
  423. }
  424. if (start === pos) {
  425. return result;
  426. }
  427. if (level !== 0) {
  428. return result;
  429. }
  430. result.str = unescapeAll(str.slice(start, pos));
  431. result.pos = pos;
  432. result.ok = true;
  433. return result;
  434. }
  435. // Parse link title
  436. //
  437. // Parse link title within `str` in [start, max] range,
  438. // or continue previous parsing if `prev_state` is defined (equal to result of last execution).
  439. //
  440. function parseLinkTitle(str, start, max, prev_state) {
  441. let code;
  442. let pos = start;
  443. const state = {
  444. // if `true`, this is a valid link title
  445. ok: false,
  446. // if `true`, this link can be continued on the next line
  447. can_continue: false,
  448. // if `ok`, it's the position of the first character after the closing marker
  449. pos: 0,
  450. // if `ok`, it's the unescaped title
  451. str: '',
  452. // expected closing marker character code
  453. marker: 0
  454. };
  455. if (prev_state) {
  456. // this is a continuation of a previous parseLinkTitle call on the next line,
  457. // used in reference links only
  458. state.str = prev_state.str;
  459. state.marker = prev_state.marker;
  460. } else {
  461. if (pos >= max) {
  462. return state;
  463. }
  464. let marker = str.charCodeAt(pos);
  465. if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) {
  466. return state;
  467. }
  468. start++;
  469. pos++;
  470. // if opening marker is "(", switch it to closing marker ")"
  471. if (marker === 0x28) {
  472. marker = 0x29;
  473. }
  474. state.marker = marker;
  475. }
  476. while (pos < max) {
  477. code = str.charCodeAt(pos);
  478. if (code === state.marker) {
  479. state.pos = pos + 1;
  480. state.str += unescapeAll(str.slice(start, pos));
  481. state.ok = true;
  482. return state;
  483. } else if (code === 0x28 /* ( */ && state.marker === 0x29 /* ) */) {
  484. return state;
  485. } else if (code === 0x5C /* \ */ && pos + 1 < max) {
  486. pos++;
  487. }
  488. pos++;
  489. }
  490. // no closing marker found, but this link title may continue on the next line (for references)
  491. state.can_continue = true;
  492. state.str += unescapeAll(str.slice(start, pos));
  493. return state;
  494. }
  495. // Just a shortcut for bulk export
  496. var helpers = /*#__PURE__*/Object.freeze({
  497. __proto__: null,
  498. parseLinkDestination: parseLinkDestination,
  499. parseLinkLabel: parseLinkLabel,
  500. parseLinkTitle: parseLinkTitle
  501. });
  502. /**
  503. * class Renderer
  504. *
  505. * Generates HTML from parsed token stream. Each instance has independent
  506. * copy of rules. Those can be rewritten with ease. Also, you can add new
  507. * rules if you create plugin and adds new token types.
  508. **/
  509. const default_rules = {};
  510. default_rules.code_inline = function (tokens, idx, options, env, slf) {
  511. const token = tokens[idx];
  512. return '<code' + slf.renderAttrs(token) + '>' + escapeHtml(token.content) + '</code>';
  513. };
  514. default_rules.code_block = function (tokens, idx, options, env, slf) {
  515. const token = tokens[idx];
  516. return '<pre' + slf.renderAttrs(token) + '><code>' + escapeHtml(tokens[idx].content) + '</code></pre>\n';
  517. };
  518. default_rules.fence = function (tokens, idx, options, env, slf) {
  519. const token = tokens[idx];
  520. const info = token.info ? unescapeAll(token.info).trim() : '';
  521. let langName = '';
  522. let langAttrs = '';
  523. if (info) {
  524. const arr = info.split(/(\s+)/g);
  525. langName = arr[0];
  526. langAttrs = arr.slice(2).join('');
  527. }
  528. let highlighted;
  529. if (options.highlight) {
  530. highlighted = options.highlight(token.content, langName, langAttrs) || escapeHtml(token.content);
  531. } else {
  532. highlighted = escapeHtml(token.content);
  533. }
  534. if (highlighted.indexOf('<pre') === 0) {
  535. return highlighted + '\n';
  536. }
  537. // If language exists, inject class gently, without modifying original token.
  538. // May be, one day we will add .deepClone() for token and simplify this part, but
  539. // now we prefer to keep things local.
  540. if (info) {
  541. const i = token.attrIndex('class');
  542. const tmpAttrs = token.attrs ? token.attrs.slice() : [];
  543. if (i < 0) {
  544. tmpAttrs.push(['class', options.langPrefix + langName]);
  545. } else {
  546. tmpAttrs[i] = tmpAttrs[i].slice();
  547. tmpAttrs[i][1] += ' ' + options.langPrefix + langName;
  548. }
  549. // Fake token just to render attributes
  550. const tmpToken = {
  551. attrs: tmpAttrs
  552. };
  553. return `<pre><code${slf.renderAttrs(tmpToken)}>${highlighted}</code></pre>\n`;
  554. }
  555. return `<pre><code${slf.renderAttrs(token)}>${highlighted}</code></pre>\n`;
  556. };
  557. default_rules.image = function (tokens, idx, options, env, slf) {
  558. const token = tokens[idx];
  559. // "alt" attr MUST be set, even if empty. Because it's mandatory and
  560. // should be placed on proper position for tests.
  561. //
  562. // Replace content with actual value
  563. token.attrs[token.attrIndex('alt')][1] = slf.renderInlineAsText(token.children, options, env);
  564. return slf.renderToken(tokens, idx, options);
  565. };
  566. default_rules.hardbreak = function (tokens, idx, options /*, env */) {
  567. return options.xhtmlOut ? '<br />\n' : '<br>\n';
  568. };
  569. default_rules.softbreak = function (tokens, idx, options /*, env */) {
  570. return options.breaks ? options.xhtmlOut ? '<br />\n' : '<br>\n' : '\n';
  571. };
  572. default_rules.text = function (tokens, idx /*, options, env */) {
  573. return escapeHtml(tokens[idx].content);
  574. };
  575. default_rules.html_block = function (tokens, idx /*, options, env */) {
  576. return tokens[idx].content;
  577. };
  578. default_rules.html_inline = function (tokens, idx /*, options, env */) {
  579. return tokens[idx].content;
  580. };
  581. /**
  582. * new Renderer()
  583. *
  584. * Creates new [[Renderer]] instance and fill [[Renderer#rules]] with defaults.
  585. **/
  586. function Renderer() {
  587. /**
  588. * Renderer#rules -> Object
  589. *
  590. * Contains render rules for tokens. Can be updated and extended.
  591. *
  592. * ##### Example
  593. *
  594. * ```javascript
  595. * var md = require('markdown-it')();
  596. *
  597. * md.renderer.rules.strong_open = function () { return '<b>'; };
  598. * md.renderer.rules.strong_close = function () { return '</b>'; };
  599. *
  600. * var result = md.renderInline(...);
  601. * ```
  602. *
  603. * Each rule is called as independent static function with fixed signature:
  604. *
  605. * ```javascript
  606. * function my_token_render(tokens, idx, options, env, renderer) {
  607. * // ...
  608. * return renderedHTML;
  609. * }
  610. * ```
  611. *
  612. * See [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.mjs)
  613. * for more details and examples.
  614. **/
  615. this.rules = assign({}, default_rules);
  616. }
  617. /**
  618. * Renderer.renderAttrs(token) -> String
  619. *
  620. * Render token attributes to string.
  621. **/
  622. Renderer.prototype.renderAttrs = function renderAttrs(token) {
  623. let i, l, result;
  624. if (!token.attrs) {
  625. return '';
  626. }
  627. result = '';
  628. for (i = 0, l = token.attrs.length; i < l; i++) {
  629. result += ' ' + escapeHtml(token.attrs[i][0]) + '="' + escapeHtml(token.attrs[i][1]) + '"';
  630. }
  631. return result;
  632. };
  633. /**
  634. * Renderer.renderToken(tokens, idx, options) -> String
  635. * - tokens (Array): list of tokens
  636. * - idx (Numbed): token index to render
  637. * - options (Object): params of parser instance
  638. *
  639. * Default token renderer. Can be overriden by custom function
  640. * in [[Renderer#rules]].
  641. **/
  642. Renderer.prototype.renderToken = function renderToken(tokens, idx, options) {
  643. const token = tokens[idx];
  644. let result = '';
  645. // Tight list paragraphs
  646. if (token.hidden) {
  647. return '';
  648. }
  649. // Insert a newline between hidden paragraph and subsequent opening
  650. // block-level tag.
  651. //
  652. // For example, here we should insert a newline before blockquote:
  653. // - a
  654. // >
  655. //
  656. if (token.block && token.nesting !== -1 && idx && tokens[idx - 1].hidden) {
  657. result += '\n';
  658. }
  659. // Add token name, e.g. `<img`
  660. result += (token.nesting === -1 ? '</' : '<') + token.tag;
  661. // Encode attributes, e.g. `<img src="foo"`
  662. result += this.renderAttrs(token);
  663. // Add a slash for self-closing tags, e.g. `<img src="foo" /`
  664. if (token.nesting === 0 && options.xhtmlOut) {
  665. result += ' /';
  666. }
  667. // Check if we need to add a newline after this tag
  668. let needLf = false;
  669. if (token.block) {
  670. needLf = true;
  671. if (token.nesting === 1) {
  672. if (idx + 1 < tokens.length) {
  673. const nextToken = tokens[idx + 1];
  674. if (nextToken.type === 'inline' || nextToken.hidden) {
  675. // Block-level tag containing an inline tag.
  676. //
  677. needLf = false;
  678. } else if (nextToken.nesting === -1 && nextToken.tag === token.tag) {
  679. // Opening tag + closing tag of the same type. E.g. `<li></li>`.
  680. //
  681. needLf = false;
  682. }
  683. }
  684. }
  685. }
  686. result += needLf ? '>\n' : '>';
  687. return result;
  688. };
  689. /**
  690. * Renderer.renderInline(tokens, options, env) -> String
  691. * - tokens (Array): list on block tokens to render
  692. * - options (Object): params of parser instance
  693. * - env (Object): additional data from parsed input (references, for example)
  694. *
  695. * The same as [[Renderer.render]], but for single token of `inline` type.
  696. **/
  697. Renderer.prototype.renderInline = function (tokens, options, env) {
  698. let result = '';
  699. const rules = this.rules;
  700. for (let i = 0, len = tokens.length; i < len; i++) {
  701. const type = tokens[i].type;
  702. if (typeof rules[type] !== 'undefined') {
  703. result += rules[type](tokens, i, options, env, this);
  704. } else {
  705. result += this.renderToken(tokens, i, options);
  706. }
  707. }
  708. return result;
  709. };
  710. /** internal
  711. * Renderer.renderInlineAsText(tokens, options, env) -> String
  712. * - tokens (Array): list on block tokens to render
  713. * - options (Object): params of parser instance
  714. * - env (Object): additional data from parsed input (references, for example)
  715. *
  716. * Special kludge for image `alt` attributes to conform CommonMark spec.
  717. * Don't try to use it! Spec requires to show `alt` content with stripped markup,
  718. * instead of simple escaping.
  719. **/
  720. Renderer.prototype.renderInlineAsText = function (tokens, options, env) {
  721. let result = '';
  722. for (let i = 0, len = tokens.length; i < len; i++) {
  723. switch (tokens[i].type) {
  724. case 'text':
  725. result += tokens[i].content;
  726. break;
  727. case 'image':
  728. result += this.renderInlineAsText(tokens[i].children, options, env);
  729. break;
  730. case 'html_inline':
  731. case 'html_block':
  732. result += tokens[i].content;
  733. break;
  734. case 'softbreak':
  735. case 'hardbreak':
  736. result += '\n';
  737. break;
  738. // all other tokens are skipped
  739. }
  740. }
  741. return result;
  742. };
  743. /**
  744. * Renderer.render(tokens, options, env) -> String
  745. * - tokens (Array): list on block tokens to render
  746. * - options (Object): params of parser instance
  747. * - env (Object): additional data from parsed input (references, for example)
  748. *
  749. * Takes token stream and generates HTML. Probably, you will never need to call
  750. * this method directly.
  751. **/
  752. Renderer.prototype.render = function (tokens, options, env) {
  753. let result = '';
  754. const rules = this.rules;
  755. for (let i = 0, len = tokens.length; i < len; i++) {
  756. const type = tokens[i].type;
  757. if (type === 'inline') {
  758. result += this.renderInline(tokens[i].children, options, env);
  759. } else if (typeof rules[type] !== 'undefined') {
  760. result += rules[type](tokens, i, options, env, this);
  761. } else {
  762. result += this.renderToken(tokens, i, options, env);
  763. }
  764. }
  765. return result;
  766. };
  767. /**
  768. * class Ruler
  769. *
  770. * Helper class, used by [[MarkdownIt#core]], [[MarkdownIt#block]] and
  771. * [[MarkdownIt#inline]] to manage sequences of functions (rules):
  772. *
  773. * - keep rules in defined order
  774. * - assign the name to each rule
  775. * - enable/disable rules
  776. * - add/replace rules
  777. * - allow assign rules to additional named chains (in the same)
  778. * - cacheing lists of active rules
  779. *
  780. * You will not need use this class directly until write plugins. For simple
  781. * rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and
  782. * [[MarkdownIt.use]].
  783. **/
  784. /**
  785. * new Ruler()
  786. **/
  787. function Ruler() {
  788. // List of added rules. Each element is:
  789. //
  790. // {
  791. // name: XXX,
  792. // enabled: Boolean,
  793. // fn: Function(),
  794. // alt: [ name2, name3 ]
  795. // }
  796. //
  797. this.__rules__ = [];
  798. // Cached rule chains.
  799. //
  800. // First level - chain name, '' for default.
  801. // Second level - diginal anchor for fast filtering by charcodes.
  802. //
  803. this.__cache__ = null;
  804. }
  805. // Helper methods, should not be used directly
  806. // Find rule index by name
  807. //
  808. Ruler.prototype.__find__ = function (name) {
  809. for (let i = 0; i < this.__rules__.length; i++) {
  810. if (this.__rules__[i].name === name) {
  811. return i;
  812. }
  813. }
  814. return -1;
  815. };
  816. // Build rules lookup cache
  817. //
  818. Ruler.prototype.__compile__ = function () {
  819. const self = this;
  820. const chains = [''];
  821. // collect unique names
  822. self.__rules__.forEach(function (rule) {
  823. if (!rule.enabled) {
  824. return;
  825. }
  826. rule.alt.forEach(function (altName) {
  827. if (chains.indexOf(altName) < 0) {
  828. chains.push(altName);
  829. }
  830. });
  831. });
  832. self.__cache__ = {};
  833. chains.forEach(function (chain) {
  834. self.__cache__[chain] = [];
  835. self.__rules__.forEach(function (rule) {
  836. if (!rule.enabled) {
  837. return;
  838. }
  839. if (chain && rule.alt.indexOf(chain) < 0) {
  840. return;
  841. }
  842. self.__cache__[chain].push(rule.fn);
  843. });
  844. });
  845. };
  846. /**
  847. * Ruler.at(name, fn [, options])
  848. * - name (String): rule name to replace.
  849. * - fn (Function): new rule function.
  850. * - options (Object): new rule options (not mandatory).
  851. *
  852. * Replace rule by name with new function & options. Throws error if name not
  853. * found.
  854. *
  855. * ##### Options:
  856. *
  857. * - __alt__ - array with names of "alternate" chains.
  858. *
  859. * ##### Example
  860. *
  861. * Replace existing typographer replacement rule with new one:
  862. *
  863. * ```javascript
  864. * var md = require('markdown-it')();
  865. *
  866. * md.core.ruler.at('replacements', function replace(state) {
  867. * //...
  868. * });
  869. * ```
  870. **/
  871. Ruler.prototype.at = function (name, fn, options) {
  872. const index = this.__find__(name);
  873. const opt = options || {};
  874. if (index === -1) {
  875. throw new Error('Parser rule not found: ' + name);
  876. }
  877. this.__rules__[index].fn = fn;
  878. this.__rules__[index].alt = opt.alt || [];
  879. this.__cache__ = null;
  880. };
  881. /**
  882. * Ruler.before(beforeName, ruleName, fn [, options])
  883. * - beforeName (String): new rule will be added before this one.
  884. * - ruleName (String): name of added rule.
  885. * - fn (Function): rule function.
  886. * - options (Object): rule options (not mandatory).
  887. *
  888. * Add new rule to chain before one with given name. See also
  889. * [[Ruler.after]], [[Ruler.push]].
  890. *
  891. * ##### Options:
  892. *
  893. * - __alt__ - array with names of "alternate" chains.
  894. *
  895. * ##### Example
  896. *
  897. * ```javascript
  898. * var md = require('markdown-it')();
  899. *
  900. * md.block.ruler.before('paragraph', 'my_rule', function replace(state) {
  901. * //...
  902. * });
  903. * ```
  904. **/
  905. Ruler.prototype.before = function (beforeName, ruleName, fn, options) {
  906. const index = this.__find__(beforeName);
  907. const opt = options || {};
  908. if (index === -1) {
  909. throw new Error('Parser rule not found: ' + beforeName);
  910. }
  911. this.__rules__.splice(index, 0, {
  912. name: ruleName,
  913. enabled: true,
  914. fn,
  915. alt: opt.alt || []
  916. });
  917. this.__cache__ = null;
  918. };
  919. /**
  920. * Ruler.after(afterName, ruleName, fn [, options])
  921. * - afterName (String): new rule will be added after this one.
  922. * - ruleName (String): name of added rule.
  923. * - fn (Function): rule function.
  924. * - options (Object): rule options (not mandatory).
  925. *
  926. * Add new rule to chain after one with given name. See also
  927. * [[Ruler.before]], [[Ruler.push]].
  928. *
  929. * ##### Options:
  930. *
  931. * - __alt__ - array with names of "alternate" chains.
  932. *
  933. * ##### Example
  934. *
  935. * ```javascript
  936. * var md = require('markdown-it')();
  937. *
  938. * md.inline.ruler.after('text', 'my_rule', function replace(state) {
  939. * //...
  940. * });
  941. * ```
  942. **/
  943. Ruler.prototype.after = function (afterName, ruleName, fn, options) {
  944. const index = this.__find__(afterName);
  945. const opt = options || {};
  946. if (index === -1) {
  947. throw new Error('Parser rule not found: ' + afterName);
  948. }
  949. this.__rules__.splice(index + 1, 0, {
  950. name: ruleName,
  951. enabled: true,
  952. fn,
  953. alt: opt.alt || []
  954. });
  955. this.__cache__ = null;
  956. };
  957. /**
  958. * Ruler.push(ruleName, fn [, options])
  959. * - ruleName (String): name of added rule.
  960. * - fn (Function): rule function.
  961. * - options (Object): rule options (not mandatory).
  962. *
  963. * Push new rule to the end of chain. See also
  964. * [[Ruler.before]], [[Ruler.after]].
  965. *
  966. * ##### Options:
  967. *
  968. * - __alt__ - array with names of "alternate" chains.
  969. *
  970. * ##### Example
  971. *
  972. * ```javascript
  973. * var md = require('markdown-it')();
  974. *
  975. * md.core.ruler.push('my_rule', function replace(state) {
  976. * //...
  977. * });
  978. * ```
  979. **/
  980. Ruler.prototype.push = function (ruleName, fn, options) {
  981. const opt = options || {};
  982. this.__rules__.push({
  983. name: ruleName,
  984. enabled: true,
  985. fn,
  986. alt: opt.alt || []
  987. });
  988. this.__cache__ = null;
  989. };
  990. /**
  991. * Ruler.enable(list [, ignoreInvalid]) -> Array
  992. * - list (String|Array): list of rule names to enable.
  993. * - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
  994. *
  995. * Enable rules with given names. If any rule name not found - throw Error.
  996. * Errors can be disabled by second param.
  997. *
  998. * Returns list of found rule names (if no exception happened).
  999. *
  1000. * See also [[Ruler.disable]], [[Ruler.enableOnly]].
  1001. **/
  1002. Ruler.prototype.enable = function (list, ignoreInvalid) {
  1003. if (!Array.isArray(list)) {
  1004. list = [list];
  1005. }
  1006. const result = [];
  1007. // Search by name and enable
  1008. list.forEach(function (name) {
  1009. const idx = this.__find__(name);
  1010. if (idx < 0) {
  1011. if (ignoreInvalid) {
  1012. return;
  1013. }
  1014. throw new Error('Rules manager: invalid rule name ' + name);
  1015. }
  1016. this.__rules__[idx].enabled = true;
  1017. result.push(name);
  1018. }, this);
  1019. this.__cache__ = null;
  1020. return result;
  1021. };
  1022. /**
  1023. * Ruler.enableOnly(list [, ignoreInvalid])
  1024. * - list (String|Array): list of rule names to enable (whitelist).
  1025. * - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
  1026. *
  1027. * Enable rules with given names, and disable everything else. If any rule name
  1028. * not found - throw Error. Errors can be disabled by second param.
  1029. *
  1030. * See also [[Ruler.disable]], [[Ruler.enable]].
  1031. **/
  1032. Ruler.prototype.enableOnly = function (list, ignoreInvalid) {
  1033. if (!Array.isArray(list)) {
  1034. list = [list];
  1035. }
  1036. this.__rules__.forEach(function (rule) {
  1037. rule.enabled = false;
  1038. });
  1039. this.enable(list, ignoreInvalid);
  1040. };
  1041. /**
  1042. * Ruler.disable(list [, ignoreInvalid]) -> Array
  1043. * - list (String|Array): list of rule names to disable.
  1044. * - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
  1045. *
  1046. * Disable rules with given names. If any rule name not found - throw Error.
  1047. * Errors can be disabled by second param.
  1048. *
  1049. * Returns list of found rule names (if no exception happened).
  1050. *
  1051. * See also [[Ruler.enable]], [[Ruler.enableOnly]].
  1052. **/
  1053. Ruler.prototype.disable = function (list, ignoreInvalid) {
  1054. if (!Array.isArray(list)) {
  1055. list = [list];
  1056. }
  1057. const result = [];
  1058. // Search by name and disable
  1059. list.forEach(function (name) {
  1060. const idx = this.__find__(name);
  1061. if (idx < 0) {
  1062. if (ignoreInvalid) {
  1063. return;
  1064. }
  1065. throw new Error('Rules manager: invalid rule name ' + name);
  1066. }
  1067. this.__rules__[idx].enabled = false;
  1068. result.push(name);
  1069. }, this);
  1070. this.__cache__ = null;
  1071. return result;
  1072. };
  1073. /**
  1074. * Ruler.getRules(chainName) -> Array
  1075. *
  1076. * Return array of active functions (rules) for given chain name. It analyzes
  1077. * rules configuration, compiles caches if not exists and returns result.
  1078. *
  1079. * Default chain name is `''` (empty string). It can't be skipped. That's
  1080. * done intentionally, to keep signature monomorphic for high speed.
  1081. **/
  1082. Ruler.prototype.getRules = function (chainName) {
  1083. if (this.__cache__ === null) {
  1084. this.__compile__();
  1085. }
  1086. // Chain can be empty, if rules disabled. But we still have to return Array.
  1087. return this.__cache__[chainName] || [];
  1088. };
  1089. // Token class
  1090. /**
  1091. * class Token
  1092. **/
  1093. /**
  1094. * new Token(type, tag, nesting)
  1095. *
  1096. * Create new token and fill passed properties.
  1097. **/
  1098. function Token(type, tag, nesting) {
  1099. /**
  1100. * Token#type -> String
  1101. *
  1102. * Type of the token (string, e.g. "paragraph_open")
  1103. **/
  1104. this.type = type;
  1105. /**
  1106. * Token#tag -> String
  1107. *
  1108. * html tag name, e.g. "p"
  1109. **/
  1110. this.tag = tag;
  1111. /**
  1112. * Token#attrs -> Array
  1113. *
  1114. * Html attributes. Format: `[ [ name1, value1 ], [ name2, value2 ] ]`
  1115. **/
  1116. this.attrs = null;
  1117. /**
  1118. * Token#map -> Array
  1119. *
  1120. * Source map info. Format: `[ line_begin, line_end ]`
  1121. **/
  1122. this.map = null;
  1123. /**
  1124. * Token#nesting -> Number
  1125. *
  1126. * Level change (number in {-1, 0, 1} set), where:
  1127. *
  1128. * - `1` means the tag is opening
  1129. * - `0` means the tag is self-closing
  1130. * - `-1` means the tag is closing
  1131. **/
  1132. this.nesting = nesting;
  1133. /**
  1134. * Token#level -> Number
  1135. *
  1136. * nesting level, the same as `state.level`
  1137. **/
  1138. this.level = 0;
  1139. /**
  1140. * Token#children -> Array
  1141. *
  1142. * An array of child nodes (inline and img tokens)
  1143. **/
  1144. this.children = null;
  1145. /**
  1146. * Token#content -> String
  1147. *
  1148. * In a case of self-closing tag (code, html, fence, etc.),
  1149. * it has contents of this tag.
  1150. **/
  1151. this.content = '';
  1152. /**
  1153. * Token#markup -> String
  1154. *
  1155. * '*' or '_' for emphasis, fence string for fence, etc.
  1156. **/
  1157. this.markup = '';
  1158. /**
  1159. * Token#info -> String
  1160. *
  1161. * Additional information:
  1162. *
  1163. * - Info string for "fence" tokens
  1164. * - The value "auto" for autolink "link_open" and "link_close" tokens
  1165. * - The string value of the item marker for ordered-list "list_item_open" tokens
  1166. **/
  1167. this.info = '';
  1168. /**
  1169. * Token#meta -> Object
  1170. *
  1171. * A place for plugins to store an arbitrary data
  1172. **/
  1173. this.meta = null;
  1174. /**
  1175. * Token#block -> Boolean
  1176. *
  1177. * True for block-level tokens, false for inline tokens.
  1178. * Used in renderer to calculate line breaks
  1179. **/
  1180. this.block = false;
  1181. /**
  1182. * Token#hidden -> Boolean
  1183. *
  1184. * If it's true, ignore this element when rendering. Used for tight lists
  1185. * to hide paragraphs.
  1186. **/
  1187. this.hidden = false;
  1188. }
  1189. /**
  1190. * Token.attrIndex(name) -> Number
  1191. *
  1192. * Search attribute index by name.
  1193. **/
  1194. Token.prototype.attrIndex = function attrIndex(name) {
  1195. if (!this.attrs) {
  1196. return -1;
  1197. }
  1198. const attrs = this.attrs;
  1199. for (let i = 0, len = attrs.length; i < len; i++) {
  1200. if (attrs[i][0] === name) {
  1201. return i;
  1202. }
  1203. }
  1204. return -1;
  1205. };
  1206. /**
  1207. * Token.attrPush(attrData)
  1208. *
  1209. * Add `[ name, value ]` attribute to list. Init attrs if necessary
  1210. **/
  1211. Token.prototype.attrPush = function attrPush(attrData) {
  1212. if (this.attrs) {
  1213. this.attrs.push(attrData);
  1214. } else {
  1215. this.attrs = [attrData];
  1216. }
  1217. };
  1218. /**
  1219. * Token.attrSet(name, value)
  1220. *
  1221. * Set `name` attribute to `value`. Override old value if exists.
  1222. **/
  1223. Token.prototype.attrSet = function attrSet(name, value) {
  1224. const idx = this.attrIndex(name);
  1225. const attrData = [name, value];
  1226. if (idx < 0) {
  1227. this.attrPush(attrData);
  1228. } else {
  1229. this.attrs[idx] = attrData;
  1230. }
  1231. };
  1232. /**
  1233. * Token.attrGet(name)
  1234. *
  1235. * Get the value of attribute `name`, or null if it does not exist.
  1236. **/
  1237. Token.prototype.attrGet = function attrGet(name) {
  1238. const idx = this.attrIndex(name);
  1239. let value = null;
  1240. if (idx >= 0) {
  1241. value = this.attrs[idx][1];
  1242. }
  1243. return value;
  1244. };
  1245. /**
  1246. * Token.attrJoin(name, value)
  1247. *
  1248. * Join value to existing attribute via space. Or create new attribute if not
  1249. * exists. Useful to operate with token classes.
  1250. **/
  1251. Token.prototype.attrJoin = function attrJoin(name, value) {
  1252. const idx = this.attrIndex(name);
  1253. if (idx < 0) {
  1254. this.attrPush([name, value]);
  1255. } else {
  1256. this.attrs[idx][1] = this.attrs[idx][1] + ' ' + value;
  1257. }
  1258. };
  1259. // Core state object
  1260. //
  1261. function StateCore(src, md, env) {
  1262. this.src = src;
  1263. this.env = env;
  1264. this.tokens = [];
  1265. this.inlineMode = false;
  1266. this.md = md; // link to parser instance
  1267. }
  1268. // re-export Token class to use in core rules
  1269. StateCore.prototype.Token = Token;
  1270. // Normalize input string
  1271. // https://spec.commonmark.org/0.29/#line-ending
  1272. const NEWLINES_RE = /\r\n?|\n/g;
  1273. const NULL_RE = /\0/g;
  1274. function normalize(state) {
  1275. let str;
  1276. // Normalize newlines
  1277. str = state.src.replace(NEWLINES_RE, '\n');
  1278. // Replace NULL characters
  1279. str = str.replace(NULL_RE, '\uFFFD');
  1280. state.src = str;
  1281. }
  1282. function block(state) {
  1283. let token;
  1284. if (state.inlineMode) {
  1285. token = new state.Token('inline', '', 0);
  1286. token.content = state.src;
  1287. token.map = [0, 1];
  1288. token.children = [];
  1289. state.tokens.push(token);
  1290. } else {
  1291. state.md.block.parse(state.src, state.md, state.env, state.tokens);
  1292. }
  1293. }
  1294. function inline(state) {
  1295. const tokens = state.tokens;
  1296. // Parse inlines
  1297. for (let i = 0, l = tokens.length; i < l; i++) {
  1298. const tok = tokens[i];
  1299. if (tok.type === 'inline') {
  1300. state.md.inline.parse(tok.content, state.md, state.env, tok.children);
  1301. }
  1302. }
  1303. }
  1304. // Replace link-like texts with link nodes.
  1305. //
  1306. // Currently restricted by `md.validateLink()` to http/https/ftp
  1307. //
  1308. function isLinkOpen$1(str) {
  1309. return /^<a[>\s]/i.test(str);
  1310. }
  1311. function isLinkClose$1(str) {
  1312. return /^<\/a\s*>/i.test(str);
  1313. }
  1314. function linkify$1(state) {
  1315. const blockTokens = state.tokens;
  1316. if (!state.md.options.linkify) {
  1317. return;
  1318. }
  1319. for (let j = 0, l = blockTokens.length; j < l; j++) {
  1320. if (blockTokens[j].type !== 'inline' || !state.md.linkify.pretest(blockTokens[j].content)) {
  1321. continue;
  1322. }
  1323. let tokens = blockTokens[j].children;
  1324. let htmlLinkLevel = 0;
  1325. // We scan from the end, to keep position when new tags added.
  1326. // Use reversed logic in links start/end match
  1327. for (let i = tokens.length - 1; i >= 0; i--) {
  1328. const currentToken = tokens[i];
  1329. // Skip content of markdown links
  1330. if (currentToken.type === 'link_close') {
  1331. i--;
  1332. while (tokens[i].level !== currentToken.level && tokens[i].type !== 'link_open') {
  1333. i--;
  1334. }
  1335. continue;
  1336. }
  1337. // Skip content of html tag links
  1338. if (currentToken.type === 'html_inline') {
  1339. if (isLinkOpen$1(currentToken.content) && htmlLinkLevel > 0) {
  1340. htmlLinkLevel--;
  1341. }
  1342. if (isLinkClose$1(currentToken.content)) {
  1343. htmlLinkLevel++;
  1344. }
  1345. }
  1346. if (htmlLinkLevel > 0) {
  1347. continue;
  1348. }
  1349. if (currentToken.type === 'text' && state.md.linkify.test(currentToken.content)) {
  1350. const text = currentToken.content;
  1351. let links = state.md.linkify.match(text);
  1352. // Now split string to nodes
  1353. const nodes = [];
  1354. let level = currentToken.level;
  1355. let lastPos = 0;
  1356. // forbid escape sequence at the start of the string,
  1357. // this avoids http\://example.com/ from being linkified as
  1358. // http:<a href="//example.com/">//example.com/</a>
  1359. if (links.length > 0 && links[0].index === 0 && i > 0 && tokens[i - 1].type === 'text_special') {
  1360. links = links.slice(1);
  1361. }
  1362. for (let ln = 0; ln < links.length; ln++) {
  1363. const url = links[ln].url;
  1364. const fullUrl = state.md.normalizeLink(url);
  1365. if (!state.md.validateLink(fullUrl)) {
  1366. continue;
  1367. }
  1368. let urlText = links[ln].text;
  1369. // Linkifier might send raw hostnames like "example.com", where url
  1370. // starts with domain name. So we prepend http:// in those cases,
  1371. // and remove it afterwards.
  1372. //
  1373. if (!links[ln].schema) {
  1374. urlText = state.md.normalizeLinkText('http://' + urlText).replace(/^http:\/\//, '');
  1375. } else if (links[ln].schema === 'mailto:' && !/^mailto:/i.test(urlText)) {
  1376. urlText = state.md.normalizeLinkText('mailto:' + urlText).replace(/^mailto:/, '');
  1377. } else {
  1378. urlText = state.md.normalizeLinkText(urlText);
  1379. }
  1380. const pos = links[ln].index;
  1381. if (pos > lastPos) {
  1382. const token = new state.Token('text', '', 0);
  1383. token.content = text.slice(lastPos, pos);
  1384. token.level = level;
  1385. nodes.push(token);
  1386. }
  1387. const token_o = new state.Token('link_open', 'a', 1);
  1388. token_o.attrs = [['href', fullUrl]];
  1389. token_o.level = level++;
  1390. token_o.markup = 'linkify';
  1391. token_o.info = 'auto';
  1392. nodes.push(token_o);
  1393. const token_t = new state.Token('text', '', 0);
  1394. token_t.content = urlText;
  1395. token_t.level = level;
  1396. nodes.push(token_t);
  1397. const token_c = new state.Token('link_close', 'a', -1);
  1398. token_c.level = --level;
  1399. token_c.markup = 'linkify';
  1400. token_c.info = 'auto';
  1401. nodes.push(token_c);
  1402. lastPos = links[ln].lastIndex;
  1403. }
  1404. if (lastPos < text.length) {
  1405. const token = new state.Token('text', '', 0);
  1406. token.content = text.slice(lastPos);
  1407. token.level = level;
  1408. nodes.push(token);
  1409. }
  1410. // replace current node
  1411. blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes);
  1412. }
  1413. }
  1414. }
  1415. }
  1416. // Simple typographic replacements
  1417. //
  1418. // (c) (C) → ©
  1419. // (tm) (TM) → ™
  1420. // (r) (R) → ®
  1421. // +- → ±
  1422. // ... → … (also ?.... → ?.., !.... → !..)
  1423. // ???????? → ???, !!!!! → !!!, `,,` → `,`
  1424. // -- → &ndash;, --- → &mdash;
  1425. //
  1426. // TODO:
  1427. // - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
  1428. // - multiplications 2 x 4 -> 2 × 4
  1429. const RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/;
  1430. // Workaround for phantomjs - need regex without /g flag,
  1431. // or root check will fail every second time
  1432. const SCOPED_ABBR_TEST_RE = /\((c|tm|r)\)/i;
  1433. const SCOPED_ABBR_RE = /\((c|tm|r)\)/ig;
  1434. const SCOPED_ABBR = {
  1435. c: '©',
  1436. r: '®',
  1437. tm: '™'
  1438. };
  1439. function replaceFn(match, name) {
  1440. return SCOPED_ABBR[name.toLowerCase()];
  1441. }
  1442. function replace_scoped(inlineTokens) {
  1443. let inside_autolink = 0;
  1444. for (let i = inlineTokens.length - 1; i >= 0; i--) {
  1445. const token = inlineTokens[i];
  1446. if (token.type === 'text' && !inside_autolink) {
  1447. token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn);
  1448. }
  1449. if (token.type === 'link_open' && token.info === 'auto') {
  1450. inside_autolink--;
  1451. }
  1452. if (token.type === 'link_close' && token.info === 'auto') {
  1453. inside_autolink++;
  1454. }
  1455. }
  1456. }
  1457. function replace_rare(inlineTokens) {
  1458. let inside_autolink = 0;
  1459. for (let i = inlineTokens.length - 1; i >= 0; i--) {
  1460. const token = inlineTokens[i];
  1461. if (token.type === 'text' && !inside_autolink) {
  1462. if (RARE_RE.test(token.content)) {
  1463. token.content = token.content.replace(/\+-/g, '±')
  1464. // .., ..., ....... -> …
  1465. // but ?..... & !..... -> ?.. & !..
  1466. .replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..').replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',')
  1467. // em-dash
  1468. .replace(/(^|[^-])---(?=[^-]|$)/mg, '$1\u2014')
  1469. // en-dash
  1470. .replace(/(^|\s)--(?=\s|$)/mg, '$1\u2013').replace(/(^|[^-\s])--(?=[^-\s]|$)/mg, '$1\u2013');
  1471. }
  1472. }
  1473. if (token.type === 'link_open' && token.info === 'auto') {
  1474. inside_autolink--;
  1475. }
  1476. if (token.type === 'link_close' && token.info === 'auto') {
  1477. inside_autolink++;
  1478. }
  1479. }
  1480. }
  1481. function replace(state) {
  1482. let blkIdx;
  1483. if (!state.md.options.typographer) {
  1484. return;
  1485. }
  1486. for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
  1487. if (state.tokens[blkIdx].type !== 'inline') {
  1488. continue;
  1489. }
  1490. if (SCOPED_ABBR_TEST_RE.test(state.tokens[blkIdx].content)) {
  1491. replace_scoped(state.tokens[blkIdx].children);
  1492. }
  1493. if (RARE_RE.test(state.tokens[blkIdx].content)) {
  1494. replace_rare(state.tokens[blkIdx].children);
  1495. }
  1496. }
  1497. }
  1498. // Convert straight quotation marks to typographic ones
  1499. //
  1500. const QUOTE_TEST_RE = /['"]/;
  1501. const QUOTE_RE = /['"]/g;
  1502. const APOSTROPHE = '\u2019'; /* ’ */
  1503. function replaceAt(str, index, ch) {
  1504. return str.slice(0, index) + ch + str.slice(index + 1);
  1505. }
  1506. function process_inlines(tokens, state) {
  1507. let j;
  1508. const stack = [];
  1509. for (let i = 0; i < tokens.length; i++) {
  1510. const token = tokens[i];
  1511. const thisLevel = tokens[i].level;
  1512. for (j = stack.length - 1; j >= 0; j--) {
  1513. if (stack[j].level <= thisLevel) {
  1514. break;
  1515. }
  1516. }
  1517. stack.length = j + 1;
  1518. if (token.type !== 'text') {
  1519. continue;
  1520. }
  1521. let text = token.content;
  1522. let pos = 0;
  1523. let max = text.length;
  1524. /* eslint no-labels:0,block-scoped-var:0 */
  1525. OUTER: while (pos < max) {
  1526. QUOTE_RE.lastIndex = pos;
  1527. const t = QUOTE_RE.exec(text);
  1528. if (!t) {
  1529. break;
  1530. }
  1531. let canOpen = true;
  1532. let canClose = true;
  1533. pos = t.index + 1;
  1534. const isSingle = t[0] === "'";
  1535. // Find previous character,
  1536. // default to space if it's the beginning of the line
  1537. //
  1538. let lastChar = 0x20;
  1539. if (t.index - 1 >= 0) {
  1540. lastChar = text.charCodeAt(t.index - 1);
  1541. } else {
  1542. for (j = i - 1; j >= 0; j--) {
  1543. if (tokens[j].type === 'softbreak' || tokens[j].type === 'hardbreak') break; // lastChar defaults to 0x20
  1544. if (!tokens[j].content) continue; // should skip all tokens except 'text', 'html_inline' or 'code_inline'
  1545. lastChar = tokens[j].content.charCodeAt(tokens[j].content.length - 1);
  1546. break;
  1547. }
  1548. }
  1549. // Find next character,
  1550. // default to space if it's the end of the line
  1551. //
  1552. let nextChar = 0x20;
  1553. if (pos < max) {
  1554. nextChar = text.charCodeAt(pos);
  1555. } else {
  1556. for (j = i + 1; j < tokens.length; j++) {
  1557. if (tokens[j].type === 'softbreak' || tokens[j].type === 'hardbreak') break; // nextChar defaults to 0x20
  1558. if (!tokens[j].content) continue; // should skip all tokens except 'text', 'html_inline' or 'code_inline'
  1559. nextChar = tokens[j].content.charCodeAt(0);
  1560. break;
  1561. }
  1562. }
  1563. const isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(String.fromCharCode(lastChar));
  1564. const isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(String.fromCharCode(nextChar));
  1565. const isLastWhiteSpace = isWhiteSpace(lastChar);
  1566. const isNextWhiteSpace = isWhiteSpace(nextChar);
  1567. if (isNextWhiteSpace) {
  1568. canOpen = false;
  1569. } else if (isNextPunctChar) {
  1570. if (!(isLastWhiteSpace || isLastPunctChar)) {
  1571. canOpen = false;
  1572. }
  1573. }
  1574. if (isLastWhiteSpace) {
  1575. canClose = false;
  1576. } else if (isLastPunctChar) {
  1577. if (!(isNextWhiteSpace || isNextPunctChar)) {
  1578. canClose = false;
  1579. }
  1580. }
  1581. if (nextChar === 0x22 /* " */ && t[0] === '"') {
  1582. if (lastChar >= 0x30 /* 0 */ && lastChar <= 0x39 /* 9 */) {
  1583. // special case: 1"" - count first quote as an inch
  1584. canClose = canOpen = false;
  1585. }
  1586. }
  1587. if (canOpen && canClose) {
  1588. // Replace quotes in the middle of punctuation sequence, but not
  1589. // in the middle of the words, i.e.:
  1590. //
  1591. // 1. foo " bar " baz - not replaced
  1592. // 2. foo-"-bar-"-baz - replaced
  1593. // 3. foo"bar"baz - not replaced
  1594. //
  1595. canOpen = isLastPunctChar;
  1596. canClose = isNextPunctChar;
  1597. }
  1598. if (!canOpen && !canClose) {
  1599. // middle of word
  1600. if (isSingle) {
  1601. token.content = replaceAt(token.content, t.index, APOSTROPHE);
  1602. }
  1603. continue;
  1604. }
  1605. if (canClose) {
  1606. // this could be a closing quote, rewind the stack to get a match
  1607. for (j = stack.length - 1; j >= 0; j--) {
  1608. let item = stack[j];
  1609. if (stack[j].level < thisLevel) {
  1610. break;
  1611. }
  1612. if (item.single === isSingle && stack[j].level === thisLevel) {
  1613. item = stack[j];
  1614. let openQuote;
  1615. let closeQuote;
  1616. if (isSingle) {
  1617. openQuote = state.md.options.quotes[2];
  1618. closeQuote = state.md.options.quotes[3];
  1619. } else {
  1620. openQuote = state.md.options.quotes[0];
  1621. closeQuote = state.md.options.quotes[1];
  1622. }
  1623. // replace token.content *before* tokens[item.token].content,
  1624. // because, if they are pointing at the same token, replaceAt
  1625. // could mess up indices when quote length != 1
  1626. token.content = replaceAt(token.content, t.index, closeQuote);
  1627. tokens[item.token].content = replaceAt(tokens[item.token].content, item.pos, openQuote);
  1628. pos += closeQuote.length - 1;
  1629. if (item.token === i) {
  1630. pos += openQuote.length - 1;
  1631. }
  1632. text = token.content;
  1633. max = text.length;
  1634. stack.length = j;
  1635. continue OUTER;
  1636. }
  1637. }
  1638. }
  1639. if (canOpen) {
  1640. stack.push({
  1641. token: i,
  1642. pos: t.index,
  1643. single: isSingle,
  1644. level: thisLevel
  1645. });
  1646. } else if (canClose && isSingle) {
  1647. token.content = replaceAt(token.content, t.index, APOSTROPHE);
  1648. }
  1649. }
  1650. }
  1651. }
  1652. function smartquotes(state) {
  1653. /* eslint max-depth:0 */
  1654. if (!state.md.options.typographer) {
  1655. return;
  1656. }
  1657. for (let blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
  1658. if (state.tokens[blkIdx].type !== 'inline' || !QUOTE_TEST_RE.test(state.tokens[blkIdx].content)) {
  1659. continue;
  1660. }
  1661. process_inlines(state.tokens[blkIdx].children, state);
  1662. }
  1663. }
  1664. // Join raw text tokens with the rest of the text
  1665. //
  1666. // This is set as a separate rule to provide an opportunity for plugins
  1667. // to run text replacements after text join, but before escape join.
  1668. //
  1669. // For example, `\:)` shouldn't be replaced with an emoji.
  1670. //
  1671. function text_join(state) {
  1672. let curr, last;
  1673. const blockTokens = state.tokens;
  1674. const l = blockTokens.length;
  1675. for (let j = 0; j < l; j++) {
  1676. if (blockTokens[j].type !== 'inline') continue;
  1677. const tokens = blockTokens[j].children;
  1678. const max = tokens.length;
  1679. for (curr = 0; curr < max; curr++) {
  1680. if (tokens[curr].type === 'text_special') {
  1681. tokens[curr].type = 'text';
  1682. }
  1683. }
  1684. for (curr = last = 0; curr < max; curr++) {
  1685. if (tokens[curr].type === 'text' && curr + 1 < max && tokens[curr + 1].type === 'text') {
  1686. // collapse two adjacent text nodes
  1687. tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content;
  1688. } else {
  1689. if (curr !== last) {
  1690. tokens[last] = tokens[curr];
  1691. }
  1692. last++;
  1693. }
  1694. }
  1695. if (curr !== last) {
  1696. tokens.length = last;
  1697. }
  1698. }
  1699. }
  1700. /** internal
  1701. * class Core
  1702. *
  1703. * Top-level rules executor. Glues block/inline parsers and does intermediate
  1704. * transformations.
  1705. **/
  1706. const _rules$2 = [['normalize', normalize], ['block', block], ['inline', inline], ['linkify', linkify$1], ['replacements', replace], ['smartquotes', smartquotes],
  1707. // `text_join` finds `text_special` tokens (for escape sequences)
  1708. // and joins them with the rest of the text
  1709. ['text_join', text_join]];
  1710. /**
  1711. * new Core()
  1712. **/
  1713. function Core() {
  1714. /**
  1715. * Core#ruler -> Ruler
  1716. *
  1717. * [[Ruler]] instance. Keep configuration of core rules.
  1718. **/
  1719. this.ruler = new Ruler();
  1720. for (let i = 0; i < _rules$2.length; i++) {
  1721. this.ruler.push(_rules$2[i][0], _rules$2[i][1]);
  1722. }
  1723. }
  1724. /**
  1725. * Core.process(state)
  1726. *
  1727. * Executes core chain rules.
  1728. **/
  1729. Core.prototype.process = function (state) {
  1730. const rules = this.ruler.getRules('');
  1731. for (let i = 0, l = rules.length; i < l; i++) {
  1732. rules[i](state);
  1733. }
  1734. };
  1735. Core.prototype.State = StateCore;
  1736. // Parser state class
  1737. function StateBlock(src, md, env, tokens) {
  1738. this.src = src;
  1739. // link to parser instance
  1740. this.md = md;
  1741. this.env = env;
  1742. //
  1743. // Internal state vartiables
  1744. //
  1745. this.tokens = tokens;
  1746. this.bMarks = []; // line begin offsets for fast jumps
  1747. this.eMarks = []; // line end offsets for fast jumps
  1748. this.tShift = []; // offsets of the first non-space characters (tabs not expanded)
  1749. this.sCount = []; // indents for each line (tabs expanded)
  1750. // An amount of virtual spaces (tabs expanded) between beginning
  1751. // of each line (bMarks) and real beginning of that line.
  1752. //
  1753. // It exists only as a hack because blockquotes override bMarks
  1754. // losing information in the process.
  1755. //
  1756. // It's used only when expanding tabs, you can think about it as
  1757. // an initial tab length, e.g. bsCount=21 applied to string `\t123`
  1758. // means first tab should be expanded to 4-21%4 === 3 spaces.
  1759. //
  1760. this.bsCount = [];
  1761. // block parser variables
  1762. // required block content indent (for example, if we are
  1763. // inside a list, it would be positioned after list marker)
  1764. this.blkIndent = 0;
  1765. this.line = 0; // line index in src
  1766. this.lineMax = 0; // lines count
  1767. this.tight = false; // loose/tight mode for lists
  1768. this.ddIndent = -1; // indent of the current dd block (-1 if there isn't any)
  1769. this.listIndent = -1; // indent of the current list block (-1 if there isn't any)
  1770. // can be 'blockquote', 'list', 'root', 'paragraph' or 'reference'
  1771. // used in lists to determine if they interrupt a paragraph
  1772. this.parentType = 'root';
  1773. this.level = 0;
  1774. // Create caches
  1775. // Generate markers.
  1776. const s = this.src;
  1777. for (let start = 0, pos = 0, indent = 0, offset = 0, len = s.length, indent_found = false; pos < len; pos++) {
  1778. const ch = s.charCodeAt(pos);
  1779. if (!indent_found) {
  1780. if (isSpace(ch)) {
  1781. indent++;
  1782. if (ch === 0x09) {
  1783. offset += 4 - offset % 4;
  1784. } else {
  1785. offset++;
  1786. }
  1787. continue;
  1788. } else {
  1789. indent_found = true;
  1790. }
  1791. }
  1792. if (ch === 0x0A || pos === len - 1) {
  1793. if (ch !== 0x0A) {
  1794. pos++;
  1795. }
  1796. this.bMarks.push(start);
  1797. this.eMarks.push(pos);
  1798. this.tShift.push(indent);
  1799. this.sCount.push(offset);
  1800. this.bsCount.push(0);
  1801. indent_found = false;
  1802. indent = 0;
  1803. offset = 0;
  1804. start = pos + 1;
  1805. }
  1806. }
  1807. // Push fake entry to simplify cache bounds checks
  1808. this.bMarks.push(s.length);
  1809. this.eMarks.push(s.length);
  1810. this.tShift.push(0);
  1811. this.sCount.push(0);
  1812. this.bsCount.push(0);
  1813. this.lineMax = this.bMarks.length - 1; // don't count last fake line
  1814. }
  1815. // Push new token to "stream".
  1816. //
  1817. StateBlock.prototype.push = function (type, tag, nesting) {
  1818. const token = new Token(type, tag, nesting);
  1819. token.block = true;
  1820. if (nesting < 0) this.level--; // closing tag
  1821. token.level = this.level;
  1822. if (nesting > 0) this.level++; // opening tag
  1823. this.tokens.push(token);
  1824. return token;
  1825. };
  1826. StateBlock.prototype.isEmpty = function isEmpty(line) {
  1827. return this.bMarks[line] + this.tShift[line] >= this.eMarks[line];
  1828. };
  1829. StateBlock.prototype.skipEmptyLines = function skipEmptyLines(from) {
  1830. for (let max = this.lineMax; from < max; from++) {
  1831. if (this.bMarks[from] + this.tShift[from] < this.eMarks[from]) {
  1832. break;
  1833. }
  1834. }
  1835. return from;
  1836. };
  1837. // Skip spaces from given position.
  1838. StateBlock.prototype.skipSpaces = function skipSpaces(pos) {
  1839. for (let max = this.src.length; pos < max; pos++) {
  1840. const ch = this.src.charCodeAt(pos);
  1841. if (!isSpace(ch)) {
  1842. break;
  1843. }
  1844. }
  1845. return pos;
  1846. };
  1847. // Skip spaces from given position in reverse.
  1848. StateBlock.prototype.skipSpacesBack = function skipSpacesBack(pos, min) {
  1849. if (pos <= min) {
  1850. return pos;
  1851. }
  1852. while (pos > min) {
  1853. if (!isSpace(this.src.charCodeAt(--pos))) {
  1854. return pos + 1;
  1855. }
  1856. }
  1857. return pos;
  1858. };
  1859. // Skip char codes from given position
  1860. StateBlock.prototype.skipChars = function skipChars(pos, code) {
  1861. for (let max = this.src.length; pos < max; pos++) {
  1862. if (this.src.charCodeAt(pos) !== code) {
  1863. break;
  1864. }
  1865. }
  1866. return pos;
  1867. };
  1868. // Skip char codes reverse from given position - 1
  1869. StateBlock.prototype.skipCharsBack = function skipCharsBack(pos, code, min) {
  1870. if (pos <= min) {
  1871. return pos;
  1872. }
  1873. while (pos > min) {
  1874. if (code !== this.src.charCodeAt(--pos)) {
  1875. return pos + 1;
  1876. }
  1877. }
  1878. return pos;
  1879. };
  1880. // cut lines range from source.
  1881. StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF) {
  1882. if (begin >= end) {
  1883. return '';
  1884. }
  1885. const queue = new Array(end - begin);
  1886. for (let i = 0, line = begin; line < end; line++, i++) {
  1887. let lineIndent = 0;
  1888. const lineStart = this.bMarks[line];
  1889. let first = lineStart;
  1890. let last;
  1891. if (line + 1 < end || keepLastLF) {
  1892. // No need for bounds check because we have fake entry on tail.
  1893. last = this.eMarks[line] + 1;
  1894. } else {
  1895. last = this.eMarks[line];
  1896. }
  1897. while (first < last && lineIndent < indent) {
  1898. const ch = this.src.charCodeAt(first);
  1899. if (isSpace(ch)) {
  1900. if (ch === 0x09) {
  1901. lineIndent += 4 - (lineIndent + this.bsCount[line]) % 4;
  1902. } else {
  1903. lineIndent++;
  1904. }
  1905. } else if (first - lineStart < this.tShift[line]) {
  1906. // patched tShift masked characters to look like spaces (blockquotes, list markers)
  1907. lineIndent++;
  1908. } else {
  1909. break;
  1910. }
  1911. first++;
  1912. }
  1913. if (lineIndent > indent) {
  1914. // partially expanding tabs in code blocks, e.g '\t\tfoobar'
  1915. // with indent=2 becomes ' \tfoobar'
  1916. queue[i] = new Array(lineIndent - indent + 1).join(' ') + this.src.slice(first, last);
  1917. } else {
  1918. queue[i] = this.src.slice(first, last);
  1919. }
  1920. }
  1921. return queue.join('');
  1922. };
  1923. // re-export Token class to use in block rules
  1924. StateBlock.prototype.Token = Token;
  1925. // GFM table, https://github.github.com/gfm/#tables-extension-
  1926. // Limit the amount of empty autocompleted cells in a table,
  1927. // see https://github.com/markdown-it/markdown-it/issues/1000,
  1928. //
  1929. // Both pulldown-cmark and commonmark-hs limit the number of cells this way to ~200k.
  1930. // We set it to 65k, which can expand user input by a factor of x370
  1931. // (256x256 square is 1.8kB expanded into 650kB).
  1932. const MAX_AUTOCOMPLETED_CELLS = 0x10000;
  1933. function getLine(state, line) {
  1934. const pos = state.bMarks[line] + state.tShift[line];
  1935. const max = state.eMarks[line];
  1936. return state.src.slice(pos, max);
  1937. }
  1938. function escapedSplit(str) {
  1939. const result = [];
  1940. const max = str.length;
  1941. let pos = 0;
  1942. let ch = str.charCodeAt(pos);
  1943. let isEscaped = false;
  1944. let lastPos = 0;
  1945. let current = '';
  1946. while (pos < max) {
  1947. if (ch === 0x7c /* | */) {
  1948. if (!isEscaped) {
  1949. // pipe separating cells, '|'
  1950. result.push(current + str.substring(lastPos, pos));
  1951. current = '';
  1952. lastPos = pos + 1;
  1953. } else {
  1954. // escaped pipe, '\|'
  1955. current += str.substring(lastPos, pos - 1);
  1956. lastPos = pos;
  1957. }
  1958. }
  1959. isEscaped = ch === 0x5c /* \ */;
  1960. pos++;
  1961. ch = str.charCodeAt(pos);
  1962. }
  1963. result.push(current + str.substring(lastPos));
  1964. return result;
  1965. }
  1966. function table(state, startLine, endLine, silent) {
  1967. // should have at least two lines
  1968. if (startLine + 2 > endLine) {
  1969. return false;
  1970. }
  1971. let nextLine = startLine + 1;
  1972. if (state.sCount[nextLine] < state.blkIndent) {
  1973. return false;
  1974. }
  1975. // if it's indented more than 3 spaces, it should be a code block
  1976. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  1977. return false;
  1978. }
  1979. // first character of the second line should be '|', '-', ':',
  1980. // and no other characters are allowed but spaces;
  1981. // basically, this is the equivalent of /^[-:|][-:|\s]*$/ regexp
  1982. let pos = state.bMarks[nextLine] + state.tShift[nextLine];
  1983. if (pos >= state.eMarks[nextLine]) {
  1984. return false;
  1985. }
  1986. const firstCh = state.src.charCodeAt(pos++);
  1987. if (firstCh !== 0x7C /* | */ && firstCh !== 0x2D /* - */ && firstCh !== 0x3A /* : */) {
  1988. return false;
  1989. }
  1990. if (pos >= state.eMarks[nextLine]) {
  1991. return false;
  1992. }
  1993. const secondCh = state.src.charCodeAt(pos++);
  1994. if (secondCh !== 0x7C /* | */ && secondCh !== 0x2D /* - */ && secondCh !== 0x3A /* : */ && !isSpace(secondCh)) {
  1995. return false;
  1996. }
  1997. // if first character is '-', then second character must not be a space
  1998. // (due to parsing ambiguity with list)
  1999. if (firstCh === 0x2D /* - */ && isSpace(secondCh)) {
  2000. return false;
  2001. }
  2002. while (pos < state.eMarks[nextLine]) {
  2003. const ch = state.src.charCodeAt(pos);
  2004. if (ch !== 0x7C /* | */ && ch !== 0x2D /* - */ && ch !== 0x3A /* : */ && !isSpace(ch)) {
  2005. return false;
  2006. }
  2007. pos++;
  2008. }
  2009. let lineText = getLine(state, startLine + 1);
  2010. let columns = lineText.split('|');
  2011. const aligns = [];
  2012. for (let i = 0; i < columns.length; i++) {
  2013. const t = columns[i].trim();
  2014. if (!t) {
  2015. // allow empty columns before and after table, but not in between columns;
  2016. // e.g. allow ` |---| `, disallow ` ---||--- `
  2017. if (i === 0 || i === columns.length - 1) {
  2018. continue;
  2019. } else {
  2020. return false;
  2021. }
  2022. }
  2023. if (!/^:?-+:?$/.test(t)) {
  2024. return false;
  2025. }
  2026. if (t.charCodeAt(t.length - 1) === 0x3A /* : */) {
  2027. aligns.push(t.charCodeAt(0) === 0x3A /* : */ ? 'center' : 'right');
  2028. } else if (t.charCodeAt(0) === 0x3A /* : */) {
  2029. aligns.push('left');
  2030. } else {
  2031. aligns.push('');
  2032. }
  2033. }
  2034. lineText = getLine(state, startLine).trim();
  2035. if (lineText.indexOf('|') === -1) {
  2036. return false;
  2037. }
  2038. if (state.sCount[startLine] - state.blkIndent >= 4) {
  2039. return false;
  2040. }
  2041. columns = escapedSplit(lineText);
  2042. if (columns.length && columns[0] === '') columns.shift();
  2043. if (columns.length && columns[columns.length - 1] === '') columns.pop();
  2044. // header row will define an amount of columns in the entire table,
  2045. // and align row should be exactly the same (the rest of the rows can differ)
  2046. const columnCount = columns.length;
  2047. if (columnCount === 0 || columnCount !== aligns.length) {
  2048. return false;
  2049. }
  2050. if (silent) {
  2051. return true;
  2052. }
  2053. const oldParentType = state.parentType;
  2054. state.parentType = 'table';
  2055. // use 'blockquote' lists for termination because it's
  2056. // the most similar to tables
  2057. const terminatorRules = state.md.block.ruler.getRules('blockquote');
  2058. const token_to = state.push('table_open', 'table', 1);
  2059. const tableLines = [startLine, 0];
  2060. token_to.map = tableLines;
  2061. const token_tho = state.push('thead_open', 'thead', 1);
  2062. token_tho.map = [startLine, startLine + 1];
  2063. const token_htro = state.push('tr_open', 'tr', 1);
  2064. token_htro.map = [startLine, startLine + 1];
  2065. for (let i = 0; i < columns.length; i++) {
  2066. const token_ho = state.push('th_open', 'th', 1);
  2067. if (aligns[i]) {
  2068. token_ho.attrs = [['style', 'text-align:' + aligns[i]]];
  2069. }
  2070. const token_il = state.push('inline', '', 0);
  2071. token_il.content = columns[i].trim();
  2072. token_il.children = [];
  2073. state.push('th_close', 'th', -1);
  2074. }
  2075. state.push('tr_close', 'tr', -1);
  2076. state.push('thead_close', 'thead', -1);
  2077. let tbodyLines;
  2078. let autocompletedCells = 0;
  2079. for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
  2080. if (state.sCount[nextLine] < state.blkIndent) {
  2081. break;
  2082. }
  2083. let terminate = false;
  2084. for (let i = 0, l = terminatorRules.length; i < l; i++) {
  2085. if (terminatorRules[i](state, nextLine, endLine, true)) {
  2086. terminate = true;
  2087. break;
  2088. }
  2089. }
  2090. if (terminate) {
  2091. break;
  2092. }
  2093. lineText = getLine(state, nextLine).trim();
  2094. if (!lineText) {
  2095. break;
  2096. }
  2097. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  2098. break;
  2099. }
  2100. columns = escapedSplit(lineText);
  2101. if (columns.length && columns[0] === '') columns.shift();
  2102. if (columns.length && columns[columns.length - 1] === '') columns.pop();
  2103. // note: autocomplete count can be negative if user specifies more columns than header,
  2104. // but that does not affect intended use (which is limiting expansion)
  2105. autocompletedCells += columnCount - columns.length;
  2106. if (autocompletedCells > MAX_AUTOCOMPLETED_CELLS) {
  2107. break;
  2108. }
  2109. if (nextLine === startLine + 2) {
  2110. const token_tbo = state.push('tbody_open', 'tbody', 1);
  2111. token_tbo.map = tbodyLines = [startLine + 2, 0];
  2112. }
  2113. const token_tro = state.push('tr_open', 'tr', 1);
  2114. token_tro.map = [nextLine, nextLine + 1];
  2115. for (let i = 0; i < columnCount; i++) {
  2116. const token_tdo = state.push('td_open', 'td', 1);
  2117. if (aligns[i]) {
  2118. token_tdo.attrs = [['style', 'text-align:' + aligns[i]]];
  2119. }
  2120. const token_il = state.push('inline', '', 0);
  2121. token_il.content = columns[i] ? columns[i].trim() : '';
  2122. token_il.children = [];
  2123. state.push('td_close', 'td', -1);
  2124. }
  2125. state.push('tr_close', 'tr', -1);
  2126. }
  2127. if (tbodyLines) {
  2128. state.push('tbody_close', 'tbody', -1);
  2129. tbodyLines[1] = nextLine;
  2130. }
  2131. state.push('table_close', 'table', -1);
  2132. tableLines[1] = nextLine;
  2133. state.parentType = oldParentType;
  2134. state.line = nextLine;
  2135. return true;
  2136. }
  2137. // Code block (4 spaces padded)
  2138. function code(state, startLine, endLine /*, silent */) {
  2139. if (state.sCount[startLine] - state.blkIndent < 4) {
  2140. return false;
  2141. }
  2142. let nextLine = startLine + 1;
  2143. let last = nextLine;
  2144. while (nextLine < endLine) {
  2145. if (state.isEmpty(nextLine)) {
  2146. nextLine++;
  2147. continue;
  2148. }
  2149. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  2150. nextLine++;
  2151. last = nextLine;
  2152. continue;
  2153. }
  2154. break;
  2155. }
  2156. state.line = last;
  2157. const token = state.push('code_block', 'code', 0);
  2158. token.content = state.getLines(startLine, last, 4 + state.blkIndent, false) + '\n';
  2159. token.map = [startLine, state.line];
  2160. return true;
  2161. }
  2162. // fences (``` lang, ~~~ lang)
  2163. function fence(state, startLine, endLine, silent) {
  2164. let pos = state.bMarks[startLine] + state.tShift[startLine];
  2165. let max = state.eMarks[startLine];
  2166. // if it's indented more than 3 spaces, it should be a code block
  2167. if (state.sCount[startLine] - state.blkIndent >= 4) {
  2168. return false;
  2169. }
  2170. if (pos + 3 > max) {
  2171. return false;
  2172. }
  2173. const marker = state.src.charCodeAt(pos);
  2174. if (marker !== 0x7E /* ~ */ && marker !== 0x60 /* ` */) {
  2175. return false;
  2176. }
  2177. // scan marker length
  2178. let mem = pos;
  2179. pos = state.skipChars(pos, marker);
  2180. let len = pos - mem;
  2181. if (len < 3) {
  2182. return false;
  2183. }
  2184. const markup = state.src.slice(mem, pos);
  2185. const params = state.src.slice(pos, max);
  2186. if (marker === 0x60 /* ` */) {
  2187. if (params.indexOf(String.fromCharCode(marker)) >= 0) {
  2188. return false;
  2189. }
  2190. }
  2191. // Since start is found, we can report success here in validation mode
  2192. if (silent) {
  2193. return true;
  2194. }
  2195. // search end of block
  2196. let nextLine = startLine;
  2197. let haveEndMarker = false;
  2198. for (;;) {
  2199. nextLine++;
  2200. if (nextLine >= endLine) {
  2201. // unclosed block should be autoclosed by end of document.
  2202. // also block seems to be autoclosed by end of parent
  2203. break;
  2204. }
  2205. pos = mem = state.bMarks[nextLine] + state.tShift[nextLine];
  2206. max = state.eMarks[nextLine];
  2207. if (pos < max && state.sCount[nextLine] < state.blkIndent) {
  2208. // non-empty line with negative indent should stop the list:
  2209. // - ```
  2210. // test
  2211. break;
  2212. }
  2213. if (state.src.charCodeAt(pos) !== marker) {
  2214. continue;
  2215. }
  2216. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  2217. // closing fence should be indented less than 4 spaces
  2218. continue;
  2219. }
  2220. pos = state.skipChars(pos, marker);
  2221. // closing code fence must be at least as long as the opening one
  2222. if (pos - mem < len) {
  2223. continue;
  2224. }
  2225. // make sure tail has spaces only
  2226. pos = state.skipSpaces(pos);
  2227. if (pos < max) {
  2228. continue;
  2229. }
  2230. haveEndMarker = true;
  2231. // found!
  2232. break;
  2233. }
  2234. // If a fence has heading spaces, they should be removed from its inner block
  2235. len = state.sCount[startLine];
  2236. state.line = nextLine + (haveEndMarker ? 1 : 0);
  2237. const token = state.push('fence', 'code', 0);
  2238. token.info = params;
  2239. token.content = state.getLines(startLine + 1, nextLine, len, true);
  2240. token.markup = markup;
  2241. token.map = [startLine, state.line];
  2242. return true;
  2243. }
  2244. // Block quotes
  2245. function blockquote(state, startLine, endLine, silent) {
  2246. let pos = state.bMarks[startLine] + state.tShift[startLine];
  2247. let max = state.eMarks[startLine];
  2248. const oldLineMax = state.lineMax;
  2249. // if it's indented more than 3 spaces, it should be a code block
  2250. if (state.sCount[startLine] - state.blkIndent >= 4) {
  2251. return false;
  2252. }
  2253. // check the block quote marker
  2254. if (state.src.charCodeAt(pos) !== 0x3E /* > */) {
  2255. return false;
  2256. }
  2257. // we know that it's going to be a valid blockquote,
  2258. // so no point trying to find the end of it in silent mode
  2259. if (silent) {
  2260. return true;
  2261. }
  2262. const oldBMarks = [];
  2263. const oldBSCount = [];
  2264. const oldSCount = [];
  2265. const oldTShift = [];
  2266. const terminatorRules = state.md.block.ruler.getRules('blockquote');
  2267. const oldParentType = state.parentType;
  2268. state.parentType = 'blockquote';
  2269. let lastLineEmpty = false;
  2270. let nextLine;
  2271. // Search the end of the block
  2272. //
  2273. // Block ends with either:
  2274. // 1. an empty line outside:
  2275. // ```
  2276. // > test
  2277. //
  2278. // ```
  2279. // 2. an empty line inside:
  2280. // ```
  2281. // >
  2282. // test
  2283. // ```
  2284. // 3. another tag:
  2285. // ```
  2286. // > test
  2287. // - - -
  2288. // ```
  2289. for (nextLine = startLine; nextLine < endLine; nextLine++) {
  2290. // check if it's outdented, i.e. it's inside list item and indented
  2291. // less than said list item:
  2292. //
  2293. // ```
  2294. // 1. anything
  2295. // > current blockquote
  2296. // 2. checking this line
  2297. // ```
  2298. const isOutdented = state.sCount[nextLine] < state.blkIndent;
  2299. pos = state.bMarks[nextLine] + state.tShift[nextLine];
  2300. max = state.eMarks[nextLine];
  2301. if (pos >= max) {
  2302. // Case 1: line is not inside the blockquote, and this line is empty.
  2303. break;
  2304. }
  2305. if (state.src.charCodeAt(pos++) === 0x3E /* > */ && !isOutdented) {
  2306. // This line is inside the blockquote.
  2307. // set offset past spaces and ">"
  2308. let initial = state.sCount[nextLine] + 1;
  2309. let spaceAfterMarker;
  2310. let adjustTab;
  2311. // skip one optional space after '>'
  2312. if (state.src.charCodeAt(pos) === 0x20 /* space */) {
  2313. // ' > test '
  2314. // ^ -- position start of line here:
  2315. pos++;
  2316. initial++;
  2317. adjustTab = false;
  2318. spaceAfterMarker = true;
  2319. } else if (state.src.charCodeAt(pos) === 0x09 /* tab */) {
  2320. spaceAfterMarker = true;
  2321. if ((state.bsCount[nextLine] + initial) % 4 === 3) {
  2322. // ' >\t test '
  2323. // ^ -- position start of line here (tab has width===1)
  2324. pos++;
  2325. initial++;
  2326. adjustTab = false;
  2327. } else {
  2328. // ' >\t test '
  2329. // ^ -- position start of line here + shift bsCount slightly
  2330. // to make extra space appear
  2331. adjustTab = true;
  2332. }
  2333. } else {
  2334. spaceAfterMarker = false;
  2335. }
  2336. let offset = initial;
  2337. oldBMarks.push(state.bMarks[nextLine]);
  2338. state.bMarks[nextLine] = pos;
  2339. while (pos < max) {
  2340. const ch = state.src.charCodeAt(pos);
  2341. if (isSpace(ch)) {
  2342. if (ch === 0x09) {
  2343. offset += 4 - (offset + state.bsCount[nextLine] + (adjustTab ? 1 : 0)) % 4;
  2344. } else {
  2345. offset++;
  2346. }
  2347. } else {
  2348. break;
  2349. }
  2350. pos++;
  2351. }
  2352. lastLineEmpty = pos >= max;
  2353. oldBSCount.push(state.bsCount[nextLine]);
  2354. state.bsCount[nextLine] = state.sCount[nextLine] + 1 + (spaceAfterMarker ? 1 : 0);
  2355. oldSCount.push(state.sCount[nextLine]);
  2356. state.sCount[nextLine] = offset - initial;
  2357. oldTShift.push(state.tShift[nextLine]);
  2358. state.tShift[nextLine] = pos - state.bMarks[nextLine];
  2359. continue;
  2360. }
  2361. // Case 2: line is not inside the blockquote, and the last line was empty.
  2362. if (lastLineEmpty) {
  2363. break;
  2364. }
  2365. // Case 3: another tag found.
  2366. let terminate = false;
  2367. for (let i = 0, l = terminatorRules.length; i < l; i++) {
  2368. if (terminatorRules[i](state, nextLine, endLine, true)) {
  2369. terminate = true;
  2370. break;
  2371. }
  2372. }
  2373. if (terminate) {
  2374. // Quirk to enforce "hard termination mode" for paragraphs;
  2375. // normally if you call `tokenize(state, startLine, nextLine)`,
  2376. // paragraphs will look below nextLine for paragraph continuation,
  2377. // but if blockquote is terminated by another tag, they shouldn't
  2378. state.lineMax = nextLine;
  2379. if (state.blkIndent !== 0) {
  2380. // state.blkIndent was non-zero, we now set it to zero,
  2381. // so we need to re-calculate all offsets to appear as
  2382. // if indent wasn't changed
  2383. oldBMarks.push(state.bMarks[nextLine]);
  2384. oldBSCount.push(state.bsCount[nextLine]);
  2385. oldTShift.push(state.tShift[nextLine]);
  2386. oldSCount.push(state.sCount[nextLine]);
  2387. state.sCount[nextLine] -= state.blkIndent;
  2388. }
  2389. break;
  2390. }
  2391. oldBMarks.push(state.bMarks[nextLine]);
  2392. oldBSCount.push(state.bsCount[nextLine]);
  2393. oldTShift.push(state.tShift[nextLine]);
  2394. oldSCount.push(state.sCount[nextLine]);
  2395. // A negative indentation means that this is a paragraph continuation
  2396. //
  2397. state.sCount[nextLine] = -1;
  2398. }
  2399. const oldIndent = state.blkIndent;
  2400. state.blkIndent = 0;
  2401. const token_o = state.push('blockquote_open', 'blockquote', 1);
  2402. token_o.markup = '>';
  2403. const lines = [startLine, 0];
  2404. token_o.map = lines;
  2405. state.md.block.tokenize(state, startLine, nextLine);
  2406. const token_c = state.push('blockquote_close', 'blockquote', -1);
  2407. token_c.markup = '>';
  2408. state.lineMax = oldLineMax;
  2409. state.parentType = oldParentType;
  2410. lines[1] = state.line;
  2411. // Restore original tShift; this might not be necessary since the parser
  2412. // has already been here, but just to make sure we can do that.
  2413. for (let i = 0; i < oldTShift.length; i++) {
  2414. state.bMarks[i + startLine] = oldBMarks[i];
  2415. state.tShift[i + startLine] = oldTShift[i];
  2416. state.sCount[i + startLine] = oldSCount[i];
  2417. state.bsCount[i + startLine] = oldBSCount[i];
  2418. }
  2419. state.blkIndent = oldIndent;
  2420. return true;
  2421. }
  2422. // Horizontal rule
  2423. function hr(state, startLine, endLine, silent) {
  2424. const max = state.eMarks[startLine];
  2425. // if it's indented more than 3 spaces, it should be a code block
  2426. if (state.sCount[startLine] - state.blkIndent >= 4) {
  2427. return false;
  2428. }
  2429. let pos = state.bMarks[startLine] + state.tShift[startLine];
  2430. const marker = state.src.charCodeAt(pos++);
  2431. // Check hr marker
  2432. if (marker !== 0x2A /* * */ && marker !== 0x2D /* - */ && marker !== 0x5F /* _ */) {
  2433. return false;
  2434. }
  2435. // markers can be mixed with spaces, but there should be at least 3 of them
  2436. let cnt = 1;
  2437. while (pos < max) {
  2438. const ch = state.src.charCodeAt(pos++);
  2439. if (ch !== marker && !isSpace(ch)) {
  2440. return false;
  2441. }
  2442. if (ch === marker) {
  2443. cnt++;
  2444. }
  2445. }
  2446. if (cnt < 3) {
  2447. return false;
  2448. }
  2449. if (silent) {
  2450. return true;
  2451. }
  2452. state.line = startLine + 1;
  2453. const token = state.push('hr', 'hr', 0);
  2454. token.map = [startLine, state.line];
  2455. token.markup = Array(cnt + 1).join(String.fromCharCode(marker));
  2456. return true;
  2457. }
  2458. // Lists
  2459. // Search `[-+*][\n ]`, returns next pos after marker on success
  2460. // or -1 on fail.
  2461. function skipBulletListMarker(state, startLine) {
  2462. const max = state.eMarks[startLine];
  2463. let pos = state.bMarks[startLine] + state.tShift[startLine];
  2464. const marker = state.src.charCodeAt(pos++);
  2465. // Check bullet
  2466. if (marker !== 0x2A /* * */ && marker !== 0x2D /* - */ && marker !== 0x2B /* + */) {
  2467. return -1;
  2468. }
  2469. if (pos < max) {
  2470. const ch = state.src.charCodeAt(pos);
  2471. if (!isSpace(ch)) {
  2472. // " -test " - is not a list item
  2473. return -1;
  2474. }
  2475. }
  2476. return pos;
  2477. }
  2478. // Search `\d+[.)][\n ]`, returns next pos after marker on success
  2479. // or -1 on fail.
  2480. function skipOrderedListMarker(state, startLine) {
  2481. const start = state.bMarks[startLine] + state.tShift[startLine];
  2482. const max = state.eMarks[startLine];
  2483. let pos = start;
  2484. // List marker should have at least 2 chars (digit + dot)
  2485. if (pos + 1 >= max) {
  2486. return -1;
  2487. }
  2488. let ch = state.src.charCodeAt(pos++);
  2489. if (ch < 0x30 /* 0 */ || ch > 0x39 /* 9 */) {
  2490. return -1;
  2491. }
  2492. for (;;) {
  2493. // EOL -> fail
  2494. if (pos >= max) {
  2495. return -1;
  2496. }
  2497. ch = state.src.charCodeAt(pos++);
  2498. if (ch >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */) {
  2499. // List marker should have no more than 9 digits
  2500. // (prevents integer overflow in browsers)
  2501. if (pos - start >= 10) {
  2502. return -1;
  2503. }
  2504. continue;
  2505. }
  2506. // found valid marker
  2507. if (ch === 0x29 /* ) */ || ch === 0x2e /* . */) {
  2508. break;
  2509. }
  2510. return -1;
  2511. }
  2512. if (pos < max) {
  2513. ch = state.src.charCodeAt(pos);
  2514. if (!isSpace(ch)) {
  2515. // " 1.test " - is not a list item
  2516. return -1;
  2517. }
  2518. }
  2519. return pos;
  2520. }
  2521. function markTightParagraphs(state, idx) {
  2522. const level = state.level + 2;
  2523. for (let i = idx + 2, l = state.tokens.length - 2; i < l; i++) {
  2524. if (state.tokens[i].level === level && state.tokens[i].type === 'paragraph_open') {
  2525. state.tokens[i + 2].hidden = true;
  2526. state.tokens[i].hidden = true;
  2527. i += 2;
  2528. }
  2529. }
  2530. }
  2531. function list(state, startLine, endLine, silent) {
  2532. let max, pos, start, token;
  2533. let nextLine = startLine;
  2534. let tight = true;
  2535. // if it's indented more than 3 spaces, it should be a code block
  2536. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  2537. return false;
  2538. }
  2539. // Special case:
  2540. // - item 1
  2541. // - item 2
  2542. // - item 3
  2543. // - item 4
  2544. // - this one is a paragraph continuation
  2545. if (state.listIndent >= 0 && state.sCount[nextLine] - state.listIndent >= 4 && state.sCount[nextLine] < state.blkIndent) {
  2546. return false;
  2547. }
  2548. let isTerminatingParagraph = false;
  2549. // limit conditions when list can interrupt
  2550. // a paragraph (validation mode only)
  2551. if (silent && state.parentType === 'paragraph') {
  2552. // Next list item should still terminate previous list item;
  2553. //
  2554. // This code can fail if plugins use blkIndent as well as lists,
  2555. // but I hope the spec gets fixed long before that happens.
  2556. //
  2557. if (state.sCount[nextLine] >= state.blkIndent) {
  2558. isTerminatingParagraph = true;
  2559. }
  2560. }
  2561. // Detect list type and position after marker
  2562. let isOrdered;
  2563. let markerValue;
  2564. let posAfterMarker;
  2565. if ((posAfterMarker = skipOrderedListMarker(state, nextLine)) >= 0) {
  2566. isOrdered = true;
  2567. start = state.bMarks[nextLine] + state.tShift[nextLine];
  2568. markerValue = Number(state.src.slice(start, posAfterMarker - 1));
  2569. // If we're starting a new ordered list right after
  2570. // a paragraph, it should start with 1.
  2571. if (isTerminatingParagraph && markerValue !== 1) return false;
  2572. } else if ((posAfterMarker = skipBulletListMarker(state, nextLine)) >= 0) {
  2573. isOrdered = false;
  2574. } else {
  2575. return false;
  2576. }
  2577. // If we're starting a new unordered list right after
  2578. // a paragraph, first line should not be empty.
  2579. if (isTerminatingParagraph) {
  2580. if (state.skipSpaces(posAfterMarker) >= state.eMarks[nextLine]) return false;
  2581. }
  2582. // For validation mode we can terminate immediately
  2583. if (silent) {
  2584. return true;
  2585. }
  2586. // We should terminate list on style change. Remember first one to compare.
  2587. const markerCharCode = state.src.charCodeAt(posAfterMarker - 1);
  2588. // Start list
  2589. const listTokIdx = state.tokens.length;
  2590. if (isOrdered) {
  2591. token = state.push('ordered_list_open', 'ol', 1);
  2592. if (markerValue !== 1) {
  2593. token.attrs = [['start', markerValue]];
  2594. }
  2595. } else {
  2596. token = state.push('bullet_list_open', 'ul', 1);
  2597. }
  2598. const listLines = [nextLine, 0];
  2599. token.map = listLines;
  2600. token.markup = String.fromCharCode(markerCharCode);
  2601. //
  2602. // Iterate list items
  2603. //
  2604. let prevEmptyEnd = false;
  2605. const terminatorRules = state.md.block.ruler.getRules('list');
  2606. const oldParentType = state.parentType;
  2607. state.parentType = 'list';
  2608. while (nextLine < endLine) {
  2609. pos = posAfterMarker;
  2610. max = state.eMarks[nextLine];
  2611. const initial = state.sCount[nextLine] + posAfterMarker - (state.bMarks[nextLine] + state.tShift[nextLine]);
  2612. let offset = initial;
  2613. while (pos < max) {
  2614. const ch = state.src.charCodeAt(pos);
  2615. if (ch === 0x09) {
  2616. offset += 4 - (offset + state.bsCount[nextLine]) % 4;
  2617. } else if (ch === 0x20) {
  2618. offset++;
  2619. } else {
  2620. break;
  2621. }
  2622. pos++;
  2623. }
  2624. const contentStart = pos;
  2625. let indentAfterMarker;
  2626. if (contentStart >= max) {
  2627. // trimming space in "- \n 3" case, indent is 1 here
  2628. indentAfterMarker = 1;
  2629. } else {
  2630. indentAfterMarker = offset - initial;
  2631. }
  2632. // If we have more than 4 spaces, the indent is 1
  2633. // (the rest is just indented code block)
  2634. if (indentAfterMarker > 4) {
  2635. indentAfterMarker = 1;
  2636. }
  2637. // " - test"
  2638. // ^^^^^ - calculating total length of this thing
  2639. const indent = initial + indentAfterMarker;
  2640. // Run subparser & write tokens
  2641. token = state.push('list_item_open', 'li', 1);
  2642. token.markup = String.fromCharCode(markerCharCode);
  2643. const itemLines = [nextLine, 0];
  2644. token.map = itemLines;
  2645. if (isOrdered) {
  2646. token.info = state.src.slice(start, posAfterMarker - 1);
  2647. }
  2648. // change current state, then restore it after parser subcall
  2649. const oldTight = state.tight;
  2650. const oldTShift = state.tShift[nextLine];
  2651. const oldSCount = state.sCount[nextLine];
  2652. // - example list
  2653. // ^ listIndent position will be here
  2654. // ^ blkIndent position will be here
  2655. //
  2656. const oldListIndent = state.listIndent;
  2657. state.listIndent = state.blkIndent;
  2658. state.blkIndent = indent;
  2659. state.tight = true;
  2660. state.tShift[nextLine] = contentStart - state.bMarks[nextLine];
  2661. state.sCount[nextLine] = offset;
  2662. if (contentStart >= max && state.isEmpty(nextLine + 1)) {
  2663. // workaround for this case
  2664. // (list item is empty, list terminates before "foo"):
  2665. // ~~~~~~~~
  2666. // -
  2667. //
  2668. // foo
  2669. // ~~~~~~~~
  2670. state.line = Math.min(state.line + 2, endLine);
  2671. } else {
  2672. state.md.block.tokenize(state, nextLine, endLine, true);
  2673. }
  2674. // If any of list item is tight, mark list as tight
  2675. if (!state.tight || prevEmptyEnd) {
  2676. tight = false;
  2677. }
  2678. // Item become loose if finish with empty line,
  2679. // but we should filter last element, because it means list finish
  2680. prevEmptyEnd = state.line - nextLine > 1 && state.isEmpty(state.line - 1);
  2681. state.blkIndent = state.listIndent;
  2682. state.listIndent = oldListIndent;
  2683. state.tShift[nextLine] = oldTShift;
  2684. state.sCount[nextLine] = oldSCount;
  2685. state.tight = oldTight;
  2686. token = state.push('list_item_close', 'li', -1);
  2687. token.markup = String.fromCharCode(markerCharCode);
  2688. nextLine = state.line;
  2689. itemLines[1] = nextLine;
  2690. if (nextLine >= endLine) {
  2691. break;
  2692. }
  2693. //
  2694. // Try to check if list is terminated or continued.
  2695. //
  2696. if (state.sCount[nextLine] < state.blkIndent) {
  2697. break;
  2698. }
  2699. // if it's indented more than 3 spaces, it should be a code block
  2700. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  2701. break;
  2702. }
  2703. // fail if terminating block found
  2704. let terminate = false;
  2705. for (let i = 0, l = terminatorRules.length; i < l; i++) {
  2706. if (terminatorRules[i](state, nextLine, endLine, true)) {
  2707. terminate = true;
  2708. break;
  2709. }
  2710. }
  2711. if (terminate) {
  2712. break;
  2713. }
  2714. // fail if list has another type
  2715. if (isOrdered) {
  2716. posAfterMarker = skipOrderedListMarker(state, nextLine);
  2717. if (posAfterMarker < 0) {
  2718. break;
  2719. }
  2720. start = state.bMarks[nextLine] + state.tShift[nextLine];
  2721. } else {
  2722. posAfterMarker = skipBulletListMarker(state, nextLine);
  2723. if (posAfterMarker < 0) {
  2724. break;
  2725. }
  2726. }
  2727. if (markerCharCode !== state.src.charCodeAt(posAfterMarker - 1)) {
  2728. break;
  2729. }
  2730. }
  2731. // Finalize list
  2732. if (isOrdered) {
  2733. token = state.push('ordered_list_close', 'ol', -1);
  2734. } else {
  2735. token = state.push('bullet_list_close', 'ul', -1);
  2736. }
  2737. token.markup = String.fromCharCode(markerCharCode);
  2738. listLines[1] = nextLine;
  2739. state.line = nextLine;
  2740. state.parentType = oldParentType;
  2741. // mark paragraphs tight if needed
  2742. if (tight) {
  2743. markTightParagraphs(state, listTokIdx);
  2744. }
  2745. return true;
  2746. }
  2747. function reference(state, startLine, _endLine, silent) {
  2748. let pos = state.bMarks[startLine] + state.tShift[startLine];
  2749. let max = state.eMarks[startLine];
  2750. let nextLine = startLine + 1;
  2751. // if it's indented more than 3 spaces, it should be a code block
  2752. if (state.sCount[startLine] - state.blkIndent >= 4) {
  2753. return false;
  2754. }
  2755. if (state.src.charCodeAt(pos) !== 0x5B /* [ */) {
  2756. return false;
  2757. }
  2758. function getNextLine(nextLine) {
  2759. const endLine = state.lineMax;
  2760. if (nextLine >= endLine || state.isEmpty(nextLine)) {
  2761. // empty line or end of input
  2762. return null;
  2763. }
  2764. let isContinuation = false;
  2765. // this would be a code block normally, but after paragraph
  2766. // it's considered a lazy continuation regardless of what's there
  2767. if (state.sCount[nextLine] - state.blkIndent > 3) {
  2768. isContinuation = true;
  2769. }
  2770. // quirk for blockquotes, this line should already be checked by that rule
  2771. if (state.sCount[nextLine] < 0) {
  2772. isContinuation = true;
  2773. }
  2774. if (!isContinuation) {
  2775. const terminatorRules = state.md.block.ruler.getRules('reference');
  2776. const oldParentType = state.parentType;
  2777. state.parentType = 'reference';
  2778. // Some tags can terminate paragraph without empty line.
  2779. let terminate = false;
  2780. for (let i = 0, l = terminatorRules.length; i < l; i++) {
  2781. if (terminatorRules[i](state, nextLine, endLine, true)) {
  2782. terminate = true;
  2783. break;
  2784. }
  2785. }
  2786. state.parentType = oldParentType;
  2787. if (terminate) {
  2788. // terminated by another block
  2789. return null;
  2790. }
  2791. }
  2792. const pos = state.bMarks[nextLine] + state.tShift[nextLine];
  2793. const max = state.eMarks[nextLine];
  2794. // max + 1 explicitly includes the newline
  2795. return state.src.slice(pos, max + 1);
  2796. }
  2797. let str = state.src.slice(pos, max + 1);
  2798. max = str.length;
  2799. let labelEnd = -1;
  2800. for (pos = 1; pos < max; pos++) {
  2801. const ch = str.charCodeAt(pos);
  2802. if (ch === 0x5B /* [ */) {
  2803. return false;
  2804. } else if (ch === 0x5D /* ] */) {
  2805. labelEnd = pos;
  2806. break;
  2807. } else if (ch === 0x0A /* \n */) {
  2808. const lineContent = getNextLine(nextLine);
  2809. if (lineContent !== null) {
  2810. str += lineContent;
  2811. max = str.length;
  2812. nextLine++;
  2813. }
  2814. } else if (ch === 0x5C /* \ */) {
  2815. pos++;
  2816. if (pos < max && str.charCodeAt(pos) === 0x0A) {
  2817. const lineContent = getNextLine(nextLine);
  2818. if (lineContent !== null) {
  2819. str += lineContent;
  2820. max = str.length;
  2821. nextLine++;
  2822. }
  2823. }
  2824. }
  2825. }
  2826. if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A /* : */) {
  2827. return false;
  2828. }
  2829. // [label]: destination 'title'
  2830. // ^^^ skip optional whitespace here
  2831. for (pos = labelEnd + 2; pos < max; pos++) {
  2832. const ch = str.charCodeAt(pos);
  2833. if (ch === 0x0A) {
  2834. const lineContent = getNextLine(nextLine);
  2835. if (lineContent !== null) {
  2836. str += lineContent;
  2837. max = str.length;
  2838. nextLine++;
  2839. }
  2840. } else if (isSpace(ch)) ; else {
  2841. break;
  2842. }
  2843. }
  2844. // [label]: destination 'title'
  2845. // ^^^^^^^^^^^ parse this
  2846. const destRes = state.md.helpers.parseLinkDestination(str, pos, max);
  2847. if (!destRes.ok) {
  2848. return false;
  2849. }
  2850. const href = state.md.normalizeLink(destRes.str);
  2851. if (!state.md.validateLink(href)) {
  2852. return false;
  2853. }
  2854. pos = destRes.pos;
  2855. // save cursor state, we could require to rollback later
  2856. const destEndPos = pos;
  2857. const destEndLineNo = nextLine;
  2858. // [label]: destination 'title'
  2859. // ^^^ skipping those spaces
  2860. const start = pos;
  2861. for (; pos < max; pos++) {
  2862. const ch = str.charCodeAt(pos);
  2863. if (ch === 0x0A) {
  2864. const lineContent = getNextLine(nextLine);
  2865. if (lineContent !== null) {
  2866. str += lineContent;
  2867. max = str.length;
  2868. nextLine++;
  2869. }
  2870. } else if (isSpace(ch)) ; else {
  2871. break;
  2872. }
  2873. }
  2874. // [label]: destination 'title'
  2875. // ^^^^^^^ parse this
  2876. let titleRes = state.md.helpers.parseLinkTitle(str, pos, max);
  2877. while (titleRes.can_continue) {
  2878. const lineContent = getNextLine(nextLine);
  2879. if (lineContent === null) break;
  2880. str += lineContent;
  2881. pos = max;
  2882. max = str.length;
  2883. nextLine++;
  2884. titleRes = state.md.helpers.parseLinkTitle(str, pos, max, titleRes);
  2885. }
  2886. let title;
  2887. if (pos < max && start !== pos && titleRes.ok) {
  2888. title = titleRes.str;
  2889. pos = titleRes.pos;
  2890. } else {
  2891. title = '';
  2892. pos = destEndPos;
  2893. nextLine = destEndLineNo;
  2894. }
  2895. // skip trailing spaces until the rest of the line
  2896. while (pos < max) {
  2897. const ch = str.charCodeAt(pos);
  2898. if (!isSpace(ch)) {
  2899. break;
  2900. }
  2901. pos++;
  2902. }
  2903. if (pos < max && str.charCodeAt(pos) !== 0x0A) {
  2904. if (title) {
  2905. // garbage at the end of the line after title,
  2906. // but it could still be a valid reference if we roll back
  2907. title = '';
  2908. pos = destEndPos;
  2909. nextLine = destEndLineNo;
  2910. while (pos < max) {
  2911. const ch = str.charCodeAt(pos);
  2912. if (!isSpace(ch)) {
  2913. break;
  2914. }
  2915. pos++;
  2916. }
  2917. }
  2918. }
  2919. if (pos < max && str.charCodeAt(pos) !== 0x0A) {
  2920. // garbage at the end of the line
  2921. return false;
  2922. }
  2923. const label = normalizeReference(str.slice(1, labelEnd));
  2924. if (!label) {
  2925. // CommonMark 0.20 disallows empty labels
  2926. return false;
  2927. }
  2928. // Reference can not terminate anything. This check is for safety only.
  2929. /* istanbul ignore if */
  2930. if (silent) {
  2931. return true;
  2932. }
  2933. if (typeof state.env.references === 'undefined') {
  2934. state.env.references = {};
  2935. }
  2936. if (typeof state.env.references[label] === 'undefined') {
  2937. state.env.references[label] = {
  2938. title,
  2939. href
  2940. };
  2941. }
  2942. state.line = nextLine;
  2943. return true;
  2944. }
  2945. // List of valid html blocks names, according to commonmark spec
  2946. // https://spec.commonmark.org/0.30/#html-blocks
  2947. var block_names = ['address', 'article', 'aside', 'base', 'basefont', 'blockquote', 'body', 'caption', 'center', 'col', 'colgroup', 'dd', 'details', 'dialog', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'iframe', 'legend', 'li', 'link', 'main', 'menu', 'menuitem', 'nav', 'noframes', 'ol', 'optgroup', 'option', 'p', 'param', 'search', 'section', 'summary', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'title', 'tr', 'track', 'ul'];
  2948. // Regexps to match html elements
  2949. const attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
  2950. const unquoted = '[^"\'=<>`\\x00-\\x20]+';
  2951. const single_quoted = "'[^']*'";
  2952. const double_quoted = '"[^"]*"';
  2953. const attr_value = '(?:' + unquoted + '|' + single_quoted + '|' + double_quoted + ')';
  2954. const attribute = '(?:\\s+' + attr_name + '(?:\\s*=\\s*' + attr_value + ')?)';
  2955. const open_tag = '<[A-Za-z][A-Za-z0-9\\-]*' + attribute + '*\\s*\\/?>';
  2956. const close_tag = '<\\/[A-Za-z][A-Za-z0-9\\-]*\\s*>';
  2957. const comment = '<!---?>|<!--(?:[^-]|-[^-]|--[^>])*-->';
  2958. const processing = '<[?][\\s\\S]*?[?]>';
  2959. const declaration = '<![A-Za-z][^>]*>';
  2960. const cdata = '<!\\[CDATA\\[[\\s\\S]*?\\]\\]>';
  2961. const HTML_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + '|' + comment + '|' + processing + '|' + declaration + '|' + cdata + ')');
  2962. const HTML_OPEN_CLOSE_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + ')');
  2963. // HTML block
  2964. // An array of opening and corresponding closing sequences for html tags,
  2965. // last argument defines whether it can terminate a paragraph or not
  2966. //
  2967. const HTML_SEQUENCES = [[/^<(script|pre|style|textarea)(?=(\s|>|$))/i, /<\/(script|pre|style|textarea)>/i, true], [/^<!--/, /-->/, true], [/^<\?/, /\?>/, true], [/^<![A-Z]/, />/, true], [/^<!\[CDATA\[/, /\]\]>/, true], [new RegExp('^</?(' + block_names.join('|') + ')(?=(\\s|/?>|$))', 'i'), /^$/, true], [new RegExp(HTML_OPEN_CLOSE_TAG_RE.source + '\\s*$'), /^$/, false]];
  2968. function html_block(state, startLine, endLine, silent) {
  2969. let pos = state.bMarks[startLine] + state.tShift[startLine];
  2970. let max = state.eMarks[startLine];
  2971. // if it's indented more than 3 spaces, it should be a code block
  2972. if (state.sCount[startLine] - state.blkIndent >= 4) {
  2973. return false;
  2974. }
  2975. if (!state.md.options.html) {
  2976. return false;
  2977. }
  2978. if (state.src.charCodeAt(pos) !== 0x3C /* < */) {
  2979. return false;
  2980. }
  2981. let lineText = state.src.slice(pos, max);
  2982. let i = 0;
  2983. for (; i < HTML_SEQUENCES.length; i++) {
  2984. if (HTML_SEQUENCES[i][0].test(lineText)) {
  2985. break;
  2986. }
  2987. }
  2988. if (i === HTML_SEQUENCES.length) {
  2989. return false;
  2990. }
  2991. if (silent) {
  2992. // true if this sequence can be a terminator, false otherwise
  2993. return HTML_SEQUENCES[i][2];
  2994. }
  2995. let nextLine = startLine + 1;
  2996. // If we are here - we detected HTML block.
  2997. // Let's roll down till block end.
  2998. if (!HTML_SEQUENCES[i][1].test(lineText)) {
  2999. for (; nextLine < endLine; nextLine++) {
  3000. if (state.sCount[nextLine] < state.blkIndent) {
  3001. break;
  3002. }
  3003. pos = state.bMarks[nextLine] + state.tShift[nextLine];
  3004. max = state.eMarks[nextLine];
  3005. lineText = state.src.slice(pos, max);
  3006. if (HTML_SEQUENCES[i][1].test(lineText)) {
  3007. if (lineText.length !== 0) {
  3008. nextLine++;
  3009. }
  3010. break;
  3011. }
  3012. }
  3013. }
  3014. state.line = nextLine;
  3015. const token = state.push('html_block', '', 0);
  3016. token.map = [startLine, nextLine];
  3017. token.content = state.getLines(startLine, nextLine, state.blkIndent, true);
  3018. return true;
  3019. }
  3020. // heading (#, ##, ...)
  3021. function heading(state, startLine, endLine, silent) {
  3022. let pos = state.bMarks[startLine] + state.tShift[startLine];
  3023. let max = state.eMarks[startLine];
  3024. // if it's indented more than 3 spaces, it should be a code block
  3025. if (state.sCount[startLine] - state.blkIndent >= 4) {
  3026. return false;
  3027. }
  3028. let ch = state.src.charCodeAt(pos);
  3029. if (ch !== 0x23 /* # */ || pos >= max) {
  3030. return false;
  3031. }
  3032. // count heading level
  3033. let level = 1;
  3034. ch = state.src.charCodeAt(++pos);
  3035. while (ch === 0x23 /* # */ && pos < max && level <= 6) {
  3036. level++;
  3037. ch = state.src.charCodeAt(++pos);
  3038. }
  3039. if (level > 6 || pos < max && !isSpace(ch)) {
  3040. return false;
  3041. }
  3042. if (silent) {
  3043. return true;
  3044. }
  3045. // Let's cut tails like ' ### ' from the end of string
  3046. max = state.skipSpacesBack(max, pos);
  3047. const tmp = state.skipCharsBack(max, 0x23, pos); // #
  3048. if (tmp > pos && isSpace(state.src.charCodeAt(tmp - 1))) {
  3049. max = tmp;
  3050. }
  3051. state.line = startLine + 1;
  3052. const token_o = state.push('heading_open', 'h' + String(level), 1);
  3053. token_o.markup = '########'.slice(0, level);
  3054. token_o.map = [startLine, state.line];
  3055. const token_i = state.push('inline', '', 0);
  3056. token_i.content = state.src.slice(pos, max).trim();
  3057. token_i.map = [startLine, state.line];
  3058. token_i.children = [];
  3059. const token_c = state.push('heading_close', 'h' + String(level), -1);
  3060. token_c.markup = '########'.slice(0, level);
  3061. return true;
  3062. }
  3063. // lheading (---, ===)
  3064. function lheading(state, startLine, endLine /*, silent */) {
  3065. const terminatorRules = state.md.block.ruler.getRules('paragraph');
  3066. // if it's indented more than 3 spaces, it should be a code block
  3067. if (state.sCount[startLine] - state.blkIndent >= 4) {
  3068. return false;
  3069. }
  3070. const oldParentType = state.parentType;
  3071. state.parentType = 'paragraph'; // use paragraph to match terminatorRules
  3072. // jump line-by-line until empty one or EOF
  3073. let level = 0;
  3074. let marker;
  3075. let nextLine = startLine + 1;
  3076. for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
  3077. // this would be a code block normally, but after paragraph
  3078. // it's considered a lazy continuation regardless of what's there
  3079. if (state.sCount[nextLine] - state.blkIndent > 3) {
  3080. continue;
  3081. }
  3082. //
  3083. // Check for underline in setext header
  3084. //
  3085. if (state.sCount[nextLine] >= state.blkIndent) {
  3086. let pos = state.bMarks[nextLine] + state.tShift[nextLine];
  3087. const max = state.eMarks[nextLine];
  3088. if (pos < max) {
  3089. marker = state.src.charCodeAt(pos);
  3090. if (marker === 0x2D /* - */ || marker === 0x3D /* = */) {
  3091. pos = state.skipChars(pos, marker);
  3092. pos = state.skipSpaces(pos);
  3093. if (pos >= max) {
  3094. level = marker === 0x3D /* = */ ? 1 : 2;
  3095. break;
  3096. }
  3097. }
  3098. }
  3099. }
  3100. // quirk for blockquotes, this line should already be checked by that rule
  3101. if (state.sCount[nextLine] < 0) {
  3102. continue;
  3103. }
  3104. // Some tags can terminate paragraph without empty line.
  3105. let terminate = false;
  3106. for (let i = 0, l = terminatorRules.length; i < l; i++) {
  3107. if (terminatorRules[i](state, nextLine, endLine, true)) {
  3108. terminate = true;
  3109. break;
  3110. }
  3111. }
  3112. if (terminate) {
  3113. break;
  3114. }
  3115. }
  3116. if (!level) {
  3117. // Didn't find valid underline
  3118. return false;
  3119. }
  3120. const content = state.getLines(startLine, nextLine, state.blkIndent, false).trim();
  3121. state.line = nextLine + 1;
  3122. const token_o = state.push('heading_open', 'h' + String(level), 1);
  3123. token_o.markup = String.fromCharCode(marker);
  3124. token_o.map = [startLine, state.line];
  3125. const token_i = state.push('inline', '', 0);
  3126. token_i.content = content;
  3127. token_i.map = [startLine, state.line - 1];
  3128. token_i.children = [];
  3129. const token_c = state.push('heading_close', 'h' + String(level), -1);
  3130. token_c.markup = String.fromCharCode(marker);
  3131. state.parentType = oldParentType;
  3132. return true;
  3133. }
  3134. // Paragraph
  3135. function paragraph(state, startLine, endLine) {
  3136. const terminatorRules = state.md.block.ruler.getRules('paragraph');
  3137. const oldParentType = state.parentType;
  3138. let nextLine = startLine + 1;
  3139. state.parentType = 'paragraph';
  3140. // jump line-by-line until empty one or EOF
  3141. for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
  3142. // this would be a code block normally, but after paragraph
  3143. // it's considered a lazy continuation regardless of what's there
  3144. if (state.sCount[nextLine] - state.blkIndent > 3) {
  3145. continue;
  3146. }
  3147. // quirk for blockquotes, this line should already be checked by that rule
  3148. if (state.sCount[nextLine] < 0) {
  3149. continue;
  3150. }
  3151. // Some tags can terminate paragraph without empty line.
  3152. let terminate = false;
  3153. for (let i = 0, l = terminatorRules.length; i < l; i++) {
  3154. if (terminatorRules[i](state, nextLine, endLine, true)) {
  3155. terminate = true;
  3156. break;
  3157. }
  3158. }
  3159. if (terminate) {
  3160. break;
  3161. }
  3162. }
  3163. const content = state.getLines(startLine, nextLine, state.blkIndent, false).trim();
  3164. state.line = nextLine;
  3165. const token_o = state.push('paragraph_open', 'p', 1);
  3166. token_o.map = [startLine, state.line];
  3167. const token_i = state.push('inline', '', 0);
  3168. token_i.content = content;
  3169. token_i.map = [startLine, state.line];
  3170. token_i.children = [];
  3171. state.push('paragraph_close', 'p', -1);
  3172. state.parentType = oldParentType;
  3173. return true;
  3174. }
  3175. /** internal
  3176. * class ParserBlock
  3177. *
  3178. * Block-level tokenizer.
  3179. **/
  3180. const _rules$1 = [
  3181. // First 2 params - rule name & source. Secondary array - list of rules,
  3182. // which can be terminated by this one.
  3183. ['table', table, ['paragraph', 'reference']], ['code', code], ['fence', fence, ['paragraph', 'reference', 'blockquote', 'list']], ['blockquote', blockquote, ['paragraph', 'reference', 'blockquote', 'list']], ['hr', hr, ['paragraph', 'reference', 'blockquote', 'list']], ['list', list, ['paragraph', 'reference', 'blockquote']], ['reference', reference], ['html_block', html_block, ['paragraph', 'reference', 'blockquote']], ['heading', heading, ['paragraph', 'reference', 'blockquote']], ['lheading', lheading], ['paragraph', paragraph]];
  3184. /**
  3185. * new ParserBlock()
  3186. **/
  3187. function ParserBlock() {
  3188. /**
  3189. * ParserBlock#ruler -> Ruler
  3190. *
  3191. * [[Ruler]] instance. Keep configuration of block rules.
  3192. **/
  3193. this.ruler = new Ruler();
  3194. for (let i = 0; i < _rules$1.length; i++) {
  3195. this.ruler.push(_rules$1[i][0], _rules$1[i][1], {
  3196. alt: (_rules$1[i][2] || []).slice()
  3197. });
  3198. }
  3199. }
  3200. // Generate tokens for input range
  3201. //
  3202. ParserBlock.prototype.tokenize = function (state, startLine, endLine) {
  3203. const rules = this.ruler.getRules('');
  3204. const len = rules.length;
  3205. const maxNesting = state.md.options.maxNesting;
  3206. let line = startLine;
  3207. let hasEmptyLines = false;
  3208. while (line < endLine) {
  3209. state.line = line = state.skipEmptyLines(line);
  3210. if (line >= endLine) {
  3211. break;
  3212. }
  3213. // Termination condition for nested calls.
  3214. // Nested calls currently used for blockquotes & lists
  3215. if (state.sCount[line] < state.blkIndent) {
  3216. break;
  3217. }
  3218. // If nesting level exceeded - skip tail to the end. That's not ordinary
  3219. // situation and we should not care about content.
  3220. if (state.level >= maxNesting) {
  3221. state.line = endLine;
  3222. break;
  3223. }
  3224. // Try all possible rules.
  3225. // On success, rule should:
  3226. //
  3227. // - update `state.line`
  3228. // - update `state.tokens`
  3229. // - return true
  3230. const prevLine = state.line;
  3231. let ok = false;
  3232. for (let i = 0; i < len; i++) {
  3233. ok = rules[i](state, line, endLine, false);
  3234. if (ok) {
  3235. if (prevLine >= state.line) {
  3236. throw new Error("block rule didn't increment state.line");
  3237. }
  3238. break;
  3239. }
  3240. }
  3241. // this can only happen if user disables paragraph rule
  3242. if (!ok) throw new Error('none of the block rules matched');
  3243. // set state.tight if we had an empty line before current tag
  3244. // i.e. latest empty line should not count
  3245. state.tight = !hasEmptyLines;
  3246. // paragraph might "eat" one newline after it in nested lists
  3247. if (state.isEmpty(state.line - 1)) {
  3248. hasEmptyLines = true;
  3249. }
  3250. line = state.line;
  3251. if (line < endLine && state.isEmpty(line)) {
  3252. hasEmptyLines = true;
  3253. line++;
  3254. state.line = line;
  3255. }
  3256. }
  3257. };
  3258. /**
  3259. * ParserBlock.parse(str, md, env, outTokens)
  3260. *
  3261. * Process input string and push block tokens into `outTokens`
  3262. **/
  3263. ParserBlock.prototype.parse = function (src, md, env, outTokens) {
  3264. if (!src) {
  3265. return;
  3266. }
  3267. const state = new this.State(src, md, env, outTokens);
  3268. this.tokenize(state, state.line, state.lineMax);
  3269. };
  3270. ParserBlock.prototype.State = StateBlock;
  3271. // Inline parser state
  3272. function StateInline(src, md, env, outTokens) {
  3273. this.src = src;
  3274. this.env = env;
  3275. this.md = md;
  3276. this.tokens = outTokens;
  3277. this.tokens_meta = Array(outTokens.length);
  3278. this.pos = 0;
  3279. this.posMax = this.src.length;
  3280. this.level = 0;
  3281. this.pending = '';
  3282. this.pendingLevel = 0;
  3283. // Stores { start: end } pairs. Useful for backtrack
  3284. // optimization of pairs parse (emphasis, strikes).
  3285. this.cache = {};
  3286. // List of emphasis-like delimiters for current tag
  3287. this.delimiters = [];
  3288. // Stack of delimiter lists for upper level tags
  3289. this._prev_delimiters = [];
  3290. // backtick length => last seen position
  3291. this.backticks = {};
  3292. this.backticksScanned = false;
  3293. // Counter used to disable inline linkify-it execution
  3294. // inside <a> and markdown links
  3295. this.linkLevel = 0;
  3296. }
  3297. // Flush pending text
  3298. //
  3299. StateInline.prototype.pushPending = function () {
  3300. const token = new Token('text', '', 0);
  3301. token.content = this.pending;
  3302. token.level = this.pendingLevel;
  3303. this.tokens.push(token);
  3304. this.pending = '';
  3305. return token;
  3306. };
  3307. // Push new token to "stream".
  3308. // If pending text exists - flush it as text token
  3309. //
  3310. StateInline.prototype.push = function (type, tag, nesting) {
  3311. if (this.pending) {
  3312. this.pushPending();
  3313. }
  3314. const token = new Token(type, tag, nesting);
  3315. let token_meta = null;
  3316. if (nesting < 0) {
  3317. // closing tag
  3318. this.level--;
  3319. this.delimiters = this._prev_delimiters.pop();
  3320. }
  3321. token.level = this.level;
  3322. if (nesting > 0) {
  3323. // opening tag
  3324. this.level++;
  3325. this._prev_delimiters.push(this.delimiters);
  3326. this.delimiters = [];
  3327. token_meta = {
  3328. delimiters: this.delimiters
  3329. };
  3330. }
  3331. this.pendingLevel = this.level;
  3332. this.tokens.push(token);
  3333. this.tokens_meta.push(token_meta);
  3334. return token;
  3335. };
  3336. // Scan a sequence of emphasis-like markers, and determine whether
  3337. // it can start an emphasis sequence or end an emphasis sequence.
  3338. //
  3339. // - start - position to scan from (it should point at a valid marker);
  3340. // - canSplitWord - determine if these markers can be found inside a word
  3341. //
  3342. StateInline.prototype.scanDelims = function (start, canSplitWord) {
  3343. const max = this.posMax;
  3344. const marker = this.src.charCodeAt(start);
  3345. // treat beginning of the line as a whitespace
  3346. const lastChar = start > 0 ? this.src.charCodeAt(start - 1) : 0x20;
  3347. let pos = start;
  3348. while (pos < max && this.src.charCodeAt(pos) === marker) {
  3349. pos++;
  3350. }
  3351. const count = pos - start;
  3352. // treat end of the line as a whitespace
  3353. const nextChar = pos < max ? this.src.charCodeAt(pos) : 0x20;
  3354. const isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(String.fromCharCode(lastChar));
  3355. const isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(String.fromCharCode(nextChar));
  3356. const isLastWhiteSpace = isWhiteSpace(lastChar);
  3357. const isNextWhiteSpace = isWhiteSpace(nextChar);
  3358. const left_flanking = !isNextWhiteSpace && (!isNextPunctChar || isLastWhiteSpace || isLastPunctChar);
  3359. const right_flanking = !isLastWhiteSpace && (!isLastPunctChar || isNextWhiteSpace || isNextPunctChar);
  3360. const can_open = left_flanking && (canSplitWord || !right_flanking || isLastPunctChar);
  3361. const can_close = right_flanking && (canSplitWord || !left_flanking || isNextPunctChar);
  3362. return {
  3363. can_open,
  3364. can_close,
  3365. length: count
  3366. };
  3367. };
  3368. // re-export Token class to use in block rules
  3369. StateInline.prototype.Token = Token;
  3370. // Skip text characters for text token, place those to pending buffer
  3371. // and increment current pos
  3372. // Rule to skip pure text
  3373. // '{}$%@~+=:' reserved for extentions
  3374. // !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, or ~
  3375. // !!!! Don't confuse with "Markdown ASCII Punctuation" chars
  3376. // http://spec.commonmark.org/0.15/#ascii-punctuation-character
  3377. function isTerminatorChar(ch) {
  3378. switch (ch) {
  3379. case 0x0A /* \n */:
  3380. case 0x21 /* ! */:
  3381. case 0x23 /* # */:
  3382. case 0x24 /* $ */:
  3383. case 0x25 /* % */:
  3384. case 0x26 /* & */:
  3385. case 0x2A /* * */:
  3386. case 0x2B /* + */:
  3387. case 0x2D /* - */:
  3388. case 0x3A /* : */:
  3389. case 0x3C /* < */:
  3390. case 0x3D /* = */:
  3391. case 0x3E /* > */:
  3392. case 0x40 /* @ */:
  3393. case 0x5B /* [ */:
  3394. case 0x5C /* \ */:
  3395. case 0x5D /* ] */:
  3396. case 0x5E /* ^ */:
  3397. case 0x5F /* _ */:
  3398. case 0x60 /* ` */:
  3399. case 0x7B /* { */:
  3400. case 0x7D /* } */:
  3401. case 0x7E /* ~ */:
  3402. return true;
  3403. default:
  3404. return false;
  3405. }
  3406. }
  3407. function text(state, silent) {
  3408. let pos = state.pos;
  3409. while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) {
  3410. pos++;
  3411. }
  3412. if (pos === state.pos) {
  3413. return false;
  3414. }
  3415. if (!silent) {
  3416. state.pending += state.src.slice(state.pos, pos);
  3417. }
  3418. state.pos = pos;
  3419. return true;
  3420. }
  3421. // Alternative implementation, for memory.
  3422. //
  3423. // It costs 10% of performance, but allows extend terminators list, if place it
  3424. // to `ParserInline` property. Probably, will switch to it sometime, such
  3425. // flexibility required.
  3426. /*
  3427. var TERMINATOR_RE = /[\n!#$%&*+\-:<=>@[\\\]^_`{}~]/;
  3428. module.exports = function text(state, silent) {
  3429. var pos = state.pos,
  3430. idx = state.src.slice(pos).search(TERMINATOR_RE);
  3431. // first char is terminator -> empty text
  3432. if (idx === 0) { return false; }
  3433. // no terminator -> text till end of string
  3434. if (idx < 0) {
  3435. if (!silent) { state.pending += state.src.slice(pos); }
  3436. state.pos = state.src.length;
  3437. return true;
  3438. }
  3439. if (!silent) { state.pending += state.src.slice(pos, pos + idx); }
  3440. state.pos += idx;
  3441. return true;
  3442. }; */
  3443. // Process links like https://example.org/
  3444. // RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  3445. const SCHEME_RE = /(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$/i;
  3446. function linkify(state, silent) {
  3447. if (!state.md.options.linkify) return false;
  3448. if (state.linkLevel > 0) return false;
  3449. const pos = state.pos;
  3450. const max = state.posMax;
  3451. if (pos + 3 > max) return false;
  3452. if (state.src.charCodeAt(pos) !== 0x3A /* : */) return false;
  3453. if (state.src.charCodeAt(pos + 1) !== 0x2F /* / */) return false;
  3454. if (state.src.charCodeAt(pos + 2) !== 0x2F /* / */) return false;
  3455. const match = state.pending.match(SCHEME_RE);
  3456. if (!match) return false;
  3457. const proto = match[1];
  3458. const link = state.md.linkify.matchAtStart(state.src.slice(pos - proto.length));
  3459. if (!link) return false;
  3460. let url = link.url;
  3461. // invalid link, but still detected by linkify somehow;
  3462. // need to check to prevent infinite loop below
  3463. if (url.length <= proto.length) return false;
  3464. // disallow '*' at the end of the link (conflicts with emphasis)
  3465. url = url.replace(/\*+$/, '');
  3466. const fullUrl = state.md.normalizeLink(url);
  3467. if (!state.md.validateLink(fullUrl)) return false;
  3468. if (!silent) {
  3469. state.pending = state.pending.slice(0, -proto.length);
  3470. const token_o = state.push('link_open', 'a', 1);
  3471. token_o.attrs = [['href', fullUrl]];
  3472. token_o.markup = 'linkify';
  3473. token_o.info = 'auto';
  3474. const token_t = state.push('text', '', 0);
  3475. token_t.content = state.md.normalizeLinkText(url);
  3476. const token_c = state.push('link_close', 'a', -1);
  3477. token_c.markup = 'linkify';
  3478. token_c.info = 'auto';
  3479. }
  3480. state.pos += url.length - proto.length;
  3481. return true;
  3482. }
  3483. // Proceess '\n'
  3484. function newline(state, silent) {
  3485. let pos = state.pos;
  3486. if (state.src.charCodeAt(pos) !== 0x0A /* \n */) {
  3487. return false;
  3488. }
  3489. const pmax = state.pending.length - 1;
  3490. const max = state.posMax;
  3491. // ' \n' -> hardbreak
  3492. // Lookup in pending chars is bad practice! Don't copy to other rules!
  3493. // Pending string is stored in concat mode, indexed lookups will cause
  3494. // convertion to flat mode.
  3495. if (!silent) {
  3496. if (pmax >= 0 && state.pending.charCodeAt(pmax) === 0x20) {
  3497. if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) {
  3498. // Find whitespaces tail of pending chars.
  3499. let ws = pmax - 1;
  3500. while (ws >= 1 && state.pending.charCodeAt(ws - 1) === 0x20) ws--;
  3501. state.pending = state.pending.slice(0, ws);
  3502. state.push('hardbreak', 'br', 0);
  3503. } else {
  3504. state.pending = state.pending.slice(0, -1);
  3505. state.push('softbreak', 'br', 0);
  3506. }
  3507. } else {
  3508. state.push('softbreak', 'br', 0);
  3509. }
  3510. }
  3511. pos++;
  3512. // skip heading spaces for next line
  3513. while (pos < max && isSpace(state.src.charCodeAt(pos))) {
  3514. pos++;
  3515. }
  3516. state.pos = pos;
  3517. return true;
  3518. }
  3519. // Process escaped chars and hardbreaks
  3520. const ESCAPED = [];
  3521. for (let i = 0; i < 256; i++) {
  3522. ESCAPED.push(0);
  3523. }
  3524. '\\!"#$%&\'()*+,./:;<=>?@[]^_`{|}~-'.split('').forEach(function (ch) {
  3525. ESCAPED[ch.charCodeAt(0)] = 1;
  3526. });
  3527. function escape(state, silent) {
  3528. let pos = state.pos;
  3529. const max = state.posMax;
  3530. if (state.src.charCodeAt(pos) !== 0x5C /* \ */) return false;
  3531. pos++;
  3532. // '\' at the end of the inline block
  3533. if (pos >= max) return false;
  3534. let ch1 = state.src.charCodeAt(pos);
  3535. if (ch1 === 0x0A) {
  3536. if (!silent) {
  3537. state.push('hardbreak', 'br', 0);
  3538. }
  3539. pos++;
  3540. // skip leading whitespaces from next line
  3541. while (pos < max) {
  3542. ch1 = state.src.charCodeAt(pos);
  3543. if (!isSpace(ch1)) break;
  3544. pos++;
  3545. }
  3546. state.pos = pos;
  3547. return true;
  3548. }
  3549. let escapedStr = state.src[pos];
  3550. if (ch1 >= 0xD800 && ch1 <= 0xDBFF && pos + 1 < max) {
  3551. const ch2 = state.src.charCodeAt(pos + 1);
  3552. if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
  3553. escapedStr += state.src[pos + 1];
  3554. pos++;
  3555. }
  3556. }
  3557. const origStr = '\\' + escapedStr;
  3558. if (!silent) {
  3559. const token = state.push('text_special', '', 0);
  3560. if (ch1 < 256 && ESCAPED[ch1] !== 0) {
  3561. token.content = escapedStr;
  3562. } else {
  3563. token.content = origStr;
  3564. }
  3565. token.markup = origStr;
  3566. token.info = 'escape';
  3567. }
  3568. state.pos = pos + 1;
  3569. return true;
  3570. }
  3571. // Parse backticks
  3572. function backtick(state, silent) {
  3573. let pos = state.pos;
  3574. const ch = state.src.charCodeAt(pos);
  3575. if (ch !== 0x60 /* ` */) {
  3576. return false;
  3577. }
  3578. const start = pos;
  3579. pos++;
  3580. const max = state.posMax;
  3581. // scan marker length
  3582. while (pos < max && state.src.charCodeAt(pos) === 0x60 /* ` */) {
  3583. pos++;
  3584. }
  3585. const marker = state.src.slice(start, pos);
  3586. const openerLength = marker.length;
  3587. if (state.backticksScanned && (state.backticks[openerLength] || 0) <= start) {
  3588. if (!silent) state.pending += marker;
  3589. state.pos += openerLength;
  3590. return true;
  3591. }
  3592. let matchEnd = pos;
  3593. let matchStart;
  3594. // Nothing found in the cache, scan until the end of the line (or until marker is found)
  3595. while ((matchStart = state.src.indexOf('`', matchEnd)) !== -1) {
  3596. matchEnd = matchStart + 1;
  3597. // scan marker length
  3598. while (matchEnd < max && state.src.charCodeAt(matchEnd) === 0x60 /* ` */) {
  3599. matchEnd++;
  3600. }
  3601. const closerLength = matchEnd - matchStart;
  3602. if (closerLength === openerLength) {
  3603. // Found matching closer length.
  3604. if (!silent) {
  3605. const token = state.push('code_inline', 'code', 0);
  3606. token.markup = marker;
  3607. token.content = state.src.slice(pos, matchStart).replace(/\n/g, ' ').replace(/^ (.+) $/, '$1');
  3608. }
  3609. state.pos = matchEnd;
  3610. return true;
  3611. }
  3612. // Some different length found, put it in cache as upper limit of where closer can be found
  3613. state.backticks[closerLength] = matchStart;
  3614. }
  3615. // Scanned through the end, didn't find anything
  3616. state.backticksScanned = true;
  3617. if (!silent) state.pending += marker;
  3618. state.pos += openerLength;
  3619. return true;
  3620. }
  3621. // ~~strike through~~
  3622. //
  3623. // Insert each marker as a separate text token, and add it to delimiter list
  3624. //
  3625. function strikethrough_tokenize(state, silent) {
  3626. const start = state.pos;
  3627. const marker = state.src.charCodeAt(start);
  3628. if (silent) {
  3629. return false;
  3630. }
  3631. if (marker !== 0x7E /* ~ */) {
  3632. return false;
  3633. }
  3634. const scanned = state.scanDelims(state.pos, true);
  3635. let len = scanned.length;
  3636. const ch = String.fromCharCode(marker);
  3637. if (len < 2) {
  3638. return false;
  3639. }
  3640. let token;
  3641. if (len % 2) {
  3642. token = state.push('text', '', 0);
  3643. token.content = ch;
  3644. len--;
  3645. }
  3646. for (let i = 0; i < len; i += 2) {
  3647. token = state.push('text', '', 0);
  3648. token.content = ch + ch;
  3649. state.delimiters.push({
  3650. marker,
  3651. length: 0,
  3652. // disable "rule of 3" length checks meant for emphasis
  3653. token: state.tokens.length - 1,
  3654. end: -1,
  3655. open: scanned.can_open,
  3656. close: scanned.can_close
  3657. });
  3658. }
  3659. state.pos += scanned.length;
  3660. return true;
  3661. }
  3662. function postProcess$1(state, delimiters) {
  3663. let token;
  3664. const loneMarkers = [];
  3665. const max = delimiters.length;
  3666. for (let i = 0; i < max; i++) {
  3667. const startDelim = delimiters[i];
  3668. if (startDelim.marker !== 0x7E /* ~ */) {
  3669. continue;
  3670. }
  3671. if (startDelim.end === -1) {
  3672. continue;
  3673. }
  3674. const endDelim = delimiters[startDelim.end];
  3675. token = state.tokens[startDelim.token];
  3676. token.type = 's_open';
  3677. token.tag = 's';
  3678. token.nesting = 1;
  3679. token.markup = '~~';
  3680. token.content = '';
  3681. token = state.tokens[endDelim.token];
  3682. token.type = 's_close';
  3683. token.tag = 's';
  3684. token.nesting = -1;
  3685. token.markup = '~~';
  3686. token.content = '';
  3687. if (state.tokens[endDelim.token - 1].type === 'text' && state.tokens[endDelim.token - 1].content === '~') {
  3688. loneMarkers.push(endDelim.token - 1);
  3689. }
  3690. }
  3691. // If a marker sequence has an odd number of characters, it's splitted
  3692. // like this: `~~~~~` -> `~` + `~~` + `~~`, leaving one marker at the
  3693. // start of the sequence.
  3694. //
  3695. // So, we have to move all those markers after subsequent s_close tags.
  3696. //
  3697. while (loneMarkers.length) {
  3698. const i = loneMarkers.pop();
  3699. let j = i + 1;
  3700. while (j < state.tokens.length && state.tokens[j].type === 's_close') {
  3701. j++;
  3702. }
  3703. j--;
  3704. if (i !== j) {
  3705. token = state.tokens[j];
  3706. state.tokens[j] = state.tokens[i];
  3707. state.tokens[i] = token;
  3708. }
  3709. }
  3710. }
  3711. // Walk through delimiter list and replace text tokens with tags
  3712. //
  3713. function strikethrough_postProcess(state) {
  3714. const tokens_meta = state.tokens_meta;
  3715. const max = state.tokens_meta.length;
  3716. postProcess$1(state, state.delimiters);
  3717. for (let curr = 0; curr < max; curr++) {
  3718. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  3719. postProcess$1(state, tokens_meta[curr].delimiters);
  3720. }
  3721. }
  3722. }
  3723. var r_strikethrough = {
  3724. tokenize: strikethrough_tokenize,
  3725. postProcess: strikethrough_postProcess
  3726. };
  3727. // Process *this* and _that_
  3728. //
  3729. // Insert each marker as a separate text token, and add it to delimiter list
  3730. //
  3731. function emphasis_tokenize(state, silent) {
  3732. const start = state.pos;
  3733. const marker = state.src.charCodeAt(start);
  3734. if (silent) {
  3735. return false;
  3736. }
  3737. if (marker !== 0x5F /* _ */ && marker !== 0x2A /* * */) {
  3738. return false;
  3739. }
  3740. const scanned = state.scanDelims(state.pos, marker === 0x2A);
  3741. for (let i = 0; i < scanned.length; i++) {
  3742. const token = state.push('text', '', 0);
  3743. token.content = String.fromCharCode(marker);
  3744. state.delimiters.push({
  3745. // Char code of the starting marker (number).
  3746. //
  3747. marker,
  3748. // Total length of these series of delimiters.
  3749. //
  3750. length: scanned.length,
  3751. // A position of the token this delimiter corresponds to.
  3752. //
  3753. token: state.tokens.length - 1,
  3754. // If this delimiter is matched as a valid opener, `end` will be
  3755. // equal to its position, otherwise it's `-1`.
  3756. //
  3757. end: -1,
  3758. // Boolean flags that determine if this delimiter could open or close
  3759. // an emphasis.
  3760. //
  3761. open: scanned.can_open,
  3762. close: scanned.can_close
  3763. });
  3764. }
  3765. state.pos += scanned.length;
  3766. return true;
  3767. }
  3768. function postProcess(state, delimiters) {
  3769. const max = delimiters.length;
  3770. for (let i = max - 1; i >= 0; i--) {
  3771. const startDelim = delimiters[i];
  3772. if (startDelim.marker !== 0x5F /* _ */ && startDelim.marker !== 0x2A /* * */) {
  3773. continue;
  3774. }
  3775. // Process only opening markers
  3776. if (startDelim.end === -1) {
  3777. continue;
  3778. }
  3779. const endDelim = delimiters[startDelim.end];
  3780. // If the previous delimiter has the same marker and is adjacent to this one,
  3781. // merge those into one strong delimiter.
  3782. //
  3783. // `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
  3784. //
  3785. const isStrong = i > 0 && delimiters[i - 1].end === startDelim.end + 1 &&
  3786. // check that first two markers match and adjacent
  3787. delimiters[i - 1].marker === startDelim.marker && delimiters[i - 1].token === startDelim.token - 1 &&
  3788. // check that last two markers are adjacent (we can safely assume they match)
  3789. delimiters[startDelim.end + 1].token === endDelim.token + 1;
  3790. const ch = String.fromCharCode(startDelim.marker);
  3791. const token_o = state.tokens[startDelim.token];
  3792. token_o.type = isStrong ? 'strong_open' : 'em_open';
  3793. token_o.tag = isStrong ? 'strong' : 'em';
  3794. token_o.nesting = 1;
  3795. token_o.markup = isStrong ? ch + ch : ch;
  3796. token_o.content = '';
  3797. const token_c = state.tokens[endDelim.token];
  3798. token_c.type = isStrong ? 'strong_close' : 'em_close';
  3799. token_c.tag = isStrong ? 'strong' : 'em';
  3800. token_c.nesting = -1;
  3801. token_c.markup = isStrong ? ch + ch : ch;
  3802. token_c.content = '';
  3803. if (isStrong) {
  3804. state.tokens[delimiters[i - 1].token].content = '';
  3805. state.tokens[delimiters[startDelim.end + 1].token].content = '';
  3806. i--;
  3807. }
  3808. }
  3809. }
  3810. // Walk through delimiter list and replace text tokens with tags
  3811. //
  3812. function emphasis_post_process(state) {
  3813. const tokens_meta = state.tokens_meta;
  3814. const max = state.tokens_meta.length;
  3815. postProcess(state, state.delimiters);
  3816. for (let curr = 0; curr < max; curr++) {
  3817. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  3818. postProcess(state, tokens_meta[curr].delimiters);
  3819. }
  3820. }
  3821. }
  3822. var r_emphasis = {
  3823. tokenize: emphasis_tokenize,
  3824. postProcess: emphasis_post_process
  3825. };
  3826. // Process [link](<to> "stuff")
  3827. function link(state, silent) {
  3828. let code, label, res, ref;
  3829. let href = '';
  3830. let title = '';
  3831. let start = state.pos;
  3832. let parseReference = true;
  3833. if (state.src.charCodeAt(state.pos) !== 0x5B /* [ */) {
  3834. return false;
  3835. }
  3836. const oldPos = state.pos;
  3837. const max = state.posMax;
  3838. const labelStart = state.pos + 1;
  3839. const labelEnd = state.md.helpers.parseLinkLabel(state, state.pos, true);
  3840. // parser failed to find ']', so it's not a valid link
  3841. if (labelEnd < 0) {
  3842. return false;
  3843. }
  3844. let pos = labelEnd + 1;
  3845. if (pos < max && state.src.charCodeAt(pos) === 0x28 /* ( */) {
  3846. //
  3847. // Inline link
  3848. //
  3849. // might have found a valid shortcut link, disable reference parsing
  3850. parseReference = false;
  3851. // [link]( <href> "title" )
  3852. // ^^ skipping these spaces
  3853. pos++;
  3854. for (; pos < max; pos++) {
  3855. code = state.src.charCodeAt(pos);
  3856. if (!isSpace(code) && code !== 0x0A) {
  3857. break;
  3858. }
  3859. }
  3860. if (pos >= max) {
  3861. return false;
  3862. }
  3863. // [link]( <href> "title" )
  3864. // ^^^^^^ parsing link destination
  3865. start = pos;
  3866. res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
  3867. if (res.ok) {
  3868. href = state.md.normalizeLink(res.str);
  3869. if (state.md.validateLink(href)) {
  3870. pos = res.pos;
  3871. } else {
  3872. href = '';
  3873. }
  3874. // [link]( <href> "title" )
  3875. // ^^ skipping these spaces
  3876. start = pos;
  3877. for (; pos < max; pos++) {
  3878. code = state.src.charCodeAt(pos);
  3879. if (!isSpace(code) && code !== 0x0A) {
  3880. break;
  3881. }
  3882. }
  3883. // [link]( <href> "title" )
  3884. // ^^^^^^^ parsing link title
  3885. res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax);
  3886. if (pos < max && start !== pos && res.ok) {
  3887. title = res.str;
  3888. pos = res.pos;
  3889. // [link]( <href> "title" )
  3890. // ^^ skipping these spaces
  3891. for (; pos < max; pos++) {
  3892. code = state.src.charCodeAt(pos);
  3893. if (!isSpace(code) && code !== 0x0A) {
  3894. break;
  3895. }
  3896. }
  3897. }
  3898. }
  3899. if (pos >= max || state.src.charCodeAt(pos) !== 0x29 /* ) */) {
  3900. // parsing a valid shortcut link failed, fallback to reference
  3901. parseReference = true;
  3902. }
  3903. pos++;
  3904. }
  3905. if (parseReference) {
  3906. //
  3907. // Link reference
  3908. //
  3909. if (typeof state.env.references === 'undefined') {
  3910. return false;
  3911. }
  3912. if (pos < max && state.src.charCodeAt(pos) === 0x5B /* [ */) {
  3913. start = pos + 1;
  3914. pos = state.md.helpers.parseLinkLabel(state, pos);
  3915. if (pos >= 0) {
  3916. label = state.src.slice(start, pos++);
  3917. } else {
  3918. pos = labelEnd + 1;
  3919. }
  3920. } else {
  3921. pos = labelEnd + 1;
  3922. }
  3923. // covers label === '' and label === undefined
  3924. // (collapsed reference link and shortcut reference link respectively)
  3925. if (!label) {
  3926. label = state.src.slice(labelStart, labelEnd);
  3927. }
  3928. ref = state.env.references[normalizeReference(label)];
  3929. if (!ref) {
  3930. state.pos = oldPos;
  3931. return false;
  3932. }
  3933. href = ref.href;
  3934. title = ref.title;
  3935. }
  3936. //
  3937. // We found the end of the link, and know for a fact it's a valid link;
  3938. // so all that's left to do is to call tokenizer.
  3939. //
  3940. if (!silent) {
  3941. state.pos = labelStart;
  3942. state.posMax = labelEnd;
  3943. const token_o = state.push('link_open', 'a', 1);
  3944. const attrs = [['href', href]];
  3945. token_o.attrs = attrs;
  3946. if (title) {
  3947. attrs.push(['title', title]);
  3948. }
  3949. state.linkLevel++;
  3950. state.md.inline.tokenize(state);
  3951. state.linkLevel--;
  3952. state.push('link_close', 'a', -1);
  3953. }
  3954. state.pos = pos;
  3955. state.posMax = max;
  3956. return true;
  3957. }
  3958. // Process ![image](<src> "title")
  3959. function image(state, silent) {
  3960. let code, content, label, pos, ref, res, title, start;
  3961. let href = '';
  3962. const oldPos = state.pos;
  3963. const max = state.posMax;
  3964. if (state.src.charCodeAt(state.pos) !== 0x21 /* ! */) {
  3965. return false;
  3966. }
  3967. if (state.src.charCodeAt(state.pos + 1) !== 0x5B /* [ */) {
  3968. return false;
  3969. }
  3970. const labelStart = state.pos + 2;
  3971. const labelEnd = state.md.helpers.parseLinkLabel(state, state.pos + 1, false);
  3972. // parser failed to find ']', so it's not a valid link
  3973. if (labelEnd < 0) {
  3974. return false;
  3975. }
  3976. pos = labelEnd + 1;
  3977. if (pos < max && state.src.charCodeAt(pos) === 0x28 /* ( */) {
  3978. //
  3979. // Inline link
  3980. //
  3981. // [link]( <href> "title" )
  3982. // ^^ skipping these spaces
  3983. pos++;
  3984. for (; pos < max; pos++) {
  3985. code = state.src.charCodeAt(pos);
  3986. if (!isSpace(code) && code !== 0x0A) {
  3987. break;
  3988. }
  3989. }
  3990. if (pos >= max) {
  3991. return false;
  3992. }
  3993. // [link]( <href> "title" )
  3994. // ^^^^^^ parsing link destination
  3995. start = pos;
  3996. res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
  3997. if (res.ok) {
  3998. href = state.md.normalizeLink(res.str);
  3999. if (state.md.validateLink(href)) {
  4000. pos = res.pos;
  4001. } else {
  4002. href = '';
  4003. }
  4004. }
  4005. // [link]( <href> "title" )
  4006. // ^^ skipping these spaces
  4007. start = pos;
  4008. for (; pos < max; pos++) {
  4009. code = state.src.charCodeAt(pos);
  4010. if (!isSpace(code) && code !== 0x0A) {
  4011. break;
  4012. }
  4013. }
  4014. // [link]( <href> "title" )
  4015. // ^^^^^^^ parsing link title
  4016. res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax);
  4017. if (pos < max && start !== pos && res.ok) {
  4018. title = res.str;
  4019. pos = res.pos;
  4020. // [link]( <href> "title" )
  4021. // ^^ skipping these spaces
  4022. for (; pos < max; pos++) {
  4023. code = state.src.charCodeAt(pos);
  4024. if (!isSpace(code) && code !== 0x0A) {
  4025. break;
  4026. }
  4027. }
  4028. } else {
  4029. title = '';
  4030. }
  4031. if (pos >= max || state.src.charCodeAt(pos) !== 0x29 /* ) */) {
  4032. state.pos = oldPos;
  4033. return false;
  4034. }
  4035. pos++;
  4036. } else {
  4037. //
  4038. // Link reference
  4039. //
  4040. if (typeof state.env.references === 'undefined') {
  4041. return false;
  4042. }
  4043. if (pos < max && state.src.charCodeAt(pos) === 0x5B /* [ */) {
  4044. start = pos + 1;
  4045. pos = state.md.helpers.parseLinkLabel(state, pos);
  4046. if (pos >= 0) {
  4047. label = state.src.slice(start, pos++);
  4048. } else {
  4049. pos = labelEnd + 1;
  4050. }
  4051. } else {
  4052. pos = labelEnd + 1;
  4053. }
  4054. // covers label === '' and label === undefined
  4055. // (collapsed reference link and shortcut reference link respectively)
  4056. if (!label) {
  4057. label = state.src.slice(labelStart, labelEnd);
  4058. }
  4059. ref = state.env.references[normalizeReference(label)];
  4060. if (!ref) {
  4061. state.pos = oldPos;
  4062. return false;
  4063. }
  4064. href = ref.href;
  4065. title = ref.title;
  4066. }
  4067. //
  4068. // We found the end of the link, and know for a fact it's a valid link;
  4069. // so all that's left to do is to call tokenizer.
  4070. //
  4071. if (!silent) {
  4072. content = state.src.slice(labelStart, labelEnd);
  4073. const tokens = [];
  4074. state.md.inline.parse(content, state.md, state.env, tokens);
  4075. const token = state.push('image', 'img', 0);
  4076. const attrs = [['src', href], ['alt', '']];
  4077. token.attrs = attrs;
  4078. token.children = tokens;
  4079. token.content = content;
  4080. if (title) {
  4081. attrs.push(['title', title]);
  4082. }
  4083. }
  4084. state.pos = pos;
  4085. state.posMax = max;
  4086. return true;
  4087. }
  4088. // Process autolinks '<protocol:...>'
  4089. /* eslint max-len:0 */
  4090. const EMAIL_RE = /^([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)$/;
  4091. /* eslint-disable-next-line no-control-regex */
  4092. const AUTOLINK_RE = /^([a-zA-Z][a-zA-Z0-9+.-]{1,31}):([^<>\x00-\x20]*)$/;
  4093. function autolink(state, silent) {
  4094. let pos = state.pos;
  4095. if (state.src.charCodeAt(pos) !== 0x3C /* < */) {
  4096. return false;
  4097. }
  4098. const start = state.pos;
  4099. const max = state.posMax;
  4100. for (;;) {
  4101. if (++pos >= max) return false;
  4102. const ch = state.src.charCodeAt(pos);
  4103. if (ch === 0x3C /* < */) return false;
  4104. if (ch === 0x3E /* > */) break;
  4105. }
  4106. const url = state.src.slice(start + 1, pos);
  4107. if (AUTOLINK_RE.test(url)) {
  4108. const fullUrl = state.md.normalizeLink(url);
  4109. if (!state.md.validateLink(fullUrl)) {
  4110. return false;
  4111. }
  4112. if (!silent) {
  4113. const token_o = state.push('link_open', 'a', 1);
  4114. token_o.attrs = [['href', fullUrl]];
  4115. token_o.markup = 'autolink';
  4116. token_o.info = 'auto';
  4117. const token_t = state.push('text', '', 0);
  4118. token_t.content = state.md.normalizeLinkText(url);
  4119. const token_c = state.push('link_close', 'a', -1);
  4120. token_c.markup = 'autolink';
  4121. token_c.info = 'auto';
  4122. }
  4123. state.pos += url.length + 2;
  4124. return true;
  4125. }
  4126. if (EMAIL_RE.test(url)) {
  4127. const fullUrl = state.md.normalizeLink('mailto:' + url);
  4128. if (!state.md.validateLink(fullUrl)) {
  4129. return false;
  4130. }
  4131. if (!silent) {
  4132. const token_o = state.push('link_open', 'a', 1);
  4133. token_o.attrs = [['href', fullUrl]];
  4134. token_o.markup = 'autolink';
  4135. token_o.info = 'auto';
  4136. const token_t = state.push('text', '', 0);
  4137. token_t.content = state.md.normalizeLinkText(url);
  4138. const token_c = state.push('link_close', 'a', -1);
  4139. token_c.markup = 'autolink';
  4140. token_c.info = 'auto';
  4141. }
  4142. state.pos += url.length + 2;
  4143. return true;
  4144. }
  4145. return false;
  4146. }
  4147. // Process html tags
  4148. function isLinkOpen(str) {
  4149. return /^<a[>\s]/i.test(str);
  4150. }
  4151. function isLinkClose(str) {
  4152. return /^<\/a\s*>/i.test(str);
  4153. }
  4154. function isLetter(ch) {
  4155. /* eslint no-bitwise:0 */
  4156. const lc = ch | 0x20; // to lower case
  4157. return lc >= 0x61 /* a */ && lc <= 0x7a /* z */;
  4158. }
  4159. function html_inline(state, silent) {
  4160. if (!state.md.options.html) {
  4161. return false;
  4162. }
  4163. // Check start
  4164. const max = state.posMax;
  4165. const pos = state.pos;
  4166. if (state.src.charCodeAt(pos) !== 0x3C /* < */ || pos + 2 >= max) {
  4167. return false;
  4168. }
  4169. // Quick fail on second char
  4170. const ch = state.src.charCodeAt(pos + 1);
  4171. if (ch !== 0x21 /* ! */ && ch !== 0x3F /* ? */ && ch !== 0x2F /* / */ && !isLetter(ch)) {
  4172. return false;
  4173. }
  4174. const match = state.src.slice(pos).match(HTML_TAG_RE);
  4175. if (!match) {
  4176. return false;
  4177. }
  4178. if (!silent) {
  4179. const token = state.push('html_inline', '', 0);
  4180. token.content = match[0];
  4181. if (isLinkOpen(token.content)) state.linkLevel++;
  4182. if (isLinkClose(token.content)) state.linkLevel--;
  4183. }
  4184. state.pos += match[0].length;
  4185. return true;
  4186. }
  4187. // Process html entity - &#123;, &#xAF;, &quot;, ...
  4188. const DIGITAL_RE = /^&#((?:x[a-f0-9]{1,6}|[0-9]{1,7}));/i;
  4189. const NAMED_RE = /^&([a-z][a-z0-9]{1,31});/i;
  4190. function entity(state, silent) {
  4191. const pos = state.pos;
  4192. const max = state.posMax;
  4193. if (state.src.charCodeAt(pos) !== 0x26 /* & */) return false;
  4194. if (pos + 1 >= max) return false;
  4195. const ch = state.src.charCodeAt(pos + 1);
  4196. if (ch === 0x23 /* # */) {
  4197. const match = state.src.slice(pos).match(DIGITAL_RE);
  4198. if (match) {
  4199. if (!silent) {
  4200. const code = match[1][0].toLowerCase() === 'x' ? parseInt(match[1].slice(1), 16) : parseInt(match[1], 10);
  4201. const token = state.push('text_special', '', 0);
  4202. token.content = isValidEntityCode(code) ? fromCodePoint(code) : fromCodePoint(0xFFFD);
  4203. token.markup = match[0];
  4204. token.info = 'entity';
  4205. }
  4206. state.pos += match[0].length;
  4207. return true;
  4208. }
  4209. } else {
  4210. const match = state.src.slice(pos).match(NAMED_RE);
  4211. if (match) {
  4212. const decoded = entities.decodeHTML(match[0]);
  4213. if (decoded !== match[0]) {
  4214. if (!silent) {
  4215. const token = state.push('text_special', '', 0);
  4216. token.content = decoded;
  4217. token.markup = match[0];
  4218. token.info = 'entity';
  4219. }
  4220. state.pos += match[0].length;
  4221. return true;
  4222. }
  4223. }
  4224. }
  4225. return false;
  4226. }
  4227. // For each opening emphasis-like marker find a matching closing one
  4228. //
  4229. function processDelimiters(delimiters) {
  4230. const openersBottom = {};
  4231. const max = delimiters.length;
  4232. if (!max) return;
  4233. // headerIdx is the first delimiter of the current (where closer is) delimiter run
  4234. let headerIdx = 0;
  4235. let lastTokenIdx = -2; // needs any value lower than -1
  4236. const jumps = [];
  4237. for (let closerIdx = 0; closerIdx < max; closerIdx++) {
  4238. const closer = delimiters[closerIdx];
  4239. jumps.push(0);
  4240. // markers belong to same delimiter run if:
  4241. // - they have adjacent tokens
  4242. // - AND markers are the same
  4243. //
  4244. if (delimiters[headerIdx].marker !== closer.marker || lastTokenIdx !== closer.token - 1) {
  4245. headerIdx = closerIdx;
  4246. }
  4247. lastTokenIdx = closer.token;
  4248. // Length is only used for emphasis-specific "rule of 3",
  4249. // if it's not defined (in strikethrough or 3rd party plugins),
  4250. // we can default it to 0 to disable those checks.
  4251. //
  4252. closer.length = closer.length || 0;
  4253. if (!closer.close) continue;
  4254. // Previously calculated lower bounds (previous fails)
  4255. // for each marker, each delimiter length modulo 3,
  4256. // and for whether this closer can be an opener;
  4257. // https://github.com/commonmark/cmark/commit/34250e12ccebdc6372b8b49c44fab57c72443460
  4258. /* eslint-disable-next-line no-prototype-builtins */
  4259. if (!openersBottom.hasOwnProperty(closer.marker)) {
  4260. openersBottom[closer.marker] = [-1, -1, -1, -1, -1, -1];
  4261. }
  4262. const minOpenerIdx = openersBottom[closer.marker][(closer.open ? 3 : 0) + closer.length % 3];
  4263. let openerIdx = headerIdx - jumps[headerIdx] - 1;
  4264. let newMinOpenerIdx = openerIdx;
  4265. for (; openerIdx > minOpenerIdx; openerIdx -= jumps[openerIdx] + 1) {
  4266. const opener = delimiters[openerIdx];
  4267. if (opener.marker !== closer.marker) continue;
  4268. if (opener.open && opener.end < 0) {
  4269. let isOddMatch = false;
  4270. // from spec:
  4271. //
  4272. // If one of the delimiters can both open and close emphasis, then the
  4273. // sum of the lengths of the delimiter runs containing the opening and
  4274. // closing delimiters must not be a multiple of 3 unless both lengths
  4275. // are multiples of 3.
  4276. //
  4277. if (opener.close || closer.open) {
  4278. if ((opener.length + closer.length) % 3 === 0) {
  4279. if (opener.length % 3 !== 0 || closer.length % 3 !== 0) {
  4280. isOddMatch = true;
  4281. }
  4282. }
  4283. }
  4284. if (!isOddMatch) {
  4285. // If previous delimiter cannot be an opener, we can safely skip
  4286. // the entire sequence in future checks. This is required to make
  4287. // sure algorithm has linear complexity (see *_*_*_*_*_... case).
  4288. //
  4289. const lastJump = openerIdx > 0 && !delimiters[openerIdx - 1].open ? jumps[openerIdx - 1] + 1 : 0;
  4290. jumps[closerIdx] = closerIdx - openerIdx + lastJump;
  4291. jumps[openerIdx] = lastJump;
  4292. closer.open = false;
  4293. opener.end = closerIdx;
  4294. opener.close = false;
  4295. newMinOpenerIdx = -1;
  4296. // treat next token as start of run,
  4297. // it optimizes skips in **<...>**a**<...>** pathological case
  4298. lastTokenIdx = -2;
  4299. break;
  4300. }
  4301. }
  4302. }
  4303. if (newMinOpenerIdx !== -1) {
  4304. // If match for this delimiter run failed, we want to set lower bound for
  4305. // future lookups. This is required to make sure algorithm has linear
  4306. // complexity.
  4307. //
  4308. // See details here:
  4309. // https://github.com/commonmark/cmark/issues/178#issuecomment-270417442
  4310. //
  4311. openersBottom[closer.marker][(closer.open ? 3 : 0) + (closer.length || 0) % 3] = newMinOpenerIdx;
  4312. }
  4313. }
  4314. }
  4315. function link_pairs(state) {
  4316. const tokens_meta = state.tokens_meta;
  4317. const max = state.tokens_meta.length;
  4318. processDelimiters(state.delimiters);
  4319. for (let curr = 0; curr < max; curr++) {
  4320. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  4321. processDelimiters(tokens_meta[curr].delimiters);
  4322. }
  4323. }
  4324. }
  4325. // Clean up tokens after emphasis and strikethrough postprocessing:
  4326. // merge adjacent text nodes into one and re-calculate all token levels
  4327. //
  4328. // This is necessary because initially emphasis delimiter markers (*, _, ~)
  4329. // are treated as their own separate text tokens. Then emphasis rule either
  4330. // leaves them as text (needed to merge with adjacent text) or turns them
  4331. // into opening/closing tags (which messes up levels inside).
  4332. //
  4333. function fragments_join(state) {
  4334. let curr, last;
  4335. let level = 0;
  4336. const tokens = state.tokens;
  4337. const max = state.tokens.length;
  4338. for (curr = last = 0; curr < max; curr++) {
  4339. // re-calculate levels after emphasis/strikethrough turns some text nodes
  4340. // into opening/closing tags
  4341. if (tokens[curr].nesting < 0) level--; // closing tag
  4342. tokens[curr].level = level;
  4343. if (tokens[curr].nesting > 0) level++; // opening tag
  4344. if (tokens[curr].type === 'text' && curr + 1 < max && tokens[curr + 1].type === 'text') {
  4345. // collapse two adjacent text nodes
  4346. tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content;
  4347. } else {
  4348. if (curr !== last) {
  4349. tokens[last] = tokens[curr];
  4350. }
  4351. last++;
  4352. }
  4353. }
  4354. if (curr !== last) {
  4355. tokens.length = last;
  4356. }
  4357. }
  4358. /** internal
  4359. * class ParserInline
  4360. *
  4361. * Tokenizes paragraph content.
  4362. **/
  4363. // Parser rules
  4364. const _rules = [['text', text], ['linkify', linkify], ['newline', newline], ['escape', escape], ['backticks', backtick], ['strikethrough', r_strikethrough.tokenize], ['emphasis', r_emphasis.tokenize], ['link', link], ['image', image], ['autolink', autolink], ['html_inline', html_inline], ['entity', entity]];
  4365. // `rule2` ruleset was created specifically for emphasis/strikethrough
  4366. // post-processing and may be changed in the future.
  4367. //
  4368. // Don't use this for anything except pairs (plugins working with `balance_pairs`).
  4369. //
  4370. const _rules2 = [['balance_pairs', link_pairs], ['strikethrough', r_strikethrough.postProcess], ['emphasis', r_emphasis.postProcess],
  4371. // rules for pairs separate '**' into its own text tokens, which may be left unused,
  4372. // rule below merges unused segments back with the rest of the text
  4373. ['fragments_join', fragments_join]];
  4374. /**
  4375. * new ParserInline()
  4376. **/
  4377. function ParserInline() {
  4378. /**
  4379. * ParserInline#ruler -> Ruler
  4380. *
  4381. * [[Ruler]] instance. Keep configuration of inline rules.
  4382. **/
  4383. this.ruler = new Ruler();
  4384. for (let i = 0; i < _rules.length; i++) {
  4385. this.ruler.push(_rules[i][0], _rules[i][1]);
  4386. }
  4387. /**
  4388. * ParserInline#ruler2 -> Ruler
  4389. *
  4390. * [[Ruler]] instance. Second ruler used for post-processing
  4391. * (e.g. in emphasis-like rules).
  4392. **/
  4393. this.ruler2 = new Ruler();
  4394. for (let i = 0; i < _rules2.length; i++) {
  4395. this.ruler2.push(_rules2[i][0], _rules2[i][1]);
  4396. }
  4397. }
  4398. // Skip single token by running all rules in validation mode;
  4399. // returns `true` if any rule reported success
  4400. //
  4401. ParserInline.prototype.skipToken = function (state) {
  4402. const pos = state.pos;
  4403. const rules = this.ruler.getRules('');
  4404. const len = rules.length;
  4405. const maxNesting = state.md.options.maxNesting;
  4406. const cache = state.cache;
  4407. if (typeof cache[pos] !== 'undefined') {
  4408. state.pos = cache[pos];
  4409. return;
  4410. }
  4411. let ok = false;
  4412. if (state.level < maxNesting) {
  4413. for (let i = 0; i < len; i++) {
  4414. // Increment state.level and decrement it later to limit recursion.
  4415. // It's harmless to do here, because no tokens are created. But ideally,
  4416. // we'd need a separate private state variable for this purpose.
  4417. //
  4418. state.level++;
  4419. ok = rules[i](state, true);
  4420. state.level--;
  4421. if (ok) {
  4422. if (pos >= state.pos) {
  4423. throw new Error("inline rule didn't increment state.pos");
  4424. }
  4425. break;
  4426. }
  4427. }
  4428. } else {
  4429. // Too much nesting, just skip until the end of the paragraph.
  4430. //
  4431. // NOTE: this will cause links to behave incorrectly in the following case,
  4432. // when an amount of `[` is exactly equal to `maxNesting + 1`:
  4433. //
  4434. // [[[[[[[[[[[[[[[[[[[[[foo]()
  4435. //
  4436. // TODO: remove this workaround when CM standard will allow nested links
  4437. // (we can replace it by preventing links from being parsed in
  4438. // validation mode)
  4439. //
  4440. state.pos = state.posMax;
  4441. }
  4442. if (!ok) {
  4443. state.pos++;
  4444. }
  4445. cache[pos] = state.pos;
  4446. };
  4447. // Generate tokens for input range
  4448. //
  4449. ParserInline.prototype.tokenize = function (state) {
  4450. const rules = this.ruler.getRules('');
  4451. const len = rules.length;
  4452. const end = state.posMax;
  4453. const maxNesting = state.md.options.maxNesting;
  4454. while (state.pos < end) {
  4455. // Try all possible rules.
  4456. // On success, rule should:
  4457. //
  4458. // - update `state.pos`
  4459. // - update `state.tokens`
  4460. // - return true
  4461. const prevPos = state.pos;
  4462. let ok = false;
  4463. if (state.level < maxNesting) {
  4464. for (let i = 0; i < len; i++) {
  4465. ok = rules[i](state, false);
  4466. if (ok) {
  4467. if (prevPos >= state.pos) {
  4468. throw new Error("inline rule didn't increment state.pos");
  4469. }
  4470. break;
  4471. }
  4472. }
  4473. }
  4474. if (ok) {
  4475. if (state.pos >= end) {
  4476. break;
  4477. }
  4478. continue;
  4479. }
  4480. state.pending += state.src[state.pos++];
  4481. }
  4482. if (state.pending) {
  4483. state.pushPending();
  4484. }
  4485. };
  4486. /**
  4487. * ParserInline.parse(str, md, env, outTokens)
  4488. *
  4489. * Process input string and push inline tokens into `outTokens`
  4490. **/
  4491. ParserInline.prototype.parse = function (str, md, env, outTokens) {
  4492. const state = new this.State(str, md, env, outTokens);
  4493. this.tokenize(state);
  4494. const rules = this.ruler2.getRules('');
  4495. const len = rules.length;
  4496. for (let i = 0; i < len; i++) {
  4497. rules[i](state);
  4498. }
  4499. };
  4500. ParserInline.prototype.State = StateInline;
  4501. // markdown-it default options
  4502. var cfg_default = {
  4503. options: {
  4504. // Enable HTML tags in source
  4505. html: false,
  4506. // Use '/' to close single tags (<br />)
  4507. xhtmlOut: false,
  4508. // Convert '\n' in paragraphs into <br>
  4509. breaks: false,
  4510. // CSS language prefix for fenced blocks
  4511. langPrefix: 'language-',
  4512. // autoconvert URL-like texts to links
  4513. linkify: false,
  4514. // Enable some language-neutral replacements + quotes beautification
  4515. typographer: false,
  4516. // Double + single quotes replacement pairs, when typographer enabled,
  4517. // and smartquotes on. Could be either a String or an Array.
  4518. //
  4519. // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  4520. // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  4521. quotes: '\u201c\u201d\u2018\u2019',
  4522. /* “”‘’ */
  4523. // Highlighter function. Should return escaped HTML,
  4524. // or '' if the source string is not changed and should be escaped externaly.
  4525. // If result starts with <pre... internal wrapper is skipped.
  4526. //
  4527. // function (/*str, lang*/) { return ''; }
  4528. //
  4529. highlight: null,
  4530. // Internal protection, recursion limit
  4531. maxNesting: 100
  4532. },
  4533. components: {
  4534. core: {},
  4535. block: {},
  4536. inline: {}
  4537. }
  4538. };
  4539. // "Zero" preset, with nothing enabled. Useful for manual configuring of simple
  4540. // modes. For example, to parse bold/italic only.
  4541. var cfg_zero = {
  4542. options: {
  4543. // Enable HTML tags in source
  4544. html: false,
  4545. // Use '/' to close single tags (<br />)
  4546. xhtmlOut: false,
  4547. // Convert '\n' in paragraphs into <br>
  4548. breaks: false,
  4549. // CSS language prefix for fenced blocks
  4550. langPrefix: 'language-',
  4551. // autoconvert URL-like texts to links
  4552. linkify: false,
  4553. // Enable some language-neutral replacements + quotes beautification
  4554. typographer: false,
  4555. // Double + single quotes replacement pairs, when typographer enabled,
  4556. // and smartquotes on. Could be either a String or an Array.
  4557. //
  4558. // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  4559. // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  4560. quotes: '\u201c\u201d\u2018\u2019',
  4561. /* “”‘’ */
  4562. // Highlighter function. Should return escaped HTML,
  4563. // or '' if the source string is not changed and should be escaped externaly.
  4564. // If result starts with <pre... internal wrapper is skipped.
  4565. //
  4566. // function (/*str, lang*/) { return ''; }
  4567. //
  4568. highlight: null,
  4569. // Internal protection, recursion limit
  4570. maxNesting: 20
  4571. },
  4572. components: {
  4573. core: {
  4574. rules: ['normalize', 'block', 'inline', 'text_join']
  4575. },
  4576. block: {
  4577. rules: ['paragraph']
  4578. },
  4579. inline: {
  4580. rules: ['text'],
  4581. rules2: ['balance_pairs', 'fragments_join']
  4582. }
  4583. }
  4584. };
  4585. // Commonmark default options
  4586. var cfg_commonmark = {
  4587. options: {
  4588. // Enable HTML tags in source
  4589. html: true,
  4590. // Use '/' to close single tags (<br />)
  4591. xhtmlOut: true,
  4592. // Convert '\n' in paragraphs into <br>
  4593. breaks: false,
  4594. // CSS language prefix for fenced blocks
  4595. langPrefix: 'language-',
  4596. // autoconvert URL-like texts to links
  4597. linkify: false,
  4598. // Enable some language-neutral replacements + quotes beautification
  4599. typographer: false,
  4600. // Double + single quotes replacement pairs, when typographer enabled,
  4601. // and smartquotes on. Could be either a String or an Array.
  4602. //
  4603. // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  4604. // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  4605. quotes: '\u201c\u201d\u2018\u2019',
  4606. /* “”‘’ */
  4607. // Highlighter function. Should return escaped HTML,
  4608. // or '' if the source string is not changed and should be escaped externaly.
  4609. // If result starts with <pre... internal wrapper is skipped.
  4610. //
  4611. // function (/*str, lang*/) { return ''; }
  4612. //
  4613. highlight: null,
  4614. // Internal protection, recursion limit
  4615. maxNesting: 20
  4616. },
  4617. components: {
  4618. core: {
  4619. rules: ['normalize', 'block', 'inline', 'text_join']
  4620. },
  4621. block: {
  4622. rules: ['blockquote', 'code', 'fence', 'heading', 'hr', 'html_block', 'lheading', 'list', 'reference', 'paragraph']
  4623. },
  4624. inline: {
  4625. rules: ['autolink', 'backticks', 'emphasis', 'entity', 'escape', 'html_inline', 'image', 'link', 'newline', 'text'],
  4626. rules2: ['balance_pairs', 'emphasis', 'fragments_join']
  4627. }
  4628. }
  4629. };
  4630. // Main parser class
  4631. const config = {
  4632. default: cfg_default,
  4633. zero: cfg_zero,
  4634. commonmark: cfg_commonmark
  4635. };
  4636. //
  4637. // This validator can prohibit more than really needed to prevent XSS. It's a
  4638. // tradeoff to keep code simple and to be secure by default.
  4639. //
  4640. // If you need different setup - override validator method as you wish. Or
  4641. // replace it with dummy function and use external sanitizer.
  4642. //
  4643. const BAD_PROTO_RE = /^(vbscript|javascript|file|data):/;
  4644. const GOOD_DATA_RE = /^data:image\/(gif|png|jpeg|webp);/;
  4645. function validateLink(url) {
  4646. // url should be normalized at this point, and existing entities are decoded
  4647. const str = url.trim().toLowerCase();
  4648. return BAD_PROTO_RE.test(str) ? GOOD_DATA_RE.test(str) : true;
  4649. }
  4650. const RECODE_HOSTNAME_FOR = ['http:', 'https:', 'mailto:'];
  4651. function normalizeLink(url) {
  4652. const parsed = mdurl__namespace.parse(url, true);
  4653. if (parsed.hostname) {
  4654. // Encode hostnames in urls like:
  4655. // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
  4656. //
  4657. // We don't encode unknown schemas, because it's likely that we encode
  4658. // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
  4659. //
  4660. if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
  4661. try {
  4662. parsed.hostname = punycode.toASCII(parsed.hostname);
  4663. } catch (er) {/**/}
  4664. }
  4665. }
  4666. return mdurl__namespace.encode(mdurl__namespace.format(parsed));
  4667. }
  4668. function normalizeLinkText(url) {
  4669. const parsed = mdurl__namespace.parse(url, true);
  4670. if (parsed.hostname) {
  4671. // Encode hostnames in urls like:
  4672. // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
  4673. //
  4674. // We don't encode unknown schemas, because it's likely that we encode
  4675. // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
  4676. //
  4677. if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
  4678. try {
  4679. parsed.hostname = punycode.toUnicode(parsed.hostname);
  4680. } catch (er) {/**/}
  4681. }
  4682. }
  4683. // add '%' to exclude list because of https://github.com/markdown-it/markdown-it/issues/720
  4684. return mdurl__namespace.decode(mdurl__namespace.format(parsed), mdurl__namespace.decode.defaultChars + '%');
  4685. }
  4686. /**
  4687. * class MarkdownIt
  4688. *
  4689. * Main parser/renderer class.
  4690. *
  4691. * ##### Usage
  4692. *
  4693. * ```javascript
  4694. * // node.js, "classic" way:
  4695. * var MarkdownIt = require('markdown-it'),
  4696. * md = new MarkdownIt();
  4697. * var result = md.render('# markdown-it rulezz!');
  4698. *
  4699. * // node.js, the same, but with sugar:
  4700. * var md = require('markdown-it')();
  4701. * var result = md.render('# markdown-it rulezz!');
  4702. *
  4703. * // browser without AMD, added to "window" on script load
  4704. * // Note, there are no dash.
  4705. * var md = window.markdownit();
  4706. * var result = md.render('# markdown-it rulezz!');
  4707. * ```
  4708. *
  4709. * Single line rendering, without paragraph wrap:
  4710. *
  4711. * ```javascript
  4712. * var md = require('markdown-it')();
  4713. * var result = md.renderInline('__markdown-it__ rulezz!');
  4714. * ```
  4715. **/
  4716. /**
  4717. * new MarkdownIt([presetName, options])
  4718. * - presetName (String): optional, `commonmark` / `zero`
  4719. * - options (Object)
  4720. *
  4721. * Creates parser instanse with given config. Can be called without `new`.
  4722. *
  4723. * ##### presetName
  4724. *
  4725. * MarkdownIt provides named presets as a convenience to quickly
  4726. * enable/disable active syntax rules and options for common use cases.
  4727. *
  4728. * - ["commonmark"](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/commonmark.mjs) -
  4729. * configures parser to strict [CommonMark](http://commonmark.org/) mode.
  4730. * - [default](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/default.mjs) -
  4731. * similar to GFM, used when no preset name given. Enables all available rules,
  4732. * but still without html, typographer & autolinker.
  4733. * - ["zero"](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.mjs) -
  4734. * all rules disabled. Useful to quickly setup your config via `.enable()`.
  4735. * For example, when you need only `bold` and `italic` markup and nothing else.
  4736. *
  4737. * ##### options:
  4738. *
  4739. * - __html__ - `false`. Set `true` to enable HTML tags in source. Be careful!
  4740. * That's not safe! You may need external sanitizer to protect output from XSS.
  4741. * It's better to extend features via plugins, instead of enabling HTML.
  4742. * - __xhtmlOut__ - `false`. Set `true` to add '/' when closing single tags
  4743. * (`<br />`). This is needed only for full CommonMark compatibility. In real
  4744. * world you will need HTML output.
  4745. * - __breaks__ - `false`. Set `true` to convert `\n` in paragraphs into `<br>`.
  4746. * - __langPrefix__ - `language-`. CSS language class prefix for fenced blocks.
  4747. * Can be useful for external highlighters.
  4748. * - __linkify__ - `false`. Set `true` to autoconvert URL-like text to links.
  4749. * - __typographer__ - `false`. Set `true` to enable [some language-neutral
  4750. * replacement](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.mjs) +
  4751. * quotes beautification (smartquotes).
  4752. * - __quotes__ - `“”‘’`, String or Array. Double + single quotes replacement
  4753. * pairs, when typographer enabled and smartquotes on. For example, you can
  4754. * use `'«»„“'` for Russian, `'„“‚‘'` for German, and
  4755. * `['«\xA0', '\xA0»', '‹\xA0', '\xA0›']` for French (including nbsp).
  4756. * - __highlight__ - `null`. Highlighter function for fenced code blocks.
  4757. * Highlighter `function (str, lang)` should return escaped HTML. It can also
  4758. * return empty string if the source was not changed and should be escaped
  4759. * externaly. If result starts with <pre... internal wrapper is skipped.
  4760. *
  4761. * ##### Example
  4762. *
  4763. * ```javascript
  4764. * // commonmark mode
  4765. * var md = require('markdown-it')('commonmark');
  4766. *
  4767. * // default mode
  4768. * var md = require('markdown-it')();
  4769. *
  4770. * // enable everything
  4771. * var md = require('markdown-it')({
  4772. * html: true,
  4773. * linkify: true,
  4774. * typographer: true
  4775. * });
  4776. * ```
  4777. *
  4778. * ##### Syntax highlighting
  4779. *
  4780. * ```js
  4781. * var hljs = require('highlight.js') // https://highlightjs.org/
  4782. *
  4783. * var md = require('markdown-it')({
  4784. * highlight: function (str, lang) {
  4785. * if (lang && hljs.getLanguage(lang)) {
  4786. * try {
  4787. * return hljs.highlight(str, { language: lang, ignoreIllegals: true }).value;
  4788. * } catch (__) {}
  4789. * }
  4790. *
  4791. * return ''; // use external default escaping
  4792. * }
  4793. * });
  4794. * ```
  4795. *
  4796. * Or with full wrapper override (if you need assign class to `<pre>` or `<code>`):
  4797. *
  4798. * ```javascript
  4799. * var hljs = require('highlight.js') // https://highlightjs.org/
  4800. *
  4801. * // Actual default values
  4802. * var md = require('markdown-it')({
  4803. * highlight: function (str, lang) {
  4804. * if (lang && hljs.getLanguage(lang)) {
  4805. * try {
  4806. * return '<pre><code class="hljs">' +
  4807. * hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
  4808. * '</code></pre>';
  4809. * } catch (__) {}
  4810. * }
  4811. *
  4812. * return '<pre><code class="hljs">' + md.utils.escapeHtml(str) + '</code></pre>';
  4813. * }
  4814. * });
  4815. * ```
  4816. *
  4817. **/
  4818. function MarkdownIt(presetName, options) {
  4819. if (!(this instanceof MarkdownIt)) {
  4820. return new MarkdownIt(presetName, options);
  4821. }
  4822. if (!options) {
  4823. if (!isString(presetName)) {
  4824. options = presetName || {};
  4825. presetName = 'default';
  4826. }
  4827. }
  4828. /**
  4829. * MarkdownIt#inline -> ParserInline
  4830. *
  4831. * Instance of [[ParserInline]]. You may need it to add new rules when
  4832. * writing plugins. For simple rules control use [[MarkdownIt.disable]] and
  4833. * [[MarkdownIt.enable]].
  4834. **/
  4835. this.inline = new ParserInline();
  4836. /**
  4837. * MarkdownIt#block -> ParserBlock
  4838. *
  4839. * Instance of [[ParserBlock]]. You may need it to add new rules when
  4840. * writing plugins. For simple rules control use [[MarkdownIt.disable]] and
  4841. * [[MarkdownIt.enable]].
  4842. **/
  4843. this.block = new ParserBlock();
  4844. /**
  4845. * MarkdownIt#core -> Core
  4846. *
  4847. * Instance of [[Core]] chain executor. You may need it to add new rules when
  4848. * writing plugins. For simple rules control use [[MarkdownIt.disable]] and
  4849. * [[MarkdownIt.enable]].
  4850. **/
  4851. this.core = new Core();
  4852. /**
  4853. * MarkdownIt#renderer -> Renderer
  4854. *
  4855. * Instance of [[Renderer]]. Use it to modify output look. Or to add rendering
  4856. * rules for new token types, generated by plugins.
  4857. *
  4858. * ##### Example
  4859. *
  4860. * ```javascript
  4861. * var md = require('markdown-it')();
  4862. *
  4863. * function myToken(tokens, idx, options, env, self) {
  4864. * //...
  4865. * return result;
  4866. * };
  4867. *
  4868. * md.renderer.rules['my_token'] = myToken
  4869. * ```
  4870. *
  4871. * See [[Renderer]] docs and [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.mjs).
  4872. **/
  4873. this.renderer = new Renderer();
  4874. /**
  4875. * MarkdownIt#linkify -> LinkifyIt
  4876. *
  4877. * [linkify-it](https://github.com/markdown-it/linkify-it) instance.
  4878. * Used by [linkify](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/linkify.mjs)
  4879. * rule.
  4880. **/
  4881. this.linkify = new LinkifyIt();
  4882. /**
  4883. * MarkdownIt#validateLink(url) -> Boolean
  4884. *
  4885. * Link validation function. CommonMark allows too much in links. By default
  4886. * we disable `javascript:`, `vbscript:`, `file:` schemas, and almost all `data:...` schemas
  4887. * except some embedded image types.
  4888. *
  4889. * You can change this behaviour:
  4890. *
  4891. * ```javascript
  4892. * var md = require('markdown-it')();
  4893. * // enable everything
  4894. * md.validateLink = function () { return true; }
  4895. * ```
  4896. **/
  4897. this.validateLink = validateLink;
  4898. /**
  4899. * MarkdownIt#normalizeLink(url) -> String
  4900. *
  4901. * Function used to encode link url to a machine-readable format,
  4902. * which includes url-encoding, punycode, etc.
  4903. **/
  4904. this.normalizeLink = normalizeLink;
  4905. /**
  4906. * MarkdownIt#normalizeLinkText(url) -> String
  4907. *
  4908. * Function used to decode link url to a human-readable format`
  4909. **/
  4910. this.normalizeLinkText = normalizeLinkText;
  4911. // Expose utils & helpers for easy acces from plugins
  4912. /**
  4913. * MarkdownIt#utils -> utils
  4914. *
  4915. * Assorted utility functions, useful to write plugins. See details
  4916. * [here](https://github.com/markdown-it/markdown-it/blob/master/lib/common/utils.mjs).
  4917. **/
  4918. this.utils = utils;
  4919. /**
  4920. * MarkdownIt#helpers -> helpers
  4921. *
  4922. * Link components parser functions, useful to write plugins. See details
  4923. * [here](https://github.com/markdown-it/markdown-it/blob/master/lib/helpers).
  4924. **/
  4925. this.helpers = assign({}, helpers);
  4926. this.options = {};
  4927. this.configure(presetName);
  4928. if (options) {
  4929. this.set(options);
  4930. }
  4931. }
  4932. /** chainable
  4933. * MarkdownIt.set(options)
  4934. *
  4935. * Set parser options (in the same format as in constructor). Probably, you
  4936. * will never need it, but you can change options after constructor call.
  4937. *
  4938. * ##### Example
  4939. *
  4940. * ```javascript
  4941. * var md = require('markdown-it')()
  4942. * .set({ html: true, breaks: true })
  4943. * .set({ typographer, true });
  4944. * ```
  4945. *
  4946. * __Note:__ To achieve the best possible performance, don't modify a
  4947. * `markdown-it` instance options on the fly. If you need multiple configurations
  4948. * it's best to create multiple instances and initialize each with separate
  4949. * config.
  4950. **/
  4951. MarkdownIt.prototype.set = function (options) {
  4952. assign(this.options, options);
  4953. return this;
  4954. };
  4955. /** chainable, internal
  4956. * MarkdownIt.configure(presets)
  4957. *
  4958. * Batch load of all options and compenent settings. This is internal method,
  4959. * and you probably will not need it. But if you will - see available presets
  4960. * and data structure [here](https://github.com/markdown-it/markdown-it/tree/master/lib/presets)
  4961. *
  4962. * We strongly recommend to use presets instead of direct config loads. That
  4963. * will give better compatibility with next versions.
  4964. **/
  4965. MarkdownIt.prototype.configure = function (presets) {
  4966. const self = this;
  4967. if (isString(presets)) {
  4968. const presetName = presets;
  4969. presets = config[presetName];
  4970. if (!presets) {
  4971. throw new Error('Wrong `markdown-it` preset "' + presetName + '", check name');
  4972. }
  4973. }
  4974. if (!presets) {
  4975. throw new Error('Wrong `markdown-it` preset, can\'t be empty');
  4976. }
  4977. if (presets.options) {
  4978. self.set(presets.options);
  4979. }
  4980. if (presets.components) {
  4981. Object.keys(presets.components).forEach(function (name) {
  4982. if (presets.components[name].rules) {
  4983. self[name].ruler.enableOnly(presets.components[name].rules);
  4984. }
  4985. if (presets.components[name].rules2) {
  4986. self[name].ruler2.enableOnly(presets.components[name].rules2);
  4987. }
  4988. });
  4989. }
  4990. return this;
  4991. };
  4992. /** chainable
  4993. * MarkdownIt.enable(list, ignoreInvalid)
  4994. * - list (String|Array): rule name or list of rule names to enable
  4995. * - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
  4996. *
  4997. * Enable list or rules. It will automatically find appropriate components,
  4998. * containing rules with given names. If rule not found, and `ignoreInvalid`
  4999. * not set - throws exception.
  5000. *
  5001. * ##### Example
  5002. *
  5003. * ```javascript
  5004. * var md = require('markdown-it')()
  5005. * .enable(['sub', 'sup'])
  5006. * .disable('smartquotes');
  5007. * ```
  5008. **/
  5009. MarkdownIt.prototype.enable = function (list, ignoreInvalid) {
  5010. let result = [];
  5011. if (!Array.isArray(list)) {
  5012. list = [list];
  5013. }
  5014. ['core', 'block', 'inline'].forEach(function (chain) {
  5015. result = result.concat(this[chain].ruler.enable(list, true));
  5016. }, this);
  5017. result = result.concat(this.inline.ruler2.enable(list, true));
  5018. const missed = list.filter(function (name) {
  5019. return result.indexOf(name) < 0;
  5020. });
  5021. if (missed.length && !ignoreInvalid) {
  5022. throw new Error('MarkdownIt. Failed to enable unknown rule(s): ' + missed);
  5023. }
  5024. return this;
  5025. };
  5026. /** chainable
  5027. * MarkdownIt.disable(list, ignoreInvalid)
  5028. * - list (String|Array): rule name or list of rule names to disable.
  5029. * - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
  5030. *
  5031. * The same as [[MarkdownIt.enable]], but turn specified rules off.
  5032. **/
  5033. MarkdownIt.prototype.disable = function (list, ignoreInvalid) {
  5034. let result = [];
  5035. if (!Array.isArray(list)) {
  5036. list = [list];
  5037. }
  5038. ['core', 'block', 'inline'].forEach(function (chain) {
  5039. result = result.concat(this[chain].ruler.disable(list, true));
  5040. }, this);
  5041. result = result.concat(this.inline.ruler2.disable(list, true));
  5042. const missed = list.filter(function (name) {
  5043. return result.indexOf(name) < 0;
  5044. });
  5045. if (missed.length && !ignoreInvalid) {
  5046. throw new Error('MarkdownIt. Failed to disable unknown rule(s): ' + missed);
  5047. }
  5048. return this;
  5049. };
  5050. /** chainable
  5051. * MarkdownIt.use(plugin, params)
  5052. *
  5053. * Load specified plugin with given params into current parser instance.
  5054. * It's just a sugar to call `plugin(md, params)` with curring.
  5055. *
  5056. * ##### Example
  5057. *
  5058. * ```javascript
  5059. * var iterator = require('markdown-it-for-inline');
  5060. * var md = require('markdown-it')()
  5061. * .use(iterator, 'foo_replace', 'text', function (tokens, idx) {
  5062. * tokens[idx].content = tokens[idx].content.replace(/foo/g, 'bar');
  5063. * });
  5064. * ```
  5065. **/
  5066. MarkdownIt.prototype.use = function (plugin /*, params, ... */) {
  5067. const args = [this].concat(Array.prototype.slice.call(arguments, 1));
  5068. plugin.apply(plugin, args);
  5069. return this;
  5070. };
  5071. /** internal
  5072. * MarkdownIt.parse(src, env) -> Array
  5073. * - src (String): source string
  5074. * - env (Object): environment sandbox
  5075. *
  5076. * Parse input string and return list of block tokens (special token type
  5077. * "inline" will contain list of inline tokens). You should not call this
  5078. * method directly, until you write custom renderer (for example, to produce
  5079. * AST).
  5080. *
  5081. * `env` is used to pass data between "distributed" rules and return additional
  5082. * metadata like reference info, needed for the renderer. It also can be used to
  5083. * inject data in specific cases. Usually, you will be ok to pass `{}`,
  5084. * and then pass updated object to renderer.
  5085. **/
  5086. MarkdownIt.prototype.parse = function (src, env) {
  5087. if (typeof src !== 'string') {
  5088. throw new Error('Input data should be a String');
  5089. }
  5090. const state = new this.core.State(src, this, env);
  5091. this.core.process(state);
  5092. return state.tokens;
  5093. };
  5094. /**
  5095. * MarkdownIt.render(src [, env]) -> String
  5096. * - src (String): source string
  5097. * - env (Object): environment sandbox
  5098. *
  5099. * Render markdown string into html. It does all magic for you :).
  5100. *
  5101. * `env` can be used to inject additional metadata (`{}` by default).
  5102. * But you will not need it with high probability. See also comment
  5103. * in [[MarkdownIt.parse]].
  5104. **/
  5105. MarkdownIt.prototype.render = function (src, env) {
  5106. env = env || {};
  5107. return this.renderer.render(this.parse(src, env), this.options, env);
  5108. };
  5109. /** internal
  5110. * MarkdownIt.parseInline(src, env) -> Array
  5111. * - src (String): source string
  5112. * - env (Object): environment sandbox
  5113. *
  5114. * The same as [[MarkdownIt.parse]] but skip all block rules. It returns the
  5115. * block tokens list with the single `inline` element, containing parsed inline
  5116. * tokens in `children` property. Also updates `env` object.
  5117. **/
  5118. MarkdownIt.prototype.parseInline = function (src, env) {
  5119. const state = new this.core.State(src, this, env);
  5120. state.inlineMode = true;
  5121. this.core.process(state);
  5122. return state.tokens;
  5123. };
  5124. /**
  5125. * MarkdownIt.renderInline(src [, env]) -> String
  5126. * - src (String): source string
  5127. * - env (Object): environment sandbox
  5128. *
  5129. * Similar to [[MarkdownIt.render]] but for single paragraph content. Result
  5130. * will NOT be wrapped into `<p>` tags.
  5131. **/
  5132. MarkdownIt.prototype.renderInline = function (src, env) {
  5133. env = env || {};
  5134. return this.renderer.render(this.parseInline(src, env), this.options, env);
  5135. };
  5136. module.exports = MarkdownIt;