PHP and Javascript implementations of a simple markdown parser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.js 92KB

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