| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430 |
- // FIXME: Nested blockquotes require blank line
- // TODO: HTML tags probably need better handling. Consider whether interior of matched tags should be interpreted as markdown.
- // TODO: Test broken/incomplete syntax thoroughly
- // TODO: Sanity checks on loops/recursion
- // TODO: Spreadsheet functions in tables
- // TODO: Support document differentiators for CSS identifiers (using markdown to render 2+ documents in the same page, need ids to be unique within the page)
- // TODO: Support language marker in ``` to at least add a CSS class to the <pre>
- // TODO: Better way to detect start of new block inside of list item without line break
-
- class MDTokenType {
- static Text = new MDTokenType('Text');
- static Whitespace = new MDTokenType('Whitespace');
-
- static Underscore = new MDTokenType('Underscore');
- static Asterisk = new MDTokenType('Asterisk');
- static Slash = new MDTokenType('Slash');
- static Tilde = new MDTokenType('Tilde');
- static Bang = new MDTokenType('Bang');
- static Backtick = new MDTokenType('Backtick');
-
- static Label = new MDTokenType('Label'); // content=label
- static URL = new MDTokenType('URL'); // content=URL, extra=title
- static Email = new MDTokenType('Email'); // content=email address, extra=title
- static SimpleLink = new MDTokenType('SimpleLink'); // content=URL
- static SimpleEmail = new MDTokenType('SimpleEmail'); // content=email address
- static Footnote = new MDTokenType('Footnote'); // content=symbol
- static Modifier = new MDTokenType('Modifier'); // content
-
- static HTMLTag = new MDTokenType('HTMLTag'); // content=tag string, tag=MDHTMLTag
-
- static META_AnyNonWhitespace = new MDTokenType('METAAnyNonWhitespace');
- static META_OptionalWhitespace = new MDTokenType('METAOptionalWhitespace');
-
- /** @type {string} */
- name;
-
- /**
- * @param {string} name
- */
- constructor(name) {
- this.name = name;
- }
-
- toString() {
- return `${this.constructor.name}.${this.name}`;
- }
-
- equals(other) {
- return (other instanceof MDTokenType) && other.name == this.name;
- }
- }
-
- class MDToken {
- /**
- * The original token string.
- * @type {string}
- */
- original;
- /** @type {MDTokenType} */
- type;
- /** @type {string|null} */
- content;
- /** @type {string|null} */
- extra;
- /** @type {MDHTMLTag|null} */
- tag;
- /** @type {MDTagModifier|null} */
- modifier;
-
- /**
- * @param {string} original
- * @param {MDTokenType} type
- * @param {string|MDTagModifier|null} content
- * @param {string|null} extra
- * @param {MDHTMLTag|null} tag
- */
- constructor(original, type, content=null, extra=null, tag=null) {
- this.original = original;
- this.type = type;
- if (content instanceof MDTagModifier) {
- this.content = null;
- this.modifier = content;
- } else {
- this.content = content;
- this.modifier = null;
- }
- this.extra = extra;
- this.tag = tag;
- }
-
- toString() {
- return `(${this.constructor.name} type=${this.type.toString()} content=${this.content})`;
- }
-
- /**
- * Searches an array of MDToken for the given pattern of MDTokenTypes.
- * If found, returns an object with the given keys.
- * - `tokens: MDToken[]` - the subarray of `tokensToSearch` that match the pattern
- * - `index: number` - index into `tokensToSearch` of first matching token
- *
- * @param {MDToken[]|MDSpan[]} tokensToSearch
- * @param {MDTokenType[]} pattern
- * @param {number} startIndex
- * @returns {object|null} match
- */
- static findFirstTokens(tokensToSearch, pattern, startIndex=0) {
- var matched = [];
- for (var t = startIndex; t < tokensToSearch.length; t++) {
- var matchedAll = true;
- matched = [];
- var patternOffset = 0;
- for (var p = 0; p < pattern.length; p++) {
- var t0 = t + p + patternOffset;
- if (t0 >= tokensToSearch.length) return null;
- let token = tokensToSearch[t0];
- let elem = pattern[p];
- if (elem == MDTokenType.META_OptionalWhitespace) {
- if (token instanceof MDToken && token.type == MDTokenType.Whitespace) {
- matched.push(token);
- } else {
- patternOffset--;
- }
- } else if (elem == MDTokenType.META_AnyNonWhitespace) {
- if (token instanceof MDToken && token.type == MDTokenType.Whitespace) {
- matchedAll = false;
- break;
- }
- matched.push(token);
- } else {
- if (!(token instanceof MDToken) || token.type != elem) {
- matchedAll = false;
- break;
- }
- matched.push(token);
- }
- }
- if (matchedAll) {
- return {
- 'tokens': matched,
- 'index': t,
- };
- }
- }
- return null;
- }
-
- /**
- * Searches an array of MDToken for a given starting pattern and ending
- * pattern and returns match info about both and the tokens in between.
- *
- * If `contentValidator` is specified, it will be called with the content
- * tokens of a potential match. If the validator returns `true`, the result
- * will be accepted and returned by this method. If the validator returns
- * `false`, this method will keep looking for another matching pair. If no
- * validator is given the first match will be returned regardless of content.
- *
- * If a match is found, returns an object with the given keys:
- * - `startTokens: MDToken[]` - tokens that matched `startPattern`
- * - `contentTokens: MDToken[]` - tokens between the start and end pattern. May be an empty array.
- * - `endTokens: MDToken[]` - tokens that matched `endPattern`
- * - `startIndex: number` - index into `tokensToSearch` where `startPattern` begins
- * - `contentIndex: number` - index into `tokensToSearch` of the first token that is between the start and end patterns
- * - `endIndex: number` - index into `tokensToSearch` where `endPattern` begins
- * - `totalLength: number` - total number of matched tokens
- *
- * @param {MDToken[]} tokensToSearch - array of `MDToken` to search in
- * @param {MDTokenType[]} startPattern - array of `MDTokenType` to find first
- * @param {MDTokenType[]} endPattern - array of `MDTokenType` to find positioned after `startPattern`
- * @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
- * @param {number} startIndex - token index where searching should begin
- * @returns {object|null} match object
- */
- static findPairedTokens(tokensToSearch, startPattern, endPattern, contentValidator=null, startIndex=0) {
- for (var s = startIndex; s < tokensToSearch.length; s++) {
- var startMatch = this.findFirstTokens(tokensToSearch, startPattern, s);
- if (startMatch === null) return null;
- var endStart = startMatch.index + startMatch.tokens.length;
- while (endStart < tokensToSearch.length) {
- var endMatch = this.findFirstTokens(tokensToSearch, endPattern, endStart);
- if (endMatch === null) break;
- var contents = tokensToSearch.slice(startMatch.index + startMatch.tokens.length, endMatch.index);
- if (contents.length > 0 && (contentValidator === null || contentValidator(contents))) {
- return {
- 'startTokens': startMatch.tokens,
- 'contentTokens': contents,
- 'endTokens': endMatch.tokens,
- 'startIndex': startMatch.index,
- 'contentIndex': startMatch.index + startMatch.tokens.length,
- 'endIndex': endMatch.index,
- 'totalLength': endMatch.index + endMatch.tokens.length - startMatch.index,
- };
- } else {
- // Contents rejected. Try next end match.
- endStart = endMatch.index + 1;
- }
- }
- // No end matches. Increment start match.
- s = startMatch.index;
- }
- return null;
- }
-
- equals(other) {
- if (!(other instanceof MDToken)) return false;
- if (other.original !== this.original) return false;
- if (!other.type.equals(this.type)) return false;
- if (other.content !== this.content) return false;
- if (other.extra !== this.extra) return false;
- if (!MDUtils.equal(other.tag, this.tag)) return false;
- if (!MDUtils.equals(other.modifier, this.modifier)) return false;
- return true
- }
- }
-
- class MDUtils {
- // Modified from https://urlregex.com/ to remove capture groups. Matches fully qualified URLs only.
- static baseURLRegex = /(?:(?:(?:[a-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[a-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[a-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/i;
- // Modified from https://emailregex.com/ to remove capture groups.
- 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;
-
- /**
- * Escapes special HTML characters.
- *
- * @param {string} str - string to escape
- * @param {boolean} encodeNewlinesAsBreaks - whether to convert newline characters to `<br>` tags
- * @returns {string} escaped HTML
- */
- static escapeHTML(str, encodeNewlinesAsBreaks=false) {
- var html = str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
- if (encodeNewlinesAsBreaks) {
- html = html.replace(/\n/g, "<br>\n");
- }
- return html;
- }
-
- /**
- * Encodes characters as HTML numeric entities to make it marginally more
- * difficult for web scrapers to grab sensitive info.
- *
- * @param {string} text - text to escape
- * @returns {string} escaped HTML
- */
- static escapeObfuscated(text) {
- var html = '';
- for (var p = 0; p < text.length; p++) {
- const cp = text.codePointAt(p);
- html += `&#${cp};`;
- }
- return html;
- }
-
- /**
- * Strips one or more leading indents from a line or lines of markdown. An
- * indent is defined as 4 spaces or one tab. Incomplete indents (i.e. 1-3
- * spaces) are treated like one indent level.
- *
- * @param {string|string[]} line - string or strings to strip
- * @param {number} levels - how many indent levels to strip
- * @returns {string|string[]} stripped lines
- */
- static stripIndent(line, levels=1) {
- const regex = new RegExp(`^(?: {1,4}|\t){${levels}}`);
- return (line instanceof Array) ? line.map((l) => l.replace(regex, '')) : line.replace(regex, '');
- }
-
- /**
- * Returns a copy of an array without any whitespace-only lines at the end.
- *
- * @param {String[]} lines - text lines
- * @returns {String[]} - text lines without trailing blank lines
- */
- static withoutTrailingBlankLines(lines) {
- var stripped = lines.slice();
- while (stripped.length > 0 && stripped[stripped.length - 1].trim().length == 0) {
- stripped.pop();
- }
- return stripped;
- }
-
- /**
- * Tests if an array of lines contains at least one blank. A blank line
- * can contain whitespace.
- *
- * @param {String[]} lines
- * @returns {boolean} whether `lines` contains any whitespace-only lines
- */
- static containsBlankLine(lines) {
- for (const line of lines) {
- if (line.trim().length == 0) return true;
- }
- return false;
- }
-
- /**
- * Counts the number of indent levels in a line of text. Partial indents
- * (1 to 3 spaces) are counted as one indent level unless `fullIndentsOnly`
- * is `true`.
- *
- * @param {string} line - line of markdown
- * @param {boolean} fullIndentsOnly - whether to only count full indent levels (4 spaces or a tab)
- * @returns {number} number of indent levels found
- */
- static countIndents(line, fullIndentsOnly=false) {
- // normalize indents to tabs
- return line.replace(fullIndentsOnly
- ? /(?: {4}|\t)/g
- : /(?: {1,4}|\t)/g,
- "\t")
- // remove content after indent
- .replace(/^(\t*)(.*?)$/, '$1')
- // count tabs
- .length;
- }
-
- /**
- * Attempts to parse a label from the beginning of `line`. A label is of the
- * form `[content]`. If found, returns an array with element 0 being the
- * entire label and element 1 being the content of the label.
- *
- * @param {string} line
- * @returns {string[]|null} match groups or null if not found
- */
- static tokenizeLabel(line) {
- if (!line.startsWith('[')) return null;
- var parenCount = 0;
- var bracketCount = 0;
- for (var p = 1; p < line.length; p++) {
- let ch = line.substring(p, p + 1);
- if (ch == '\\') {
- p++;
- } else if (ch == '(') {
- parenCount++;
- } else if (ch == ')') {
- parenCount--;
- if (parenCount < 0) return null;
- } else if (ch == '[') {
- bracketCount++;
- } else if (ch == ']') {
- if (bracketCount > 0) {
- bracketCount--;
- } else {
- return [ line.substring(0, p + 1), line.substring(1, p) ];
- }
- }
- }
- return null;
- }
-
- static #urlWithTitleRegex = /^\((\S+?)\s+"(.*?)"\)/i; // 1=URL, 2=title
- static #urlRegex = /^\((\S+?)\)/i; // 1=URL
-
- /**
- * Attempts to parse a URL from the beginning of `line`. A URL is of the
- * form `(url)` or `(url "title")`. If found, returns an array with element
- * 0 being the entire URL token, 1 is the URL, 2 is the optional title.
- *
- * @param {string} line
- * @returns {string[]} token tuple
- */
- static tokenizeURL(line) {
- var groups;
- if (groups = this.#urlWithTitleRegex.exec(line)) {
- if (this.tokenizeEmail(line)) return null; // make sure it's not better described as an email address
- return groups;
- }
- if (groups = this.#urlRegex.exec(line)) {
- if (this.tokenizeEmail(line)) return null;
- return [...groups, null];
- }
- return null;
- }
-
- static #emailWithTitleRegex = new RegExp("^\\(\\s*(" + MDUtils.baseEmailRegex.source + ")\\s+\"(.*?)\"\\s*\\)", "i"); // 1=email, 2=title
- static #emailRegex = new RegExp("^\\(\\s*(" + MDUtils.baseEmailRegex.source + ")\\s*\\)", "i"); // 1=email
-
- /**
- * Attempts to parse an email address from the beginning of `line`. An
- * email address is of the form `(user@example.com)` or `(user@example.com "link title")`.
- * If found, returns an array with element 0 being the entire token, 1 is the
- * email address, and 2 is the optional link title.
- *
- * @param {string} line
- * @returns {string[]} token tuple
- */
- static tokenizeEmail(line) {
- var groups;
- if (groups = this.#emailWithTitleRegex.exec(line)) {
- return groups;
- }
- if (groups = this.#emailRegex.exec(line)) {
- return [...groups, null];
- }
- return null;
- }
-
- /**
- * Describes the type of a variable for debugging.
- *
- * @param {any} value - value
- * @returns {String} description of type
- */
- static typename(value) {
- if (value === null) return 'null';
- if (value instanceof Object) {
- return value.constructor.name;
- }
- return typeof value;
- }
-
- static #equalArrays(a, b) {
- if (a === b) return true;
- if (!(a instanceof Array) || !(b instanceof Array)) return false;
- if (a == null || b == null) return false;
- if (a.length != b.length) return false;
- for (var i = 0; i < a.length; i++) {
- if (!this.equal(a[i], b[i])) return false;
- }
- return true;
- }
-
- static #equalObjects(a, b) {
- if (a === b) return true;
- if (!(a instanceof Object) || !(b instanceof Object)) return false;
- if (a == null || b == null) return false;
- if (a.equals !== undefined) {
- return a.equals(b);
- }
- for (const key of Object.keys(a)) {
- if (!this.equal(a[key], b[key])) return false;
- }
- for (const key of Object.keys(b)) {
- if (!this.equal(a[key], b[key])) return false;
- }
- return true;
- }
-
- /**
- * Tests for equality on lots of different kinds of values including objects
- * and arrays. Will use `.equals` on objects that implement it.
- *
- * @param {any} a
- * @param {any} b
- * @returns {boolean}
- */
- static equal(a, b) {
- if (a instanceof Array && b instanceof Array) {
- return this.#equalArrays(a, b);
- }
- if (a instanceof Object && b instanceof Object) {
- return this.#equalObjects(a, b);
- }
- return a == b;
- }
- }
-
-
- // -- Block readers ---------------------------------------------------------
-
-
- /**
- * Base class for reading from an array of markdown lines and finding a block
- * of a given type. Readers are checked in `priority` order and `readBlock` is
- * called to see the reader implementation recognizes a particular kind of block
- * at the given line pointer, returning that block if so or null if not.
- *
- * Inline markdown is processed in a separate stage by `MDInlineReader`.
- */
- class MDBlockReader {
- /** @type {number} */
- #priority;
-
- /**
- * A unitless relative priority value that determines which readers are
- * tried first. Lower values are tried first. In the range of 0.0 to 100.0.
- * @type {number} priority
- */
- get priority() { return this.#priority; }
-
- /**
- * Creates a block reader.
- *
- * When overriding the constructor, it is suggested to allow the caller to
- * specify an optional custom priority value, falling back to a reasonable
- * default if not specified.
- *
- * @param {number} priority
- */
- constructor(priority) {
- this.#priority = priority;
- }
-
- /**
- * Attempts to read a block of this type from the given read state. If
- * successful, the state's line pointer should be incremented to the line
- * just after the last line of the block and the read block should be
- * returned.
- *
- * @param {MDState} state - read state
- * @returns {MDBlock|null} the read block
- */
- readBlock(state) {
- throw Error(`Abstract readBlock must be overridden in ${this.constructor.name}`);
- }
-
- /**
- * Called after the full document has been generated for optional
- * post-processing.
- *
- * @param {MDState} state
- * @param {MDBlock[]} blocks - top-level document block list
- */
- postProcess(state, blocks) {
- // no op
- }
- }
-
- /**
- * Reads markdown blocks for headers denoted with the underline syntax.
- *
- * Example:
- *
- * > ```markdown
- * > Header 1
- * > ========
- * > ```
- */
- class MDUnderlinedHeaderBlockReader extends MDBlockReader {
- constructor(priority=0.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- */
- readBlock(state) {
- var p = state.p;
- if (!state.hasLines(2)) return null;
- var modifier;
- let contentLine = state.lines[p++].trim();
- [contentLine, modifier] = MDTagModifier.fromLine(contentLine);
- let underLine = state.lines[p++].trim();
- if (contentLine == '') return null;
- if (/^=+$/.exec(underLine)) {
- state.p = p;
- let block = new MDHeaderBlock(1, state.inlineMarkdownToSpan(contentLine));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- if (/^\-+$/.exec(underLine)) {
- state.p = p;
- let block = new MDHeaderBlock(2, state.inlineMarkdownToSpan(contentLine));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- return null;
- }
- }
-
- /**
- * Reads markdown blocks for headers denoted with hash marks. Header levels 1 to
- * 6 are supported.
- *
- * Examples:
- *
- * > ```markdown
- * > # Header 1
- * >
- * > ## Header 2
- * >
- * > # Enclosing Hashes Are Optional #
- * >
- * > ## Trailing Hashes Don't Have to Match in Number ####
- * > ```
- */
- class MDHashHeaderBlockReader extends MDBlockReader {
- static #hashHeaderRegex = /^(#{1,6})\s*([^#].*?)\s*\#*\s*$/; // 1=hashes, 2=content
-
- constructor(priority=5.0) {
- super(priority);
- }
-
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = MDTagModifier.fromLine(line);
- var groups = MDHashHeaderBlockReader.#hashHeaderRegex.exec(line);
- if (groups === null) return null;
- state.p = p;
- const level = groups[1].length;
- const content = groups[2];
- let block = new MDHeaderBlock(level, new MDInlineBlock(state.inlineMarkdownToSpan(content)));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- }
-
- /**
- * Reads markdown blocks for blockquoted text.
- *
- * Example:
- *
- * > ```markdown
- * > > Blockquoted text
- * > ```
- */
- class MDBlockQuoteBlockReader extends MDBlockReader {
- constructor(priority=10.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- */
- readBlock(state) {
- var blockquoteLines = [];
- var p = state.p;
- while (p < state.lines.length) {
- let line = state.lines[p++];
- if (line.startsWith(">")) {
- blockquoteLines.push(line);
- } else {
- break;
- }
- }
- if (blockquoteLines.length > 0) {
- let contentLines = blockquoteLines.map(function(line) {
- return line.substring(1).replace(/^ {0,3}\t?/, '');
- });
- let substate = state.copy(contentLines);
- let quotedBlocks = substate.readBlocks();
- state.p = p;
- return new MDBlockquoteBlock(quotedBlocks);
- }
- return null;
- }
- }
-
- /**
- * Abstract base class for ordered and unordered lists.
- */
- class MDBaseListBlockReader extends MDBlockReader {
- constructor(priority) {
- super(priority);
- }
-
- #readItemLines(state, firstLineStartPos) {
- var p = state.p;
- var lines = [];
- var seenBlankLine = false;
- var stripTrailingBlankLines = true;
- while (state.hasLines(1, p)) {
- const isFirstLine = p == state.p;
- var line = state.lines[p++];
- if (isFirstLine) {
- line = line.substring(firstLineStartPos);
- }
- if (/^(?:\*|\+|\-|\d+\.)\s+/.exec(line)) {
- // Found next list item
- stripTrailingBlankLines = false; // because this signals extra spacing intended
- break;
- }
- const isBlankLine = line.trim().length == 0;
- const isIndented = /^\s+\S/.exec(line) !== null;
- if (isBlankLine) {
- seenBlankLine = true;
- } else if (!isIndented && seenBlankLine) {
- // Post-list content
- break;
- }
- lines.push(line);
- }
- lines = MDUtils.withoutTrailingBlankLines(lines);
- return MDUtils.stripIndent(lines);
- }
-
- /**
- * @param {MDState} state
- * @param {number} firstLineStart
- * @return {MDBlock}
- */
- readListItemContent(state, firstLineStartPos) {
- const itemLines = this.#readItemLines(state, firstLineStartPos);
- state.p += itemLines.length;
-
- if (itemLines.length == 1) {
- return new MDInlineBlock(state.inlineMarkdownToSpans(itemLines[0]));
- }
-
- const hasBlankLines = itemLines.filter((line) => line.trim().length == 0).length > 0;
- if (hasBlankLines) {
- const substate = state.copy(itemLines);
- const blocks = substate.readBlocks();
- return (blocks.length == 1) ? blocks[0] : new MDMultiBlock(blocks);
- }
-
- // Multiline content with no blank lines. Search for new block
- // boundaries without the benefit of a blank line to demarcate it.
- for (var p = 1; p < itemLines.length; p++) {
- const line = itemLines[p];
- if (/^(?:\*|\-|\+|\d+\.)\s+/.exec(line)) {
- // Nested list found
- const firstBlock = new MDInlineBlock(state.inlineMarkdownToSpans(itemLines.slice(0, p).join("\n")));
- const substate = state.copy(itemLines.slice(p));
- const blocks = substate.readBlocks();
- return new MDMultiBlock([ firstBlock, ...blocks ]);
- }
- }
-
- // Ok, give up and just do a standard block read
- {
- const substate = state.copy(itemLines);
- const blocks = substate.readBlocks();
- return (blocks.length == 1) ? blocks[0] : new MDMultiBlock(blocks);
- }
- }
-
- readBlock(state) {
- throw new Error(`Abstract readBlock must be overridden in ${this.constructor.name}`);
- }
- }
-
- /**
- * Block reader for unordered (bulleted) lists.
- *
- * Example:
- *
- * > ```markdown
- * > * First item
- * > * Second item
- * > * Third item
- * > ```
- */
- class MDUnorderedListBlockReader extends MDBaseListBlockReader {
- static #unorderedListRegex = /^([\*\+\-]\s+)(.*)$/; // 1=bullet, 2=content
-
- constructor(priority=15.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- * @returns {MDListItemBlock|null}
- */
- #readUnorderedListItem(state) {
- var p = state.p;
- let line = state.lines[p];
- let groups = MDUnorderedListBlockReader.#unorderedListRegex.exec(line);
- if (groups === null) return null;
- const firstLineOffset = groups[1].length;
- return new MDListItemBlock(this.readListItemContent(state, firstLineOffset));
- }
-
- readBlock(state) {
- var items = [];
- var item = null;
- do {
- item = this.#readUnorderedListItem(state);
- if (item) items.push(item);
- } while (item);
- if (items.length == 0) return null;
- return new MDUnorderedListBlock(items);
- }
- }
-
- /**
- * Block reader for ordered (numbered) lists. The number of the first item is
- * used to begin counting. The subsequent items increase by 1, regardless of
- * their value.
- *
- * Example:
- *
- * > ```markdown
- * > 1. First
- * > 2. Second
- * > 3. Third
- * > ```
- */
- class MDOrderedListBlockReader extends MDBaseListBlockReader {
- static #orderedListRegex = /^(\d+)(\.\s+)(.*)$/; // 1=number, 2=dot, 3=content
-
- constructor(priority=16.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- * @returns {MDListItemBlock|null}
- */
- #readOrderedListItem(state) {
- var p = state.p;
- let line = state.lines[p];
- let groups = MDOrderedListBlockReader.#orderedListRegex.exec(line);
- if (groups === null) return null;
- const ordinal = parseInt(groups[1]);
- const firstLineOffset = groups[1].length + groups[2].length;
- return new MDListItemBlock(this.readListItemContent(state, firstLineOffset), ordinal);
- }
-
- readBlock(state) {
- var items = [];
- var item = null;
- do {
- item = this.#readOrderedListItem(state);
- if (item) items.push(item);
- } while (item);
- if (items.length == 0) return null;
- return new MDOrderedListBlock(items, items[0].ordinal);
- }
- }
-
- /**
- * Block reader for code blocks denoted by pairs of triple tickmarks.
- *
- * Example:
- *
- * > ```markdown
- * > \`\`\`
- * > function formattedAsCode() {
- * > }
- * > \`\`\`
- * > ```
- */
- class MDFencedCodeBlockReader extends MDBlockReader {
- constructor(priority=20.0) {
- super(priority);
- }
-
- readBlock(state) {
- if (!state.hasLines(2)) return null;
- var p = state.p;
- let openFenceLine = state.lines[p++];
- var modifier;
- [openFenceLine, modifier] = MDTagModifier.fromLine(openFenceLine);
- if (openFenceLine.trim() != '```') return null;
- var codeLines = [];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (line.trim() == '```') {
- state.p = p;
- let block = new MDCodeBlock(codeLines.join("\n"));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- codeLines.push(line);
- }
- return null;
- }
- }
-
- /**
- * Block reader for code blocks denoted by indenting text.
- *
- * Example (indent spaces rendered visibly for clarity):
- *
- * > ```markdown
- * > ⎵⎵⎵⎵function formattedAsCode() {
- * > ⎵⎵⎵⎵}
- * > ```
- */
- class MDIndentedCodeBlockReader extends MDBlockReader {
- constructor(priority=21.0) {
- super(priority);
- }
-
- readBlock(state) {
- var p = state.p;
- var codeLines = [];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (MDUtils.countIndents(line, true) < 1) {
- p--;
- break;
- }
- codeLines.push(MDUtils.stripIndent(line));
- }
- if (codeLines.length == 0) return null;
- state.p = p;
- return new MDCodeBlock(codeLines.join("\n"));
- }
- }
-
- /**
- * Block reader for horizontal rules. Composed of three or more hypens or
- * asterisks on a line by themselves, with or without intermediate whitespace.
- *
- * Examples:
- *
- * > ```markdown
- * > ---
- * >
- * > - - -
- * >
- * > * * * * *
- * >
- * > ****
- * > ```
- */
- class MDHorizontalRuleBlockReader extends MDBlockReader {
- static #horizontalRuleRegex = /^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;
-
- constructor(priority=25.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- * @returns {MDBlock|null}
- */
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = MDTagModifier.fromLine(line);
- if (MDHorizontalRuleBlockReader.#horizontalRuleRegex.exec(line)) {
- state.p = p;
- let block = new MDHorizontalRuleBlock();
- if (modifier) modifier.applyTo(block);
- return block;
- }
- return null;
- }
- }
-
- /**
- * Block reader for tables.
- *
- * Examples:
- *
- * > ```markdown
- * > Name | Age
- * > --- | ---
- * > Joe | 34
- * > Alice | 25
- * >
- * > | Leading | And Trailing |
- * > | - | - |
- * > | Required | for single column tables |
- * >
- * > | Left aligned column | Center aligned | Right aligned |
- * > | :-- | :--: | --: |
- * > | Joe | x | 34 |
- * > ```
- */
- class MDTableBlockReader extends MDBlockReader {
- constructor(priority=30.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- * @param {boolean} isHeader
- * @return {MDTableRowBlock|null}
- */
- #readTableRow(state, isHeader) {
- if (!state.hasLines(1)) return null;
- var p = state.p;
- let line = MDTagModifier.strip(state.lines[p++].trim());
- if (/.*\|.*/.exec(line) === null) return null;
- if (line.startsWith('|')) line = line.substring(1);
- if (line.endsWith('|')) line = line.substring(0, line.length - 1);
- let cellTokens = line.split('|');
- let cells = cellTokens.map(function(token) {
- let content = state.inlineMarkdownToSpan(token.trim());
- return isHeader ? new MDTableHeaderCellBlock(content) : new MDTableCellBlock(content);
- });
- state.p = p;
- return new MDTableRowBlock(cells);
- }
-
- /**
- * @param {string} line
- * @returns {string[]}
- */
- #parseColumnAlignments(line) {
- line = line.trim();
- if (line.startsWith('|')) line = line.substring(1);
- if (line.endsWith('|')) line = line.substring(0, line.length - 1);
- return line.split(/\s*\|\s*/).map(function(token) {
- if (token.startsWith(':')) {
- if (token.endsWith(':')) {
- return MDTableCellBlock.AlignCenter;
- }
- return MDTableCellBlock.AlignLeft;
- } else if (token.endsWith(':')) {
- return MDTableCellBlock.AlignRight;
- }
- return null;
- });
- }
-
- static #tableDividerRegex = /^\s*[|]?\s*(?:[:]?-+[:]?)(?:\s*\|\s*[:]?-+[:]?)*\s*[|]?\s*$/;
-
- readBlock(state) {
- if (!state.hasLines(2)) return null;
- let startP = state.p;
- let firstLine = state.lines[startP];
- var modifier = MDTagModifier.fromLine(firstLine)[1];
- let headerRow = this.#readTableRow(state, true);
- if (headerRow === null) {
- state.p = startP;
- return null;
- }
- let dividerLine = state.lines[state.p++];
- let dividerGroups = MDTableBlockReader.#tableDividerRegex.exec(dividerLine);
- if (dividerGroups === null) {
- state.p = startP;
- return null;
- }
- let columnAlignments = this.#parseColumnAlignments(dividerLine);
- headerRow.applyAlignments(columnAlignments);
- var bodyRows = [];
- while (state.hasLines(1)) {
- let row = this.#readTableRow(state, false);
- if (row === null) break;
- row.applyAlignments(columnAlignments);
- bodyRows.push(row);
- }
- let table = new MDTableBlock(headerRow, bodyRows);
- if (modifier) modifier.applyTo(table);
- return table;
- }
- }
-
- /**
- * Block reader for definition lists. Definitions go directly under terms starting
- * with a colon.
- *
- * Example:
- *
- * > ```markdown
- * > markdown
- * > : a language for generating HTML from simplified syntax
- * > parser
- * > : code that converts human-readable code into machine language
- * > ```
- */
- class MDDefinitionListBlockReader extends MDBlockReader {
- constructor(priority=35.0) {
- super(priority);
- }
-
- readBlock(state) {
- var p = state.p;
- var groups;
- var termCount = 0;
- var definitionCount = 0;
- var defLines = [];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (line.trim().length == 0) {
- break;
- }
- if (/^\s+/.exec(line)) {
- if (defLines.length == 0) return null;
- defLines[defLines.length - 1] += "\n" + line;
- } else if (/^:\s+/.exec(line)) {
- defLines.push(line);
- definitionCount++;
- } else {
- defLines.push(line);
- termCount++;
- }
- }
- if (termCount == 0 || definitionCount == 0) return null;
- let blocks = defLines.map(function(line) {
- if (groups = /^:\s+(.*?)$/s.exec(line)) {
- return new MDDefinitionDefinitionBlock(state.inlineMarkdownToSpans(groups[1]));
- } else {
- return new MDDefinitionTermBlock(state.inlineMarkdownToSpans(line));
- }
- });
- state.p = p;
- return new MDDefinitionListBlock(blocks);
- }
- }
-
- /**
- * Block reader for defining footnote contents. Footnotes can be defined anywhere
- * in the document but will always be rendered at the end of a page or end of
- * the document.
- *
- * Examples:
- *
- * > ```markdown
- * > [^1]: Content of a footnote. Anywhere `[^1]` appears in the
- * > main text, it will hyperlink to this content at the bottom
- * > of the document. There will also be backlinks at the end
- * > of this footnote to all references to it.
- * > ```
- */
- class MDFootnoteDefinitionBlockReader extends MDBlockReader {
- constructor(priority=40.0) {
- super(priority);
- }
-
- /**
- * @param {MDState} state
- */
- readBlock(state) {
- var p = state.p;
- let groups = /^\s*\[\^\s*([^\]]+)\s*\]:\s+(.*)\s*$/.exec(state.lines[p++]);
- if (groups === null) return null;
- let symbol = groups[1];
- let def = groups[2];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (/^\s+/.exec(line)) {
- def += "\n" + line;
- } else {
- p--;
- break;
- }
- }
- state.p = p;
- let content = state.inlineMarkdownToSpan(def);
- state.defineFootnote(symbol, content);
- state.p = p;
- return new MDMultiBlock([]);
- }
- }
-
- /**
- * Block reader for abbreviation definitions. Anywhere the abbreviation appears
- * in the text will have its definition available when hovering over it.
- * Definitions can appear anywhere in the document. Their content should only
- * contain simple text, not markdown.
- *
- * Example:
- *
- * > ```markdown
- * > *[HTML]: Hyper Text Markup Language
- * > ```
- */
- class MDAbbreviationDefinitionBlockReader extends MDBlockReader {
- constructor(priority=45.0) {
- super(priority);
- }
-
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- let groups = /^\s*\*\[([^\]]+?)\]:\s+(.*?)\s*$/.exec(line);
- if (groups === null) return null;
- let abbrev = groups[1];
- let def = groups[2];
- state.defineAbbreviation(abbrev, def);
- state.p = p;
- return new MDMultiBlock([]);
- }
- }
-
- /**
- * Block reader for URL definitions. Links in the document can include a
- * reference instead of a verbatim URL so it can be defined in one place and
- * reused in many places. These can be defined anywhere in the document. Nothing
- * of the definition is rendered in the document.
- *
- * Example:
- *
- * > ```markdown
- * > [foo]: https://example.com
- * > ```
- */
- class MDURLDefinitionBlockReader extends MDBlockReader {
- constructor(priority=50.0) {
- super(priority);
- }
-
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- var symbol;
- var url;
- var title = null;
- let groups = /^\s*\[(.+?)]:\s*(\S+)\s+"(.*?)"\s*$/.exec(line);
- if (groups) {
- symbol = groups[1];
- url = groups[2];
- title = groups[3];
- } else {
- groups = /^\s*\[(.+?)]:\s*(\S+)\s*$/.exec(line);
- if (groups) {
- symbol = groups[1];
- url = groups[2];
- } else {
- return null;
- }
- }
- state.defineURL(symbol, url, title);
- state.p = p;
- return new MDInlineBlock([]);
- }
- }
-
- /**
- * Block reader for simple paragraphs. Paragraphs are separated by a blank (or
- * whitespace-only) line. This reader should be prioritized last since there
- * is no distinguishing syntax.
- *
- * Example:
- *
- * > ```markdown
- * > Lorem ipsum dolor
- * > sit amet. This is all one paragraph.
- * >
- * > Beginning of a new paragraph.
- * > ```
- */
- class MDParagraphBlockReader extends MDBlockReader {
- constructor(priority=100.0) {
- super(priority);
- }
-
- readBlock(state) {
- var paragraphLines = [];
- var p = state.p;
- var foundBlankLine = false;
- while (p < state.lines.length) {
- let line = state.lines[p++];
- if (line.trim().length == 0) {
- foundBlankLine = true;
- break;
- }
- paragraphLines.push(line);
- }
- if (state.p == 0 && p >= state.lines.length) {
- // If it's the entire document don't wrap it in a paragraph
- return null;
- }
- if (paragraphLines.length > 0) {
- state.p = p;
- let content = paragraphLines.join("\n");
- return new MDParagraphBlock(new MDInlineBlock(state.inlineMarkdownToSpans(content)));
- }
- return null;
- }
- }
-
-
- // -- Inline reader ---------------------------------------------------------
-
-
- class MDInlineReader {
- /** @type {number} */
- #tokenizePriority;
-
- /** @type {number|number[]} */
- #substitutePriority;
-
- /**
- * A unitless relative tokenizing priority value that determines which
- * readers are tried first. Lower values are tried first. Standard readers
- * are in the range of 0.0 to 100.0 but any value is valid. Longer
- * tokens should generally be prioritized over short or single character tokens.
- *
- * @returns {number} priority or priorities for tokenization
- */
- get tokenizePriority() { return this.#tokenizePriority; }
-
- /**
- * A unitless relative substitution priority value that determines which
- * readers are tried first. Lower values are tried first. If an array of
- * values is given, the same reader will be included twice in the
- * prioritization. This allows for multiple passes. Standard readers
- * are in the range of 0.0 to 100.0 but any value is valid. Priority should
- * be used to help resolve ambiguous parsings, with longer, more complex
- * constructions best prioritized before shorter, simpler ones.
- *
- * @returns {number|number[]} priority or priorities for substitution
- */
- get substitutePriority() { return this.#substitutePriority; }
-
- /**
- * @param {number} tokenizePriority
- * @param {number|number[]} substitutePriority
- */
- constructor(tokenizePriority, substitutePriority) {
- this.#tokenizePriority = tokenizePriority;
- this.#substitutePriority = substitutePriority;
- }
-
- /**
- * Attempts to read a token from the start of the given string.
- *
- * @param {string} remainingText - remainder of the current line of markdown text left to tokenize
- * @returns {MDToken|null} a token or `null` if not found
- */
- readFirstToken(state, priority, remainingText) {
- throw new Error(`Abstract readFirstToken must be overridden in ${this.constructor.name}`);
- }
-
- /**
- * Attempts to substitute one or more tokens in the given array. The given
- * array is edited in-place via `.splice` operations. It consists of mixed
- * elements of unprocessed `MDToken` and interpreted `MDSpan` elements.
- *
- * If a structure consists of inner content that is also markdown encoded,
- * those elements can be passed to `state.tokensToSpans` to resolve to an
- * array of `MDSpan` elements.
- *
- * @param {MDState} state
- * @param {number} priority - used to differentiate passes when multiple
- * values of `this.substitutePriority` are given
- * @param {MDToken[]|MDSpan[]} tokens - mixed array of `MDToken` and
- * `MDSpan` elements to be modified by reference
- * @returns {boolean} `true` if any substitutions were made; `false` if not
- */
- substituteTokens(state, priority, tokens) {
- throw new Error(`Abstract substituteTokens must be overridden in ${this.constructor.name}`);
- }
-
- /**
- * Called after the full document has been generated for optional
- * post-processing.
- *
- * @param {MDState} state
- * @param {MDBlock[]} blocks - top-level document block list
- */
- postProcess(state, blocks) {
- // no op
- }
- }
-
- /**
- * Abstract base class for readers that look for one or more delimiting tokens
- * around some content.
- */
- class MDSimplePairInlineReader extends MDInlineReader {
- constructor(tokenizePriority, substitutePriority) {
- super(tokenizePriority, substitutePriority);
- }
-
- /**
- * Attempts a substitution of a matched pair of delimiting token types.
- * If successful, the substitution is performed on `tokens` and `true` is
- * returned, otherwise `false` is returned and the array is untouched.
- *
- * If multiple `substitutePriority` values are specified, the first pass
- * will reject matches with the delimiting character inside the content
- * tokens. If a single `substitutePriority` is given or a subsequent pass
- * is performed with multiple values any contents will be accepted.
- *
- * @param {MDState} state
- * @param {number} priority
- * @param {MDToken[]} tokens
- * @param {class} spanClass
- * @param {MDTokenType} delimiter
- * @param {number} count - how many times the token is repeated to form the delimiter
- * @returns {boolean} `true` if substitution performed, `false` if not
- */
- attemptPair(state, priority, tokens, spanClass, delimiter, count=1, plaintext=false) {
- let delimiters = Array(count).fill(delimiter);
- let firstPassPriority = (this.substitutePriority instanceof Array) ? this.substitutePriority[0] : null;
- let match = MDToken.findPairedTokens(tokens, delimiters, delimiters, function(content) {
- const firstType = content[0] instanceof MDToken ? content[0].type : null;
- const lastType = content[content.length - 1] instanceof MDToken ? content[content.length - 1].type : null;
- if (firstType == MDTokenType.Whitespace) return false;
- if (lastType == MDTokenType.Whitespace) return false;
- if (priority == firstPassPriority) {
- var innerCount = 0;
- for (let token of content) {
- if (token instanceof MDToken && token.type == delimiter) innerCount++;
- }
- if ((innerCount % 2) != 0) return false;
- }
- return true;
- });
- if (match === null) return false;
- let content = (plaintext)
- ? match.contentTokens.map((token) => token.original).join('')
- : state.tokensToSpans(match.contentTokens);
- tokens.splice(match.startIndex, match.totalLength, new spanClass(content));
- return true;
- }
- }
-
- class MDStrongInlineReader extends MDSimplePairInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=[0.0, 50.0]) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- if (remainingText.startsWith('*')) return new MDToken('*', MDTokenType.Asterisk);
- if (remainingText.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- if (this.attemptPair(state, priority, tokens, MDStrongSpan, MDTokenType.Asterisk, 2)) return true;
- if (this.attemptPair(state, priority, tokens, MDStrongSpan, MDTokenType.Underscore, 2)) return true;
- return false;
- }
- }
-
- class MDEmphasisInlineReader extends MDSimplePairInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=[0.0, 50.0]) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- if (remainingText.startsWith('*')) return new MDToken('*', MDTokenType.Asterisk);
- if (remainingText.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- if (this.attemptPair(state, priority, tokens, MDEmphasisSpan, MDTokenType.Asterisk)) return true;
- if (this.attemptPair(state, priority, tokens, MDEmphasisSpan, MDTokenType.Underscore)) return true;
- return false;
- }
- }
-
- class MDCodeInlineReader extends MDSimplePairInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=0.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- if (remainingText.startsWith('`')) return new MDToken('`', MDTokenType.Backtick);
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- // ignore priority
- if (this.attemptPair(state, -1, tokens, MDCodeSpan, MDTokenType.Backtick, 2, true)) return true;
- if (this.attemptPair(state, -1, tokens, MDCodeSpan, MDTokenType.Backtick, 1, true)) return true;
- return false;
- }
- }
-
- class MDStrikethroughInlineReader extends MDSimplePairInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=[0.0, 50.0]) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- if (remainingText.startsWith('~')) return new MDToken('~', MDTokenType.Tilde);
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- if (this.attemptPair(state, priority, tokens, MDStrikethroughSpan, MDTokenType.Tilde, 2)) return true;
- if (this.attemptPair(state, priority, tokens, MDStrikethroughSpan, MDTokenType.Tilde)) return true;
- return false;
- }
- }
-
- class MDImageInlineReader extends MDInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=0.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- if (remainingText.startsWith('!')) return new MDToken('!', MDTokenType.Bang);
- var groups;
- if (groups = MDUtils.tokenizeLabel(remainingText)) {
- return new MDToken(groups[0], MDTokenType.Label, groups[1]);
- }
- if (groups = MDUtils.tokenizeURL(remainingText)) {
- return new MDToken(groups[0], MDTokenType.URL, groups[1], groups[2]);
- }
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- var match;
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Bang, MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.URL ])) {
- let alt = match.tokens[1].content;
- let url = match.tokens[match.tokens.length - 1].content;
- let title = match.tokens[match.tokens.length - 1].extra;
- tokens.splice(match.index, match.tokens.length, new MDImageSpan(url, alt, title));
- return true;
- }
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Bang, MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.Label ])) {
- let alt = match.tokens[1].content;
- let ref = match.tokens[match.tokens.length - 1].content;
- tokens.splice(match.index, match.tokens.length, new MDReferencedImageSpan(ref, alt));
- return true;
- }
- return false;
- }
- }
-
- class MDFootnoteInlineReader extends MDInlineReader {
- static #footnoteWithTitleRegex = /^\[\^([^\]]+?)\s+"(.*?)"\]/; // 1=symbol, 2=title
- static #footnoteRegex = /^\[\^([^\]]+?)\]/; // 1=symbol
-
- constructor(tokenizePriority=0.0, substitutePriority=0.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- var groups;
- if (groups = MDFootnoteInlineReader.#footnoteWithTitleRegex.exec(remainingText)) {
- return new MDToken(groups[0], MDTokenType.Footnote, groups[1], groups[2]);
- }
- if (groups = MDFootnoteInlineReader.#footnoteRegex.exec(remainingText)) {
- return new MDToken(groups[0], MDTokenType.Footnote, groups[1]);
- }
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- var match;
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Footnote ])) {
- let footnoteToken = match.tokens[0];
- tokens.splice(match.index, 1, new MDFootnoteReferenceSpan(footnoteToken.content));
- return true;
- }
- return false;
- }
-
- /**
- * @param {MDState} state
- * @param {MDBlock[]} blocks
- */
- postProcess(state, blocks) {
- var unique = 1;
- for (const block of blocks) {
- block.visitChildren(function(node) {
- if (!(node instanceof MDFootnoteReferenceSpan)) return;
- node.differentiator = unique++;
- state.registerUniqueFootnote(node.symbol, node.differentiator);
- });
- }
- if (Object.keys(state.footnotes).length == 0) return;
- blocks.push(new MDFootnoteListingBlock());
- }
- }
-
- class MDLinkInlineReader extends MDInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=0.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- var groups;
- if (groups = MDUtils.tokenizeLabel(remainingText)) {
- return new MDToken(groups[0], MDTokenType.Label, groups[1]);
- }
- if (groups = MDUtils.tokenizeEmail(remainingText)) {
- return new MDToken(groups[0], MDTokenType.Email, groups[1], groups[2]);
- }
- if (groups = MDUtils.tokenizeURL(remainingText)) {
- return new MDToken(groups[0], MDTokenType.URL, groups[1], groups[2]);
- }
- return null;
- }
-
- /**
- * @param {MDState} state
- */
- substituteTokens(state, priority, tokens) {
- var match;
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.URL ])) {
- let text = match.tokens[0].content;
- let url = match.tokens[match.tokens.length - 1].content;
- let title = match.tokens[match.tokens.length - 1].extra;
- tokens.splice(match.index, match.tokens.length, new MDLinkSpan(url, state.inlineMarkdownToSpan(text), title));
- return true;
- }
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.Email ])) {
- let text = match.tokens[0].content;
- let email = match.tokens[match.tokens.length - 1].content;
- let url = `mailto:${email}`;
- let title = match.tokens[match.tokens.length - 1].extra;
- tokens.splice(match.index, match.tokens.length, new MDLinkSpan(url, state.inlineMarkdownToSpan(text), title));
- return true;
- }
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Label, MDTokenType.META_OptionalWhitespace, MDTokenType.Label ])) {
- let text = match.tokens[0].content;
- let ref = match.tokens[match.tokens.length - 1].content;
- tokens.splice(match.index, match.tokens.length, new MDReferencedLinkSpan(ref, state.inlineMarkdownToSpan(text)));
- return true;
- }
- return false;
- }
- }
-
- class MDSimpleLinkInlineReader extends MDInlineReader {
- static #simpleEmailRegex = new RegExp("^<(" + MDUtils.baseEmailRegex.source + ")>", "i"); // 1=email
- static #simpleURLRegex = new RegExp("^<(" + MDUtils.baseURLRegex.source + ")>", "i"); // 1=URL
-
- constructor(tokenizePriority=0.0, substitutePriority=0.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- var groups;
- if (groups = MDSimpleLinkInlineReader.#simpleEmailRegex.exec(remainingText)) {
- return new MDToken(groups[0], MDTokenType.SimpleEmail, groups[1]);
- }
- if (groups = MDSimpleLinkInlineReader.#simpleURLRegex.exec(remainingText)) {
- return new MDToken(groups[0], MDTokenType.SimpleLink, groups[1]);
- }
- return null;
- }
-
- #substituteEmail(state, tokens) {
- const result = MDToken.findFirstTokens(tokens, [ MDTokenType.SimpleEmail ]);
- if (result === null) return false;
- /** @type {MDToken} */
- const token = result.tokens[0];
- const link = `mailto:${token.content}`;
- const span = new MDLinkSpan(link, new MDObfuscatedTextSpan(token.content));
- tokens.splice(result.index, 1, span);
- return true;
- }
-
- #substituteURL(state, tokens) {
- const result = MDToken.findFirstTokens(tokens, [ MDTokenType.SimpleLink ]);
- if (result === null) return false;
- /** @type {MDToken} */
- const token = result.tokens[0];
- const link = token.content;
- const span = new MDLinkSpan(link, new MDTextSpan(link));
- tokens.splice(result.index, 1, span);
- return true;
- }
-
- substituteTokens(state, priority, tokens) {
- if (this.#substituteEmail(state, tokens)) return true;
- if (this.#substituteURL(state, tokens)) return true;
- return false;
- }
- }
-
- class MDHTMLTagInlineReader extends MDInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=95.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- var tag = MDHTMLTag.fromLineStart(remainingText);
- if (tag) return new MDToken(tag.fullTag, MDTokenType.HTMLTag, tag.fullTag, null, tag);
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- const result = MDToken.findFirstTokens(tokens, [ MDTokenType.HTMLTag ]);
- if (result === null) return false;
- /** @type {MDToken} */
- const token = result.tokens[0];
- const tag = token.tag;
- const span = new MDHTMLSpan(tag.fullTag);
- tokens.splice(result.index, 1, span);
- return true;
- }
- }
-
- class MDModifierInlineReader extends MDInlineReader {
- constructor(tokenizePriority=0.0, substitutePriority=100.0) {
- super(tokenizePriority, substitutePriority);
- }
-
- readFirstToken(state, priority, remainingText) {
- var modifier = MDTagModifier.fromStart(remainingText);
- if (modifier) return new MDToken(modifier.original, MDTokenType.Modifier, modifier);
- return null;
- }
-
- substituteTokens(state, priority, tokens) {
- // Modifiers are applied elsewhere, and if they're not it's fine if they're
- // rendered as the original syntax.
- return false;
- }
- }
-
-
- // -- Blocks ----------------------------------------------------------------
-
-
- class MDBlock {
- /** @type {string[]} */
- cssClasses = [];
- /** @type {string|null} */
- cssId = null;
- /** @type {object} */
- attributes = {};
-
- /**
- * @param {MDState} state
- */
- toHTML(state) {
- throw new Error(`Abstract toHTML must be overridden in ${self.constructor.name}`);
- }
-
- htmlAttributes() {
- var html = '';
- if (this.cssClasses.length > 0) {
- html += ` class="${this.cssClasses.join(' ')}"`;
- }
- if (this.cssId !== null) {
- html += ` id="${this.cssId}"`;
- }
- for (const name in this.attributes) {
- let value = this.attributes[name];
- html += ` ${name}="${MDUtils.escapeHTML(value)}"`;
- }
- return html;
- }
-
- /**
- * @param {MDBlock[]} blocks
- * @param {MDState} state
- * @returns {string}
- */
- static toHTML(blocks, state) {
- return blocks.map((block) => block.toHTML(state)).join("\n");
- }
-
- /**
- * Visits all block and inline children of this block, calling the given
- * function with each. Should be implemented for any block with child nodes.
- *
- * @param {function} fn
- */
- visitChildren(fn) {}
- }
-
- class MDMultiBlock extends MDBlock {
- /** @type {MDBlock[]} */
- #blocks;
-
- /**
- * @param {MDBlock[]} blocks
- */
- constructor(blocks) {
- super();
- if (blocks instanceof Array) {
- this.#blocks = blocks;
- } else {
- throw new Error(`${MDUtils.typename(this)} expects MDBlock[], got ${MDUtils.typename(blocks)}`);
- }
- }
-
- toHTML(state) {
- return MDBlock.toHTML(this.#blocks, state);
- }
-
- visitChildren(fn) {
- for (const block of this.#blocks) {
- fn(block);
- block.visitChildren(fn);
- }
- }
- }
-
- class MDParagraphBlock extends MDBlock {
- /** @type {MDBlock[]} */
- #content;
-
- /**
- * @param {MDBlock|MDBlock[]} content
- */
- constructor(content) {
- super();
- if (content instanceof Array) {
- this.#content = content;
- } else if (content instanceof MDBlock) {
- this.#content = [ content ];
- } else {
- throw new Error(`${MDUtils.typename(this)} expects MDBlock[] or MDBlock, got ${MDUtils.typename(content)}`);
- }
- }
-
- toHTML(state) {
- const contentHTML = MDBlock.toHTML(this.#content, state);
- return `<p${this.htmlAttributes()}>${contentHTML}</p>\n`;
- }
-
- visitChildren(fn) {
- for (const child of this.#content) {
- fn(child);
- child.visitChildren(fn);
- }
- }
- }
-
- class MDHeaderBlock extends MDBlock {
- /** @type {number} */
- #level;
- /** @type {MDBlock[]} */
- #content;
-
- /**
- * @param {number} level
- * @param {MDBlock|MDBlock[]} content
- */
- constructor(level, content) {
- super();
- this.#level = level;
- this.#content = (content instanceof Array) ? content : [ content ];
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.#content, state);
- return `<h${this.#level}${this.htmlAttributes()}>${contentHTML}</h${this.#level}>\n`;
- }
-
- visitChildren(fn) {
- for (const child of this.#content) {
- fn(child);
- child.visitChildren(fn);
- }
- }
- }
-
- class MDBlockquoteBlock extends MDBlock {
- /** @type {MDBlock[]} */
- content;
-
- /**
- * @param {MDBlock|MDBlock[]} content
- */
- constructor(content) {
- super();
- this.content = (content instanceof MDBlock) ? [ content ] : content;
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.content, state);
- return `<blockquote${this.htmlAttributes()}>\n${contentHTML}\n</blockquote>`;
- }
-
- visitChildren(fn) {
- for (const block of this.content) {
- fn(block);
- block.visitChildren(fn);
- }
- }
- }
-
- class MDUnorderedListBlock extends MDBlock {
- /** @type {MDListItemBlock[]} */
- items;
-
- /**
- * @param {MDListItemBlock[]} items
- */
- constructor(items) {
- super();
- this.items = items;
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.items, state);
- return `<ul${this.htmlAttributes()}>\n${contentHTML}\n</ul>`;
- }
-
- visitChildren(fn) {
- for (const item of this.items) {
- fn(item);
- item.visitChildren(fn);
- }
- }
- }
-
- class MDOrderedListBlock extends MDBlock {
- /** @type {MDListItemBlock[]} */
- items;
-
- /** @type {number|null} */
- startOrdinal;
-
- /**
- * @param {MDListItemBlock[]} items
- */
- constructor(items, startOrdinal=null) {
- super();
- this.items = items;
- this.startOrdinal = startOrdinal;
- }
-
- htmlAttributes() {
- var html = super.htmlAttributes();
- if (this.startOrdinal !== null && this.startOrdinal != 1) {
- html += ` start="${this.startOrdinal}"`;
- }
- return html;
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.items, state);
- return `<ol${this.htmlAttributes()}>\n${contentHTML}\n</ol>`;
- }
-
- visitChildren(fn) {
- for (const item of this.items) {
- fn(item);
- item.visitChildren(fn);
- }
- }
- }
-
- class MDListItemBlock extends MDBlock {
- /** @type {MDBlock} */
- content;
- /** @type {number|null} */
- ordinal;
-
- /**
- * @param {MDBlock} content
- */
- constructor(content, ordinal=null) {
- super();
- this.content = content;
- this.ordinal = ordinal;
- }
-
- toHTML(state) {
- let contentHTML = this.content.toHTML(state);
- return `<li${this.htmlAttributes()}>${contentHTML}</li>`;
- }
-
- visitChildren(fn) {
- fn(this.content);
- this.content.visitChildren(fn);
- }
- }
-
- class MDCodeBlock extends MDBlock {
- /** @type {string} */
- #code;
-
- /**
- * @param {string} code
- */
- constructor(code) {
- super();
- this.#code = code;
- }
-
- toHTML(state) {
- return `<pre${this.htmlAttributes()}><code>${MDUtils.escapeHTML(this.#code)}</code></pre>`;
- }
- }
-
- class MDHorizontalRuleBlock extends MDBlock {
- toHTML(state) {
- return `<hr${this.htmlAttributes()}>\n`;
- }
- }
-
- class MDTableCellBlock extends MDBlock {
- static AlignLeft = 'left';
- static AlignCenter = 'center';
- static AlignRight = 'right';
-
- /** @type {MDBlock} */
- #content;
- /** @type {string|null} */
- align = null;
-
- /**
- * @param {MDBlock} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- #alignAttribute() {
- switch (this.align) {
- case MDTableCellBlock.AlignLeft: return ' align="left"';
- case MDTableCellBlock.AlignCenter: return ' align="center"';
- case MDTableCellBlock.AlignRight: return ' align="right"';
- default: return '';
- }
- }
-
- htmlAttributes() {
- var html = super.htmlAttributes();
- html += this.#alignAttribute();
- return html;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<td${this.htmlAttributes()}>${contentHTML}</td>`;
- }
-
- visitChildren(fn) {
- fn(this.#content);
- this.#content.visitChildren(fn);
- }
- }
-
- class MDTableHeaderCellBlock extends MDTableCellBlock {
- toHTML(state) {
- let html = super.toHTML(state);
- let groups = /^<td(.*)td>$/.exec(html);
- return `<th${groups[1]}th>`;
- }
- }
-
- class MDTableRowBlock extends MDBlock {
- /** @type {MDTableCellBlock[]|MDTableHeaderCellBlock[]} */
- #cells;
-
- /**
- * @param {MDTableCellBlock[]|MDTableHeaderCellBlock[]} cells
- */
- constructor(cells) {
- super();
- this.#cells = cells;
- }
-
- /**
- * @param {string[]} alignments
- */
- applyAlignments(alignments) {
- for (var i = 0; i < this.#cells.length; i++) {
- let cell = this.#cells[i];
- let align = i < alignments.length ? alignments[i] : null;
- cell.align = align;
- }
- }
-
- toHTML(state) {
- let cellsHTML = MDBlock.toHTML(this.#cells, state);
- return `<tr${this.htmlAttributes()}>\n${cellsHTML}\n</tr>`;
- }
-
- visitChildren(fn) {
- for (const cell of this.#cells) {
- fn(cell);
- cell.visitChildren(fn);
- }
- }
- }
-
- class MDTableBlock extends MDBlock {
- /** @type {MDTableRowBlock} */
- #headerRow;
- /** @type {MDTableRowBlock[]} */
- #bodyRows;
-
- /**
- * @param {MDTableRowBlock} headerRow
- * @param {MDTableRowBlock[]} bodyRows
- */
- constructor(headerRow, bodyRows) {
- super();
- this.#headerRow = headerRow;
- this.#bodyRows = bodyRows;
- }
-
- toHTML(state) {
- let headerRowHTML = this.#headerRow.toHTML(state);
- let bodyRowsHTML = MDBlock.toHTML(this.#bodyRows, state);
- return `<table${this.htmlAttributes()}>\n<thead>\n${headerRowHTML}\n</thead>\n<tbody>\n${bodyRowsHTML}\n</tbody>\n</table>`;
- }
-
- visitChildren(fn) {
- fn(this.#headerRow);
- this.#headerRow.visitChildren(fn);
- for (const row of this.#bodyRows) {
- fn(row);
- row.visitChildren(fn);
- }
- }
- }
-
- class MDDefinitionListBlock extends MDBlock {
- /** @type {MDBlock[]} */
- #content;
-
- /**
- * @param {MDBlock[]} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.#content, state);
- return `<dl${this.htmlAttributes()}>\n${contentHTML}\n</dl>`;
- }
-
- visitChildren(fn) {
- for (const block of this.#content) {
- fn(block);
- block.visitChildren(fn);
- }
- }
- }
-
- class MDDefinitionTermBlock extends MDBlock {
- /** @type {MDBlock[]} */
- #content;
-
- /**
- * @param {MDBlock|MDBlock[]} content
- */
- constructor(content) {
- super();
- if (content instanceof Array) {
- this.#content = content;
- } else if (content instanceof MDBlock) {
- this.#content = [ content ];
- } else {
- throw new Error(`${this.constructor.name} expects MDBlock or MDBlock[], got ${typeof content}`);
- }
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.#content, state);
- return `<dt${this.htmlAttributes()}>${contentHTML}</dt>`;
- }
-
- visitChildren(fn) {
- for (const child of this.#content) {
- fn(child);
- child.visitChildren(fn);
- }
- }
- }
-
- class MDDefinitionDefinitionBlock extends MDBlock {
- /** @type {MDBlock[]} */
- #content;
-
- /**
- * @param {MDBlock|MDBlock[]} content
- */
- constructor(content) {
- super();
- if (content instanceof Array) {
- this.#content = content;
- } else if (content instanceof MDBlock) {
- this.#content = [ content ];
- } else {
- throw new Error(`${this.constructor.name} expects MDBlock or MDBlock[], got ${typeof content}`);
- }
- }
-
- toHTML(state) {
- let contentHTML = MDBlock.toHTML(this.#content, state);
- return `<dd${this.htmlAttributes()}>${contentHTML}</dd>`;
- }
-
- visitChildren(fn) {
- for (const child of this.#content) {
- fn(child);
- child.visitChildren(fn);
- }
- }
- }
-
- class MDFootnoteListingBlock extends MDBlock {
- constructor() {
- super();
- }
-
- /**
- * @param {MDState} state
- */
- toHTML(state) {
- const footnotes = state.footnotes;
- var symbolOrder = Object.keys(footnotes);
- if (Object.keys(footnotes).length == 0) return '';
- const footnoteUniques = state.footnoteInstances;
- var html = '';
- html += '<div class="footnotes"><hr/>';
- html += '<ol>';
- for (const symbol of symbolOrder) {
- /** @type {MDBlock} */
- let content = footnotes[symbol];
- if (!content) continue;
- html += `<li value="${symbol}" id="footnote_${symbol}">${content.toHTML(state)}`;
- for (const unique of footnoteUniques[symbol]) {
- html += ` <a href="#footnoteref_${unique}" class="footnote-backref">↩︎</a>`;
- }
- html += `</li>\n`;
- }
- html += '</ol>';
- html += '</div>';
- return html;
- }
- }
-
- class MDInlineBlock extends MDBlock {
- /** @type {MDSpan[]} */
- #content;
-
- /**
- * @param {MDSpan|MDSpan[]} content
- */
- constructor(content) {
- super();
- this.#content = (content instanceof Array) ? content : [ content ];
- for (const span of this.#content) {
- if (!(span instanceof MDSpan)) {
- throw new Error(`${this.constructor.name} expects MDSpan or MDSpan[], got ${MDUtils.typename(span)}`);
- }
- }
- }
-
- toHTML(state) {
- return MDSpan.toHTML(this.#content, state);
- }
-
- visitChildren(fn) {
- for (const span of this.#content) {
- fn(span);
- span.visitChildren(fn);
- }
- }
- }
-
- // -- Spans -----------------------------------------------------------------
-
-
- class MDSpan {
- /** @type {string[]} */
- cssClasses = [];
- /** @type {string|null} */
- cssId = null;
- /** @type {object} */
- attributes = {};
-
- /**
- * @param {MDState} state
- * @returns {string} HTML
- */
- toHTML(state) {
- throw new Error(`Abstract toHTML must be overridden in ${self.constructor.name}`);
- }
-
- htmlAttributes() {
- var html = '';
- if (this.cssClasses.length > 0) {
- html += ` class="${this.cssClasses.join(' ')}"`;
- }
- if (this.cssId !== null) {
- html += ` id="${this.cssId}"`;
- }
- for (const name in this.attributes) {
- let value = this.attributes[name];
- html += ` ${name}="${MDUtils.escapeHTML(value)}"`;
- }
- return html;
- }
-
- /**
- * @param {MDSpan[]} spans
- * @param {MDState} state
- */
- static toHTML(spans, state) {
- return spans.map((span) => span.toHTML(state)).join("");
- }
-
- /**
- * Visits all inline children of this span, calling the given function with
- * each. Should be implemented for any span with child nodes.
- *
- * @param {function} fn
- */
- visitChildren(fn) {}
- }
-
- class MDMultiSpan extends MDSpan {
- /** @type {MDSpan[]} */
- content;
-
- /**
- * @param {MDSpan[]} content
- */
- constructor(content) {
- super();
- this.content = content;
- }
-
- toHTML(state) {
- return MDSpan.toHTML(this.content, state);
- }
-
- visitChildren(fn) {
- for (const span of this.content) {
- fn(span);
- span.visitChildren(fn);
- }
- }
- }
-
- class MDTextSpan extends MDSpan {
- /** @param {string} text */
- text;
-
- /**
- * @param {string} text
- */
- constructor(text) {
- super();
- this.text = text;
- }
-
- toHTML(state) {
- let html = MDUtils.escapeHTML(this.text);
- let abbrevs = state.abbreviations;
- let regexes = state.abbreviationRegexes;
- for (const abbrev in abbrevs) {
- let def = abbrevs[abbrev];
- let regex = regexes[abbrev];
- let escapedDef = MDUtils.escapeHTML(def);
- html = html.replace(regex, `<abbr title="${escapedDef}">$1</abbr>`);
- }
- return html;
- }
- }
-
- class MDHTMLSpan extends MDSpan {
- /** @param {string} html */
- html;
-
- /**
- * @param {string} html
- */
- constructor(html) {
- super();
- this.html = html;
- }
-
- toHTML(state) {
- return this.html;
- }
- }
-
- class MDObfuscatedTextSpan extends MDSpan {
- /** @param {string} text */
- text;
-
- /**
- * @param {string} text
- */
- constructor(text) {
- super();
- this.text = text;
- }
-
- toHTML(state) {
- return MDUtils.escapeObfuscated(this.text);
- }
- }
-
- class MDLinkSpan extends MDSpan {
- /** @type {string} */
- link;
- /** @type {string|null} */
- target = null;
- /** @type {MDSpan} */
- content;
- /** @type {string|null} */
- title = null;
-
- /**
- * @param {string} link
- * @param {MDSpan} content
- * @param {string|null} title
- */
- constructor(link, content, title=null) {
- super();
- this.link = link;
- this.content = content;
- this.title = title;
- }
-
- toHTML(state) {
- var escapedLink;
- if (this.link.startsWith('mailto:')) {
- escapedLink = 'mailto:' + MDUtils.escapeObfuscated(this.link.substring(7));
- } else {
- escapedLink = MDUtils.escapeHTML(this.link);
- }
- var html = `<a href="${escapedLink}"`;
- if (this.target) {
- html += ` target="${MDUtils.escapeHTML(this.target)}"`;
- }
- if (this.title) {
- html += ` title="${MDUtils.escapeHTML(this.title)}"`;
- }
- html += this.htmlAttributes();
- html += '>' + this.content.toHTML(state) + '</a>';
- return html;
- }
-
- visitChildren(fn) {
- fn(this.content);
- this.content.visitChildren(fn);
- }
- }
-
- class MDReferencedLinkSpan extends MDLinkSpan {
- /** @type {string} */
- ref;
-
- constructor(ref, content) {
- super(null, content);
- this.ref = ref;
- }
-
- /**
- * @param {MDState} state
- */
- toHTML(state) {
- if (!this.link) {
- let url = state.urls[this.ref.toLowerCase()];
- let title = state.urlTitles[this.ref.toLowerCase()];
- this.link = url;
- this.title = title || this.title;
- }
- if (this.link) {
- return super.toHTML(state);
- } else {
- let contentHTML = this.content.toHTML(state);
- return `[${contentHTML}][${this.ref}]`;
- }
- }
- }
-
- class MDEmphasisSpan extends MDSpan {
- /** @type {MDSpan[]} */
- #content;
-
- /**
- * @param {MDSpan|MDSpan[]} content
- */
- constructor(content) {
- super();
- this.#content = (content instanceof MDSpan) ? [ content ] : content;
- }
-
- toHTML(state) {
- let contentHTML = MDSpan.toHTML(this.#content, state);
- return `<em${this.htmlAttributes()}>${contentHTML}</em>`;
- }
-
- visitChildren(fn) {
- for (const span of this.#content) {
- fn(span);
- span.visitChildren(fn);
- }
- }
- }
-
- class MDStrongSpan extends MDSpan {
- /** @type {MDSpan[]} content */
- #content;
-
- /**
- * @param {MDSpan|MDSpan[]} content
- */
- constructor(content) {
- super();
- this.#content = (content instanceof MDSpan) ? [content] : content;
- }
-
- toHTML(state) {
- let contentHTML = MDSpan.toHTML(this.#content, state);
- return `<strong${this.htmlAttributes()}>${contentHTML}</strong>`;
- }
-
- visitChildren(fn) {
- for (const span of this.#content) {
- fn(span);
- span.visitChildren(fn);
- }
- }
- }
-
- class MDStrikethroughSpan extends MDSpan {
- /** @type {MDSpan[]} content */
- #content;
-
- /**
- * @param {MDSpan|MDSpan[]} content
- */
- constructor(content) {
- super();
- this.#content = (content instanceof MDSpan) ? [content] : content;
- }
-
- toHTML(state) {
- let contentHTML = MDSpan.toHTML(this.#content, state);
- return `<strike${this.htmlAttributes()}>${contentHTML}</strike>`;
- }
-
- visitChildren(fn) {
- for (const span of this.#content) {
- fn(span);
- span.visitChildren(fn);
- }
- }
- }
-
- class MDCodeSpan extends MDSpan {
- /** @type {String} content */
- #content;
-
- /**
- * @param {String} content
- */
- constructor(content) {
- super();
- if (typeof content == 'string') {
- this.#content = content;
- } else {
- throw new Error(`${this.constructor.name} content must be String, got ${typeof content}`);
- }
- }
-
- toHTML(state) {
- return `<code${this.htmlAttributes()}>${MDUtils.escapeHTML(this.#content)}</code>`;
- }
- }
-
- class MDImageSpan extends MDSpan {
- /** @type {string} */
- source;
- /** @type {string|null} */
- alt;
- /** @type {string|null} */
- title;
-
- /**
- * @param {string} source - image URL
- * @param {string} alt - alt text
- * @param {string|null} title - optional title attribute
- */
- constructor(source, alt, title=null) {
- super();
- this.source = source;
- this.alt = alt;
- this.title = title;
- }
-
- toHTML(state) {
- let html = `<img src="${MDUtils.escapeHTML(this.source)}"`;
- if (this.alt) {
- html += ` alt="${MDUtils.escapeHTML(this.alt)}"`;
- }
- if (this.title) {
- html += ` title="${MDUtils.escapeHTML(this.title)}"`;
- }
- html += this.htmlAttributes();
- html += '>';
- return html;
- }
- }
-
- class MDReferencedImageSpan extends MDImageSpan {
- /** @type {string} */
- ref;
-
- /**
- * @param {string} ref
- * @param {string|null} alt
- */
- constructor(ref, alt) {
- super(null, alt);
- this.ref = ref;
- }
-
- toHTML(state) {
- if (!this.source) {
- let url = state.urls[this.ref.toLowerCase()];
- let title = state.urlTitles[this.ref.toLowerCase()];
- this.source = url;
- this.title = title || this.title;
- }
- if (this.source) {
- return super.toHTML(state);
- } else {
- return `![${MDUtils.escapeHTML(this.alt)}][${MDUtils.escapeHTML(this.ref)}]`;
- }
- }
- }
-
- class MDFootnoteReferenceSpan extends MDSpan {
- /** @type {string} */
- symbol;
-
- /** @type {number|null} */
- differentiator = null;
-
- /**
- * @param {string} symbol
- */
- constructor(symbol) {
- super();
- this.symbol = symbol;
- }
-
- toHTML(state) {
- if (this.differentiator !== null) {
- return `<sup id="footnoteref_${this.differentiator}"><a href="#footnote_${this.symbol}">${this.symbol}</a></sup>`;
- }
- return `<!--FNREF:{${this.symbol}}-->`;
- }
- }
-
-
- // -- Other -----------------------------------------------------------------
-
-
- class MDHTMLTag {
- /** @type {string} */
- fullTag;
- /** @type {string} */
- tagName;
- /** @type {boolean} */
- isCloser;
- /** @type {object} */
- attributes;
-
- /**
- * @param {string} fullTag
- * @param {string} tagName
- * @param {boolean} isCloser
- * @param {object} attributes
- */
- constructor(fullTag, tagName, isCloser, attributes) {
- this.fullTag = fullTag;
- this.tagName = tagName;
- this.isCloser = isCloser;
- this.attributes = attributes;
- }
-
- toString() {
- return this.fullTag;
- }
-
- equals(other) {
- if (!(other instanceof MDHTMLTag)) return false;
- return other.fullTag == this.fullTag;
- }
-
- static #htmlTagNameFirstRegex = /[a-z]/i;
- static #htmlTagNameMedialRegex = /[a-z0-9]/i;
- static #htmlAttributeNameFirstRegex = /[a-z]/i;
- static #htmlAttributeNameMedialRegex = /[a-z0-9-]/i;
- static #whitespaceCharRegex = /\s/;
-
- /**
- * @param {string} line
- * @returns {MDHTMLTag|null} HTML tag if possible
- */
- static fromLineStart(line) {
- let expectOpenBracket = 0;
- let expectCloserOrName = 1;
- let expectName = 2;
- let expectAttributeNameOrEnd = 3;
- let expectEqualsOrAttributeOrEnd = 4;
- let expectAttributeValue = 5;
- let expectCloseBracket = 6;
-
- var isCloser = false;
- var tagName = '';
- var attributeName = '';
- var attributeValue = '';
- var attributeQuote = null;
- var attributes = {};
- var fullTag = null;
- let endAttribute = function() {
- if (attributeName.length > 0) {
- if (attributeValue.length > 0 || attributeQuote) {
- attributes[attributeName] = attributeValue;
- } else {
- attributes[attributeName] = true;
- }
- }
- attributeName = '';
- attributeValue = '';
- attributeQuote = null;
- };
-
- var expect = expectOpenBracket;
- for (var p = 0; p < line.length && fullTag === null; p++) {
- let ch = line.substring(p, p + 1);
- let isWhitespace = this.#whitespaceCharRegex.exec(ch) !== null;
- switch (expect) {
- case expectOpenBracket:
- if (ch != '<') return null;
- expect = expectCloserOrName;
- break;
- case expectCloserOrName:
- if (ch == '/') {
- isCloser = true;
- } else {
- p--;
- }
- expect = expectName;
- break;
- case expectName:
- if (tagName.length == 0) {
- if (this.#htmlTagNameFirstRegex.exec(ch) === null) return null;
- tagName += ch;
- } else {
- if (this.#htmlTagNameMedialRegex.exec(ch)) {
- tagName += ch;
- } else {
- p--;
- expect = (isCloser) ? expectCloseBracket : expectAttributeNameOrEnd;
- }
- }
- break;
- case expectAttributeNameOrEnd:
- if (attributeName.length == 0) {
- if (isWhitespace) {
- // skip whitespace
- } else if (ch == '/') {
- expect = expectCloseBracket;
- } else if (ch == '>') {
- fullTag = line.substring(0, p + 1);
- break;
- } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
- attributeName += ch;
- } else {
- return null;
- }
- } else if (isWhitespace) {
- expect = expectEqualsOrAttributeOrEnd;
- } else if (ch == '/') {
- endAttribute();
- expect = expectCloseBracket;
- } else if (ch == '>') {
- endAttribute();
- fullTag = line.substring(0, p + 1);
- break;
- } else if (ch == '=') {
- expect = expectAttributeValue;
- } else if (this.#htmlAttributeNameMedialRegex.exec(ch)) {
- attributeName += ch;
- } else {
- return null;
- }
- break;
- case expectEqualsOrAttributeOrEnd:
- if (ch == '=') {
- expect = expectAttributeValue;
- } else if (isWhitespace) {
- // skip whitespace
- } else if (ch == '/') {
- expect = expectCloseBracket;
- } else if (ch == '>') {
- fullTag = line.substring(0, p + 1);
- break;
- } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
- endAttribute();
- expect = expectAttributeNameOrEnd;
- p--;
- }
- break;
- case expectAttributeValue:
- if (attributeValue.length == 0) {
- if (attributeQuote === null) {
- if (isWhitespace) {
- // skip whitespace
- } else if (ch == '"' || ch == "'") {
- attributeQuote = ch;
- } else {
- attributeQuote = ''; // explicitly unquoted
- p--;
- }
- } else {
- if (ch === attributeQuote) {
- // Empty string
- endAttribute();
- expect = expectAttributeNameOrEnd;
- } else if (attributeQuote === '' && (ch == '/' || ch == '>')) {
- return null;
- } else {
- attributeValue += ch;
- }
- }
- } else {
- if (ch === attributeQuote) {
- endAttribute();
- expect = expectAttributeNameOrEnd;
- } else if (attributeQuote === '' && isWhitespace) {
- endAttribute();
- expect = expectAttributeNameOrEnd;
- } else {
- attributeValue += ch;
- }
- }
- break;
- case expectCloseBracket:
- if (isWhitespace) {
- // ignore whitespace
- } else if (ch == '>') {
- fullTag = line.substring(0, p + 1);
- break;
- }
- break;
- }
- }
- if (fullTag === null) return null;
- endAttribute();
- return new MDHTMLTag(fullTag, tagName, isCloser, attributes);
- }
- }
-
- class MDTagModifier {
- /** @type {string} */
- original;
- /** @type {string[]} */
- cssClasses = [];
- /** @type {string|null} */
- cssId = null;
- /** @type {object} */
- attributes = {};
-
- static #baseClassRegex = /\.([a-z_\-][a-z0-9_\-]*?)/i;
- static #baseIdRegex = /#([a-z_\-][a-z0-9_\-]*?)/i;
- static #baseAttributeRegex = /([a-z0-9]+?)=([^\s\}]+?)/i;
- static #baseRegex = /\{([^}]+?)}/i;
- static #leadingClassRegex = new RegExp('^' + this.#baseRegex.source, 'i');
- static #trailingClassRegex = new RegExp('^(.*?)\\s*' + this.#baseRegex.source + '\\s*$', 'i');
- static #classRegex = new RegExp('^' + this.#baseClassRegex.source + '$', 'i'); // 1=classname
- static #idRegex = new RegExp('^' + this.#baseIdRegex.source + '$', 'i'); // 1=id
- static #attributeRegex = new RegExp('^' + this.#baseAttributeRegex.source + '$', 'i'); // 1=attribute name, 2=attribute value
-
- /**
- * @param {MDBlock|MDSpan} elem
- */
- applyTo(elem) {
- if (elem instanceof MDBlock || elem instanceof MDSpan) {
- elem.cssClasses = elem.cssClasses.concat(this.cssClasses);
- if (this.cssId) elem.cssId = this.cssId;
- for (const name in this.attributes) {
- elem.attributes[name] = this.attributes[name];
- }
- }
- }
-
- equals(other) {
- if (!(other instanceof MDTagModifier)) return false;
- if (!MDUtils.equal(other.cssClasses, this.cssClasses)) return false;
- if (other.cssId !== this.cssId) return false;
- if (!MDUtils.equal(other.attributes, this.attributes)) return false;
- return true;
- }
-
- toString() {
- return this.original;
- }
-
- static #fromContents(contents) {
- let modifierTokens = contents.split(/\s+/);
- let mod = new MDTagModifier();
- mod.original = `{${contents}}`;
- var groups;
- for (const token of modifierTokens) {
- if (token.trim() == '') continue;
- if (groups = this.#classRegex.exec(token)) {
- mod.cssClasses.push(groups[1]);
- } else if (groups = this.#idRegex.exec(token)) {
- mod.cssId = groups[1];
- } else if (groups = this.#attributeRegex.exec(token)) {
- mod.attributes[groups[1]] = groups[2];
- } else {
- return null;
- }
- }
- return mod;
- }
-
- /**
- * Extracts modifier from line.
- * @param {string} line
- * @returns {Array} Tuple with remaining line and MDTagModifier.
- */
- static fromLine(line) {
- let groups = this.#trailingClassRegex.exec(line);
- if (groups === null) return [ line, null ];
- let bareLine = groups[1];
- let mod = this.#fromContents(groups[2]);
- return [ bareLine, mod ];
- }
-
- /**
- * Extracts modifier from head of string.
- * @param {string} line
- * @returns {MDTagModifier}
- */
- static fromStart(line) {
- let groups = this.#leadingClassRegex.exec(line);
- if (groups === null) return null;
- return this.#fromContents(groups[1]);
- }
-
- /**
- * @param {string} line
- * @returns {string}
- */
- static strip(line) {
- let groups = this.#trailingClassRegex.exec(line);
- if (groups === null) return line;
- return groups[1];
- }
- }
-
- class MDState {
- /** @type {string[]} */
- #lines = [];
-
- /** @type {object} */
- #abbreviations = {};
-
- /** @type {object} */
- #abbreviationRegexes = {};
-
- /**
- * symbol:string -> content:MDBlock
- * @type {object}
- */
- #footnotes = {};
-
- /**
- * symbol:string -> number[]
- * @type {object}
- */
- #footnoteInstances = {};
-
- /** @type {object} */
- #urlDefinitions = {};
-
- /** @type {object} */
- #urlTitles = {};
-
- /** @type {number} */
- p = 0;
-
- /** @type {MDState|null} */
- #parent = null;
-
- /**
- * Block readers sorted by priority.
- * @type {MDBlockReader[]} readers
- */
- #blockReadersByPriority = [];
-
- /**
- * Tuples of priority:number and MDInlineReader sorted by `tokenizePriority`.
- * @type {Array}
- */
- #inlineReadersByTokenPriority = [];
-
- /**
- * Tuples of priority:number and MDInlineReader sorted by `substitutePriority`.
- */
- #inlineReadersBySubstitutePriority = [];
-
- /** @returns {string[]} */
- get lines() { return this.#lines; }
-
- /** @returns {string|null} */
- get currentLine() { return (this.p < this.#lines.length) ? this.#lines[this.p] : null; }
-
- /** @returns {object} */
- get abbreviations() {
- return (this.#parent) ? this.#parent.abbreviations : this.#abbreviations;
- }
-
- /** @returns {object} */
- get abbreviationRegexes() {
- return (this.#parent) ? this.#parent.abbreviationRegexes : this.#abbreviationRegexes;
- }
-
- /** @returns {object} */
- get footnotes() {
- return (this.#parent) ? this.#parent.footnotes : this.#footnotes;
- }
-
- /** @returns {object} */
- get footnoteInstances() {
- return (this.#parent) ? this.#parent.footnoteInstances : this.#footnoteInstances;
- }
-
- /** @returns {object} */
- get urls() {
- return (this.#parent) ? this.#parent.urls : this.#urlDefinitions;
- }
-
- /** @returns {object} */
- get urlTitles() {
- return (this.#parent) ? this.#parent.urlTitles : this.#urlTitles;
- }
-
- /**
- * Block readers sorted by priority.
- * @type {MDBlockReader[]} readers
- */
- get blockReadersByPriority() {
- return (this.#parent) ? this.#parent.blockReadersByPriority : this.#blockReadersByPriority;
- }
- set blockReadersByPriority(newValue) {
- this.#blockReadersByPriority = newValue;
- }
-
- /**
- * Tuples of priority:number and MDInlineReader sorted by `tokenizePriority`.
- * @type {Array}
- */
- get inlineReadersByTokenPriority() {
- return (this.#parent) ? this.#parent.inlineReadersByTokenPriority : this.#inlineReadersByTokenPriority;
- }
- set inlineReadersByTokenPriority(newValue) {
- this.#inlineReadersByTokenPriority = newValue;
- }
-
- /**
- * Tuples of priority:number and MDInlineReader sorted by `substitutePriority`.
- */
- get inlineReadersBySubstitutePriority() {
- return (this.#parent) ? this.#parent.inlineReadersBySubstitutePriority : this.#inlineReadersBySubstitutePriority;
- }
- set inlineReadersBySubstitutePriority(newValue) {
- this.#inlineReadersBySubstitutePriority = newValue;
- }
-
- /**
- * @param {string[]} lines
- */
- constructor(lines) {
- this.#lines = lines;
- }
-
- /**
- * Creates a copy of this state with new lines. Useful for parsing nested
- * content.
- *
- * @param {string[]} lines
- * @returns {MDState} copied sub-state
- */
- copy(lines) {
- let cp = new MDState(lines);
- cp.#parent = this;
- return cp;
- }
-
- /**
- * Defines an abbreviation.
- *
- * @param {string} abbreviation - case sensitive
- * @param {string} definition - brief definition of the abbreviation
- */
- defineAbbreviation(abbreviation, definition) {
- if (this.#parent) {
- this.#parent.defineAbbreviation(abbreviation, definition);
- return;
- }
- this.#abbreviations[abbreviation] = definition;
- const regex = new RegExp("\\b(" + abbreviation + ")\\b", "ig");
- this.#abbreviationRegexes[abbreviation] = regex;
- }
-
- /**
- * Defines content of a footnote.
- *
- * @param {string} symbol - footnote symbol (e.g. "1")
- * @param {MDBlock} footnote - content of the footnote
- */
- defineFootnote(symbol, footnote) {
- if (this.#parent) {
- this.#parent.defineFootnote(symbol, footnote);
- } else {
- this.#footnotes[symbol] = footnote;
- }
- }
-
- /**
- * @param {string} symbol
- * @param {number} unique
- */
- registerUniqueFootnote(symbol, unique) {
- if (this.#parent) {
- this.#parent.registerUniqueFootnote(symbol, unique);
- } else {
- var uniques = this.#footnoteInstances[symbol] || [];
- uniques.push(unique);
- this.#footnoteInstances[symbol] = uniques;
- }
- }
-
- /**
- * Defines the URL for a given reference symbol.
- *
- * @param {string} symbol - reference symbol
- * @param {string} url - URL
- * @param {string|null} title - mouseover title attribute for links
- */
- defineURL(symbol, url, title=null) {
- if (this.#parent) {
- this.#parent.defineURL(symbol, url, title);
- } else {
- this.#urlDefinitions[symbol.toLowerCase()] = url;
- if (title !== null) {
- this.#urlTitles[symbol.toLowerCase()] = title;
- }
- }
- }
-
- /**
- * Tests if there are at least `minCount` lines available to read. If `p`
- * is not provided it will be relative to `this.p`.
- *
- * @param {number} minCount - minimum number of lines
- * @param {number|null} p - line pointer
- * @returns {boolean} whether at least the given number of lines is available
- */
- hasLines(minCount, p=null) {
- let relativeTo = (p === null) ? this.p : p;
- return relativeTo + minCount <= this.lines.length;
- }
-
- /**
- * Reads and returns an array of blocks from the current line pointer.
- *
- * @returns {MDBlock[]} parsed blocks
- */
- readBlocks() {
- var blocks = [];
- while (this.hasLines(1)) {
- let block = this.#readNextBlock();
- if (block) {
- blocks.push(block);
- } else {
- break;
- }
- }
- return blocks;
- }
-
- /**
- * Creates a simple `MDInlineBlock` if no other registered blocks match.
- *
- * @returns {MDInlineBlock|null} fallback block
- */
- #readFallbackBlock() {
- if (this.p >= this.lines.length) return null;
- const lines = MDUtils.withoutTrailingBlankLines(this.lines.slice(this.p));
- if (lines.length == 0) return null;
- this.p = this.lines.length;
- return new MDInlineBlock(this.inlineMarkdownToSpans(lines.join("\n")));
- }
-
- /**
- * Attempts to read one block from the current line pointer. The pointer
- * will be positioned just after the end of the block.
- *
- * @param {MDState} state
- * @returns {MDBlock}
- */
- #readNextBlock() {
- while (this.hasLines(1) && this.lines[this.p].trim().length == 0) {
- this.p++;
- }
- if (!this.hasLines(1)) return null;
- for (const reader of this.blockReadersByPriority) {
- const block = reader.readBlock(this);
- if (block) return block;
- }
- const fallback = this.#readFallbackBlock();
- return fallback;
- }
-
- static #textWhitespaceRegex = /^(\s*)(?:(\S|\S.*\S)(\s*?))?$/; // 1=leading WS, 2=text, 3=trailing WS
-
- /**
- * @param {string} line
- * @returns {MDToken[]}
- */
- #inlineMarkdownToTokens(line) {
- if (this.#parent) return this.#parent.#inlineMarkdownToTokens(line);
-
- var tokens = [];
- var text = '';
- var expectLiteral = false;
-
- /**
- * Flushes accumulated content in `text` to `tokens`.
- */
- const endText = function() {
- if (text.length == 0) return;
- const textGroups = MDState.#textWhitespaceRegex.exec(text);
- if (textGroups !== null) {
- if (textGroups[1].length > 0) {
- tokens.push(new MDToken(textGroups[1], MDTokenType.Whitespace, textGroups[1]));
- }
- if (textGroups[2] !== undefined && textGroups[2].length > 0) {
- tokens.push(new MDToken(textGroups[2], MDTokenType.Text, textGroups[2]));
- }
- if (textGroups[3] !== undefined && textGroups[3].length > 0) {
- tokens.push(new MDToken(textGroups[3], MDTokenType.Whitespace, textGroups[3]));
- }
- } else {
- tokens.push(new MDToken(text, MDTokenType.Text, text));
- }
- text = '';
- }
-
- for (var p = 0; p < line.length; p++) {
- const ch = line.substring(p, p + 1);
- const remainder = line.substring(p);
- if (expectLiteral) {
- text += ch;
- expectLiteral = false;
- continue;
- }
- if (ch == '\\') {
- expectLiteral = true;
- continue;
- }
- var found = false;
- for (const readerTuple of this.inlineReadersByTokenPriority) {
- /** @type {number} */
- const priority = readerTuple[0];
- /** @type {MDInlineReader} */
- const reader = readerTuple[1];
- const token = reader.readFirstToken(this, priority, remainder);
- if (token === null) continue;
- endText();
- tokens.push(token);
- p += token.original.length - 1;
- found = true;
- break;
- }
- if (!found) {
- text += ch;
- }
- }
- endText();
- return tokens;
- }
-
- /**
- * Converts a line of markdown to an MDSpan.
- *
- * @param {string|string[]} line
- * @returns {MDSpan}
- */
- inlineMarkdownToSpan(line) {
- let spans = this.inlineMarkdownToSpans(line);
- return (spans.length == 1) ? spans[0] : new MDMultiSpan(spans);
- }
-
- /**
- * Converts a line of markdown to an array of MDSpan.
- *
- * @param {string|string[]} line
- * @returns {MDSpan[]}
- */
- inlineMarkdownToSpans(line) {
- var tokens = this.#inlineMarkdownToTokens((line instanceof Array) ? line.join('\n') : line);
- return this.tokensToSpans(tokens);
- }
-
- /**
- * Converts a mixed array of `MDToken` and `MDSpan` elements into an array
- * of only `MDSpan`.
- *
- * @param {MDToken[]|MDSpan[]} tokens
- * @returns {MDSpan[]}
- */
- tokensToSpans(tokens) {
- var spans = tokens.slice();
-
- // Perform repeated substitutions, converting sequences of tokens into
- // spans, until no more substitutions can be made.
- var anyChanges = false;
- do {
- anyChanges = false;
- for (const readerTuple of this.inlineReadersBySubstitutePriority) {
- /** @type {number} */
- const priority = readerTuple[0];
- /** @type {MDInlineReader} */
- const reader = readerTuple[1];
- const changed = reader.substituteTokens(this, priority, spans);
- if (!changed) continue;
- anyChanges = true;
- break;
- }
- } while (anyChanges);
-
- // Convert any remaining tokens to spans, apply CSS modifiers.
- var lastSpan = null;
- spans = spans.map(function(span) {
- if (span instanceof MDToken) {
- if (span.type == MDTokenType.Modifier && lastSpan) {
- span.modifier.applyTo(lastSpan);
- lastSpan = null;
- return new MDTextSpan('');
- }
- lastSpan = null;
- return new MDTextSpan(span.original);
- } else if (span instanceof MDSpan) {
- lastSpan = (span instanceof MDTextSpan) ? null : span;
- return span;
- } else {
- throw new Error(`Unexpected span type ${span.constructor.name}`);
- }
- });
-
- return spans;
- }
- }
-
- class Markdown {
- /**
- * Set of standard block readers.
- * @type {MDBlockReader[]}
- */
- static standardBlockReaders = [
- new MDUnderlinedHeaderBlockReader(10.0),
- new MDHashHeaderBlockReader(15.0),
- new MDBlockQuoteBlockReader(20.0),
- new MDHorizontalRuleBlockReader(25.0),
- new MDFencedCodeBlockReader(30.0),
- new MDIndentedCodeBlockReader(40.0),
- new MDOrderedListBlockReader(45.0),
- new MDUnorderedListBlockReader(50.0),
- new MDURLDefinitionBlockReader(95.0),
- new MDParagraphBlockReader(100.0),
- ];
-
- /**
- * All supported block readers.
- * @type {MDBlockReader[]}
- */
- static allBlockReaders = [
- ...this.standardBlockReaders,
- new MDTableBlockReader(55.0),
- new MDDefinitionListBlockReader(60.0),
- new MDAbbreviationDefinitionBlockReader(90.0),
- new MDFootnoteDefinitionBlockReader(91.0),
- ];
-
- /**
- * Set of standard inline readers.
- * @type {MDInlineReader[]}
- */
- static standardInlineReaders = [
- new MDStrongInlineReader(10.0, [ 0.0, 2.0 ]),
- new MDEmphasisInlineReader(15.0, [ 5.0, 55.0 ]),
- new MDCodeInlineReader(20.0, [ 10.0, 60.0 ]),
- new MDImageInlineReader(25.0, 15.0),
- new MDLinkInlineReader(30.0, 20.0),
- new MDSimpleLinkInlineReader(35.0, 25.0),
- new MDHTMLTagInlineReader(80.0, 30.0),
- ];
-
- /**
- * All supported inline readers.
- * @type {MDInlineReader[]}
- */
- static allInlineReaders = [
- ...this.standardInlineReaders,
- new MDStrikethroughInlineReader(21.0, [ 12.0, 50.0 ]),
- new MDFootnoteInlineReader(5.0, 40.0),
- new MDModifierInlineReader(90.0, 45.0),
- ];
-
- /**
- * Shared instance of a parser with standard syntax.
- */
- static standardParser = new Markdown(this.standardBlockReaders, this.standardInlineReaders);
-
- /**
- * Shared instance of a parser with all supported syntax.
- */
- static completeParser = new Markdown(this.allBlockReaders, this.allInlineReaders);
-
- #blockReaders;
- #inlineReaders;
-
- #blockReadersByPriority;
- #inlineReadersByTokenPriority;
- #inlineReadersBySubstitutePriority;
-
- /**
- * @param {MDBlockReader[]} blockReaders
- * @param {MDInlineReader[]} inlineReaders
- */
- constructor(blockReaders=Markdown.allBlockReaders, inlineReaders=Markdown.allInlineReaders) {
- this.#blockReaders = blockReaders;
- this.#inlineReaders = inlineReaders;
-
- this.#blockReadersByPriority = blockReaders.slice();
- this.#blockReadersByPriority.sort((a, b) => a.priority - b.priority);
- const duplicateAndSort = function(priorityFn) {
- var readers = [];
- for (const reader of inlineReaders) {
- const p = priorityFn(reader);
- const priorities = (p instanceof Array) ? p : [ p ];
- for (const priority of priorities) {
- readers.push([priority, reader]);
- }
- }
- readers.sort((a, b) => a[0] - b[0]);
- return readers;
- }
- this.#inlineReadersByTokenPriority = duplicateAndSort((reader) => reader.tokenizePriority);
- this.#inlineReadersBySubstitutePriority = duplicateAndSort((reader) => reader.substitutePriority);
- }
-
- /**
- * @param {string} markdown
- * @returns {string} HTML
- */
- toHTML(markdown) {
- const lines = markdown.split(/(?:\n|\r|\r\n)/);
- const state = new MDState(lines);
- state.blockReadersByPriority = this.#blockReadersByPriority;
- state.inlineReadersByTokenPriority = this.#inlineReadersByTokenPriority;
- state.inlineReadersBySubstitutePriority = this.#inlineReadersBySubstitutePriority;
- const blocks = state.readBlocks();
- for (const reader of this.#blockReaders) {
- reader.postProcess(state, blocks);
- }
- for (const reader of this.#inlineReaders) {
- reader.postProcess(state, blocks);
- }
- return MDBlock.toHTML(blocks, state);
- }
- }
|