PHP and Javascript implementations of a simple markdown parser
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

markdown.js 93KB

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