PHP and Javascript implementations of a simple markdown parser
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

markdown.js 94KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653
  1. class MDTokenType {
  2. static Text = new MDTokenType('Text');
  3. static Whitespace = new MDTokenType('Whitespace');
  4. static Underscore = new MDTokenType('Underscore');
  5. static Asterisk = new MDTokenType('Asterisk');
  6. static Slash = new MDTokenType('Slash');
  7. static Tilde = new MDTokenType('Tilde');
  8. static Bang = new MDTokenType('Bang');
  9. static Backtick = new MDTokenType('Backtick');
  10. static Label = new MDTokenType('Label'); // content=label
  11. static URL = new MDTokenType('URL'); // content=URL, extra=title
  12. static Email = new MDTokenType('Email'); // content=email address, extra=title
  13. static SimpleLink = new MDTokenType('SimpleLink'); // content=URL
  14. static SimpleEmail = new MDTokenType('SimpleEmail'); // content=email address
  15. static Footnote = new MDTokenType('Footnote'); // content=symbol
  16. static Modifier = new MDTokenType('Modifier'); // content
  17. static HTMLTag = new MDTokenType('HTMLTag'); // content=tag string, tag=MDHTMLTag
  18. static META_AnyNonWhitespace = new MDTokenType('METAAnyNonWhitespace');
  19. static META_OptionalWhitespace = new MDTokenType('METAOptionalWhitespace');
  20. /** @type {string} */
  21. name;
  22. /**
  23. * @param {string} name
  24. */
  25. constructor(name) {
  26. this.name = name;
  27. }
  28. toString() {
  29. return `${this.constructor.name}.${this.name}`;
  30. }
  31. equals(other) {
  32. return (other instanceof MDTokenType) && other.name == this.name;
  33. }
  34. }
  35. class MDToken {
  36. /**
  37. * The original token string.
  38. * @type {string}
  39. */
  40. original;
  41. /** @type {MDTokenType} */
  42. type;
  43. /** @type {string|null} */
  44. content;
  45. /** @type {string|null} */
  46. extra;
  47. /** @type {MDHTMLTag|null} */
  48. tag;
  49. /** @type {MDTagModifier|null} */
  50. modifier;
  51. /**
  52. * @param {string} original
  53. * @param {MDTokenType} type
  54. * @param {string|MDTagModifier|null} content
  55. * @param {string|null} extra
  56. * @param {MDHTMLTag|null} tag
  57. */
  58. constructor(original, type, content=null, extra=null, tag=null) {
  59. this.original = original;
  60. this.type = type;
  61. if (content instanceof MDTagModifier) {
  62. this.content = null;
  63. this.modifier = content;
  64. } else {
  65. this.content = content;
  66. this.modifier = null;
  67. }
  68. this.extra = extra;
  69. this.tag = tag;
  70. }
  71. toString() {
  72. return `(${this.constructor.name} type=${this.type.toString()} content=${this.content})`;
  73. }
  74. /**
  75. * Searches an array of MDToken for the given pattern of MDTokenTypes.
  76. * If found, returns an object with the given keys.
  77. * - `tokens: MDToken[]` - the subarray of `tokensToSearch` that match the pattern
  78. * - `index: number` - index into `tokensToSearch` of first matching token
  79. *
  80. * @param {MDToken[]|MDSpan[]} tokensToSearch
  81. * @param {MDTokenType[]} pattern
  82. * @param {number} startIndex
  83. * @returns {object|null} match
  84. */
  85. static findFirstTokens(tokensToSearch, pattern, startIndex=0) {
  86. var matched = [];
  87. for (var t = startIndex; t < tokensToSearch.length; t++) {
  88. var matchedAll = true;
  89. matched = [];
  90. var patternOffset = 0;
  91. for (var p = 0; p < pattern.length; p++) {
  92. var t0 = t + p + patternOffset;
  93. if (t0 >= tokensToSearch.length) return null;
  94. let token = tokensToSearch[t0];
  95. let elem = pattern[p];
  96. if (elem == MDTokenType.META_OptionalWhitespace) {
  97. if (token instanceof MDToken && token.type == MDTokenType.Whitespace) {
  98. matched.push(token);
  99. } else {
  100. patternOffset--;
  101. }
  102. } else if (elem == MDTokenType.META_AnyNonWhitespace) {
  103. if (token instanceof MDToken && token.type == MDTokenType.Whitespace) {
  104. matchedAll = false;
  105. break;
  106. }
  107. matched.push(token);
  108. } else {
  109. if (!(token instanceof MDToken) || token.type != elem) {
  110. matchedAll = false;
  111. break;
  112. }
  113. matched.push(token);
  114. }
  115. }
  116. if (matchedAll) {
  117. return {
  118. 'tokens': matched,
  119. 'index': t,
  120. };
  121. }
  122. }
  123. return null;
  124. }
  125. /**
  126. * Searches an array of MDToken for a given starting pattern and ending
  127. * pattern and returns match info about both and the tokens in between.
  128. *
  129. * If `contentValidator` is specified, it will be called with the content
  130. * tokens of a potential match. If the validator returns `true`, the result
  131. * will be accepted and returned by this method. If the validator returns
  132. * `false`, this method will keep looking for another matching pair. If no
  133. * validator is given the first match will be returned regardless of content.
  134. *
  135. * If a match is found, returns an object with the given keys:
  136. * - `startTokens: MDToken[]` - tokens that matched `startPattern`
  137. * - `contentTokens: MDToken[]` - tokens between the start and end pattern. May be an empty array.
  138. * - `endTokens: MDToken[]` - tokens that matched `endPattern`
  139. * - `startIndex: number` - index into `tokensToSearch` where `startPattern` begins
  140. * - `contentIndex: number` - index into `tokensToSearch` of the first token that is between the start and end patterns
  141. * - `endIndex: number` - index into `tokensToSearch` where `endPattern` begins
  142. * - `totalLength: number` - total number of matched tokens
  143. *
  144. * @param {MDToken[]} tokensToSearch - array of `MDToken` to search in
  145. * @param {MDTokenType[]} startPattern - array of `MDTokenType` to find first
  146. * @param {MDTokenType[]} endPattern - array of `MDTokenType` to find positioned after `startPattern`
  147. * @param {function|null} contentValidator - optional validator function. If provided, will be passed an array of inner `MDToken`, and the function can return `true` to accept the contents or `false` to keep searching
  148. * @param {number} startIndex - token index where searching should begin
  149. * @returns {object|null} match object
  150. */
  151. static findPairedTokens(tokensToSearch, startPattern, endPattern, contentValidator=null, startIndex=0) {
  152. for (var s = startIndex; s < tokensToSearch.length; s++) {
  153. var startMatch = this.findFirstTokens(tokensToSearch, startPattern, s);
  154. if (startMatch === null) return null;
  155. var endStart = startMatch.index + startMatch.tokens.length;
  156. while (endStart < tokensToSearch.length) {
  157. var endMatch = this.findFirstTokens(tokensToSearch, endPattern, endStart);
  158. if (endMatch === null) break;
  159. var contents = tokensToSearch.slice(startMatch.index + startMatch.tokens.length, endMatch.index);
  160. if (contents.length > 0 && (contentValidator === null || contentValidator(contents))) {
  161. return {
  162. 'startTokens': startMatch.tokens,
  163. 'contentTokens': contents,
  164. 'endTokens': endMatch.tokens,
  165. 'startIndex': startMatch.index,
  166. 'contentIndex': startMatch.index + startMatch.tokens.length,
  167. 'endIndex': endMatch.index,
  168. 'totalLength': endMatch.index + endMatch.tokens.length - startMatch.index,
  169. };
  170. } else {
  171. // Contents rejected. Try next end match.
  172. endStart = endMatch.index + 1;
  173. }
  174. }
  175. // No end matches. Increment start match.
  176. s = startMatch.index;
  177. }
  178. return null;
  179. }
  180. equals(other) {
  181. if (!(other instanceof MDToken)) return false;
  182. if (other.original !== this.original) return false;
  183. if (!other.type.equals(this.type)) return false;
  184. if (other.content !== this.content) return false;
  185. if (other.extra !== this.extra) return false;
  186. if (!MDUtils.equal(other.tag, this.tag)) return false;
  187. if (!MDUtils.equals(other.modifier, this.modifier)) return false;
  188. return true
  189. }
  190. }
  191. class MDUtils {
  192. // Modified from https://urlregex.com/ to remove capture groups. Matches fully qualified URLs only.
  193. static baseURLRegex = /(?:(?:(?:[a-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[a-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[a-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i;
  194. // Modified from https://emailregex.com/ to remove capture groups.
  195. static baseEmailRegex = /(?:(?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@(?:(?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(?:(?:[a-z\-0-9]+\.)+[a-z]{2,}))/i;
  196. /**
  197. * Escapes special HTML characters.
  198. *
  199. * @param {string} str - string to escape
  200. * @param {boolean} encodeNewlinesAsBreaks - whether to convert newline characters to `<br>` tags
  201. * @returns {string} escaped HTML
  202. */
  203. static escapeHTML(str, encodeNewlinesAsBreaks=false) {
  204. if (typeof str !== 'string') return '';
  205. var html = str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  206. if (encodeNewlinesAsBreaks) {
  207. html = html.replace(/\n/g, "<br>\n");
  208. }
  209. return html;
  210. }
  211. /**
  212. * Encodes characters as HTML numeric entities to make it marginally more
  213. * difficult for web scrapers to grab sensitive info.
  214. *
  215. * @param {string} text - text to escape
  216. * @returns {string} escaped HTML
  217. */
  218. static escapeObfuscated(text) {
  219. var html = '';
  220. for (var p = 0; p < text.length; p++) {
  221. const cp = text.codePointAt(p);
  222. html += `&#${cp};`;
  223. }
  224. return html;
  225. }
  226. /**
  227. * Strips one or more leading indents from a line or lines of markdown. An
  228. * indent is defined as 4 spaces or one tab. Incomplete indents (i.e. 1-3
  229. * spaces) are treated like one indent level.
  230. *
  231. * @param {string|string[]} line - string or strings to strip
  232. * @param {number} levels - how many indent levels to strip
  233. * @returns {string|string[]} stripped lines
  234. */
  235. static stripIndent(line, levels=1) {
  236. const regex = new RegExp(`^(?: {1,4}|\t){${levels}}`);
  237. return (line instanceof Array) ? line.map((l) => l.replace(regex, '')) : line.replace(regex, '');
  238. }
  239. /**
  240. * Returns a copy of an array without any whitespace-only lines at the end.
  241. *
  242. * @param {String[]} lines - text lines
  243. * @returns {String[]} - text lines without trailing blank lines
  244. */
  245. static withoutTrailingBlankLines(lines) {
  246. var stripped = lines.slice();
  247. while (stripped.length > 0 && stripped[stripped.length - 1].trim().length == 0) {
  248. stripped.pop();
  249. }
  250. return stripped;
  251. }
  252. /**
  253. * Tests if an array of lines contains at least one blank. A blank line
  254. * can contain whitespace.
  255. *
  256. * @param {String[]} lines
  257. * @returns {boolean} whether `lines` contains any whitespace-only lines
  258. */
  259. static containsBlankLine(lines) {
  260. for (const line of lines) {
  261. if (line.trim().length == 0) return true;
  262. }
  263. return false;
  264. }
  265. /**
  266. * Counts the number of indent levels in a line of text. Partial indents
  267. * (1 to 3 spaces) are counted as one indent level unless `fullIndentsOnly`
  268. * is `true`.
  269. *
  270. * @param {string} line - line of markdown
  271. * @param {boolean} fullIndentsOnly - whether to only count full indent levels (4 spaces or a tab)
  272. * @returns {number} number of indent levels found
  273. */
  274. static countIndents(line, fullIndentsOnly=false) {
  275. // normalize indents to tabs
  276. return line.replace(fullIndentsOnly
  277. ? /(?: {4}|\t)/g
  278. : /(?: {1,4}|\t)/g,
  279. "\t")
  280. // remove content after indent
  281. .replace(/^(\t*)(.*?)$/, '$1')
  282. // count tabs
  283. .length;
  284. }
  285. /**
  286. * Attempts to parse a label from the beginning of `line`. A label is of the
  287. * form `[content]`. If found, returns an array with element 0 being the
  288. * entire label and element 1 being the content of the label.
  289. *
  290. * @param {string} line
  291. * @returns {string[]|null} match groups or null if not found
  292. */
  293. static tokenizeLabel(line) {
  294. if (!line.startsWith('[')) return null;
  295. var parenCount = 0;
  296. var bracketCount = 0;
  297. for (var p = 1; p < line.length; p++) {
  298. let ch = line.substring(p, p + 1);
  299. if (ch == '\\') {
  300. p++;
  301. } else if (ch == '(') {
  302. parenCount++;
  303. } else if (ch == ')') {
  304. parenCount--;
  305. if (parenCount < 0) return null;
  306. } else if (ch == '[') {
  307. bracketCount++;
  308. } else if (ch == ']') {
  309. if (bracketCount > 0) {
  310. bracketCount--;
  311. } else {
  312. return [ line.substring(0, p + 1), line.substring(1, p) ];
  313. }
  314. }
  315. }
  316. return null;
  317. }
  318. static #urlWithTitleRegex = /^\((\S+?)\s+"(.*?)"\)/i; // 1=URL, 2=title
  319. static #urlRegex = /^\((\S+?)\)/i; // 1=URL
  320. /**
  321. * Attempts to parse a URL from the beginning of `line`. A URL is of the
  322. * form `(url)` or `(url "title")`. If found, returns an array with element
  323. * 0 being the entire URL token, 1 is the URL, 2 is the optional title.
  324. *
  325. * @param {string} line
  326. * @returns {string[]} token tuple
  327. */
  328. static tokenizeURL(line) {
  329. var groups;
  330. if (groups = this.#urlWithTitleRegex.exec(line)) {
  331. if (this.tokenizeEmail(line)) return null; // make sure it's not better described as an email address
  332. return groups;
  333. }
  334. if (groups = this.#urlRegex.exec(line)) {
  335. if (this.tokenizeEmail(line)) return null;
  336. return [...groups, null];
  337. }
  338. return null;
  339. }
  340. static #emailWithTitleRegex = new RegExp("^\\(\\s*(" + MDUtils.baseEmailRegex.source + ")\\s+\"(.*?)\"\\s*\\)", "i"); // 1=email, 2=title
  341. static #emailRegex = new RegExp("^\\(\\s*(" + MDUtils.baseEmailRegex.source + ")\\s*\\)", "i"); // 1=email
  342. /**
  343. * Attempts to parse an email address from the beginning of `line`. An
  344. * email address is of the form `(user@example.com)` or `(user@example.com "link title")`.
  345. * If found, returns an array with element 0 being the entire token, 1 is the
  346. * email address, and 2 is the optional link title.
  347. *
  348. * @param {string} line
  349. * @returns {string[]} token tuple
  350. */
  351. static tokenizeEmail(line) {
  352. var groups;
  353. if (groups = this.#emailWithTitleRegex.exec(line)) {
  354. return groups;
  355. }
  356. if (groups = this.#emailRegex.exec(line)) {
  357. return [...groups, null];
  358. }
  359. return null;
  360. }
  361. /**
  362. * Describes the type of a variable for debugging.
  363. *
  364. * @param {any} value - value
  365. * @returns {String} description of type
  366. */
  367. static typename(value) {
  368. if (value === null) return 'null';
  369. if (value instanceof Object) {
  370. return value.constructor.name;
  371. }
  372. return typeof value;
  373. }
  374. static #equalArrays(a, b) {
  375. if (a === b) return true;
  376. if (!(a instanceof Array) || !(b instanceof Array)) return false;
  377. if (a == null || b == null) return false;
  378. if (a.length != b.length) return false;
  379. for (var i = 0; i < a.length; i++) {
  380. if (!this.equal(a[i], b[i])) return false;
  381. }
  382. return true;
  383. }
  384. static #equalObjects(a, b) {
  385. if (a === b) return true;
  386. if (!(a instanceof Object) || !(b instanceof Object)) return false;
  387. if (a == null || b == null) return false;
  388. if (a.equals !== undefined) {
  389. return a.equals(b);
  390. }
  391. for (const key of Object.keys(a)) {
  392. if (!this.equal(a[key], b[key])) return false;
  393. }
  394. for (const key of Object.keys(b)) {
  395. if (!this.equal(a[key], b[key])) return false;
  396. }
  397. return true;
  398. }
  399. /**
  400. * Tests for equality on lots of different kinds of values including objects
  401. * and arrays. Will use `.equals` on objects that implement it.
  402. *
  403. * @param {any} a
  404. * @param {any} b
  405. * @returns {boolean}
  406. */
  407. static equal(a, b, floatDifferencePercent=0.0) {
  408. if (a instanceof Array && b instanceof Array) {
  409. return this.#equalArrays(a, b);
  410. }
  411. if (a instanceof Object && b instanceof Object) {
  412. return this.#equalObjects(a, b);
  413. }
  414. if (typeof a == 'number' && typeof b == 'number') {
  415. if (a === b) return true;
  416. const delta = b - a;
  417. const ratio = delta / a;
  418. return Math.abs(ratio) <= floatDifferencePercent;
  419. }
  420. return a == b;
  421. }
  422. /**
  423. * @param {string} text
  424. */
  425. static escapeRegex(text) {
  426. // Partially following escaping scheme from not-yet-widely-supported RegExp.escape.
  427. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape
  428. const escapeHex = function(ch) {
  429. const codepoint = ch.codePointAt(0);
  430. const s = '00' + codepoint.toString(16);
  431. return `\\x${s.substring(s.length - 2)}`;
  432. }
  433. var escaped = '';
  434. const l = text.length;
  435. for (var i = 0; i < l; i++) {
  436. const ch = text.substring(i, i + 1);
  437. if (i == 0 && /[a-zA-Z0-9]/.exec(ch)) {
  438. escaped += escapeHex(ch);
  439. } else if ("^$\\.*+?()[]{}|/".indexOf(ch) >= 0) {
  440. escaped += `\\${ch}`;
  441. } else if (",-=<>#&!%:;@~'`\"".indexOf(ch) >= 0) {
  442. escaped += escapeHex(ch);
  443. } else if (ch == '\f') {
  444. escaped += "\\f";
  445. } else if (ch == '\n') {
  446. escaped += "\\n";
  447. } else if (ch == '\r') {
  448. escaped += "\\r";
  449. } else if (ch == '\t') {
  450. escaped += "\\t";
  451. } else if (ch == '\v') {
  452. escaped += "\\v";
  453. } else {
  454. escaped += ch;
  455. }
  456. }
  457. return escaped;
  458. }
  459. }
  460. /**
  461. * Parsing and rendering state
  462. */
  463. class MDState {
  464. /**
  465. * Ascends the parent chain to the root `MDState` instance. This should be
  466. * used when referencing most stored fields except `lines` and `p`.
  467. *
  468. * @type {MDState}
  469. */
  470. get root() { return this.#parent ? this.#parent.root : this; }
  471. /**
  472. * Lines of the markdown document. The current line index is pointed to by `p`.
  473. *
  474. * @returns {string[]} markdown lines
  475. */
  476. get lines() { return this.#lines; }
  477. /**
  478. * The current line in `lines`.
  479. *
  480. * @returns {string|null} current line or `null` if out of content
  481. */
  482. get currentLine() { return (this.p < this.#lines.length) ? this.#lines[this.p] : null; }
  483. /**
  484. * Current line pointer into array `lines`.
  485. *
  486. * @type {number} line pointer
  487. */
  488. p = 0;
  489. /** @type {string[]} */
  490. #lines = [];
  491. /** @type {MDState|null} */
  492. #parent = null;
  493. /**
  494. * Block readers sorted by `priority`.
  495. * @type {MDBlockReader[]} readers
  496. */
  497. #blockReadersByPriority = [];
  498. /**
  499. * Tuples of priority:number and MDInlineReader sorted by `tokenizePriority`.
  500. * @type {Array}
  501. */
  502. #inlineReadersByTokenPriority = [];
  503. /**
  504. * Tuples of priority:number and MDInlineReader sorted by `substitutePriority`.
  505. * @type {Array}
  506. */
  507. #inlineReadersBySubstitutePriority = [];
  508. static #textWhitespaceRegex = /^(\s*)(?:(\S|\S.*\S)(\s*?))?$/; // 1=leading WS, 2=text, 3=trailing WS
  509. /**
  510. * @param {string[]} lines
  511. */
  512. constructor(lines, blockReadersByPriority=null,
  513. inlineReadersByTokenPriority=null, inlineReadersBySubstitutePriority=null) {
  514. this.#lines = lines;
  515. this.#blockReadersByPriority = blockReadersByPriority;
  516. this.#inlineReadersByTokenPriority = inlineReadersByTokenPriority;
  517. this.#inlineReadersBySubstitutePriority = inlineReadersBySubstitutePriority;
  518. }
  519. /**
  520. * Creates a copy of this state with new lines. Useful for parsing nested
  521. * content.
  522. *
  523. * @param {string[]} lines
  524. * @returns {MDState} copied sub-state
  525. */
  526. copy(lines) {
  527. let cp = new MDState(lines);
  528. cp.#parent = this;
  529. return cp;
  530. }
  531. /**
  532. * Tests if there are at least `minCount` lines available to read. If `p`
  533. * is not provided it will be relative to `this.p`.
  534. *
  535. * @param {number} minCount - minimum number of lines
  536. * @param {number|null} p - line pointer, or `null` to use `this.p`
  537. * @returns {boolean} whether at least the given number of lines is available
  538. */
  539. hasLines(minCount, p=null) {
  540. let relativeTo = (p === null) ? this.p : p;
  541. return relativeTo + minCount <= this.lines.length;
  542. }
  543. /**
  544. * Reads and returns an array of blocks from the current line pointer.
  545. *
  546. * @returns {MDBlock[]} parsed blocks
  547. */
  548. readBlocks() {
  549. var blocks = [];
  550. while (this.hasLines(1)) {
  551. let block = this.#readNextBlock();
  552. if (block) {
  553. blocks.push(block);
  554. } else {
  555. break;
  556. }
  557. }
  558. return blocks;
  559. }
  560. /**
  561. * Creates a simple `MDInlineBlock` if no other registered blocks match.
  562. *
  563. * @returns {MDInlineBlock|null} fallback block
  564. */
  565. #readFallbackBlock() {
  566. if (this.p >= this.lines.length) return null;
  567. const lines = MDUtils.withoutTrailingBlankLines(this.lines.slice(this.p));
  568. if (lines.length == 0) return null;
  569. this.p = this.lines.length;
  570. return new MDInlineBlock(this.inlineMarkdownToSpans(lines.join("\n")));
  571. }
  572. /**
  573. * Attempts to read one block from the current line pointer. The pointer
  574. * will be positioned just after the end of the block.
  575. *
  576. * @param {MDState} state
  577. * @returns {MDBlock}
  578. */
  579. #readNextBlock() {
  580. while (this.hasLines(1) && this.lines[this.p].trim().length == 0) {
  581. this.p++;
  582. }
  583. if (!this.hasLines(1)) return null;
  584. for (const reader of this.root.#blockReadersByPriority) {
  585. const startP = this.p;
  586. const block = reader.readBlock(this);
  587. if (block) {
  588. if (this.p == startP) {
  589. throw new Error(`${reader.constructor.name} returned an ${block.constructor.name} without incrementing MDState.p. This could lead to an infinite loop.`);
  590. }
  591. return block;
  592. }
  593. }
  594. const fallback = this.#readFallbackBlock();
  595. return fallback;
  596. }
  597. /**
  598. * @param {string} line
  599. * @returns {MDToken[]}
  600. */
  601. #inlineMarkdownToTokens(line) {
  602. if (this.#parent) return this.#parent.#inlineMarkdownToTokens(line);
  603. var tokens = [];
  604. var text = '';
  605. var expectLiteral = false;
  606. /**
  607. * Flushes accumulated content in `text` to `tokens`.
  608. */
  609. const endText = function() {
  610. if (text.length == 0) return;
  611. const textGroups = MDState.#textWhitespaceRegex.exec(text);
  612. if (textGroups !== null) {
  613. if (textGroups[1].length > 0) {
  614. tokens.push(new MDToken(textGroups[1], MDTokenType.Whitespace, textGroups[1]));
  615. }
  616. if (textGroups[2] !== undefined && textGroups[2].length > 0) {
  617. tokens.push(new MDToken(textGroups[2], MDTokenType.Text, textGroups[2]));
  618. }
  619. if (textGroups[3] !== undefined && textGroups[3].length > 0) {
  620. tokens.push(new MDToken(textGroups[3], MDTokenType.Whitespace, textGroups[3]));
  621. }
  622. } else {
  623. tokens.push(new MDToken(text, MDTokenType.Text, text));
  624. }
  625. text = '';
  626. }
  627. for (var p = 0; p < line.length; p++) {
  628. const ch = line.substring(p, p + 1);
  629. const remainder = line.substring(p);
  630. if (expectLiteral) {
  631. text += ch;
  632. expectLiteral = false;
  633. continue;
  634. }
  635. if (ch == '\\') {
  636. expectLiteral = true;
  637. continue;
  638. }
  639. var found = false;
  640. for (const readerTuple of this.root.#inlineReadersByTokenPriority) {
  641. /** @type {number} */
  642. const priority = readerTuple[0];
  643. /** @type {MDInlineReader} */
  644. const reader = readerTuple[1];
  645. const token = reader.readFirstToken(this, priority, remainder);
  646. if (token === null) continue;
  647. endText();
  648. tokens.push(token);
  649. if (token.original == null || token.original.length == 0) {
  650. throw new Error(`${reader.constructor.name} returned a token with an empty .original. This would cause an infinite loop.`);
  651. }
  652. p += token.original.length - 1;
  653. found = true;
  654. break;
  655. }
  656. if (!found) {
  657. text += ch;
  658. }
  659. }
  660. endText();
  661. return tokens;
  662. }
  663. /**
  664. * Converts a line of markdown to an MDSpan.
  665. *
  666. * @param {string|string[]} line
  667. * @returns {MDSpan}
  668. */
  669. inlineMarkdownToSpan(line) {
  670. let spans = this.inlineMarkdownToSpans(line);
  671. return (spans.length == 1) ? spans[0] : new MDMultiSpan(spans);
  672. }
  673. /**
  674. * Converts a line of markdown to an array of MDSpan.
  675. *
  676. * @param {string|string[]} line
  677. * @returns {MDSpan[]}
  678. */
  679. inlineMarkdownToSpans(line) {
  680. var tokens = this.#inlineMarkdownToTokens((line instanceof Array) ? line.join('\n') : line);
  681. return this.tokensToSpans(tokens);
  682. }
  683. /**
  684. * Converts a mixed array of `MDToken` and `MDSpan` elements into an array
  685. * of only `MDSpan`.
  686. *
  687. * @param {MDToken[]|MDSpan[]} tokens
  688. * @returns {MDSpan[]}
  689. */
  690. tokensToSpans(tokens) {
  691. var spans = tokens.slice();
  692. // Perform repeated substitutions, converting sequences of tokens into
  693. // spans, until no more substitutions can be made.
  694. var anyChanges = false;
  695. do {
  696. anyChanges = false;
  697. for (const readerTuple of this.root.#inlineReadersBySubstitutePriority) {
  698. /** @type {number} */
  699. const priority = readerTuple[0];
  700. /** @type {MDInlineReader} */
  701. const reader = readerTuple[1];
  702. const changed = reader.substituteTokens(this, priority, spans);
  703. if (!changed) continue;
  704. anyChanges = true;
  705. break;
  706. }
  707. } while (anyChanges);
  708. // Convert any remaining tokens to spans, apply CSS modifiers.
  709. var lastSpan = null;
  710. spans = spans.map(function(span) {
  711. if (span instanceof MDToken) {
  712. if (span.type == MDTokenType.Modifier && lastSpan) {
  713. span.modifier.applyTo(lastSpan);
  714. lastSpan = null;
  715. return new MDTextSpan('');
  716. }
  717. lastSpan = null;
  718. return new MDTextSpan(span.original);
  719. } else if (span instanceof MDSpan) {
  720. lastSpan = (span instanceof MDTextSpan) ? null : span;
  721. return span;
  722. } else {
  723. throw new Error(`Unexpected span type ${span.constructor.name}`);
  724. }
  725. });
  726. return spans;
  727. }
  728. }
  729. // -- Block readers ---------------------------------------------------------
  730. /**
  731. * Base class for reading from an array of markdown lines and finding a block
  732. * of a given type. Readers are checked in `priority` order and `readBlock` is
  733. * called to see the reader implementation recognizes a particular kind of block
  734. * at the given line pointer, returning that block if so or null if not.
  735. *
  736. * Inline markdown is processed in a separate stage by `MDInlineReader`.
  737. */
  738. class MDBlockReader {
  739. /** @type {number} */
  740. #priority;
  741. /**
  742. * A unitless relative priority value that determines which readers are
  743. * tried first. Lower values are tried first. In the range of 0.0 to 100.0.
  744. * @type {number} priority
  745. */
  746. get priority() { return this.#priority; }
  747. /**
  748. * Creates a block reader.
  749. *
  750. * When overriding the constructor, it is suggested to allow the caller to
  751. * specify an optional custom priority value, falling back to a reasonable
  752. * default if not specified.
  753. *
  754. * @param {number} priority
  755. */
  756. constructor(priority) {
  757. this.#priority = priority;
  758. }
  759. /**
  760. * Attempts to read a block of this type from the given read state. If
  761. * successful, the state's line pointer should be incremented to the line
  762. * just after the last line of the block and the read block should be
  763. * returned.
  764. *
  765. * @param {MDState} state - read state
  766. * @returns {MDBlock|null} the read block
  767. */
  768. readBlock(state) {
  769. throw Error(`Abstract readBlock must be overridden in ${this.constructor.name}`);
  770. }
  771. /**
  772. * Called before processing a document.
  773. *
  774. * @param {MDState} state
  775. */
  776. preProcess(state) {
  777. // no op
  778. }
  779. /**
  780. * Called after the full document has been generated for optional
  781. * post-processing.
  782. *
  783. * @param {MDState} state
  784. * @param {MDBlock[]} blocks - top-level document block list
  785. */
  786. postProcess(state, blocks) {
  787. // no op
  788. }
  789. }
  790. /**
  791. * Reads markdown blocks for headers denoted with the underline syntax.
  792. *
  793. * Example:
  794. *
  795. * > ```markdown
  796. * > Header 1
  797. * > ========
  798. * > ```
  799. */
  800. class MDUnderlinedHeaderBlockReader extends MDBlockReader {
  801. constructor(priority=0.0) {
  802. super(priority);
  803. }
  804. /**
  805. * @param {MDState} state
  806. */
  807. readBlock(state) {
  808. var p = state.p;
  809. if (!state.hasLines(2)) return null;
  810. var modifier;
  811. let contentLine = state.lines[p++].trim();
  812. [contentLine, modifier] = MDTagModifier.fromLine(contentLine);
  813. let underLine = state.lines[p++].trim();
  814. if (contentLine == '') return null;
  815. if (/^=+$/.exec(underLine)) {
  816. state.p = p;
  817. let block = new MDHeaderBlock(1, state.inlineMarkdownToSpan(contentLine));
  818. if (modifier) modifier.applyTo(block);
  819. return block;
  820. }
  821. if (/^\-+$/.exec(underLine)) {
  822. state.p = p;
  823. let block = new MDHeaderBlock(2, state.inlineMarkdownToSpan(contentLine));
  824. if (modifier) modifier.applyTo(block);
  825. return block;
  826. }
  827. return null;
  828. }
  829. }
  830. /**
  831. * Reads markdown blocks for headers denoted with hash marks. Header levels 1 to
  832. * 6 are supported.
  833. *
  834. * Examples:
  835. *
  836. * > ```markdown
  837. * > # Header 1
  838. * >
  839. * > ## Header 2
  840. * >
  841. * > # Enclosing Hashes Are Optional #
  842. * >
  843. * > ## Trailing Hashes Don't Have to Match in Number ####
  844. * > ```
  845. */
  846. class MDHashHeaderBlockReader extends MDBlockReader {
  847. static #hashHeaderRegex = /^(#{1,6})\s*([^#].*?)\s*\#*\s*$/; // 1=hashes, 2=content
  848. constructor(priority=5.0) {
  849. super(priority);
  850. }
  851. readBlock(state) {
  852. var p = state.p;
  853. let line = state.lines[p++];
  854. var modifier;
  855. [line, modifier] = MDTagModifier.fromLine(line);
  856. var groups = MDHashHeaderBlockReader.#hashHeaderRegex.exec(line);
  857. if (groups === null) return null;
  858. state.p = p;
  859. const level = groups[1].length;
  860. const content = groups[2];
  861. let block = new MDHeaderBlock(level, new MDInlineBlock(state.inlineMarkdownToSpan(content)));
  862. if (modifier) modifier.applyTo(block);
  863. return block;
  864. }
  865. }
  866. /**
  867. * Reads markdown blocks for blockquoted text.
  868. *
  869. * Example:
  870. *
  871. * > ```markdown
  872. * > > Blockquoted text
  873. * > ```
  874. */
  875. class MDBlockQuoteBlockReader extends MDBlockReader {
  876. constructor(priority=10.0) {
  877. super(priority);
  878. }
  879. /**
  880. * @param {MDState} state
  881. */
  882. readBlock(state) {
  883. var blockquoteLines = [];
  884. var p = state.p;
  885. while (p < state.lines.length) {
  886. let line = state.lines[p++];
  887. if (line.startsWith(">")) {
  888. blockquoteLines.push(line);
  889. } else {
  890. break;
  891. }
  892. }
  893. if (blockquoteLines.length > 0) {
  894. let contentLines = blockquoteLines.map(function(line) {
  895. return line.substring(1).replace(/^ {0,3}\t?/, '');
  896. });
  897. let substate = state.copy(contentLines);
  898. let quotedBlocks = substate.readBlocks();
  899. state.p = p;
  900. return new MDBlockquoteBlock(quotedBlocks);
  901. }
  902. return null;
  903. }
  904. }
  905. /**
  906. * Abstract base class for ordered and unordered lists.
  907. */
  908. class MDBaseListBlockReader extends MDBlockReader {
  909. constructor(priority) {
  910. super(priority);
  911. }
  912. #readItemLines(state, firstLineStartPos) {
  913. var p = state.p;
  914. var lines = [];
  915. var seenBlankLine = false;
  916. var stripTrailingBlankLines = true;
  917. while (state.hasLines(1, p)) {
  918. const isFirstLine = p == state.p;
  919. var line = state.lines[p++];
  920. if (isFirstLine) {
  921. line = line.substring(firstLineStartPos);
  922. }
  923. if (/^(?:\*|\+|\-|\d+\.)\s+/.exec(line)) {
  924. // Found next list item
  925. stripTrailingBlankLines = false; // because this signals extra spacing intended
  926. break;
  927. }
  928. const isBlankLine = line.trim().length == 0;
  929. const isIndented = /^\s+\S/.exec(line) !== null;
  930. if (isBlankLine) {
  931. seenBlankLine = true;
  932. } else if (!isIndented && seenBlankLine) {
  933. // Post-list content
  934. break;
  935. }
  936. lines.push(line);
  937. }
  938. lines = MDUtils.withoutTrailingBlankLines(lines);
  939. return MDUtils.stripIndent(lines);
  940. }
  941. /**
  942. * @param {MDState} state
  943. * @param {number} firstLineStart
  944. * @return {MDBlock}
  945. */
  946. readListItemContent(state, firstLineStartPos) {
  947. const itemLines = this.#readItemLines(state, firstLineStartPos);
  948. state.p += Math.max(itemLines.length, 1);
  949. if (itemLines.length == 1) {
  950. return new MDInlineBlock(state.inlineMarkdownToSpans(itemLines[0]));
  951. }
  952. const hasBlankLines = itemLines.filter((line) => line.trim().length == 0).length > 0;
  953. if (hasBlankLines) {
  954. const substate = state.copy(itemLines);
  955. const blocks = substate.readBlocks();
  956. return (blocks.length == 1) ? blocks[0] : new MDMultiBlock(blocks);
  957. }
  958. // Multiline content with no blank lines. Search for new block
  959. // boundaries without the benefit of a blank line to demarcate it.
  960. for (var p = 1; p < itemLines.length; p++) {
  961. const line = itemLines[p];
  962. if (/^(?:\*|\-|\+|\d+\.)\s+/.exec(line)) {
  963. // Nested list found
  964. const firstBlock = new MDInlineBlock(state.inlineMarkdownToSpans(itemLines.slice(0, p).join("\n")));
  965. const substate = state.copy(itemLines.slice(p));
  966. const blocks = substate.readBlocks();
  967. return new MDMultiBlock([ firstBlock, ...blocks ]);
  968. }
  969. }
  970. // Ok, give up and just do a standard block read
  971. {
  972. const substate = state.copy(itemLines);
  973. const blocks = substate.readBlocks();
  974. return (blocks.length == 1) ? blocks[0] : new MDMultiBlock(blocks);
  975. }
  976. }
  977. readBlock(state) {
  978. throw new Error(`Abstract readBlock must be overridden in ${this.constructor.name}`);
  979. }
  980. }
  981. /**
  982. * Block reader for unordered (bulleted) lists.
  983. *
  984. * Example:
  985. *
  986. * > ```markdown
  987. * > * First item
  988. * > * Second item
  989. * > * Third item
  990. * > ```
  991. */
  992. class MDUnorderedListBlockReader extends MDBaseListBlockReader {
  993. static #unorderedListRegex = /^([\*\+\-]\s+)(.*)$/; // 1=bullet, 2=content
  994. constructor(priority=15.0) {
  995. super(priority);
  996. }
  997. /**
  998. * @param {MDState} state
  999. * @returns {MDListItemBlock|null}
  1000. */
  1001. #readUnorderedListItem(state) {
  1002. var p = state.p;
  1003. let line = state.lines[p];
  1004. let groups = MDUnorderedListBlockReader.#unorderedListRegex.exec(line);
  1005. if (groups === null) return null;
  1006. const firstLineOffset = groups[1].length;
  1007. return new MDListItemBlock(this.readListItemContent(state, firstLineOffset));
  1008. }
  1009. readBlock(state) {
  1010. var items = [];
  1011. var item = null;
  1012. do {
  1013. item = this.#readUnorderedListItem(state);
  1014. if (item) items.push(item);
  1015. } while (item);
  1016. if (items.length == 0) return null;
  1017. return new MDUnorderedListBlock(items);
  1018. }
  1019. }
  1020. /**
  1021. * Block reader for ordered (numbered) lists. The number of the first item is
  1022. * used to begin counting. The subsequent items increase by 1, regardless of
  1023. * their value.
  1024. *
  1025. * Example:
  1026. *
  1027. * > ```markdown
  1028. * > 1. First
  1029. * > 2. Second
  1030. * > 3. Third
  1031. * > ```
  1032. */
  1033. class MDOrderedListBlockReader extends MDBaseListBlockReader {
  1034. static #orderedListRegex = /^(\d+)(\.\s+)(.*)$/; // 1=number, 2=dot, 3=content
  1035. constructor(priority=16.0) {
  1036. super(priority);
  1037. }
  1038. /**
  1039. * @param {MDState} state
  1040. * @returns {MDListItemBlock|null}
  1041. */
  1042. #readOrderedListItem(state) {
  1043. var p = state.p;
  1044. let line = state.lines[p];
  1045. let groups = MDOrderedListBlockReader.#orderedListRegex.exec(line);
  1046. if (groups === null) return null;
  1047. const ordinal = parseInt(groups[1]);
  1048. const firstLineOffset = groups[1].length + groups[2].length;
  1049. return new MDListItemBlock(this.readListItemContent(state, firstLineOffset), ordinal);
  1050. }
  1051. readBlock(state) {
  1052. var items = [];
  1053. var item = null;
  1054. do {
  1055. item = this.#readOrderedListItem(state);
  1056. if (item) items.push(item);
  1057. } while (item);
  1058. if (items.length == 0) return null;
  1059. return new MDOrderedListBlock(items, items[0].ordinal);
  1060. }
  1061. }
  1062. /**
  1063. * Block reader for code blocks denoted by pairs of triple tickmarks.
  1064. *
  1065. * Example:
  1066. *
  1067. * > ```markdown
  1068. * > \`\`\`
  1069. * > function formattedAsCode() {
  1070. * > }
  1071. * > \`\`\`
  1072. * > ```
  1073. */
  1074. class MDFencedCodeBlockReader extends MDBlockReader {
  1075. constructor(priority=20.0) {
  1076. super(priority);
  1077. }
  1078. readBlock(state) {
  1079. if (!state.hasLines(2)) return null;
  1080. var p = state.p;
  1081. let openFenceLine = state.lines[p++];
  1082. var modifier;
  1083. [openFenceLine, modifier] = MDTagModifier.fromLine(openFenceLine);
  1084. if (openFenceLine.trim() != '```') return null;
  1085. var codeLines = [];
  1086. while (state.hasLines(1, p)) {
  1087. let line = state.lines[p++];
  1088. if (line.trim() == '```') {
  1089. state.p = p;
  1090. let block = new MDCodeBlock(codeLines.join("\n"));
  1091. if (modifier) modifier.applyTo(block);
  1092. return block;
  1093. }
  1094. codeLines.push(line);
  1095. }
  1096. return null;
  1097. }
  1098. }
  1099. /**
  1100. * Block reader for code blocks denoted by indenting text.
  1101. *
  1102. * Example (indent spaces rendered visibly for clarity):
  1103. *
  1104. * > ```markdown
  1105. * > ⎵⎵⎵⎵function formattedAsCode() {
  1106. * > ⎵⎵⎵⎵}
  1107. * > ```
  1108. */
  1109. class MDIndentedCodeBlockReader extends MDBlockReader {
  1110. constructor(priority=21.0) {
  1111. super(priority);
  1112. }
  1113. readBlock(state) {
  1114. var p = state.p;
  1115. var codeLines = [];
  1116. while (state.hasLines(1, p)) {
  1117. let line = state.lines[p++];
  1118. if (MDUtils.countIndents(line, true) < 1) {
  1119. p--;
  1120. break;
  1121. }
  1122. codeLines.push(MDUtils.stripIndent(line));
  1123. }
  1124. if (codeLines.length == 0) return null;
  1125. state.p = p;
  1126. return new MDCodeBlock(codeLines.join("\n"));
  1127. }
  1128. }
  1129. /**
  1130. * Block reader for horizontal rules. Composed of three or more hypens or
  1131. * asterisks on a line by themselves, with or without intermediate whitespace.
  1132. *
  1133. * Examples:
  1134. *
  1135. * > ```markdown
  1136. * > ---
  1137. * >
  1138. * > - - -
  1139. * >
  1140. * > * * * * *
  1141. * >
  1142. * > ****
  1143. * > ```
  1144. */
  1145. class MDHorizontalRuleBlockReader extends MDBlockReader {
  1146. static #horizontalRuleRegex = /^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;
  1147. constructor(priority=25.0) {
  1148. super(priority);
  1149. }
  1150. /**
  1151. * @param {MDState} state
  1152. * @returns {MDBlock|null}
  1153. */
  1154. readBlock(state) {
  1155. var p = state.p;
  1156. let line = state.lines[p++];
  1157. var modifier;
  1158. [line, modifier] = MDTagModifier.fromLine(line);
  1159. if (MDHorizontalRuleBlockReader.#horizontalRuleRegex.exec(line)) {
  1160. state.p = p;
  1161. let block = new MDHorizontalRuleBlock();
  1162. if (modifier) modifier.applyTo(block);
  1163. return block;
  1164. }
  1165. return null;
  1166. }
  1167. }
  1168. /**
  1169. * Block reader for tables.
  1170. *
  1171. * Examples:
  1172. *
  1173. * > ```markdown
  1174. * > Name | Age
  1175. * > --- | ---
  1176. * > Joe | 34
  1177. * > Alice | 25
  1178. * >
  1179. * > | Leading | And Trailing |
  1180. * > | - | - |
  1181. * > | Required | for single column tables |
  1182. * >
  1183. * > | Left aligned column | Center aligned | Right aligned |
  1184. * > | :-- | :--: | --: |
  1185. * > | Joe | x | 34 |
  1186. * > ```
  1187. */
  1188. class MDTableBlockReader extends MDBlockReader {
  1189. constructor(priority=30.0) {
  1190. super(priority);
  1191. }
  1192. /**
  1193. * @param {MDState} state
  1194. * @param {boolean} isHeader
  1195. * @return {MDTableRowBlock|null}
  1196. */
  1197. #readTableRow(state, isHeader) {
  1198. if (!state.hasLines(1)) return null;
  1199. var p = state.p;
  1200. let line = MDTagModifier.strip(state.lines[p++].trim());
  1201. if (/.*\|.*/.exec(line) === null) return null;
  1202. if (line.startsWith('|')) line = line.substring(1);
  1203. if (line.endsWith('|')) line = line.substring(0, line.length - 1);
  1204. let cellTokens = line.split('|');
  1205. let cells = cellTokens.map(function(token) {
  1206. let content = state.inlineMarkdownToSpan(token.trim());
  1207. return isHeader ? new MDTableHeaderCellBlock(content) : new MDTableCellBlock(content);
  1208. });
  1209. state.p = p;
  1210. return new MDTableRowBlock(cells);
  1211. }
  1212. /**
  1213. * @param {string} line
  1214. * @returns {string[]}
  1215. */
  1216. #parseColumnAlignments(line) {
  1217. line = line.trim();
  1218. if (line.startsWith('|')) line = line.substring(1);
  1219. if (line.endsWith('|')) line = line.substring(0, line.length - 1);
  1220. return line.split(/\s*\|\s*/).map(function(token) {
  1221. if (token.startsWith(':')) {
  1222. if (token.endsWith(':')) {
  1223. return MDTableCellBlock.AlignCenter;
  1224. }
  1225. return MDTableCellBlock.AlignLeft;
  1226. } else if (token.endsWith(':')) {
  1227. return MDTableCellBlock.AlignRight;
  1228. }
  1229. return null;
  1230. });
  1231. }
  1232. static #tableDividerRegex = /^\s*[|]?\s*(?:[:]?-+[:]?)(?:\s*\|\s*[:]?-+[:]?)*\s*[|]?\s*$/;
  1233. readBlock(state) {
  1234. if (!state.hasLines(2)) return null;
  1235. let startP = state.p;
  1236. let firstLine = state.lines[startP];
  1237. var modifier = MDTagModifier.fromLine(firstLine)[1];
  1238. let headerRow = this.#readTableRow(state, true);
  1239. if (headerRow === null) {
  1240. state.p = startP;
  1241. return null;
  1242. }
  1243. let dividerLine = state.lines[state.p++];
  1244. let dividerGroups = MDTableBlockReader.#tableDividerRegex.exec(dividerLine);
  1245. if (dividerGroups === null) {
  1246. state.p = startP;
  1247. return null;
  1248. }
  1249. let columnAlignments = this.#parseColumnAlignments(dividerLine);
  1250. headerRow.applyAlignments(columnAlignments);
  1251. var bodyRows = [];
  1252. while (state.hasLines(1)) {
  1253. let row = this.#readTableRow(state, false);
  1254. if (row === null) break;
  1255. row.applyAlignments(columnAlignments);
  1256. bodyRows.push(row);
  1257. }
  1258. let table = new MDTableBlock(headerRow, bodyRows);
  1259. if (modifier) modifier.applyTo(table);
  1260. return table;
  1261. }
  1262. }
  1263. /**
  1264. * Block reader for definition lists. Definitions go directly under terms starting
  1265. * with a colon.
  1266. *
  1267. * Example:
  1268. *
  1269. * > ```markdown
  1270. * > markdown
  1271. * > : a language for generating HTML from simplified syntax
  1272. * > parser
  1273. * > : code that converts human-readable code into machine language
  1274. * > ```
  1275. */
  1276. class MDDefinitionListBlockReader extends MDBlockReader {
  1277. constructor(priority=35.0) {
  1278. super(priority);
  1279. }
  1280. readBlock(state) {
  1281. var p = state.p;
  1282. var groups;
  1283. var termCount = 0;
  1284. var definitionCount = 0;
  1285. var defLines = [];
  1286. while (state.hasLines(1, p)) {
  1287. let line = state.lines[p++];
  1288. if (line.trim().length == 0) {
  1289. break;
  1290. }
  1291. if (/^\s+/.exec(line)) {
  1292. if (defLines.length == 0) return null;
  1293. defLines[defLines.length - 1] += "\n" + line;
  1294. } else if (/^:\s+/.exec(line)) {
  1295. defLines.push(line);
  1296. definitionCount++;
  1297. } else {
  1298. defLines.push(line);
  1299. termCount++;
  1300. }
  1301. }
  1302. if (termCount == 0 || definitionCount == 0) return null;
  1303. let blocks = defLines.map(function(line) {
  1304. if (groups = /^:\s+(.*?)$/s.exec(line)) {
  1305. return new MDDefinitionDefinitionBlock(state.inlineMarkdownToSpans(groups[1]));
  1306. } else {
  1307. return new MDDefinitionTermBlock(state.inlineMarkdownToSpans(line));
  1308. }
  1309. });
  1310. state.p = p;
  1311. return new MDDefinitionListBlock(blocks);
  1312. }
  1313. }
  1314. /**
  1315. * Block reader for defining footnote contents. Footnotes can be defined anywhere
  1316. * in the document but will always be rendered at the end of a page or end of
  1317. * the document.
  1318. *
  1319. * Examples:
  1320. *
  1321. * > ```markdown
  1322. * > [^1]: Content of a footnote. Anywhere `[^1]` appears in the
  1323. * > main text, it will hyperlink to this content at the bottom
  1324. * > of the document. There will also be backlinks at the end
  1325. * > of this footnote to all references to it.
  1326. * > ```
  1327. */
  1328. class MDFootnoteDefinitionBlockReader extends MDBlockReader {
  1329. static {
  1330. Object.defineProperties(MDState.prototype, {
  1331. // symbol string (e.g. '1') -> content MDBlock
  1332. footnotes: {
  1333. get: function footnotes() {
  1334. if (this.root['_footnotes'] === undefined) this.root['_footnotes'] = {};
  1335. return this.root._footnotes;
  1336. },
  1337. set: function footnotes(newValue) {
  1338. this.root['_footnotes'] = newValue;
  1339. }
  1340. },
  1341. // symbol string (e.g. '1') -> unique number[]
  1342. footnoteInstances: {
  1343. get: function footnoteInstances() {
  1344. if (this.root['_footnoteInstances'] === undefined) this.root['_footnoteInstances'] = {};
  1345. return this.root._footnoteInstances;
  1346. },
  1347. set: function(newValue) {
  1348. this.root['_footnoteInstances'] = newValue;
  1349. }
  1350. }
  1351. });
  1352. MDState.prototype.defineFootnote = function(symbol, footnote) {
  1353. this.footnotes[symbol] = footnote;
  1354. };
  1355. MDState.prototype.registerUniqueFootnote = function(symbol, unique) {
  1356. var uniques = this.footnoteInstances[symbol] || [];
  1357. uniques.push(unique);
  1358. this.footnoteInstances[symbol] = uniques;
  1359. };
  1360. }
  1361. constructor(priority=40.0) {
  1362. super(priority);
  1363. }
  1364. /**
  1365. * @param {MDState} state
  1366. */
  1367. readBlock(state) {
  1368. var p = state.p;
  1369. let groups = /^\s*\[\^\s*([^\]]+)\s*\]:\s+(.*)\s*$/.exec(state.lines[p++]);
  1370. if (groups === null) return null;
  1371. let symbol = groups[1];
  1372. let def = groups[2];
  1373. while (state.hasLines(1, p)) {
  1374. let line = state.lines[p++];
  1375. if (/^\s+/.exec(line)) {
  1376. def += "\n" + line;
  1377. } else {
  1378. p--;
  1379. break;
  1380. }
  1381. }
  1382. state.p = p;
  1383. let content = state.inlineMarkdownToSpan(def);
  1384. state.defineFootnote(symbol, content);
  1385. state.p = p;
  1386. return new MDMultiBlock([]);
  1387. }
  1388. /**
  1389. * @param {MDState} state
  1390. * @param {MDBlock[]} blocks
  1391. */
  1392. postProcess(state, blocks) {
  1393. var unique = 1;
  1394. for (const block of blocks) {
  1395. block.visitChildren(function(node) {
  1396. if (!(node instanceof MDFootnoteReferenceSpan)) return;
  1397. node.differentiator = unique++;
  1398. state.registerUniqueFootnote(node.symbol, node.differentiator);
  1399. });
  1400. }
  1401. if (Object.keys(state.footnotes).length == 0) return;
  1402. blocks.push(new MDFootnoteListingBlock());
  1403. }
  1404. }
  1405. /**
  1406. * Block reader for abbreviation definitions. Anywhere the abbreviation appears
  1407. * in the text will have its definition available when hovering over it.
  1408. * Definitions can appear anywhere in the document. Their content should only
  1409. * contain simple text, not markdown.
  1410. *
  1411. * Example:
  1412. *
  1413. * > ```markdown
  1414. * > *[HTML]: Hyper Text Markup Language
  1415. * > ```
  1416. */
  1417. class MDAbbreviationDefinitionBlockReader extends MDBlockReader {
  1418. static {
  1419. Object.defineProperties(MDState.prototype, {
  1420. abbreviations: {
  1421. get: function abbreviations() {
  1422. if (this.root['_abbreviations'] === undefined) this.root['_abbreviations'] = {};
  1423. return this.root._abbreviations;
  1424. },
  1425. set: function(newValue) {
  1426. this.root['_abbreviations'] = newValue;
  1427. }
  1428. },
  1429. abbreviationRegexes: {
  1430. get: function abbreviationRegexes() {
  1431. if (this.root['_abbreviationRegexes'] === undefined) this.root['_abbreviationRegexes'] = {};
  1432. return this.root._abbreviationRegexes;
  1433. },
  1434. set: function(newValue) {
  1435. this.root['_abbreviationRegexes'] = newValue;
  1436. }
  1437. }
  1438. });
  1439. MDState.prototype.defineAbbreviation = function(abbreviation, definition) {
  1440. this.abbreviations[abbreviation] = definition;
  1441. const regex = new RegExp("\\b(" + MDUtils.escapeRegex(abbreviation) + ")\\b", "ig");
  1442. this.abbreviationRegexes[abbreviation] = regex;
  1443. };
  1444. }
  1445. constructor(priority=45.0) {
  1446. super(priority);
  1447. }
  1448. readBlock(state) {
  1449. var p = state.p;
  1450. let line = state.lines[p++];
  1451. let groups = /^\s*\*\[([^\]]+?)\]:\s+(.*?)\s*$/.exec(line);
  1452. if (groups === null) return null;
  1453. let abbrev = groups[1];
  1454. let def = groups[2];
  1455. state.defineAbbreviation(abbrev, def);
  1456. state.p = p;
  1457. return new MDMultiBlock([]);
  1458. }
  1459. }
  1460. /**
  1461. * Block reader for URL definitions. Links in the document can include a
  1462. * reference instead of a verbatim URL so it can be defined in one place and
  1463. * reused in many places. These can be defined anywhere in the document. Nothing
  1464. * of the definition is rendered in the document.
  1465. *
  1466. * Example:
  1467. *
  1468. * > ```markdown
  1469. * > [foo]: https://example.com
  1470. * > ```
  1471. */
  1472. class MDURLDefinitionBlockReader extends MDBlockReader {
  1473. constructor(priority=50.0) {
  1474. super(priority);
  1475. }
  1476. readBlock(state) {
  1477. var p = state.p;
  1478. let line = state.lines[p++];
  1479. var symbol;
  1480. var url;
  1481. var title = null;
  1482. let groups = /^\s*\[(.+?)]:\s*(\S+)\s+"(.*?)"\s*$/.exec(line);
  1483. if (groups) {
  1484. symbol = groups[1];
  1485. url = groups[2];
  1486. title = groups[3];
  1487. } else {
  1488. groups = /^\s*\[(.+?)]:\s*(\S+)\s*$/.exec(line);
  1489. if (groups) {
  1490. symbol = groups[1];
  1491. url = groups[2];
  1492. } else {
  1493. return null;
  1494. }
  1495. }
  1496. state.defineURL(symbol, url, title);
  1497. state.p = p;
  1498. return new MDInlineBlock([]);
  1499. }
  1500. }
  1501. /**
  1502. * Block reader for simple paragraphs. Paragraphs are separated by a blank (or
  1503. * whitespace-only) line. This reader should be prioritized last since there
  1504. * is no distinguishing syntax.
  1505. *
  1506. * Example:
  1507. *
  1508. * > ```markdown
  1509. * > Lorem ipsum dolor
  1510. * > sit amet. This is all one paragraph.
  1511. * >
  1512. * > Beginning of a new paragraph.
  1513. * > ```
  1514. */
  1515. class MDParagraphBlockReader extends MDBlockReader {
  1516. constructor(priority=100.0) {
  1517. super(priority);
  1518. }
  1519. readBlock(state) {
  1520. var paragraphLines = [];
  1521. var p = state.p;
  1522. var foundBlankLine = false;
  1523. while (p < state.lines.length) {
  1524. let line = state.lines[p++];
  1525. if (line.trim().length == 0) {
  1526. foundBlankLine = true;
  1527. break;
  1528. }
  1529. paragraphLines.push(line);
  1530. }
  1531. if (state.p == 0 && p >= state.lines.length) {
  1532. // If it's the entire document don't wrap it in a paragraph
  1533. return null;
  1534. }
  1535. if (paragraphLines.length > 0) {
  1536. state.p = p;
  1537. let content = paragraphLines.join("\n");
  1538. return new MDParagraphBlock(new MDInlineBlock(state.inlineMarkdownToSpans(content)));
  1539. }
  1540. return null;
  1541. }
  1542. }
  1543. // -- Inline reader ---------------------------------------------------------
  1544. class MDInlineReader {
  1545. /** @type {number} */
  1546. #tokenizePriority;
  1547. /** @type {number|number[]} */
  1548. #substitutePriority;
  1549. /**
  1550. * A unitless relative tokenizing priority value that determines which
  1551. * readers are tried first. Lower values are tried first. Standard readers
  1552. * are in the range of 0.0 to 100.0 but any value is valid. Longer
  1553. * tokens should generally be prioritized over short or single character tokens.
  1554. *
  1555. * @returns {number} priority or priorities for tokenization
  1556. */
  1557. get tokenizePriority() { return this.#tokenizePriority; }
  1558. /**
  1559. * A unitless relative substitution priority value that determines which
  1560. * readers are tried first. Lower values are tried first. If an array of
  1561. * values is given, the same reader will be included twice in the
  1562. * prioritization. This allows for multiple passes. Standard readers
  1563. * are in the range of 0.0 to 100.0 but any value is valid. Priority should
  1564. * be used to help resolve ambiguous parsings, with longer, more complex
  1565. * constructions best prioritized before shorter, simpler ones.
  1566. *
  1567. * @returns {number|number[]} priority or priorities for substitution
  1568. */
  1569. get substitutePriority() { return this.#substitutePriority; }
  1570. /**
  1571. * @param {number} tokenizePriority
  1572. * @param {number|number[]} substitutePriority
  1573. */
  1574. constructor(tokenizePriority, substitutePriority) {
  1575. this.#tokenizePriority = tokenizePriority;
  1576. this.#substitutePriority = substitutePriority;
  1577. }
  1578. /**
  1579. * Attempts to read a token from the start of the given string.
  1580. *
  1581. * @param {string} remainingText - remainder of the current line of markdown text left to tokenize
  1582. * @returns {MDToken|null} a token or `null` if not found
  1583. */
  1584. readFirstToken(state, priority, remainingText) {
  1585. throw new Error(`Abstract readFirstToken must be overridden in ${this.constructor.name}`);
  1586. }
  1587. /**
  1588. * Attempts to substitute one or more tokens in the given array. The given
  1589. * array is edited in-place via `.splice` operations. It consists of mixed
  1590. * elements of unprocessed `MDToken` and interpreted `MDSpan` elements.
  1591. *
  1592. * If a structure consists of inner content that is also markdown encoded,
  1593. * those elements can be passed to `state.tokensToSpans` to resolve to an
  1594. * array of `MDSpan` elements.
  1595. *
  1596. * @param {MDState} state
  1597. * @param {number} priority - used to differentiate passes when multiple
  1598. * values of `this.substitutePriority` are given
  1599. * @param {MDToken[]|MDSpan[]} tokens - mixed array of `MDToken` and
  1600. * `MDSpan` elements to be modified by reference
  1601. * @returns {boolean} `true` if any substitutions were made; `false` if not
  1602. */
  1603. substituteTokens(state, priority, tokens) {
  1604. throw new Error(`Abstract substituteTokens must be overridden in ${this.constructor.name}`);
  1605. }
  1606. /**
  1607. * Called before parsing the document.
  1608. *
  1609. * @param {MDState} state
  1610. */
  1611. preProcess(state) {
  1612. // no op
  1613. }
  1614. /**
  1615. * Called after the full document has been generated for optional
  1616. * post-processing.
  1617. *
  1618. * @param {MDState} state
  1619. * @param {MDBlock[]} blocks - top-level document block list
  1620. */
  1621. postProcess(state, blocks) {
  1622. // no op
  1623. }
  1624. }
  1625. /**
  1626. * Abstract base class for readers that look for one or more delimiting tokens
  1627. * around some content.
  1628. */
  1629. class MDSimplePairInlineReader extends MDInlineReader {
  1630. constructor(tokenizePriority, substitutePriority) {
  1631. super(tokenizePriority, substitutePriority);
  1632. }
  1633. /**
  1634. * Attempts a substitution of a matched pair of delimiting token types.
  1635. * If successful, the substitution is performed on `tokens` and `true` is
  1636. * returned, otherwise `false` is returned and the array is untouched.
  1637. *
  1638. * If multiple `substitutePriority` values are specified, the first pass
  1639. * will reject matches with the delimiting character inside the content
  1640. * tokens. If a single `substitutePriority` is given or a subsequent pass
  1641. * is performed with multiple values any contents will be accepted.
  1642. *
  1643. * @param {MDState} state
  1644. * @param {number} priority
  1645. * @param {MDToken[]} tokens
  1646. * @param {class} spanClass
  1647. * @param {MDTokenType} delimiter
  1648. * @param {number} count - how many times the token is repeated to form the delimiter
  1649. * @returns {boolean} `true` if substitution performed, `false` if not
  1650. */
  1651. attemptPair(state, priority, tokens, spanClass, delimiter, count=1, plaintext=false) {
  1652. let delimiters = Array(count).fill(delimiter);
  1653. let firstPassPriority = (this.substitutePriority instanceof Array) ? this.substitutePriority[0] : null;
  1654. let match = MDToken.findPairedTokens(tokens, delimiters, delimiters, function(content) {
  1655. const firstType = content[0] instanceof MDToken ? content[0].type : null;
  1656. const lastType = content[content.length - 1] instanceof MDToken ? content[content.length - 1].type : null;
  1657. if (firstType == MDTokenType.Whitespace) return false;
  1658. if (lastType == MDTokenType.Whitespace) return false;
  1659. if (priority == firstPassPriority) {
  1660. var innerCount = 0;
  1661. for (let token of content) {
  1662. if (token instanceof MDToken && token.type == delimiter) innerCount++;
  1663. }
  1664. if ((innerCount % 2) != 0) return false;
  1665. }
  1666. return true;
  1667. });
  1668. if (match === null) return false;
  1669. let content = (plaintext)
  1670. ? match.contentTokens.map((token) => token.original).join('')
  1671. : state.tokensToSpans(match.contentTokens);
  1672. tokens.splice(match.startIndex, match.totalLength, new spanClass(content));
  1673. return true;
  1674. }
  1675. }
  1676. class MDStrongInlineReader extends MDSimplePairInlineReader {
  1677. constructor(tokenizePriority=0.0, substitutePriority=[0.0, 50.0]) {
  1678. super(tokenizePriority, substitutePriority);
  1679. }
  1680. readFirstToken(state, priority, remainingText) {
  1681. if (remainingText.startsWith('*')) return new MDToken('*', MDTokenType.Asterisk);
  1682. if (remainingText.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
  1683. return null;
  1684. }
  1685. substituteTokens(state, priority, tokens) {
  1686. if (this.attemptPair(state, priority, tokens, MDStrongSpan, MDTokenType.Asterisk, 2)) return true;
  1687. if (this.attemptPair(state, priority, tokens, MDStrongSpan, MDTokenType.Underscore, 2)) return true;
  1688. return false;
  1689. }
  1690. }
  1691. class MDEmphasisInlineReader extends MDSimplePairInlineReader {
  1692. constructor(tokenizePriority=0.0, substitutePriority=[0.0, 50.0]) {
  1693. super(tokenizePriority, substitutePriority);
  1694. }
  1695. readFirstToken(state, priority, remainingText) {
  1696. if (remainingText.startsWith('*')) return new MDToken('*', MDTokenType.Asterisk);
  1697. if (remainingText.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
  1698. return null;
  1699. }
  1700. substituteTokens(state, priority, tokens) {
  1701. if (this.attemptPair(state, priority, tokens, MDEmphasisSpan, MDTokenType.Asterisk)) return true;
  1702. if (this.attemptPair(state, priority, tokens, MDEmphasisSpan, MDTokenType.Underscore)) return true;
  1703. return false;
  1704. }
  1705. }
  1706. class MDCodeInlineReader extends MDSimplePairInlineReader {
  1707. constructor(tokenizePriority=0.0, substitutePriority=0.0) {
  1708. super(tokenizePriority, substitutePriority);
  1709. }
  1710. readFirstToken(state, priority, remainingText) {
  1711. if (remainingText.startsWith('`')) return new MDToken('`', MDTokenType.Backtick);
  1712. return null;
  1713. }
  1714. substituteTokens(state, priority, tokens) {
  1715. // ignore priority
  1716. if (this.attemptPair(state, -1, tokens, MDCodeSpan, MDTokenType.Backtick, 2, true)) return true;
  1717. if (this.attemptPair(state, -1, tokens, MDCodeSpan, MDTokenType.Backtick, 1, true)) return true;
  1718. return false;
  1719. }
  1720. }
  1721. class MDStrikethroughInlineReader extends MDSimplePairInlineReader {
  1722. constructor(tokenizePriority=0.0, substitutePriority=[0.0, 50.0]) {
  1723. super(tokenizePriority, substitutePriority);
  1724. }
  1725. readFirstToken(state, priority, remainingText) {
  1726. if (remainingText.startsWith('~')) return new MDToken('~', MDTokenType.Tilde);
  1727. return null;
  1728. }
  1729. substituteTokens(state, priority, tokens) {
  1730. if (this.attemptPair(state, priority, tokens, MDStrikethroughSpan, MDTokenType.Tilde, 2)) return true;
  1731. if (this.attemptPair(state, priority, tokens, MDStrikethroughSpan, MDTokenType.Tilde)) return true;
  1732. return false;
  1733. }
  1734. }
  1735. class MDImageInlineReader extends MDInlineReader {
  1736. constructor(tokenizePriority=0.0, substitutePriority=0.0) {
  1737. super(tokenizePriority, substitutePriority);
  1738. }
  1739. readFirstToken(state, priority, remainingText) {
  1740. if (remainingText.startsWith('!')) return new MDToken('!', MDTokenType.Bang);
  1741. var groups;
  1742. if (groups = MDUtils.tokenizeLabel(remainingText)) {
  1743. return new MDToken(groups[0], MDTokenType.Label, groups[1]);
  1744. }
  1745. if (groups = MDUtils.tokenizeURL(remainingText)) {
  1746. return new MDToken(groups[0], MDTokenType.URL, groups[1], groups[2]);
  1747. }
  1748. return null;
  1749. }
  1750. substituteTokens(state, priority, tokens) {
  1751. var match;
  1752. if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Bang, MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.URL ])) {
  1753. let alt = match.tokens[1].content;
  1754. let url = match.tokens[match.tokens.length - 1].content;
  1755. let title = match.tokens[match.tokens.length - 1].extra;
  1756. tokens.splice(match.index, match.tokens.length, new MDImageSpan(url, alt, title));
  1757. return true;
  1758. }
  1759. if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Bang, MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.Label ])) {
  1760. let alt = match.tokens[1].content;
  1761. let ref = match.tokens[match.tokens.length - 1].content;
  1762. tokens.splice(match.index, match.tokens.length, new MDReferencedImageSpan(ref, alt));
  1763. return true;
  1764. }
  1765. return false;
  1766. }
  1767. }
  1768. class MDFootnoteInlineReader extends MDInlineReader {
  1769. static #footnoteWithTitleRegex = /^\[\^([^\]]+?)\s+"(.*?)"\]/; // 1=symbol, 2=title
  1770. static #footnoteRegex = /^\[\^([^\]]+?)\]/; // 1=symbol
  1771. constructor(tokenizePriority=0.0, substitutePriority=0.0) {
  1772. super(tokenizePriority, substitutePriority);
  1773. }
  1774. readFirstToken(state, priority, remainingText) {
  1775. var groups;
  1776. if (groups = MDFootnoteInlineReader.#footnoteWithTitleRegex.exec(remainingText)) {
  1777. return new MDToken(groups[0], MDTokenType.Footnote, groups[1], groups[2]);
  1778. }
  1779. if (groups = MDFootnoteInlineReader.#footnoteRegex.exec(remainingText)) {
  1780. return new MDToken(groups[0], MDTokenType.Footnote, groups[1]);
  1781. }
  1782. return null;
  1783. }
  1784. substituteTokens(state, priority, tokens) {
  1785. var match;
  1786. if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Footnote ])) {
  1787. let footnoteToken = match.tokens[0];
  1788. tokens.splice(match.index, 1, new MDFootnoteReferenceSpan(footnoteToken.content));
  1789. return true;
  1790. }
  1791. return false;
  1792. }
  1793. }
  1794. class MDLinkInlineReader extends MDInlineReader {
  1795. static {
  1796. Object.defineProperties(MDState.prototype, {
  1797. /**
  1798. * Reference symbol -> URL string
  1799. * @type {object}
  1800. */
  1801. urlDefinitions: {
  1802. get: function() {
  1803. if (this.root['_urlDefinitions'] === undefined) this.root['_urlDefinitions'] = {};
  1804. return this.root._urlDefinitions;
  1805. },
  1806. set: function(newValue) {
  1807. this.root['_urlDefinitions'] = newValue;
  1808. }
  1809. },
  1810. /**
  1811. * Reference symbol -> title string
  1812. * @type {object}
  1813. */
  1814. urlTitles: {
  1815. get: function() {
  1816. if (this.root['_urlTitles'] === undefined) this.root['_urlTitles'] = {};
  1817. return this.root._urlTitles;
  1818. },
  1819. set: function(newValue) {
  1820. this.root['_urlTitles'] = newValue;
  1821. }
  1822. }
  1823. });
  1824. /**
  1825. * Defines the URL for a given reference symbol.
  1826. *
  1827. * @param {string} symbol - reference symbol
  1828. * @param {string} url - URL
  1829. * @param {string|null} title - mouseover title attribute for links
  1830. */
  1831. MDState.prototype.defineURL = function(symbol, url, title=null) {
  1832. this.urlDefinitions[symbol.toLowerCase()] = url;
  1833. if (title !== null) {
  1834. this.urlTitles[symbol.toLowerCase()] = title;
  1835. }
  1836. };
  1837. MDState.prototype.urlForLinkRef = function(ref) {
  1838. return this.urlDefinitions[ref.toLowerCase()];
  1839. };
  1840. MDState.prototype.titleForLinkRef = function(ref) {
  1841. return this.urlTitles[ref.toLowerCase()];
  1842. }
  1843. }
  1844. constructor(tokenizePriority=0.0, substitutePriority=0.0) {
  1845. super(tokenizePriority, substitutePriority);
  1846. }
  1847. readFirstToken(state, priority, remainingText) {
  1848. var groups;
  1849. if (groups = MDUtils.tokenizeLabel(remainingText)) {
  1850. return new MDToken(groups[0], MDTokenType.Label, groups[1]);
  1851. }
  1852. if (groups = MDUtils.tokenizeEmail(remainingText)) {
  1853. return new MDToken(groups[0], MDTokenType.Email, groups[1], groups[2]);
  1854. }
  1855. if (groups = MDUtils.tokenizeURL(remainingText)) {
  1856. return new MDToken(groups[0], MDTokenType.URL, groups[1], groups[2]);
  1857. }
  1858. return null;
  1859. }
  1860. /**
  1861. * @param {MDState} state
  1862. */
  1863. substituteTokens(state, priority, tokens) {
  1864. var match;
  1865. if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.URL ])) {
  1866. let text = match.tokens[0].content;
  1867. let url = match.tokens[match.tokens.length - 1].content;
  1868. let title = match.tokens[match.tokens.length - 1].extra;
  1869. tokens.splice(match.index, match.tokens.length, new MDLinkSpan(url, state.inlineMarkdownToSpan(text), title));
  1870. return true;
  1871. }
  1872. if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.Email ])) {
  1873. let text = match.tokens[0].content;
  1874. let email = match.tokens[match.tokens.length - 1].content;
  1875. let url = `mailto:${email}`;
  1876. let title = match.tokens[match.tokens.length - 1].extra;
  1877. tokens.splice(match.index, match.tokens.length, new MDLinkSpan(url, state.inlineMarkdownToSpan(text), title));
  1878. return true;
  1879. }
  1880. if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.Label ])) {
  1881. let text = match.tokens[0].content;
  1882. let ref = match.tokens[match.tokens.length - 1].content;
  1883. tokens.splice(match.index, match.tokens.length, new MDReferencedLinkSpan(ref, state.inlineMarkdownToSpan(text)));
  1884. return true;
  1885. }
  1886. return false;
  1887. }
  1888. }
  1889. class MDSimpleLinkInlineReader extends MDInlineReader {
  1890. static #simpleEmailRegex = new RegExp("^<(" + MDUtils.baseEmailRegex.source + ")>", "i"); // 1=email
  1891. static #simpleURLRegex = new RegExp("^<(" + MDUtils.baseURLRegex.source + ")>", "i"); // 1=URL
  1892. constructor(tokenizePriority=0.0, substitutePriority=0.0) {
  1893. super(tokenizePriority, substitutePriority);
  1894. }
  1895. readFirstToken(state, priority, remainingText) {
  1896. var groups;
  1897. if (groups = MDSimpleLinkInlineReader.#simpleEmailRegex.exec(remainingText)) {
  1898. return new MDToken(groups[0], MDTokenType.SimpleEmail, groups[1]);
  1899. }
  1900. if (groups = MDSimpleLinkInlineReader.#simpleURLRegex.exec(remainingText)) {
  1901. return new MDToken(groups[0], MDTokenType.SimpleLink, groups[1]);
  1902. }
  1903. return null;
  1904. }
  1905. #substituteEmail(state, tokens) {
  1906. const result = MDToken.findFirstTokens(tokens, [ MDTokenType.SimpleEmail ]);
  1907. if (result === null) return false;
  1908. /** @type {MDToken} */
  1909. const token = result.tokens[0];
  1910. const link = `mailto:${token.content}`;
  1911. const span = new MDLinkSpan(link, new MDObfuscatedTextSpan(token.content));
  1912. tokens.splice(result.index, 1, span);
  1913. return true;
  1914. }
  1915. #substituteURL(state, tokens) {
  1916. const result = MDToken.findFirstTokens(tokens, [ MDTokenType.SimpleLink ]);
  1917. if (result === null) return false;
  1918. /** @type {MDToken} */
  1919. const token = result.tokens[0];
  1920. const link = token.content;
  1921. const span = new MDLinkSpan(link, new MDTextSpan(link));
  1922. tokens.splice(result.index, 1, span);
  1923. return true;
  1924. }
  1925. substituteTokens(state, priority, tokens) {
  1926. if (this.#substituteEmail(state, tokens)) return true;
  1927. if (this.#substituteURL(state, tokens)) return true;
  1928. return false;
  1929. }
  1930. }
  1931. class MDHTMLTagInlineReader extends MDInlineReader {
  1932. constructor(tokenizePriority=0.0, substitutePriority=95.0) {
  1933. super(tokenizePriority, substitutePriority);
  1934. }
  1935. readFirstToken(state, priority, remainingText) {
  1936. var tag = MDHTMLTag.fromLineStart(remainingText);
  1937. if (tag) return new MDToken(tag.fullTag, MDTokenType.HTMLTag, tag.fullTag, null, tag);
  1938. return null;
  1939. }
  1940. substituteTokens(state, priority, tokens) {
  1941. const result = MDToken.findFirstTokens(tokens, [ MDTokenType.HTMLTag ]);
  1942. if (result === null) return false;
  1943. /** @type {MDToken} */
  1944. const token = result.tokens[0];
  1945. const tag = token.tag;
  1946. const span = new MDHTMLSpan(tag);
  1947. tokens.splice(result.index, 1, span);
  1948. return true;
  1949. }
  1950. }
  1951. class MDModifierInlineReader extends MDInlineReader {
  1952. constructor(tokenizePriority=0.0, substitutePriority=100.0) {
  1953. super(tokenizePriority, substitutePriority);
  1954. }
  1955. readFirstToken(state, priority, remainingText) {
  1956. var modifier = MDTagModifier.fromStart(remainingText);
  1957. if (modifier) return new MDToken(modifier.original, MDTokenType.Modifier, modifier);
  1958. return null;
  1959. }
  1960. substituteTokens(state, priority, tokens) {
  1961. // Modifiers are applied elsewhere, and if they're not it's fine if they're
  1962. // rendered as the original syntax.
  1963. return false;
  1964. }
  1965. }
  1966. // -- Blocks ----------------------------------------------------------------
  1967. class MDBlock {
  1968. /** @type {string[]} */
  1969. cssClasses = [];
  1970. /** @type {string|null} */
  1971. cssId = null;
  1972. /** @type {object} */
  1973. attributes = {};
  1974. /**
  1975. * @param {MDState} state
  1976. * @returns {string}
  1977. */
  1978. toHTML(state) {
  1979. throw new Error(`Abstract ${this.constructor.name}.toHTML must be implemented`);
  1980. }
  1981. /**
  1982. * @param {MDState} state
  1983. * @returns {string}
  1984. */
  1985. toPlaintext(state) {
  1986. throw new Error(`Abstract ${this.constructor.name}.toPlaintext must be implemented`);
  1987. }
  1988. htmlAttributes() {
  1989. var html = '';
  1990. if (this.cssClasses.length > 0) {
  1991. html += ` class="${this.cssClasses.join(' ')}"`;
  1992. }
  1993. if (this.cssId !== null) {
  1994. html += ` id="${this.cssId}"`;
  1995. }
  1996. for (const name in this.attributes) {
  1997. let value = this.attributes[name];
  1998. html += ` ${name}="${MDUtils.escapeHTML(value)}"`;
  1999. }
  2000. return html;
  2001. }
  2002. /**
  2003. * @param {MDBlock[]} blocks
  2004. * @param {MDState} state
  2005. * @returns {string}
  2006. */
  2007. static toHTML(blocks, state) {
  2008. return blocks.map((block) => block.toHTML(state)).join("\n");
  2009. }
  2010. /**
  2011. * @param {MDBlock[]} blocks
  2012. * @param {MDState} state
  2013. * @returns {string}
  2014. */
  2015. static toPlaintext(blocks, state) {
  2016. return blocks.map((block) => block.toPlaintext(state)).join("\n");
  2017. }
  2018. /**
  2019. * Visits all block and inline children of this block, calling the given
  2020. * function with each. Should be implemented for any block with child nodes.
  2021. *
  2022. * @param {function} fn
  2023. */
  2024. visitChildren(fn) {}
  2025. }
  2026. class MDMultiBlock extends MDBlock {
  2027. /** @type {MDBlock[]} */
  2028. blocks;
  2029. /**
  2030. * @param {MDBlock[]} blocks
  2031. */
  2032. constructor(blocks) {
  2033. super();
  2034. if (blocks instanceof Array) {
  2035. this.blocks = blocks;
  2036. } else {
  2037. throw new Error(`${MDUtils.typename(this)} expects MDBlock[], got ${MDUtils.typename(blocks)}`);
  2038. }
  2039. }
  2040. toHTML(state) {
  2041. return MDBlock.toHTML(this.blocks, state);
  2042. }
  2043. toPlaintext(state) {
  2044. return MDBlock.toPlaintext(this.blocks, state);
  2045. }
  2046. visitChildren(fn) {
  2047. for (const block of this.blocks) {
  2048. fn(block);
  2049. block.visitChildren(fn);
  2050. }
  2051. }
  2052. }
  2053. class MDParagraphBlock extends MDBlock {
  2054. /** @type {MDBlock[]} */
  2055. content;
  2056. /**
  2057. * @param {MDBlock|MDBlock[]} content
  2058. */
  2059. constructor(content) {
  2060. super();
  2061. if (content instanceof Array) {
  2062. this.content = content;
  2063. } else if (content instanceof MDBlock) {
  2064. this.content = [ content ];
  2065. } else {
  2066. throw new Error(`${MDUtils.typename(this)} expects MDBlock[] or MDBlock, got ${MDUtils.typename(content)}`);
  2067. }
  2068. }
  2069. toHTML(state) {
  2070. const contentHTML = MDBlock.toHTML(this.content, state);
  2071. return `<p${this.htmlAttributes()}>${contentHTML}</p>\n`;
  2072. }
  2073. toPlaintext(state) {
  2074. return MDBlock.toPlaintext(this.content, state);
  2075. }
  2076. visitChildren(fn) {
  2077. for (const child of this.content) {
  2078. fn(child);
  2079. child.visitChildren(fn);
  2080. }
  2081. }
  2082. }
  2083. class MDHeaderBlock extends MDBlock {
  2084. /** @type {number} */
  2085. level;
  2086. /** @type {MDBlock[]} */
  2087. content;
  2088. /**
  2089. * @param {number} level
  2090. * @param {MDBlock|MDBlock[]} content
  2091. */
  2092. constructor(level, content) {
  2093. super();
  2094. this.level = level;
  2095. this.content = (content instanceof Array) ? content : [ content ];
  2096. }
  2097. toHTML(state) {
  2098. let contentHTML = MDBlock.toHTML(this.content, state);
  2099. return `<h${this.level}${this.htmlAttributes()}>${contentHTML}</h${this.level}>\n`;
  2100. }
  2101. toPlaintext(state) {
  2102. return MDBlock.toPlaintext(this.content, state);
  2103. }
  2104. visitChildren(fn) {
  2105. for (const child of this.content) {
  2106. fn(child);
  2107. child.visitChildren(fn);
  2108. }
  2109. }
  2110. }
  2111. class MDBlockquoteBlock extends MDBlock {
  2112. /** @type {MDBlock[]} */
  2113. content;
  2114. /**
  2115. * @param {MDBlock|MDBlock[]} content
  2116. */
  2117. constructor(content) {
  2118. super();
  2119. this.content = (content instanceof MDBlock) ? [ content ] : content;
  2120. }
  2121. toHTML(state) {
  2122. let contentHTML = MDBlock.toHTML(this.content, state);
  2123. return `<blockquote${this.htmlAttributes()}>\n${contentHTML}\n</blockquote>`;
  2124. }
  2125. toPlaintext(state) {
  2126. return MDBlock.toPlaintext(this.content, state);
  2127. }
  2128. visitChildren(fn) {
  2129. for (const block of this.content) {
  2130. fn(block);
  2131. block.visitChildren(fn);
  2132. }
  2133. }
  2134. }
  2135. class MDUnorderedListBlock extends MDBlock {
  2136. /** @type {MDListItemBlock[]} */
  2137. items;
  2138. /**
  2139. * @param {MDListItemBlock[]} items
  2140. */
  2141. constructor(items) {
  2142. super();
  2143. this.items = items;
  2144. }
  2145. toHTML(state) {
  2146. let contentHTML = MDBlock.toHTML(this.items, state);
  2147. return `<ul${this.htmlAttributes()}>\n${contentHTML}\n</ul>`;
  2148. }
  2149. toPlaintext(state) {
  2150. return MDBlock.toPlaintext(this.items, state);
  2151. }
  2152. visitChildren(fn) {
  2153. for (const item of this.items) {
  2154. fn(item);
  2155. item.visitChildren(fn);
  2156. }
  2157. }
  2158. }
  2159. class MDOrderedListBlock extends MDBlock {
  2160. /** @type {MDListItemBlock[]} */
  2161. items;
  2162. /** @type {number|null} */
  2163. startOrdinal;
  2164. /**
  2165. * @param {MDListItemBlock[]} items
  2166. */
  2167. constructor(items, startOrdinal=null) {
  2168. super();
  2169. this.items = items;
  2170. this.startOrdinal = startOrdinal;
  2171. }
  2172. htmlAttributes() {
  2173. var html = super.htmlAttributes();
  2174. if (this.startOrdinal !== null && this.startOrdinal != 1) {
  2175. html += ` start="${this.startOrdinal}"`;
  2176. }
  2177. return html;
  2178. }
  2179. toHTML(state) {
  2180. let contentHTML = MDBlock.toHTML(this.items, state);
  2181. return `<ol${this.htmlAttributes()}>\n${contentHTML}\n</ol>`;
  2182. }
  2183. toPlaintext(state) {
  2184. return MDBlock.toPlaintext(this.items, state);
  2185. }
  2186. visitChildren(fn) {
  2187. for (const item of this.items) {
  2188. fn(item);
  2189. item.visitChildren(fn);
  2190. }
  2191. }
  2192. }
  2193. class MDListItemBlock extends MDBlock {
  2194. /** @type {MDBlock[]} */
  2195. content;
  2196. /** @type {number|null} */
  2197. ordinal;
  2198. /**
  2199. * @param {MDBlock|MDBlock[]} content
  2200. * @param {number|null} ordinal
  2201. */
  2202. constructor(content, ordinal=null) {
  2203. super();
  2204. if (content instanceof Array) {
  2205. this.content = content;
  2206. } else {
  2207. this.content = [ content ];
  2208. }
  2209. this.ordinal = ordinal;
  2210. }
  2211. toHTML(state) {
  2212. let contentHTML = MDBlock.toHTML(this.content, state);
  2213. return `<li${this.htmlAttributes()}>${contentHTML}</li>`;
  2214. }
  2215. toPlaintext(state) {
  2216. return MDBlock.toPlaintext(this.content, state);
  2217. }
  2218. visitChildren(fn) {
  2219. for (const child of this.content) {
  2220. fn(child);
  2221. child.visitChildren(fn);
  2222. }
  2223. }
  2224. }
  2225. class MDCodeBlock extends MDBlock {
  2226. /** @type {string} */
  2227. code;
  2228. /**
  2229. * @param {string} code
  2230. */
  2231. constructor(code) {
  2232. super();
  2233. this.code = code;
  2234. }
  2235. toHTML(state) {
  2236. return `<pre${this.htmlAttributes()}><code>${MDUtils.escapeHTML(this.code)}</code></pre>`;
  2237. }
  2238. toPlaintext(state) {
  2239. return this.code;
  2240. }
  2241. }
  2242. class MDHorizontalRuleBlock extends MDBlock {
  2243. toHTML(state) {
  2244. return `<hr${this.htmlAttributes()}>\n`;
  2245. }
  2246. toPlaintext(state) {
  2247. return '';
  2248. }
  2249. }
  2250. class MDTableCellBlock extends MDBlock {
  2251. static AlignLeft = 'left';
  2252. static AlignCenter = 'center';
  2253. static AlignRight = 'right';
  2254. /** @type {MDBlock} */
  2255. content;
  2256. /** @type {string|null} */
  2257. align = null;
  2258. /**
  2259. * @param {MDBlock} content
  2260. */
  2261. constructor(content) {
  2262. super();
  2263. this.content = content;
  2264. }
  2265. #alignAttribute() {
  2266. switch (this.align) {
  2267. case MDTableCellBlock.AlignLeft: return ' align="left"';
  2268. case MDTableCellBlock.AlignCenter: return ' align="center"';
  2269. case MDTableCellBlock.AlignRight: return ' align="right"';
  2270. default: return '';
  2271. }
  2272. }
  2273. htmlAttributes() {
  2274. var html = super.htmlAttributes();
  2275. html += this.#alignAttribute();
  2276. return html;
  2277. }
  2278. toHTML(state) {
  2279. let contentHTML = this.content.toHTML(state);
  2280. return `<td${this.htmlAttributes()}>${contentHTML}</td>`;
  2281. }
  2282. toPlaintext(state) {
  2283. return this.content.toPlaintext(state);
  2284. }
  2285. visitChildren(fn) {
  2286. fn(this.content);
  2287. this.content.visitChildren(fn);
  2288. }
  2289. }
  2290. class MDTableHeaderCellBlock extends MDTableCellBlock {
  2291. toHTML(state) {
  2292. let html = super.toHTML(state);
  2293. let groups = /^<td(.*)td>$/.exec(html);
  2294. return `<th${groups[1]}th>`;
  2295. }
  2296. }
  2297. class MDTableRowBlock extends MDBlock {
  2298. /** @type {MDTableCellBlock[]|MDTableHeaderCellBlock[]} */
  2299. cells;
  2300. /**
  2301. * @param {MDTableCellBlock[]|MDTableHeaderCellBlock[]} cells
  2302. */
  2303. constructor(cells) {
  2304. super();
  2305. this.cells = cells;
  2306. }
  2307. /**
  2308. * @param {string[]} alignments
  2309. */
  2310. applyAlignments(alignments) {
  2311. for (var i = 0; i < this.cells.length; i++) {
  2312. let cell = this.cells[i];
  2313. let align = i < alignments.length ? alignments[i] : null;
  2314. cell.align = align;
  2315. }
  2316. }
  2317. toHTML(state) {
  2318. let cellsHTML = MDBlock.toHTML(this.cells, state);
  2319. return `<tr${this.htmlAttributes()}>\n${cellsHTML}\n</tr>`;
  2320. }
  2321. toPlaintext(state) {
  2322. return this.cells.map((cell) => cell.toPlaintext(state)).join(' ');
  2323. }
  2324. visitChildren(fn) {
  2325. for (const cell of this.cells) {
  2326. fn(cell);
  2327. cell.visitChildren(fn);
  2328. }
  2329. }
  2330. }
  2331. class MDTableBlock extends MDBlock {
  2332. /** @type {MDTableRowBlock} */
  2333. headerRow;
  2334. /** @type {MDTableRowBlock[]} */
  2335. bodyRows;
  2336. /**
  2337. * @param {MDTableRowBlock} headerRow
  2338. * @param {MDTableRowBlock[]} bodyRows
  2339. */
  2340. constructor(headerRow, bodyRows) {
  2341. super();
  2342. this.headerRow = headerRow;
  2343. this.bodyRows = bodyRows;
  2344. }
  2345. toHTML(state) {
  2346. let headerRowHTML = this.headerRow.toHTML(state);
  2347. let bodyRowsHTML = MDBlock.toHTML(this.bodyRows, state);
  2348. return `<table${this.htmlAttributes()}>\n<thead>\n${headerRowHTML}\n</thead>\n<tbody>\n${bodyRowsHTML}\n</tbody>\n</table>`;
  2349. }
  2350. toPlaintext(state) {
  2351. return this.headerRow.toPlaintext(state) + "\n" + this.bodyRows.map((row) => row.toPlaintext(state)).join("\n");
  2352. }
  2353. visitChildren(fn) {
  2354. fn(this.headerRow);
  2355. this.headerRow.visitChildren(fn);
  2356. for (const row of this.bodyRows) {
  2357. fn(row);
  2358. row.visitChildren(fn);
  2359. }
  2360. }
  2361. }
  2362. class MDDefinitionListBlock extends MDBlock {
  2363. /** @type {MDBlock[]} */
  2364. content;
  2365. /**
  2366. * @param {MDBlock[]} content
  2367. */
  2368. constructor(content) {
  2369. super();
  2370. this.content = content;
  2371. }
  2372. toHTML(state) {
  2373. let contentHTML = MDBlock.toHTML(this.content, state);
  2374. return `<dl${this.htmlAttributes()}>\n${contentHTML}\n</dl>`;
  2375. }
  2376. toPlaintext(state) {
  2377. return MDBlock.toPlaintext(this.content, state);
  2378. }
  2379. visitChildren(fn) {
  2380. for (const block of this.content) {
  2381. fn(block);
  2382. block.visitChildren(fn);
  2383. }
  2384. }
  2385. }
  2386. class MDDefinitionTermBlock extends MDBlock {
  2387. /** @type {MDBlock[]} */
  2388. content;
  2389. /**
  2390. * @param {MDBlock|MDBlock[]} content
  2391. */
  2392. constructor(content) {
  2393. super();
  2394. if (content instanceof Array) {
  2395. this.content = content;
  2396. } else if (content instanceof MDBlock) {
  2397. this.content = [ content ];
  2398. } else {
  2399. throw new Error(`${this.constructor.name} expects MDBlock or MDBlock[], got ${typeof content}`);
  2400. }
  2401. }
  2402. toHTML(state) {
  2403. let contentHTML = MDBlock.toHTML(this.content, state);
  2404. return `<dt${this.htmlAttributes()}>${contentHTML}</dt>`;
  2405. }
  2406. toPlaintext(state) {
  2407. return MDBlock.toPlaintext(this.content, state);
  2408. }
  2409. visitChildren(fn) {
  2410. for (const child of this.content) {
  2411. fn(child);
  2412. child.visitChildren(fn);
  2413. }
  2414. }
  2415. }
  2416. class MDDefinitionDefinitionBlock extends MDBlock {
  2417. /** @type {MDBlock[]} */
  2418. content;
  2419. /**
  2420. * @param {MDBlock|MDBlock[]} content
  2421. */
  2422. constructor(content) {
  2423. super();
  2424. if (content instanceof Array) {
  2425. this.content = content;
  2426. } else if (content instanceof MDBlock) {
  2427. this.content = [ content ];
  2428. } else {
  2429. throw new Error(`${this.constructor.name} expects MDBlock or MDBlock[], got ${typeof content}`);
  2430. }
  2431. }
  2432. toHTML(state) {
  2433. let contentHTML = MDBlock.toHTML(this.content, state);
  2434. return `<dd${this.htmlAttributes()}>${contentHTML}</dd>`;
  2435. }
  2436. toPlaintext(state) {
  2437. return MDBlock.toPlaintext(this.content, state);
  2438. }
  2439. visitChildren(fn) {
  2440. for (const child of this.content) {
  2441. fn(child);
  2442. child.visitChildren(fn);
  2443. }
  2444. }
  2445. }
  2446. class MDFootnoteListingBlock extends MDBlock {
  2447. constructor() {
  2448. super();
  2449. }
  2450. /**
  2451. * @param {MDState} state
  2452. */
  2453. toHTML(state) {
  2454. const footnotes = state.footnotes;
  2455. var symbolOrder = Object.keys(footnotes);
  2456. if (Object.keys(footnotes).length == 0) return '';
  2457. const footnoteUniques = state.footnoteInstances;
  2458. var html = '';
  2459. html += '<div class="footnotes"><hr/>';
  2460. html += '<ol>';
  2461. for (const symbol of symbolOrder) {
  2462. /** @type {MDBlock} */
  2463. let content = footnotes[symbol];
  2464. if (!content) continue;
  2465. html += `<li value="${symbol}" id="footnote_${symbol}">${content.toHTML(state)}`;
  2466. for (const unique of footnoteUniques[symbol]) {
  2467. html += ` <a href="#footnoteref_${unique}" class="footnote-backref">↩︎</a>`;
  2468. }
  2469. html += `</li>\n`;
  2470. }
  2471. html += '</ol>';
  2472. html += '</div>';
  2473. return html;
  2474. }
  2475. toPlaintext(state) {
  2476. const footnotes = state.footnotes;
  2477. var symbolOrder = Object.keys(footnotes);
  2478. if (Object.keys(footnotes).length == 0) return '';
  2479. var text = '';
  2480. for (const symbol of symbolOrder) {
  2481. let content = footnotes[symbol];
  2482. if (!content) continue;
  2483. text += `${symbol}. ${content.toPlaintext(state)}\n`;
  2484. }
  2485. return text.trim();
  2486. }
  2487. }
  2488. class MDInlineBlock extends MDBlock {
  2489. /** @type {MDSpan[]} */
  2490. content;
  2491. /**
  2492. * @param {MDSpan|MDSpan[]} content
  2493. */
  2494. constructor(content) {
  2495. super();
  2496. this.content = (content instanceof Array) ? content : [ content ];
  2497. for (const span of this.content) {
  2498. if (!(span instanceof MDSpan)) {
  2499. throw new Error(`${this.constructor.name} expects MDSpan or MDSpan[], got ${MDUtils.typename(span)}`);
  2500. }
  2501. }
  2502. }
  2503. toHTML(state) {
  2504. return MDSpan.toHTML(this.content, state);
  2505. }
  2506. toPlaintext(state) {
  2507. return MDSpan.toPlaintext(this.content, state);
  2508. }
  2509. visitChildren(fn) {
  2510. for (const span of this.content) {
  2511. fn(span);
  2512. span.visitChildren(fn);
  2513. }
  2514. }
  2515. }
  2516. // -- Spans -----------------------------------------------------------------
  2517. class MDSpan {
  2518. /** @type {string[]} */
  2519. cssClasses = [];
  2520. /** @type {string|null} */
  2521. cssId = null;
  2522. /** @type {object} */
  2523. attributes = {};
  2524. /**
  2525. * @param {MDState} state
  2526. * @returns {string} HTML
  2527. */
  2528. toHTML(state) {
  2529. throw new Error(`Abstract ${this.constructor.name}.toHTML must be implemented`);
  2530. }
  2531. toPlaintext(state) {
  2532. throw new Error(`Abstract ${this.constructor.name}.toPlaintext must be implemented`);
  2533. }
  2534. htmlAttributes() {
  2535. var html = '';
  2536. if (this.cssClasses.length > 0) {
  2537. html += ` class="${this.cssClasses.join(' ')}"`;
  2538. }
  2539. if (this.cssId !== null) {
  2540. html += ` id="${this.cssId}"`;
  2541. }
  2542. for (const name in this.attributes) {
  2543. let value = this.attributes[name];
  2544. html += ` ${name}="${MDUtils.escapeHTML(value)}"`;
  2545. }
  2546. return html;
  2547. }
  2548. /**
  2549. * @param {MDSpan[]} spans
  2550. * @param {MDState} state
  2551. * @returns {string}
  2552. */
  2553. static toHTML(spans, state) {
  2554. return spans.map((span) => span.toHTML(state)).join('');
  2555. }
  2556. /**
  2557. * @param {MDSpan[]} spans
  2558. * @param {MDState} state
  2559. * @returns {string}
  2560. */
  2561. static toPlaintext(spans, state) {
  2562. return spans.map((span) => span.toPlaintext(state)).join('');
  2563. }
  2564. /**
  2565. * Visits all inline children of this span, calling the given function with
  2566. * each. Should be implemented for any span with child nodes.
  2567. *
  2568. * @param {function} fn
  2569. */
  2570. visitChildren(fn) {}
  2571. }
  2572. class MDMultiSpan extends MDSpan {
  2573. /** @type {MDSpan[]} */
  2574. content;
  2575. /**
  2576. * @param {MDSpan[]} content
  2577. */
  2578. constructor(content) {
  2579. super();
  2580. this.content = content;
  2581. }
  2582. toHTML(state) {
  2583. return MDSpan.toHTML(this.content, state);
  2584. }
  2585. toPlaintext(state) {
  2586. return MDSpan.toPlaintext(this.content, state);
  2587. }
  2588. visitChildren(fn) {
  2589. for (const span of this.content) {
  2590. fn(span);
  2591. span.visitChildren(fn);
  2592. }
  2593. }
  2594. }
  2595. class MDTextSpan extends MDSpan {
  2596. /** @param {string} text */
  2597. text;
  2598. /**
  2599. * @param {string} text
  2600. */
  2601. constructor(text) {
  2602. super();
  2603. this.text = text;
  2604. }
  2605. toHTML(state) {
  2606. let html = MDUtils.escapeHTML(this.text);
  2607. let abbrevs = state.abbreviations;
  2608. let regexes = state.abbreviationRegexes;
  2609. for (const abbrev in abbrevs) {
  2610. let def = abbrevs[abbrev];
  2611. let regex = regexes[abbrev];
  2612. let escapedDef = MDUtils.escapeHTML(def);
  2613. html = html.replace(regex, `<abbr title="${escapedDef}">$1</abbr>`);
  2614. }
  2615. return html;
  2616. }
  2617. toPlaintext(state) {
  2618. return this.text;
  2619. }
  2620. }
  2621. class MDHTMLSpan extends MDSpan {
  2622. /** @param {string} */
  2623. tag;
  2624. /**
  2625. * @param {MDHTMLTag} tag
  2626. */
  2627. constructor(tag) {
  2628. super();
  2629. this.tag = tag;
  2630. }
  2631. toHTML(state) {
  2632. return this.tag.fullTag;
  2633. }
  2634. toPlaintext(state) {
  2635. return '';
  2636. }
  2637. }
  2638. class MDObfuscatedTextSpan extends MDSpan {
  2639. /** @param {string} text */
  2640. text;
  2641. /**
  2642. * @param {string} text
  2643. */
  2644. constructor(text) {
  2645. super();
  2646. this.text = text;
  2647. }
  2648. toHTML(state) {
  2649. return MDUtils.escapeObfuscated(this.text);
  2650. }
  2651. toPlaintext(state) {
  2652. return this.text;
  2653. }
  2654. }
  2655. class MDLinkSpan extends MDSpan {
  2656. /** @type {string} */
  2657. link;
  2658. /** @type {string|null} */
  2659. target = null;
  2660. /** @type {MDSpan} */
  2661. content;
  2662. /** @type {string|null} */
  2663. title = null;
  2664. /**
  2665. * @param {string} link
  2666. * @param {MDSpan} content
  2667. * @param {string|null} title
  2668. */
  2669. constructor(link, content, title=null) {
  2670. super();
  2671. this.link = link;
  2672. this.content = content;
  2673. this.title = title;
  2674. }
  2675. toHTML(state) {
  2676. var escapedLink;
  2677. if (this.link.startsWith('mailto:')) {
  2678. escapedLink = 'mailto:' + MDUtils.escapeObfuscated(this.link.substring(7));
  2679. } else {
  2680. escapedLink = MDUtils.escapeHTML(this.link);
  2681. }
  2682. var html = `<a href="${escapedLink}"`;
  2683. if (this.target) {
  2684. html += ` target="${MDUtils.escapeHTML(this.target)}"`;
  2685. }
  2686. if (this.title) {
  2687. html += ` title="${MDUtils.escapeHTML(this.title)}"`;
  2688. }
  2689. html += this.htmlAttributes();
  2690. html += '>' + this.content.toHTML(state) + '</a>';
  2691. return html;
  2692. }
  2693. toPlaintext(state) {
  2694. return this.content.toPlaintext(state);
  2695. }
  2696. visitChildren(fn) {
  2697. fn(this.content);
  2698. this.content.visitChildren(fn);
  2699. }
  2700. }
  2701. class MDReferencedLinkSpan extends MDLinkSpan {
  2702. /** @type {string} */
  2703. ref;
  2704. constructor(ref, content) {
  2705. super(null, content, null);
  2706. this.ref = ref;
  2707. }
  2708. /**
  2709. * @param {MDState} state
  2710. */
  2711. toHTML(state) {
  2712. if (!this.link) {
  2713. let url = state.urlForLinkRef(this.ref);
  2714. let title = state.titleForLinkRef(this.ref);
  2715. this.link = url;
  2716. this.title = title || this.title;
  2717. }
  2718. if (this.link) {
  2719. return super.toHTML(state);
  2720. } else {
  2721. let contentHTML = this.content.toHTML(state);
  2722. return `[${contentHTML}][${this.ref}]`;
  2723. }
  2724. }
  2725. }
  2726. class MDEmphasisSpan extends MDSpan {
  2727. /** @type {MDSpan[]} */
  2728. content;
  2729. /**
  2730. * @param {MDSpan|MDSpan[]} content
  2731. */
  2732. constructor(content) {
  2733. super();
  2734. this.content = (content instanceof MDSpan) ? [ content ] : content;
  2735. }
  2736. toHTML(state) {
  2737. let contentHTML = MDSpan.toHTML(this.content, state);
  2738. return `<em${this.htmlAttributes()}>${contentHTML}</em>`;
  2739. }
  2740. toPlaintext(state) {
  2741. return MDSpan.toPlaintext(this.content, state);
  2742. }
  2743. visitChildren(fn) {
  2744. for (const span of this.content) {
  2745. fn(span);
  2746. span.visitChildren(fn);
  2747. }
  2748. }
  2749. }
  2750. class MDStrongSpan extends MDSpan {
  2751. /** @type {MDSpan[]} content */
  2752. content;
  2753. /**
  2754. * @param {MDSpan|MDSpan[]} content
  2755. */
  2756. constructor(content) {
  2757. super();
  2758. this.content = (content instanceof MDSpan) ? [content] : content;
  2759. }
  2760. toHTML(state) {
  2761. let contentHTML = MDSpan.toHTML(this.content, state);
  2762. return `<strong${this.htmlAttributes()}>${contentHTML}</strong>`;
  2763. }
  2764. toPlaintext(state) {
  2765. return MDSpan.toPlaintext(this.content, state);
  2766. }
  2767. visitChildren(fn) {
  2768. for (const span of this.content) {
  2769. fn(span);
  2770. span.visitChildren(fn);
  2771. }
  2772. }
  2773. }
  2774. class MDStrikethroughSpan extends MDSpan {
  2775. /** @type {MDSpan[]} content */
  2776. content;
  2777. /**
  2778. * @param {MDSpan|MDSpan[]} content
  2779. */
  2780. constructor(content) {
  2781. super();
  2782. this.content = (content instanceof MDSpan) ? [content] : content;
  2783. }
  2784. toHTML(state) {
  2785. let contentHTML = MDSpan.toHTML(this.content, state);
  2786. return `<strike${this.htmlAttributes()}>${contentHTML}</strike>`;
  2787. }
  2788. toPlaintext(state) {
  2789. return MDSpan.toPlaintext(this.content, state);
  2790. }
  2791. visitChildren(fn) {
  2792. for (const span of this.content) {
  2793. fn(span);
  2794. span.visitChildren(fn);
  2795. }
  2796. }
  2797. }
  2798. class MDCodeSpan extends MDSpan {
  2799. /** @type {String} content */
  2800. content;
  2801. /**
  2802. * @param {String} content
  2803. */
  2804. constructor(content) {
  2805. super();
  2806. if (typeof content == 'string') {
  2807. this.content = content;
  2808. } else {
  2809. throw new Error(`${this.constructor.name} content must be String, got ${typeof content}`);
  2810. }
  2811. }
  2812. toHTML(state) {
  2813. return `<code${this.htmlAttributes()}>${MDUtils.escapeHTML(this.content)}</code>`;
  2814. }
  2815. toPlaintext(state) {
  2816. return this.content;
  2817. }
  2818. }
  2819. class MDImageSpan extends MDSpan {
  2820. /** @type {string} */
  2821. source;
  2822. /** @type {string|null} */
  2823. alt;
  2824. /** @type {string|null} */
  2825. title;
  2826. /**
  2827. * @param {string} source - image URL
  2828. * @param {string} alt - alt text
  2829. * @param {string|null} title - optional title attribute
  2830. */
  2831. constructor(source, alt, title=null) {
  2832. super();
  2833. this.source = source;
  2834. this.alt = alt;
  2835. this.title = title;
  2836. }
  2837. toHTML(state) {
  2838. let html = `<img src="${MDUtils.escapeHTML(this.source)}"`;
  2839. if (this.alt) {
  2840. html += ` alt="${MDUtils.escapeHTML(this.alt)}"`;
  2841. }
  2842. if (this.title) {
  2843. html += ` title="${MDUtils.escapeHTML(this.title)}"`;
  2844. }
  2845. html += this.htmlAttributes();
  2846. html += '>';
  2847. return html;
  2848. }
  2849. toPlaintext(state) {
  2850. return this.alt || '';
  2851. }
  2852. }
  2853. class MDReferencedImageSpan extends MDImageSpan {
  2854. /** @type {string} */
  2855. ref;
  2856. /**
  2857. * @param {string} ref
  2858. * @param {string|null} alt
  2859. */
  2860. constructor(ref, alt) {
  2861. super(null, alt);
  2862. this.ref = ref;
  2863. }
  2864. toHTML(state) {
  2865. if (!this.source) {
  2866. let url = state.urlForLinkRef(this.ref);
  2867. let title = state.titleForLinkRef(this.ref);
  2868. this.source = url;
  2869. this.title = title || this.title;
  2870. }
  2871. if (this.source) {
  2872. return super.toHTML(state);
  2873. } else {
  2874. return `![${MDUtils.escapeHTML(this.alt)}][${MDUtils.escapeHTML(this.ref)}]`;
  2875. }
  2876. }
  2877. }
  2878. class MDFootnoteReferenceSpan extends MDSpan {
  2879. /** @type {string} */
  2880. symbol;
  2881. /** @type {number|null} */
  2882. differentiator = null;
  2883. /**
  2884. * @param {string} symbol
  2885. */
  2886. constructor(symbol) {
  2887. super();
  2888. this.symbol = symbol;
  2889. }
  2890. toHTML(state) {
  2891. if (this.differentiator !== null) {
  2892. return `<sup id="footnoteref_${this.differentiator}"><a href="#footnote_${this.symbol}">${this.symbol}</a></sup>`;
  2893. }
  2894. return `<!--FNREF:{${this.symbol}}-->`;
  2895. }
  2896. toPlaintext(state) {
  2897. return this.symbol;
  2898. }
  2899. }
  2900. // -- Other -----------------------------------------------------------------
  2901. class MDHTMLTag {
  2902. /** @type {string} */
  2903. fullTag;
  2904. /** @type {string} */
  2905. tagName;
  2906. /** @type {boolean} */
  2907. isCloser;
  2908. /** @type {object} */
  2909. attributes;
  2910. /**
  2911. * @param {string} fullTag
  2912. * @param {string} tagName
  2913. * @param {boolean} isCloser
  2914. * @param {object} attributes
  2915. */
  2916. constructor(fullTag, tagName, isCloser, attributes) {
  2917. this.fullTag = fullTag;
  2918. this.tagName = tagName;
  2919. this.isCloser = isCloser;
  2920. this.attributes = attributes;
  2921. }
  2922. toString() {
  2923. return this.fullTag;
  2924. }
  2925. equals(other) {
  2926. if (!(other instanceof MDHTMLTag)) return false;
  2927. return other.fullTag == this.fullTag;
  2928. }
  2929. static #htmlTagNameFirstRegex = /[a-z]/i;
  2930. static #htmlTagNameMedialRegex = /[a-z0-9]/i;
  2931. static #htmlAttributeNameFirstRegex = /[a-z]/i;
  2932. static #htmlAttributeNameMedialRegex = /[a-z0-9-]/i;
  2933. static #whitespaceCharRegex = /\s/;
  2934. /**
  2935. * @param {string} line
  2936. * @returns {MDHTMLTag|null} HTML tag if possible
  2937. */
  2938. static fromLineStart(line) {
  2939. let expectOpenBracket = 0;
  2940. let expectCloserOrName = 1;
  2941. let expectName = 2;
  2942. let expectAttributeNameOrEnd = 3;
  2943. let expectEqualsOrAttributeOrEnd = 4;
  2944. let expectAttributeValue = 5;
  2945. let expectCloseBracket = 6;
  2946. var isCloser = false;
  2947. var tagName = '';
  2948. var attributeName = '';
  2949. var attributeValue = '';
  2950. var attributeQuote = null;
  2951. var attributes = {};
  2952. var fullTag = null;
  2953. let endAttribute = function() {
  2954. if (attributeName.length > 0) {
  2955. if (attributeValue.length > 0 || attributeQuote) {
  2956. attributes[attributeName] = attributeValue;
  2957. } else {
  2958. attributes[attributeName] = true;
  2959. }
  2960. }
  2961. attributeName = '';
  2962. attributeValue = '';
  2963. attributeQuote = null;
  2964. };
  2965. var expect = expectOpenBracket;
  2966. for (var p = 0; p < line.length && fullTag === null; p++) {
  2967. let ch = line.substring(p, p + 1);
  2968. let isWhitespace = this.#whitespaceCharRegex.exec(ch) !== null;
  2969. switch (expect) {
  2970. case expectOpenBracket:
  2971. if (ch != '<') return null;
  2972. expect = expectCloserOrName;
  2973. break;
  2974. case expectCloserOrName:
  2975. if (ch == '/') {
  2976. isCloser = true;
  2977. } else {
  2978. p--;
  2979. }
  2980. expect = expectName;
  2981. break;
  2982. case expectName:
  2983. if (tagName.length == 0) {
  2984. if (this.#htmlTagNameFirstRegex.exec(ch) === null) return null;
  2985. tagName += ch;
  2986. } else {
  2987. if (this.#htmlTagNameMedialRegex.exec(ch)) {
  2988. tagName += ch;
  2989. } else {
  2990. p--;
  2991. expect = (isCloser) ? expectCloseBracket : expectAttributeNameOrEnd;
  2992. }
  2993. }
  2994. break;
  2995. case expectAttributeNameOrEnd:
  2996. if (attributeName.length == 0) {
  2997. if (isWhitespace) {
  2998. // skip whitespace
  2999. } else if (ch == '/') {
  3000. expect = expectCloseBracket;
  3001. } else if (ch == '>') {
  3002. fullTag = line.substring(0, p + 1);
  3003. break;
  3004. } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
  3005. attributeName += ch;
  3006. } else {
  3007. return null;
  3008. }
  3009. } else if (isWhitespace) {
  3010. expect = expectEqualsOrAttributeOrEnd;
  3011. } else if (ch == '/') {
  3012. endAttribute();
  3013. expect = expectCloseBracket;
  3014. } else if (ch == '>') {
  3015. endAttribute();
  3016. fullTag = line.substring(0, p + 1);
  3017. break;
  3018. } else if (ch == '=') {
  3019. expect = expectAttributeValue;
  3020. } else if (this.#htmlAttributeNameMedialRegex.exec(ch)) {
  3021. attributeName += ch;
  3022. } else {
  3023. return null;
  3024. }
  3025. break;
  3026. case expectEqualsOrAttributeOrEnd:
  3027. if (ch == '=') {
  3028. expect = expectAttributeValue;
  3029. } else if (isWhitespace) {
  3030. // skip whitespace
  3031. } else if (ch == '/') {
  3032. expect = expectCloseBracket;
  3033. } else if (ch == '>') {
  3034. fullTag = line.substring(0, p + 1);
  3035. break;
  3036. } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
  3037. endAttribute();
  3038. expect = expectAttributeNameOrEnd;
  3039. p--;
  3040. }
  3041. break;
  3042. case expectAttributeValue:
  3043. if (attributeValue.length == 0) {
  3044. if (attributeQuote === null) {
  3045. if (isWhitespace) {
  3046. // skip whitespace
  3047. } else if (ch == '"' || ch == "'") {
  3048. attributeQuote = ch;
  3049. } else {
  3050. attributeQuote = ''; // explicitly unquoted
  3051. p--;
  3052. }
  3053. } else {
  3054. if (ch === attributeQuote) {
  3055. // Empty string
  3056. endAttribute();
  3057. expect = expectAttributeNameOrEnd;
  3058. } else if (attributeQuote === '' && (ch == '/' || ch == '>')) {
  3059. return null;
  3060. } else {
  3061. attributeValue += ch;
  3062. }
  3063. }
  3064. } else {
  3065. if (ch === attributeQuote) {
  3066. endAttribute();
  3067. expect = expectAttributeNameOrEnd;
  3068. } else if (attributeQuote === '' && isWhitespace) {
  3069. endAttribute();
  3070. expect = expectAttributeNameOrEnd;
  3071. } else {
  3072. attributeValue += ch;
  3073. }
  3074. }
  3075. break;
  3076. case expectCloseBracket:
  3077. if (isWhitespace) {
  3078. // ignore whitespace
  3079. } else if (ch == '>') {
  3080. fullTag = line.substring(0, p + 1);
  3081. break;
  3082. }
  3083. break;
  3084. }
  3085. }
  3086. if (fullTag === null) return null;
  3087. endAttribute();
  3088. return new MDHTMLTag(fullTag, tagName, isCloser, attributes);
  3089. }
  3090. }
  3091. class MDTagModifier {
  3092. /** @type {string} */
  3093. original;
  3094. /** @type {string[]} */
  3095. cssClasses = [];
  3096. /** @type {string|null} */
  3097. cssId = null;
  3098. /** @type {object} */
  3099. attributes = {};
  3100. static #baseClassRegex = /\.([a-z_\-][a-z0-9_\-]*?)/i;
  3101. static #baseIdRegex = /#([a-z_\-][a-z0-9_\-]*?)/i;
  3102. static #baseAttributeRegex = /([a-z0-9]+?)=([^\s\}]+?)/i;
  3103. static #baseRegex = /\{([^}]+?)}/i;
  3104. static #leadingClassRegex = new RegExp('^' + this.#baseRegex.source, 'i');
  3105. static #trailingClassRegex = new RegExp('^(.*?)\\s*' + this.#baseRegex.source + '\\s*$', 'i');
  3106. static #classRegex = new RegExp('^' + this.#baseClassRegex.source + '$', 'i'); // 1=classname
  3107. static #idRegex = new RegExp('^' + this.#baseIdRegex.source + '$', 'i'); // 1=id
  3108. static #attributeRegex = new RegExp('^' + this.#baseAttributeRegex.source + '$', 'i'); // 1=attribute name, 2=attribute value
  3109. /**
  3110. * @param {MDBlock|MDSpan} elem
  3111. */
  3112. applyTo(elem) {
  3113. if (elem instanceof MDBlock || elem instanceof MDSpan) {
  3114. elem.cssClasses = elem.cssClasses.concat(this.cssClasses);
  3115. if (this.cssId) elem.cssId = this.cssId;
  3116. for (const name in this.attributes) {
  3117. elem.attributes[name] = this.attributes[name];
  3118. }
  3119. }
  3120. }
  3121. equals(other) {
  3122. if (!(other instanceof MDTagModifier)) return false;
  3123. if (!MDUtils.equal(other.cssClasses, this.cssClasses)) return false;
  3124. if (other.cssId !== this.cssId) return false;
  3125. if (!MDUtils.equal(other.attributes, this.attributes)) return false;
  3126. return true;
  3127. }
  3128. toString() {
  3129. return this.original;
  3130. }
  3131. static #fromContents(contents) {
  3132. let modifierTokens = contents.split(/\s+/);
  3133. let mod = new MDTagModifier();
  3134. mod.original = `{${contents}}`;
  3135. var groups;
  3136. for (const token of modifierTokens) {
  3137. if (token.trim() == '') continue;
  3138. if (groups = this.#classRegex.exec(token)) {
  3139. mod.cssClasses.push(groups[1]);
  3140. } else if (groups = this.#idRegex.exec(token)) {
  3141. mod.cssId = groups[1];
  3142. } else if (groups = this.#attributeRegex.exec(token)) {
  3143. mod.attributes[groups[1]] = groups[2];
  3144. } else {
  3145. return null;
  3146. }
  3147. }
  3148. return mod;
  3149. }
  3150. /**
  3151. * Extracts modifier from line.
  3152. * @param {string} line
  3153. * @returns {Array} Tuple with remaining line and MDTagModifier.
  3154. */
  3155. static fromLine(line) {
  3156. let groups = this.#trailingClassRegex.exec(line);
  3157. if (groups === null) return [ line, null ];
  3158. let bareLine = groups[1];
  3159. let mod = this.#fromContents(groups[2]);
  3160. return [ bareLine, mod ];
  3161. }
  3162. /**
  3163. * Extracts modifier from head of string.
  3164. * @param {string} line
  3165. * @returns {MDTagModifier}
  3166. */
  3167. static fromStart(line) {
  3168. let groups = this.#leadingClassRegex.exec(line);
  3169. if (groups === null) return null;
  3170. return this.#fromContents(groups[1]);
  3171. }
  3172. /**
  3173. * @param {string} line
  3174. * @returns {string}
  3175. */
  3176. static strip(line) {
  3177. let groups = this.#trailingClassRegex.exec(line);
  3178. if (groups === null) return line;
  3179. return groups[1];
  3180. }
  3181. }
  3182. class MDConfig {
  3183. }
  3184. class Markdown {
  3185. /**
  3186. * Set of standard block readers.
  3187. * @type {MDBlockReader[]}
  3188. */
  3189. static standardBlockReaders = [
  3190. new MDUnderlinedHeaderBlockReader(10.0),
  3191. new MDHashHeaderBlockReader(15.0),
  3192. new MDBlockQuoteBlockReader(20.0),
  3193. new MDHorizontalRuleBlockReader(25.0),
  3194. new MDFencedCodeBlockReader(30.0),
  3195. new MDIndentedCodeBlockReader(40.0),
  3196. new MDOrderedListBlockReader(45.0),
  3197. new MDUnorderedListBlockReader(50.0),
  3198. new MDURLDefinitionBlockReader(95.0),
  3199. new MDParagraphBlockReader(100.0),
  3200. ];
  3201. /**
  3202. * All supported block readers.
  3203. * @type {MDBlockReader[]}
  3204. */
  3205. static allBlockReaders = [
  3206. ...this.standardBlockReaders,
  3207. new MDTableBlockReader(55.0),
  3208. new MDDefinitionListBlockReader(60.0),
  3209. new MDAbbreviationDefinitionBlockReader(90.0),
  3210. new MDFootnoteDefinitionBlockReader(91.0),
  3211. ];
  3212. /**
  3213. * Set of standard inline readers.
  3214. * @type {MDInlineReader[]}
  3215. */
  3216. static standardInlineReaders = [
  3217. new MDStrongInlineReader(10.0, [ 0.0, 2.0 ]),
  3218. new MDEmphasisInlineReader(15.0, [ 5.0, 55.0 ]),
  3219. new MDCodeInlineReader(20.0, [ 10.0, 60.0 ]),
  3220. new MDImageInlineReader(25.0, 15.0),
  3221. new MDLinkInlineReader(30.0, 20.0),
  3222. new MDSimpleLinkInlineReader(35.0, 25.0),
  3223. new MDHTMLTagInlineReader(80.0, 30.0),
  3224. ];
  3225. /**
  3226. * All supported inline readers.
  3227. * @type {MDInlineReader[]}
  3228. */
  3229. static allInlineReaders = [
  3230. ...this.standardInlineReaders,
  3231. new MDStrikethroughInlineReader(21.0, [ 12.0, 50.0 ]),
  3232. new MDFootnoteInlineReader(5.0, 40.0),
  3233. new MDModifierInlineReader(90.0, 45.0),
  3234. ];
  3235. /**
  3236. * Shared instance of a parser with standard syntax.
  3237. */
  3238. static standardParser = new Markdown(this.standardBlockReaders, this.standardInlineReaders);
  3239. /**
  3240. * Shared instance of a parser with all supported syntax.
  3241. */
  3242. static completeParser = new Markdown(this.allBlockReaders, this.allInlineReaders);
  3243. #blockReaders;
  3244. #inlineReaders;
  3245. #blockReadersByPriority;
  3246. #inlineReadersByTokenPriority;
  3247. #inlineReadersBySubstitutePriority;
  3248. /**
  3249. * @param {MDBlockReader[]} blockReaders
  3250. * @param {MDInlineReader[]} inlineReaders
  3251. */
  3252. constructor(blockReaders=Markdown.allBlockReaders, inlineReaders=Markdown.allInlineReaders) {
  3253. this.#blockReaders = blockReaders;
  3254. this.#inlineReaders = inlineReaders;
  3255. this.#blockReadersByPriority = blockReaders.slice();
  3256. this.#blockReadersByPriority.sort((a, b) => a.priority - b.priority);
  3257. const duplicateAndSort = function(priorityFn) {
  3258. var readers = [];
  3259. for (const reader of inlineReaders) {
  3260. const p = priorityFn(reader);
  3261. const priorities = (p instanceof Array) ? p : [ p ];
  3262. for (const priority of priorities) {
  3263. readers.push([priority, reader]);
  3264. }
  3265. }
  3266. readers.sort((a, b) => a[0] - b[0]);
  3267. return readers;
  3268. }
  3269. this.#inlineReadersByTokenPriority = duplicateAndSort((reader) => reader.tokenizePriority);
  3270. this.#inlineReadersBySubstitutePriority = duplicateAndSort((reader) => reader.substitutePriority);
  3271. }
  3272. /**
  3273. * @param {string} markdown
  3274. * @returns {string} HTML
  3275. */
  3276. toHTML(markdown) {
  3277. const lines = markdown.split(/(?:\n|\r|\r\n)/);
  3278. const state = new MDState(lines, this.#blockReadersByPriority,
  3279. this.#inlineReadersByTokenPriority, this.#inlineReadersBySubstitutePriority);
  3280. for (const reader of this.#blockReaders) {
  3281. reader.preProcess(state);
  3282. }
  3283. for (const reader of this.#inlineReaders) {
  3284. reader.preProcess(state);
  3285. }
  3286. const blocks = state.readBlocks();
  3287. for (const reader of this.#blockReaders) {
  3288. reader.postProcess(state, blocks);
  3289. }
  3290. for (const reader of this.#inlineReaders) {
  3291. reader.postProcess(state, blocks);
  3292. }
  3293. return MDBlock.toHTML(blocks, state);
  3294. }
  3295. }