| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261 |
- /**
- * Static utilities.
- */
- 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) {
- if (typeof str !== 'string') return '';
- var html = str.replace(/&/g, '&').replace(/</g, '<')
- .replace(/>/g, '>').replace(/"/g, '"');
- if (encodeNewlinesAsBreaks) {
- html = html.replace(/\n/g, "<br>\n");
- }
- return html;
- }
-
- /**
- * Converts HTML entities to characters. HTML tags are stripped.
- *
- * @param {string} html
- * @returns {string} plain text
- */
- static unescapeHTML(html, decodeBRsAsNewlines=false) {
- if (decodeBRsAsNewlines) {
- html = html.replace(/<br[\/]?>\n?/g, "\n");
- }
- const doc = (new DOMParser()).parseFromString(html, "text/html");
- return doc.documentElement.textContent;
- }
-
- /**
- * Encodes characters as HTML numeric entities to make it marginally more
- * difficult for web scrapers to grab sensitive info. If `text` starts with
- * `mailto:` only the email address following it will be obfuscated.
- *
- * @param {string} text - text to escape
- * @returns {string} escaped HTML
- */
- static escapeObfuscated(text) {
- if (text.startsWith('mailto:')) {
- return 'mailto:' + this.escapeObfuscated(text.substring(7));
- }
- var html = '';
- for (var p = 0; p < text.length; p++) {
- const cp = text.codePointAt(p);
- html += `&#${cp};`;
- }
- return html;
- }
-
- /**
- * Removes illegal characters from an HTML attribute name.
- *
- * @param {string} name
- * @returns {string}
- */
- static scrubAttributeName(name) {
- return name.replace(/[\t\n\f \/>"'=]+/, '');
- }
-
- /**
- * 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, '');
- }
-
- /**
- * 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;
- }
-
- /**
- * 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;
- }
-
- /**
- * 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, floatDifferencePercent=0.0) {
- if (a instanceof Array && b instanceof Array) {
- return this.#equalArrays(a, b);
- }
- if (a instanceof Object && b instanceof Object) {
- return this.#equalObjects(a, b);
- }
- if (typeof a == 'number' && typeof b == 'number') {
- if (a === b) return true;
- const delta = b - a;
- const ratio = delta / a;
- return Math.abs(ratio) <= floatDifferencePercent;
- }
- return a == b;
- }
-
- /**
- * Escapes special characters in a string for inclusion as a literal in a
- * regular expression.
- *
- * @param {string} text
- */
- static escapeRegex(text) {
- // Partially following escaping scheme from not-yet-widely-supported RegExp.escape().
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape
- const escapeHex = function(ch) {
- const codepoint = ch.codePointAt(0);
- const s = '00' + codepoint.toString(16);
- return `\\x${s.substring(s.length - 2)}`;
- }
- var escaped = '';
- const l = text.length;
- for (var i = 0; i < l; i++) {
- const ch = text.substring(i, i + 1);
- if (i == 0 && /[a-zA-Z0-9]/.exec(ch)) {
- escaped += escapeHex(ch);
- } else if ("^$\\.*+?()[]{}|/".indexOf(ch) >= 0) {
- escaped += `\\${ch}`;
- } else if (",-=<>#&!%:;@~'`\"".indexOf(ch) >= 0) {
- escaped += escapeHex(ch);
- } else if (ch == '\f') {
- escaped += "\\f";
- } else if (ch == '\n') {
- escaped += "\\n";
- } else if (ch == '\r') {
- escaped += "\\r";
- } else if (ch == '\t') {
- escaped += "\\t";
- } else if (ch == '\v') {
- escaped += "\\v";
- } else {
- escaped += ch;
- }
- }
- return escaped;
- }
- }
-
- /**
- * Token type enum for `MDToken`.
- */
- class MDTokenType {
- static Text = new MDTokenType('Text');
- /**
- * Only used for the leading and trailing whitespace around a run of text,
- * not every single whitespace character.
- */
- 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 Equal = new MDTokenType('Equal');
- static Caret = new MDTokenType('Caret');
-
- 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'); // modifier=MDTagModifier
-
- static HTMLTag = new MDTokenType('HTMLTag'); // tag=MDHTMLTag
-
- /** Wildcard for `MDToken.findFirstTokens` */
- static META_AnyNonWhitespace = new MDTokenType('META_AnyNonWhitespace');
- /** Wildcard for `MDToken.findFirstTokens` */
- static META_OptionalWhitespace = new MDTokenType('META_OptionalWhitespace');
-
- /** @type {string} */
- name;
-
- /**
- * @param {string} name
- */
- constructor(name) {
- this.name = name;
- }
-
- /** @returns {string} */
- toString() {
- return `${this.constructor.name}.${this.name}`;
- }
- }
-
- /**
- * Search results from `MDToken.findFirstTokens`.
- */
- class MDTokenMatch {
- /** @type {MDToken{}} */
- tokens;
- /** @type {number} */
- index;
-
- constructor(tokens, index) {
- this.tokens = tokens;
- this.index = index;
- }
- }
-
- /**
- * Search results from `MDToken.findPairedTokens`.
- */
- class MDPairedTokenMatch {
- /** @type {MDToken[]} */
- startTokens;
- /** @type {MDToken[]} */
- contentTokens;
- /** @type {MDToken[]} */
- endTokens;
- /** @type {number} */
- startIndex;
- /** @type {number} */
- contentIndex;
- /** @type {number} */
- endIndex;
- /** @type {number} */
- totalLength;
-
- constructor(startTokens, contentTokens, endTokens, startIndex, contentIndex, endIndex, totalLength) {
- this.startTokens = startTokens;
- this.contentTokens = contentTokens;
- this.endTokens = endTokens;
- this.startIndex = startIndex;
- this.contentIndex = contentIndex;
- this.endIndex = endIndex;
- this.totalLength = totalLength;
- }
- }
-
- /**
- * One lexical unit in inline markdown syntax parsing.
- */
- class MDToken {
- /**
- * The original verbatim token string. Required as a plaintext fallback if
- * the token remains unresolved.
- * @type {string}
- */
- original;
- /** @type {MDTokenType} */
- type;
- /** @type {string|null} */
- content = null;
- /** @type {string|null} */
- extra = null;
- /** @type {MDHTMLTag|null} */
- tag = null;
- /** @type {MDTagModifier|null} */
- modifier = null;
-
- /**
- * Creates a token.
- *
- * @param {string} original - verbatim token string
- * @param {MDTokenType} type - token type
- * @param {string|MDTagModifier|MDHTMLTag|null} content - primary content of the token
- * @param {string|null} extra - additional content
- */
- constructor(original, type, content=null, extra=null) {
- this.original = original;
- this.type = type;
- if (content instanceof MDTagModifier) {
- this.modifier = content;
- } else if (content instanceof MDHTMLTag) {
- this.tag = content;
- } else {
- this.content = content;
- }
- this.extra = extra;
- }
-
- toString() {
- return `(${this.constructor.name} type=${this.type.toString()} content=${this.content})`;
- }
-
- /**
- * Attempts to parse a label token from the beginning of `line`. A label is
- * of the form `[content]`. If found, returns an array:
- * - `0`: the entire label including brackets
- * - `1`: 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 token from the beginning of `line`. A URL token
- * is of the form `(url)` or `(url "title")`. If found, returns an array:
- * - `0`: the entire URL token including parentheses
- * - `1`: the URL
- * - `2`: the optional title, or `null`
- *
- * @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:
- * - `0`: the entire token including parentheses
- * - `1`: the email address
- * - `2`: the optional link title, or `null`
- *
- * @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;
- }
-
- /**
- * Searches an array of `MDToken` for the given pattern of `MDTokenType`s.
- * If found, returns a `MDTokenMatch`, otherwise `null`.
- *
- * Special token types `META_AnyNonWhitespace` and `META_OptionalWhitespace`
- * are special supported token types. Note that `META_OptionalWhitespace`
- * may give a result with a variable number of tokens.
- *
- * @param {MDToken[]|MDNode[]} tokensToSearch - mixed array of `MDToken` and
- * `MDNode` elements
- * @param {MDTokenType[]} pattern - contiguous run of token types to find
- * @param {number} startIndex - token index to begin searching (defaults to 0)
- * @returns {MDTokenMatch|null} match object, or `null` if not found
- */
- 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 new MDTokenMatch(matched, 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, a `MDPairedTokenMatch` is returned with details
- * of the opening tokens, closing tokens, and content tokens between. Otherwise
- * `null` is returned.
- *
- * @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 {MDPairedTokenMatch|null} match, or `null`
- */
- 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 new MDPairedTokenMatch(startMatch.tokens,
- contents,
- endMatch.tokens,
- startMatch.index,
- startMatch.index + startMatch.tokens.length,
- endMatch.index,
- 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
- }
- }
-
- /**
- * Parsing and rendering state. Passed around throughout the parsing process.
- *
- * States are hierarchical. A sub-state can be created by calling `.copy()` with
- * a new array of lines. The sub-state points back to its parent state. This
- * is done to parse inner content of a syntax as its own standalone document.
- *
- * If a custom `MDReader` implementation wants to store data in this object,
- * always do so on `state.root` to ensure it's stored on the original state,
- * not a child state. Otherwise data may be lost when the sub-state is discarded.
- */
- class MDState {
- /**
- * Ascends the parent chain to the root `MDState` instance. This should be
- * used when referencing most stored fields except `lines` and `p`.
- *
- * @type {MDState}
- */
- get root() { return this.#parent ? this.#parent.root : this; }
-
- /**
- * Lines of the markdown document. The current line index is pointed to by `p`.
- *
- * @type {string[]}
- */
- lines;
-
- /**
- * The current line in `lines`.
- *
- * @returns {string|null} current line or `null` if out of content
- */
- get currentLine() { return (this.p < this.lines.length) ? this.lines[this.p] : null; }
-
- /**
- * Current line pointer into array `lines`.
- *
- * @type {number} line pointer
- */
- p = 0;
-
- /** @type {MDState|null} */
- #parent = null;
-
- /**
- * Array of `MDReader`s sorted by block reading priority.
- * @type {MDReader[]}
- */
- readersByBlockPriority = [];
-
- /**
- * Array of `MDReader`s sorted by tokenization priority.
- * @type {MDReader[]}
- */
- readersByTokenPriority = [];
-
- /**
- * Array of tuples of `pass:number` and `MDReader` sorted by substitution
- * priority.
- * @type {Array}
- */
- readersBySubstitutePriority = [];
-
- /**
- * Prefix to include in any generated `id` attributes on HTML elements.
- * Useful for keeping elements unique in multiple parsed documents in the
- * same HTML page.
- *
- * @type {string}
- */
- elementIdPrefix = '';
-
- /**
- * Filter for removing unapproved HTML tags, attributes, and values.
- * @type {MDHTMLFilter}
- */
- tagFilter;
-
- static #textWhitespaceRegex = /^(\s*)(?:(\S|\S.*\S)(\s*?))?$/; // 1=leading WS, 2=text, 3=trailing WS
-
- /**
- * @param {string[]} lines - lines of markdown text
- */
- 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;
- }
-
- /**
- * 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, or `null` to use `this.p`
- * @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 {MDBlockNode[]} 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 `MDBlockNode` if no other registered blocks match.
- *
- * @returns {MDBlockNode|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 this.inlineMarkdownToNode(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 {MDBlockNode|null}
- */
- #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.root.readersByBlockPriority) {
- const startP = this.p;
- const block = reader.readBlock(this);
- if (block) {
- if (this.p == startP) {
- throw new Error(`${reader.constructor.name} returned an ` +
- `${block.constructor.name} without incrementing MDState.p. ` +
- `This could lead to an infinite loop.`);
- }
- return block;
- }
- }
- const fallback = this.#readFallbackBlock();
- return fallback;
- }
-
- /**
- * @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 reader of this.root.readersByTokenPriority) {
- const token = reader.readToken(this, remainder);
- if (token === null) continue;
- if (token === undefined) {
- console.warn(`${reader.constructor.name}.readToken returned undefined instead of null`);
- }
- endText();
- tokens.push(token);
- if (token.original == null || token.original.length == 0) {
- throw new Error(`${reader.constructor.name} returned a token with an empty .original. This would cause an infinite loop.`);
- }
- p += token.original.length - 1;
- found = true;
- break;
- }
- if (!found) {
- text += ch;
- }
- }
- endText();
- return tokens;
- }
-
- /**
- * Converts a line of markdown to an `MDInlineNode`.
- *
- * @param {string|string[]} line
- * @returns {MDInlineNode}
- */
- inlineMarkdownToNode(line) {
- let nodes = this.inlineMarkdownToNodes(line);
- return (nodes.length == 1) ? nodes[0] : new MDInlineNode(nodes);
- }
-
- /**
- * Converts a line of markdown to an array of `MDInlineNode`s.
- *
- * @param {string|string[]} line
- * @returns {MDInlineNode[]}
- */
- inlineMarkdownToNodes(line) {
- var tokens = this.#inlineMarkdownToTokens((line instanceof Array) ? line.join('\n') : line);
- return this.tokensToNodes(tokens);
- }
-
- /**
- * Converts a mixed array of `MDToken` and `MDInlineNode` elements into an array
- * of only `MDInlineNode` via repeated `MDReader` substition.
- *
- * @param {MDToken[]|MDInlineNode[]} tokens
- * @returns {MDInlineNode[]}
- */
- tokensToNodes(tokens) {
- var nodes = tokens.slice();
-
- // Perform repeated substitutions, converting sequences of tokens into
- // nodes, until no more substitutions can be made.
- var anyChanges = false;
- do {
- anyChanges = false;
- for (const readerTuple of this.root.readersBySubstitutePriority) {
- /** @type {number} */
- const pass = readerTuple[0];
- /** @type {MDReader} */
- const reader = readerTuple[1];
- const changed = reader.substituteTokens(this, pass, nodes);
- if (!changed) continue;
- anyChanges = true;
- break;
- }
- } while (anyChanges);
-
- // Convert any remaining tokens to text nodes. Also apply any inline
- // CSS modifiers.
- var lastNode = null;
- const me = this;
- nodes = nodes.map(function(node) {
- if (node instanceof MDToken) {
- /** @type {MDToken} */
- const token = node;
- if (token.type == MDTokenType.Modifier && lastNode) {
- me.root.tagFilter.scrubModifier(token.modifier);
- token.modifier.applyTo(lastNode);
- lastNode = null;
- return new MDTextNode('');
- }
- lastNode = null;
- return new MDTextNode(token.original);
- } else if (node instanceof MDNode) {
- lastNode = (node instanceof MDTextNode) ? null : node;
- return node;
- } else {
- throw new Error(`Unexpected node type ${node.constructor.name}`);
- }
- });
-
- return nodes;
- }
-
- /**
- * Mapping of reference symbols to URLs. Used by `MDReferencedLinkReader`
- * and `MDReferencedImageReader`.
- * @type {object} symbol -> URL
- */
- #referenceToURL = {};
-
- /**
- * Mapping of reference symbols to titles. Used by `MDReferencedLinkReader`
- * and `MDReferencedImageReader`.
- * @type {object} symbol -> title string
- */
- #referenceToTitle = {};
-
- /**
- * Defines a URL by reference symbol.
- *
- * @param {string} reference - case-insensitive reference symbol
- * @param {string} url - URL to map the symbol to
- * @param {string|null} title - optional link title
- */
- defineURL(reference, url, title=null) {
- this.root.#referenceToURL[reference.toLowerCase()] = url;
- if (title !== null) this.root.#referenceToTitle[reference.toLowerCase()] = title;
- }
-
- /**
- * Returns the URL associated with a reference symbol.
- *
- * @param {string} reference - case-insensitive reference symbol
- * @returns {string|null} URL for the given reference, or `null` if not defined
- */
- urlForReference(reference) {
- return this.root.#referenceToURL[reference.toLowerCase()] ?? null;
- }
-
- /**
- * Returns the link title associated with a reference symbol.
- *
- * @param {string} reference - case-insensitive reference symbol
- * @returns {string|null} link title for the given reference, or `null` if not defined
- */
- urlTitleForReference(reference) {
- return this.root.#referenceToTitle[reference.toLowerCase()] ?? null;
- }
- }
-
- /**
- * Defines a set of allowable HTML tags, attributes, and CSS.
- */
- class MDHTMLFilter {
- /**
- * Mapping of permitted lowercase tag names to objects containing allowable
- * attributes for those tags. Does not need to include those attributes
- * defined in `allowableGlobalAttributes`.
- *
- * Values are objects with allowable lowercase attribute names mapped to
- * allowable value patterns. A `*` means any value is acceptable. Multiple
- * allowable values can be joined together with `|`. These special symbols
- * represent certain kinds of values and can be used in combination or in
- * place of literal values.
- *
- * - `{classlist}`: A list of legal CSS classnames, separated by spaces
- * - `{int}`: An integer
- * - `{none}`: No value (an attribute with no `=` or value, like `checked`)
- * - `{style}`: One or more CSS declarations, separated by semicolons (simple
- * `key: value;` syntax only)
- * - `{url}`: A URL
- * @type {object}
- */
- allowableTags = {
- 'address': {
- 'cite': '{url}',
- },
- 'h1': {},
- 'h2': {},
- 'h3': {},
- 'h4': {},
- 'h5': {},
- 'h6': {},
- 'blockquote': {},
- 'dl': {},
- 'dt': {},
- 'dd': {},
- 'div': {},
- 'hr': {},
- 'ul': {},
- 'ol': {
- 'start': '{int}',
- 'type': 'a|A|i|I|1',
- },
- 'li': {
- 'value': '{int}',
- },
- 'p': {},
- 'pre': {},
- 'table': {},
- 'thead': {},
- 'tbody': {},
- 'tfoot': {},
- 'tr': {},
- 'td': {},
- 'th': {},
- 'a': {
- 'href': '{url}',
- 'target': '*',
- },
- 'abbr': {},
- 'b': {},
- 'br': {},
- 'cite': {},
- 'code': {},
- 'data': {
- 'value': '*',
- },
- 'dfn': {},
- 'em': {},
- 'i': {},
- 'kbd': {},
- 'mark': {},
- 'q': {
- 'cite': '{url}',
- },
- 's': {},
- 'samp': {},
- 'small': {},
- 'span': {},
- 'strong': {},
- 'sub': {},
- 'sup': {},
- 'time': {
- 'datetime': '*',
- },
- 'u': {},
- 'var': {},
- 'wbr': {},
- 'img': {
- 'alt': '*',
- 'href': '{url}',
- },
- 'figure': {},
- 'figcaption': {},
- 'del': {},
- 'ins': {},
- 'details': {},
- 'summary': {},
- };
-
- /**
- * Mapping of allowable lowercase global attributes to their permitted
- * values. Uses same value pattern syntax as described in `allowableTags`.
- * @type {object}
- */
- allowableGlobalAttributes = {
- 'class': '{classlist}',
- 'data-*': '*',
- 'dir': 'ltr|rtl|auto',
- 'id': '*',
- 'lang': '*',
- 'style': '{style}',
- 'title': '*',
- 'translate': 'yes|no|{none}',
- };
-
- /**
- * Mapping of allowable CSS style names to their allowable value patterns.
- * Multiple values can be delimited with `|` characters. Limited support
- * so far.
- *
- * Recognized special values:
- * - `{color}`: A hex or named color
- *
- * @type {object}
- */
- allowableStyleKeys = {
- 'background-color': '{color}',
- 'color': '{color}',
- };
-
- /**
- * Scrubs all forbidden attributes from an HTML tag. Assumes the tag name
- * itself has already been whitelisted.
- *
- * @param {MDHTMLTag} tag - HTML tag
- */
- scrubTag(tag) {
- for (const name of Object.keys(tag.attributes)) {
- if (!this.isValidAttributeName(tag.tagName, name)) {
- delete tag.attributes[name];
- }
- if (!this.isValidAttributeValue(tag.tagName, name, tag.attributes[name])) {
- delete tag.attributes[name];
- }
- }
- }
-
- /**
- * Scrubs all forbidden attributes from an HTML modifier.
- *
- * @param {MDTagModifier} modifier
- * @param {string|null} tagName - HTML tag name, if known, otherwise only
- * global attributes will be permitted
- */
- scrubModifier(modifier, tagName) {
- if (modifier.cssClasses.length > 0) {
- const classList = modifier.cssClasses.join(' ');
- if (!this.isValidAttributeValue(tagName, 'class', classList)) {
- modifier.cssClasses = [];
- }
- }
- if (modifier.cssId !== null) {
- if (!this.isValidAttributeValue(tagName, 'id', modifier.cssId)) {
- modifier.cssId = null;
- }
- }
- if (!this.isValidAttributeName(tagName, 'style')) {
- modifier.cssStyles = {};
- } else {
- for (const key of Object.keys(modifier.cssStyles)) {
- const val = modifier.cssStyles[key];
- if (!this.isValidStyleValue(key, val)) {
- delete modifier.cssStyles[key];
- }
- }
- }
- for (const key of Object.keys(modifier.attributes)) {
- const val = modifier.attributes[key];
- if (!this.isValidAttributeValue(tagName, key, val)) {
- delete modifier.attributes[key];
- }
- }
- }
-
- /**
- * Tests if an HTML tag name is permitted.
- *
- * @param {string} tagName
- * @returns {boolean}
- */
- isValidTagName(tagName) {
- return this.allowableTags[tagName.toLowerCase()] !== undefined;
- }
-
- /**
- * Tests if an HTML attribute name is permitted.
- *
- * @param {string|null} tagName - HTML tag name or null to only check global
- * attributes
- * @param {string} attributeName - attribute name
- * @returns {boolean}
- */
- isValidAttributeName(tagName, attributeName) {
- const lcAttributeName = attributeName.toLowerCase();
- if (this.allowableGlobalAttributes[lcAttributeName] !== undefined) {
- return true;
- }
- for (const pattern in this.allowableGlobalAttributes) {
- if (pattern.endsWith('*') && lcAttributeName.startsWith(pattern.substring(0, pattern.length - 1))) {
- return true;
- }
- }
- if (tagName === null) return false;
- const lcTagName = tagName.toLowerCase();
- const tagAttributes = this.allowableTags[lcTagName];
- if (tagAttributes) {
- return tagAttributes[lcAttributeName] !== undefined;
- }
- return false;
- }
-
- /**
- * Tests if an attribute value is allowable.
- *
- * @param {string|null} tagName
- * @param {string} attributeName
- * @param {string} attributeValue
- * @returns {boolean}
- */
- isValidAttributeValue(tagName, attributeName, attributeValue) {
- const lcAttributeName = attributeName.toLowerCase();
- const globalPattern = this.allowableGlobalAttributes[lcAttributeName];
- if (globalPattern !== undefined) {
- return this.#attributeValueMatchesPattern(attributeValue, globalPattern);
- }
- for (const namePattern in this.allowableGlobalAttributes) {
- if (namePattern.endsWith('*') && lcAttributeName.startsWith(namePattern.substring(0, namePattern.length - 1))) {
- return this.#attributeValueMatchesPattern(attributeValue, this.allowableGlobalAttributes[namePattern]);
- }
- }
- if (tagName === null) return false;
- const lcTagName = tagName.toLowerCase();
- const tagAttributes = this.allowableTags[lcTagName];
- if (tagAttributes === undefined) return false;
- const valuePattern = tagAttributes[lcAttributeName];
- if (valuePattern === undefined) return false;
- return this.#attributeValueMatchesPattern(attributeValue, valuePattern);
- }
-
- static #permissiveURLRegex = /^\S+$/;
- static #integerRegex = /^[\-]?\d+$/;
- static #classListRegex = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*(?:\s+-?[_a-zA-Z]+[_a-zA-Z0-9-]*)*$/;
-
- /**
- * @param {string} value
- * @param {string} pattern
- * @returns {boolean}
- */
- #attributeValueMatchesPattern(value, pattern) {
- const options = pattern.split('|');
- for (const option of options) {
- switch (option) {
- case '*':
- return true;
- case '{classlist}':
- if (MDHTMLFilter.#classListRegex.exec(value)) return true;
- break;
- case '{int}':
- if (MDHTMLFilter.#integerRegex.exec(value)) return true;
- break;
- case '{none}':
- if (value === true) return true;
- break;
- case '{style}':
- if (this.isValidStyleDeclaration(value)) return true;
- break;
- case '{url}':
- if (MDHTMLFilter.#permissiveURLRegex.exec(value)) return true;
- break;
- default:
- if (value === option) return true;
- break;
- }
- }
- return false;
- }
-
- /**
- * Tests if a string of one or more style `key: value;` declarations is
- * fully allowable.
- *
- * @param {string} styles
- * @returns {boolean}
- */
- isValidStyleDeclaration(styles) {
- const settings = styles.split(';');
- for (const setting of settings) {
- if (setting.trim().length == 0) continue;
- const parts = setting.split(':');
- if (parts.length != 2) return false;
- const name = parts[0].trim();
- if (!this.isValidStyleKey(name)) return false;
- const value = parts[1].trim();
- if (!this.isValidStyleValue(name, value)) return false;
- }
- return true;
- }
-
- /**
- * Tests if a CSS style key is allowable.
- *
- * @param {string} key - CSS key
- * @returns {boolean}
- */
- isValidStyleKey(key) {
- return this.allowableStyleKeys[key] !== undefined;
- }
-
- /**
- * Tests if a CSS style value is allowable.
- *
- * @param {string} key
- * @param {string} value
- * @returns {boolean}
- */
- isValidStyleValue(key, value) {
- const pattern = this.allowableStyleKeys[key];
- if (pattern === undefined) return false;
- const options = pattern.split('|');
- for (const option of options) {
- switch (option) {
- case '{color}':
- if (this.#isValidCSSColor(value)) return true;
- default:
- if (value === option) return true;
- }
- }
- return false;
- }
-
- static #styleColorRegex = /^#[0-9a-f]{3}(?:[0-9a-f]{3})?$|^[a-zA-Z]+$/i;
-
- #isValidCSSColor(value) {
- return MDHTMLFilter.#styleColorRegex.exec(value) !== null;
- }
- }
-
- /**
- * Represents a single HTML tag. Paired tags are represented separately.
- */
- class MDHTMLTag {
- /**
- * Verbatim string of the original parsed tag. Not modified. Should be
- * considered unsafe for inclusion in the final document. Use `toString()`
- * instead.
- * @type {string}
- */
- original;
- /** @type {string} */
- tagName;
- /** @type {boolean} */
- isCloser;
- /**
- * Map of attribute names to value strings.
- *
- * @type {object}
- */
- attributes;
-
- /**
- * @param {string} original
- * @param {string} tagName
- * @param {boolean} isCloser
- * @param {object} attributes
- */
- constructor(original, tagName, isCloser, attributes) {
- this.original = original;
- this.tagName = tagName;
- this.isCloser = isCloser;
- this.attributes = attributes;
- }
-
- toString() {
- if (this.isCloser) {
- return `</${this.tagName}>`;
- }
- var html = '<';
- html += this.tagName;
- for (const key in this.attributes) {
- const safeName = MDUtils.scrubAttributeName(key);
- const value = this.attributes[key];
- if (value === true) {
- html += ` ${safeName}`;
- } else {
- const escapedValue = MDUtils.escapeHTML(`${value}`);
- html += ` ${safeName}="${escapedValue}"`;
- }
- }
- html += '>';
- return html;
- }
-
- equals(other) {
- if (!(other instanceof MDHTMLTag)) return false;
- if (other.tagName != this.tagName) return false;
- if (other.isCloser != this.isCloser) return false;
- return MDUtils.equal(other.attributes, this.attributes);
- }
-
- 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/;
-
- /**
- * Checks the start of the given string for presence of an HTML tag.
- *
- * @param {string} line
- * @returns {MDHTMLTag|null} HTML tag if found, `null` otherwise
- */
- 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(unescape=false) {
- if (attributeName.length > 0) {
- if (attributeValue.length > 0 || attributeQuote) {
- attributes[attributeName] = unescape ? MDUtils.unescapeHTML(attributeValue) : 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(attributeQuote != '');
- 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);
- }
- }
-
- /**
- * Represents HTML modifications to a node, such as CSS classes to add or
- * additional attributes. See `MDHTMLFilter.scrubModifier()` to remove disallowed
- * values.
- */
- class MDTagModifier {
- /**
- * Verbatim markdown syntax. Unmodified by changes to other properties.
- * @type {string}
- */
- original;
- /** @type {string[]} */
- cssClasses = [];
- /** @type {string|null} */
- cssId = null;
- /** @type {object} */
- cssStyles = {};
- /** @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 {MDNode} node
- */
- applyTo(node) {
- if (node instanceof MDNode) {
- for (const cssClass of this.cssClasses) {
- node.addClass(cssClass);
- }
- if (this.cssId) node.cssId = this.cssId;
- for (const name in this.attributes) {
- node.attributes[name] = this.attributes[name];
- }
- for (const name in this.cssStyles) {
- node.cssStyles[name] = this.cssStyles[name];
- }
- }
- }
-
- /**
- * Adds a CSS class. If already present it will not be duplicated.
- *
- * @param {string} cssClass
- * @returns {boolean} whether the class was added
- */
- addClass(cssClass) {
- if (this.cssClasses.indexOf(cssClass) >= 0) return false;
- this.cssClasses.push(cssClass);
- return true;
- }
-
- /**
- * Removes a CSS class.
- *
- * @param {string} cssClass
- * @returns {boolean} whether the class was present and removed
- */
- removeClass(cssClass) {
- const beforeLength = this.cssClasses.length;
- this.cssClasses = this.cssClasses.filter((val) => val !== cssClass);
- return this.cssClasses.length != beforeLength;
- }
-
- 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 #styleToObject(styleValue) {
- const pairs = styleValue.split(';');
- var styles = {};
- for (const pair of pairs) {
- const keyAndValue = pair.split(':');
- if (keyAndValue.length != 2) continue;
- styles[keyAndValue[0]] = keyAndValue[1];
- }
- return styles;
- }
-
- 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.addClass(groups[1]);
- } else if (groups = this.#idRegex.exec(token)) {
- mod.cssId = groups[1];
- } else if (groups = this.#attributeRegex.exec(token)) {
- if (groups[1] == 'style') {
- mod.cssStyles = this.#styleToObject(groups[2]);
- } else {
- mod.attributes[groups[1]] = groups[2];
- }
- } else {
- return null;
- }
- }
- return mod;
- }
-
- /**
- * Extracts block modifier from end of a line. Always returns a 2-element
- * tuple array:
- * - `0`: the line without the modifier
- * - `1`: an `MDTagModifier` if found or `null` if not
- *
- * @param {string} line
- * @param {MDState} state
- * @returns {Array} tuple with remaining line and `MDTagModifier` or `null`
- */
- static fromLine(line, state) {
- if (state) {
- var found = false;
- for (const reader of state.root.readersByBlockPriority) {
- if (reader instanceof MDModifierReader) {
- found = true;
- break;
- }
- }
- if (!found) return [ line, null ];
- }
- 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 ];
- }
-
- /**
- * Attempts to extract modifier from head of string.
- *
- * @param {string} line
- * @returns {MDTagModifier|null}
- */
- static fromStart(line) {
- let groups = this.#leadingClassRegex.exec(line);
- if (groups === null) return null;
- return this.#fromContents(groups[1]);
- }
-
- /**
- * Discards any modifiers from a line and returns what remains.
- *
- * @param {string} line
- * @returns {string}
- */
- static strip(line) {
- let groups = this.#trailingClassRegex.exec(line);
- if (groups === null) return line;
- return groups[1];
- }
- }
-
-
- // -- Readers ---------------------------------------------------------------
-
-
- /**
- * Base class for readers of various markdown syntax. A `Markdown` instance can
- * be created with any combination of subclasses of these to customize the
- * flavor of markdown parsed.
- *
- * @see {@link custom.md} for details on subclassing
- */
- class MDReader {
- /**
- * Called before processing begins. `state.lines` is populated and the
- * line pointer `state.p` will be at `0`.
- *
- * Default implementation does nothing.
- *
- * @param {MDState} state
- */
- preProcess(state) {}
-
- /**
- * Attempts to read an `MDBlockNode` subclass at the current line pointer
- * `state.p`. Only matches if the block pattern starts at the line pointer,
- * not elsewhere in the `state.lines` array. If a block is found, `state.p`
- * should be incremented to the next line _after_ the block structure and
- * a `MDBlockNode` subclass instance is returned. If no block is found,
- * returns `null`.
- *
- * Default implementation always returns `null`.
- *
- * @param {MDState} state
- * @returns {MDBlockNode|null} found block, or `null` if not found
- */
- readBlock(state) { return null; }
-
- /**
- * Attempts to read an inline token from the beginning of `line`. Only the
- * start of the given `line` is considered. If a matching token is found, an
- * `MDToken` is returned. Otherwise `null` is returned.
- *
- * Default implementation always returns `null`.
- *
- * @param {MDState} state
- * @param {string} line - string to check for a leading token
- * @returns {MDToken|null} found token, or `null` if not found
- */
- readToken(state, line) { return null; }
-
- /**
- * Attempts to find a pattern anywhere in `tokens` and perform a _single_
- * in-place substitution with one or more `MDNode` subclass instances.
- * If a substitution is performed, must return `true`, otherwise `false`.
- *
- * Default implementation always returns `false`.
- *
- * @param {MDState} state
- * @param {number} pass - what substitution pass this is, starting with 1
- * @param {Array} tokens - mixed array of `MDToken` and `MDInlineNode` elements
- * @returns {boolean} `true` if a substitution was performed, `false` if not
- */
- substituteTokens(state, pass, tokens) { return false; }
-
- /**
- * Called after all parsing has completed. An array `blocks` is passed of
- * all the top-level `MDBlockNode` elements in the document which this
- * method can traverse or alter in-place via `.splice` operations if
- * necessary.
- *
- * `MDNode.visitChildren` is useful for recursively looking for certain
- * `MDNode` instances. `MDNode.replaceNodes` is useful for swapping in
- * replacements.
- *
- * Default implementation does nothing.
- *
- * @param {MDState} state
- * @param {MDBlockNode[]} blocks
- */
- postProcess(state, blocks) {}
-
- /**
- * Can be overridden to influence ordering of this reader with respect to
- * another during the block parsing phase. Return `-1` to be ordered before
- * the given reader, `1` to be ordered after it, or `0` for no preference.
- * Only return non-`0` values to resolve specific conflicts.
- *
- * Default implementation always returns `0` (no preference).
- *
- * @param {MDReader} other
- * @returns {number} a negative, positive, or 0 value to be ordered before,
- * after, or anwhere relative to `other`, respectively
- */
- compareBlockOrdering(other) {
- return 0;
- }
-
- /**
- * Can be overridden to influence ordering of this reader with respect to
- * another during the tokenizing phase. Return `-1` to be ordered before
- * the given reader, `1` to be ordered after it, or `0` for no preference.
- * Only return non-`0` values to resolve specific conflicts.
- *
- * Default implementation always returns `0` (no preference).
- *
- * @param {MDReader} other
- * @returns {number} a negative, positive, or 0 value to be ordered before,
- * after, or anwhere relative to `other`, respectively
- */
- compareTokenizeOrdering(other) {
- return 0;
- }
-
- /**
- * Can be overridden to influence ordering of this reader with respect to
- * another during the substitution phase. Return `-1` to be ordered before
- * the given reader, `1` to be ordered after it, or `0` for no preference.
- * Only return non-`0` values to resolve specific conflicts.
- *
- * Readers are sorted within each substitution pass. All pass 1 readers are
- * processed first, then all pass 2 readers, etc. The number of passes this
- * reader participates in is dictated by `substitionPassCount`.
- *
- * Default implementation always returns `0` (no preference).
- *
- * @param {MDReader} other
- * @param {number} pass - substitution pass, with numbering starting at `1`
- * @returns {number} a negative, positive, or 0 value to be ordered before,
- * after, or anwhere relative to `other`, respectively
- */
- compareSubstituteOrdering(other, pass) {
- return 0;
- }
-
- /**
- * How many substitution passes this reader requires. Substitution allows
- * all pass 1 readers to process first, then all pass 2 readers, etc.
- */
- get substitutionPassCount() { return 1; }
-
- /**
- * For sorting readers with ordering preferences. The `compare` methods
- * don't have the properties of normal sorting compares so need to sort
- * differently.
- *
- * @param {MDReader[]} arr - array to sort
- * @param {function} compareFn - comparison function, taking two array element
- * arguments and returning -1, 0, or 1 for a < b, a == b, and a > b,
- * respectively
- * @param {function} idFn - function for returning a unique hashable id for
- * the array element
- * @returns {MDReader[]} sorted array
- */
- static #kahnTopologicalSort(arr, compareFn, idFn) {
- const graph = {};
- const inDegrees = {};
- const valuesById = {};
-
- // Build the graph and compute in-degrees
- for (const elem of arr) {
- const id = idFn(elem);
- graph[id] = [];
- inDegrees[id] = 0;
- valuesById[id] = elem;
- }
-
- for (let i = 0; i < arr.length; i++) {
- const elemA = arr[i];
- const idA = idFn(elemA);
- for (let j = 0; j < arr.length; j++) {
- if (i === j) continue;
- const elemB = arr[j];
- const idB = idFn(elemB);
- const comparisonResult = compareFn(elemA, elemB);
- if (comparisonResult < 0) {
- graph[idA].push(idB);
- inDegrees[idB]++;
- } else if (comparisonResult > 0) {
- graph[idB].push(idA);
- inDegrees[idA]++;
- }
- }
- }
-
- // Initialize the queue with zero-inDegree nodes
- const queue = [];
- for (const elemId in inDegrees) {
- if (inDegrees[elemId] === 0) {
- queue.push(elemId);
- }
- }
-
- // Process the queue and build the topological order list
- const sorted = [];
- while (queue.length > 0) {
- const elemId = queue.shift();
- sorted.push(valuesById[elemId]);
- delete valuesById[elemId];
-
- for (const neighbor of graph[elemId]) {
- inDegrees[neighbor]--;
- if (inDegrees[neighbor] === 0) {
- queue.push(neighbor);
- }
- }
- }
- // Anything left over can go at the end. No ordering dependencies.
- for (const elemId in valuesById) {
- sorted.push(valuesById[elemId]);
- }
-
- return sorted;
- }
-
- /**
- * Returns a sorted array of readers by their block priority preferences.
- *
- * @param {MDReader[]} readers
- * @returns {MDReader[]} sorted readers
- */
- static sortReaderForBlocks(readers) {
- const sorted = readers.slice();
- return MDReader.#kahnTopologicalSort(sorted, (a, b) => {
- return a.compareBlockOrdering(b);
- }, (elem) => elem.constructor.name);
- }
-
- /**
- * Returns a sorted array of readers by their tokenization priority preferences.
- *
- * @param {MDReader[]} readers
- * @returns {MDReader[]} sorted readers
- */
- static sortReadersForTokenizing(readers) {
- const sorted = readers.slice();
- return MDReader.#kahnTopologicalSort(sorted, (a, b) => {
- return a.compareTokenizeOrdering(b);
- }, (elem) => elem.constructor.name);
- }
-
- /**
- * Returns a sorted array of tuples (arrays) containing the substitution
- * pass number and reader instance, sorted by their substitution priority
- * preferences.
- *
- * For readers with `substitutionPassCount` > `1`, the same reader will
- * appear multiple times in the resulting array, one per pass.
- *
- * @param {MDReader[]} readers
- * @returns {MDReader[]} sorted array of tuples with the pass number and
- * reader instance in each
- */
- static sortReadersForSubstitution(readers) {
- var tuples = [];
- var maxPass = 1;
- for (const reader of readers) {
- const passCount = reader.substitutionPassCount;
- for (var pass = 1; pass <= passCount; pass++) {
- tuples.push([ pass, reader ]);
- }
- maxPass = Math.max(maxPass, pass);
- }
- var result = [];
- for (var pass = 1; pass <= maxPass; pass++) {
- var readersThisPass = tuples.filter((tup) => tup[0] == pass);
- const passResult = MDReader.#kahnTopologicalSort(readersThisPass, (a, b) => {
- const aReader = a[1];
- const bReader = b[1];
- return aReader.compareSubstituteOrdering(bReader, pass);
- }, (elem) => `${elem[1].constructor.name}:${elem[0]}`);
- result = result.concat(passResult);
- }
- return result;
- }
- }
-
- /**
- * Reads markdown blocks for headings denoted with the underline syntax.
- *
- * Supports `MDTagModifier` suffixes.
- */
- class MDUnderlinedHeadingReader extends MDReader {
- 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, state);
- let underLine = state.lines[p++].trim();
- if (contentLine == '') return null;
- if (/^=+$/.exec(underLine)) {
- state.p = p;
- let block = new MDHeadingNode(1, state.inlineMarkdownToNodes(contentLine));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- if (/^\-+$/.exec(underLine)) {
- state.p = p;
- let block = new MDHeadingNode(2, state.inlineMarkdownToNodes(contentLine));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- return null;
- }
- }
-
- /**
- * Reads markdown blocks for headings denoted with hash marks. Heading levels 1
- * to 6 are supported.
- *
- * Supports `MDTagModifier` suffixes.
- */
- class MDHashHeadingReader extends MDReader {
- static #hashHeadingRegex = /^(#{1,6})\s*([^#].*?)\s*\#*\s*$/; // 1=hashes, 2=content
-
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = MDTagModifier.fromLine(line, state);
- var groups = MDHashHeadingReader.#hashHeadingRegex.exec(line);
- if (groups === null) return null;
- state.p = p;
- const level = groups[1].length;
- const content = groups[2];
- let block = new MDHeadingNode(level, state.inlineMarkdownToNodes(content));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- }
-
- /**
- * Reads subtext blocks. Subtext is smaller, fainter text for things like
- * disclaimers or sources.
- *
- * Supports `MDTagModifier` suffixes.
- */
- class MDSubtextReader extends MDReader {
- static #subtextRegex = /^\-#\s*(.*?)\s*$/; // 1=content
-
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = MDTagModifier.fromLine(line, state);
- var groups = MDSubtextReader.#subtextRegex.exec(line);
- if (groups === null) return null;
- state.p = p;
- const content = groups[1];
- let block = new MDSubtextNode(state.inlineMarkdownToNodes(content));
- if (modifier) modifier.applyTo(block);
- return block;
- }
-
- compareBlockOrdering(other) {
- if (other instanceof MDUnorderedListReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Reads markdown blocks for blockquoted text.
- */
- class MDBlockQuoteReader extends MDReader {
- 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) return null;
- 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 MDBlockquoteNode(quotedBlocks);
- }
- }
-
- /**
- * Internal abstract base class for ordered and unordered lists.
- */
- class _MDListReader extends MDReader {
- #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 {MDBlockNode}
- */
- _readListItemContent(state, firstLineStartPos) {
- const itemLines = this.#readItemLines(state, firstLineStartPos);
- state.p += Math.max(itemLines.length, 1);
-
- if (itemLines.length == 1) {
- return state.inlineMarkdownToNode(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 MDNode(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 = state.inlineMarkdownToNode(itemLines.slice(0, p).join("\n"));
- const substate = state.copy(itemLines.slice(p));
- const blocks = substate.readBlocks();
- return new MDNode([ 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 MDNode(blocks);
- }
- }
-
- readBlock(state) {
- throw new Error(`Abstract readBlock must be overridden in ${this.constructor.name}`);
- }
- }
-
- /**
- * Block reader for unordered (bulleted) lists.
- */
- class MDUnorderedListReader extends _MDListReader {
- static #unorderedListRegex = /^([\*\+\-]\s+)(.*)$/; // 1=bullet, 2=content
-
- /**
- * @param {MDState} state
- * @returns {MDListItemNode|null}
- */
- #readUnorderedListItem(state) {
- var p = state.p;
- let line = state.lines[p];
- let groups = MDUnorderedListReader.#unorderedListRegex.exec(line);
- if (groups === null) return null;
- const firstLineOffset = groups[1].length;
- return new MDListItemNode(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 MDUnorderedListNode(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.
- */
- class MDOrderedListReader extends _MDListReader {
- static #orderedListRegex = /^(\d+)(\.\s+)(.*)$/; // 1=number, 2=dot, 3=content
-
- /**
- * @param {MDState} state
- * @returns {MDListItemNode|null}
- */
- #readOrderedListItem(state) {
- var p = state.p;
- let line = state.lines[p];
- let groups = MDOrderedListReader.#orderedListRegex.exec(line);
- if (groups === null) return null;
- const ordinal = parseInt(groups[1]);
- const firstLineOffset = groups[1].length + groups[2].length;
- return new MDListItemNode(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 MDOrderedListNode(items, items[0].ordinal);
- }
- }
-
- /**
- * Block reader for code blocks denoted by pairs of triple tickmarks. If
- * a programming language name, _xyz_, immediately follows the backticks, a
- * `language-xyz` CSS class will be added to the resulting `<code>`
- * element.
- *
- * Supports `MDTagModifier` suffix.
- */
- class MDFencedCodeBlockReader extends MDReader {
- readBlock(state) {
- if (!state.hasLines(2)) return null;
- var p = state.p;
- let openFenceLine = state.lines[p++];
- var modifier;
- [openFenceLine, modifier] = MDTagModifier.fromLine(openFenceLine, state);
- const match = /^```\s*([a-z0-9]*)\s*$/.exec(openFenceLine);
- if (match === null) return null;
- const language = match[1].length > 0 ? match[1] : null;
- var codeLines = [];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (line.trim() == '```') {
- state.p = p;
- let block = new MDCodeBlockNode(codeLines.join("\n"), language);
- if (modifier) modifier.applyTo(block);
- return block;
- }
- codeLines.push(line);
- }
- return null;
- }
- }
-
- /**
- * Block reader for code blocks denoted by indenting text.
- */
- class MDIndentedCodeBlockReader extends MDReader {
- 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 MDCodeBlockNode(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.
- */
- class MDHorizontalRuleReader extends MDReader {
- static #horizontalRuleRegex = /^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;
-
- readBlock(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = MDTagModifier.fromLine(line, state);
- if (MDHorizontalRuleReader.#horizontalRuleRegex.exec(line)) {
- state.p = p;
- let block = new MDHorizontalRuleNode();
- if (modifier) modifier.applyTo(block);
- return block;
- }
- return null;
- }
-
- compareBlockOrdering(other) {
- if (other instanceof MDUnorderedListReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Block reader for tables.
- *
- * Supports `MDTagModifier` suffix.
- */
- class MDTableReader extends MDReader {
- /**
- * @param {MDState} state
- * @param {boolean} isHeader
- * @return {MDTableRowNode|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.inlineMarkdownToNode(token.trim());
- return isHeader ? new MDTableHeaderCellNode(content) : new MDTableCellNode(content);
- });
- state.p = p;
- return new MDTableRowNode(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 'center';
- }
- return 'left';
- } else if (token.endsWith(':')) {
- return 'right';
- }
- 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, state)[1];
- let headerRow = this.#readTableRow(state, true);
- if (headerRow === null) {
- state.p = startP;
- return null;
- }
- let dividerLine = state.lines[state.p++];
- let dividerGroups = MDTableReader.#tableDividerRegex.exec(dividerLine);
- if (dividerGroups === null) {
- state.p = startP;
- return null;
- }
- let columnAlignments = this.#parseColumnAlignments(dividerLine);
- var bodyRows = [];
- while (state.hasLines(1)) {
- let row = this.#readTableRow(state, false);
- if (row === null) break;
- bodyRows.push(row);
- }
- let table = new MDTableNode(headerRow, bodyRows);
- table.columnAlignments = columnAlignments;
- if (modifier) modifier.applyTo(table);
- return table;
- }
- }
-
- /**
- * Block reader for definition lists. Definitions go directly under terms starting
- * with a colon.
- */
- class MDDefinitionListReader extends MDReader {
- 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 MDDefinitionListDefinitionNode(state.inlineMarkdownToNodes(groups[1]));
- } else {
- return new MDDefinitionListTermNode(state.inlineMarkdownToNodes(line));
- }
- });
- state.p = p;
- return new MDDefinitionListNode(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.
- */
- class MDFootnoteReader extends MDReader {
- static #footnoteWithTitleRegex = /^\[\^([^\s\[\]]+?)\s+"(.*?)"\]/; // 1=symbol, 2=title
- static #footnoteRegex = /^\[\^([^\s\[\]]+?)\]/; // 1=symbol
-
- /**
- * @param {MDState} state
- * @param {string} symbol
- * @param {MDNode[]} content
- */
- #defineFootnote(state, symbol, footnote) {
- var footnotes = state.root['footnotes'] ?? {};
- footnotes[symbol] = footnote;
- state.root['footnotes'] = footnotes;
- }
-
- /**
- * @param {MDState} state
- * @param {string} symbol
- * @param {number} unique
- */
- #registerUniqueInstance(state, symbol, unique) {
- var footnoteInstances = state.root['footnoteInstances'];
- var instances = footnoteInstances[symbol] ?? [];
- instances.push(unique);
- footnoteInstances[symbol] = instances;
- }
-
- #idForFootnoteSymbol(state, symbol) {
- var footnoteIds = state.root['footnoteIds'];
- const existing = footnoteIds[symbol];
- if (existing) return existing;
- var nextFootnoteId = state.root['nextFootnoteId'];
- const id = nextFootnoteId++;
- footnoteIds[symbol] = id;
- state.root['nextFootnoteId'] = nextFootnoteId;
- return id;
- }
-
- preProcess(state) {
- state.root['footnoteInstances'] = {};
- state.root['footnotes'] = {};
- state.root['footnoteIds'] = {};
- state.root['nextFootnoteId'] = 1;
- }
-
- /**
- * @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;
- }
- }
- let content = state.inlineMarkdownToNodes(def);
- this.#defineFootnote(state, symbol, content);
- state.p = p;
- return new MDNode(); // empty
- }
-
- readToken(state, line) {
- var groups;
- if (groups = MDFootnoteReader.#footnoteWithTitleRegex.exec(line)) {
- return new MDToken(groups[0], MDTokenType.Footnote, groups[1], groups[2]);
- }
- if (groups = MDFootnoteReader.#footnoteRegex.exec(line)) {
- return new MDToken(groups[0], MDTokenType.Footnote, groups[1]);
- }
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- var match;
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.Footnote ])) {
- let symbol = match.tokens[0].content;
- tokens.splice(match.index, 1, new MDFootnoteNode(symbol));
- return true;
- }
- return false;
- }
-
- /**
- * @param {MDState} state
- * @param {MDBlockNode[]} blocks
- */
- postProcess(state, blocks) {
- var nextOccurrenceId = 1;
- for (const block of blocks) {
- const me = this;
- block.visitChildren((function(node) {
- if (!(node instanceof MDFootnoteNode)) return;
- node.footnoteId = me.#idForFootnoteSymbol(state, node.symbol);
- node.occurrenceId = nextOccurrenceId++;
- node.displaySymbol = `${node.footnoteId}`;
- me.#registerUniqueInstance(state, node.symbol, node.occurrenceId);
- }).bind(this));
- }
- if (Object.keys(state.footnotes).length == 0) return;
- blocks.push(new MDFootnoteListNode());
- }
-
- compareBlockOrdering(other) {
- if (other instanceof MDLinkReader || other instanceof MDImageReader) {
- return -1;
- }
- return 0;
- }
-
- compareTokenizeOrdering(other) {
- if (other instanceof MDLinkReader || other instanceof MDImageReader) {
- return -1;
- }
- return 0;
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other instanceof MDLinkReader || other instanceof MDImageReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Block reader for abbreviation definitions. Anywhere the abbreviation appears
- * in plain 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.
- */
- class MDAbbreviationReader extends MDReader {
- /**
- * @param {MDState} state
- * @param {string} abbreviation
- * @param {string} definition
- */
- #defineAbbreviation(state, abbreviation, definition) {
- state.abbreviations[abbreviation] = definition;
- const regex = new RegExp("\\b(" + MDUtils.escapeRegex(abbreviation) + ")\\b", "ig");
- state.abbreviationRegexes[abbreviation] = regex;
- }
-
- preProcess(state) {
- state.root['abbreviations'] = {};
- state.root['abbreviationRegexes'] = {};
- }
-
- 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];
- this.#defineAbbreviation(state, abbrev, def);
- state.p = p;
- return new MDNode(); // empty
- }
-
- /**
- * @param {MDState} state
- * @param {MDNode[]} blocks
- */
- postProcess(state, blocks) {
- const abbreviations = state.root['abbreviations'];
- const regexes = state.root['abbreviationRegexes'];
- MDNode.replaceNodes(state, blocks, (original) => {
- if (!(original instanceof MDTextNode)) return null;
- var changed = false;
- var elems = [ original.text ]; // mix of strings and MDNodes
- for (var i = 0; i < elems.length; i++) {
- var text = elems[i];
- if (typeof text !== 'string') continue;
- for (const abbreviation in abbreviations) {
- const groups = regexes[abbreviation].exec(text);
- if (groups === null) continue;
- const definition = abbreviations[abbreviation];
- const prefix = text.substring(0, groups.index);
- const suffix = text.substring(groups.index + groups[0].length);
- elems.splice(i, 1, prefix, new MDAbbreviationNode(groups[0], definition), suffix);
- i = -1; // start over
- changed = true;
- break;
- }
- }
- if (!changed) return null;
- const nodes = elems.map((elem) => typeof elem === 'string' ? new MDTextNode(elem) : elem);
- return new MDNode(nodes);
- });
- }
- }
-
- /**
- * Block reader for simple paragraphs. Paragraphs are separated by a blank (or
- * whitespace-only) line. This reader is prioritized after every other reader
- * since there is no distinguishing syntax.
- */
- class MDParagraphReader extends MDReader {
- readBlock(state) {
- var paragraphLines = [];
- var p = state.p;
- while (p < state.lines.length) {
- let line = state.lines[p++];
- if (line.trim().length == 0) {
- 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 MDParagraphNode(state.inlineMarkdownToNodes(content));
- }
- return null;
- }
-
- compareBlockOrdering(other) {
- return 1; // always dead last
- }
- }
-
- /**
- * Abstract base class for readers that look for one or two delimiting tokens
- * on either side of some content. E.g. `**strong**`.
- */
- class MDSimplePairInlineReader extends MDReader {
- // Passes:
- // 1. Syntaxes with two delimiting tokens, interior tokens of the same
- // kind must be even in number
- // 2. Syntaxes with one delimiting token, interior tokens of the same
- // kind must be even in number
- // 3. Syntaxes with two delimiting tokens, any tokens inside
- // 4. Syntaxes with one delimiting token, any tokens inside
- get substitutionPassCount() { return 4; }
-
- /**
- * 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 `this.substitutionPassCount` is greater than 1, the first pass
- * will reject matches with the delimiting character inside the content
- * tokens. If the reader uses a single pass or a subsequent pass is performed
- * with multiple pass any contents will be accepted.
- *
- * @param {MDState} state
- * @param {number} pass - pass number, starting with `1`
- * @param {MDToken[]} tokens - tokens/nodes to perform substitution on
- * @param {class} nodeClass - class of the node to return if matched
- * @param {MDTokenType} delimiter - delimiting token
- * @param {number} count - how many times the token is repeated to form the delimiter
- * @param {boolean} plaintext - whether to invoke `nodeClass` with a verbatim
- * content string instead of parsed `MDNode`s
- * @returns {boolean} `true` if substitution was performed, `false` if not
- */
- attemptPair(state, pass, tokens, nodeClass, delimiter, count=1, plaintext=false) {
- // We do four passes. #1: doubles without inner tokens, #2: singles
- // without inner tokens, #3: doubles with paired inner tokens,
- // #4: singles with paired inner tokens
- if (count == 1 && pass != 2 && pass != 4) return;
- if (count > 1 && pass != 1 && pass != 3) return;
- let delimiters = Array(count).fill(delimiter);
- const isFirstOfMultiplePasses = this.substitutionPassCount > 1 && pass == 1;
- 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;
- for (const token of content) {
- // Don't allow nesting
- if (token.constructor == nodeClass) return false;
- }
- if (isFirstOfMultiplePasses) {
- 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.tokensToNodes(match.contentTokens);
- tokens.splice(match.startIndex, match.totalLength, new nodeClass(content));
- return true;
- }
- }
-
- /**
- * Reader for emphasis syntax. Denoted with a single underscore on either side of
- * some text (preferred) or a single asterisk on either side.
- */
- class MDEmphasisReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
- if (line.startsWith('*')) return new MDToken('*', MDTokenType.Asterisk);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- if (this.attemptPair(state, pass, tokens, MDEmphasisNode, MDTokenType.Underscore)) return true;
- if (this.attemptPair(state, pass, tokens, MDEmphasisNode, MDTokenType.Asterisk)) return true;
- return false;
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other instanceof MDStrongReader) {
- return 1;
- }
- return 0;
- }
- }
-
- /**
- * Reader for strong syntax. Denoted with two asterisks on either side of some
- * text (preferred) or two underscores on either side. Note that if
- * `MDUnderlineReader` is in use, it will replace the double-underscore syntax.
- */
- class MDStrongReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('*')) return new MDToken('*', MDTokenType.Asterisk);
- if (line.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- if (this.attemptPair(state, pass, tokens, MDStrongNode, MDTokenType.Asterisk, 2)) return true;
- if (this.attemptPair(state, pass, tokens, MDStrongNode, MDTokenType.Underscore, 2)) return true;
- return false;
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other instanceof MDEmphasisReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Reader for strikethrough syntax. Consists of two tildes on either side of
- * some text (preferred) or single tildes on either side. Note that if
- * `MDSubscriptReader` is in use, it will replace the single-tilde syntax.
- *
- * The number of recognized tildes can be configured.
- */
- class MDStrikethroughReader extends MDSimplePairInlineReader {
- /** @type {boolean} */
- singleTildeEnabled = true;
- /** @type {boolean} */
- doubleTildeEnabled = true;
-
- readToken(state, line) {
- if (line.startsWith('~')) return new MDToken('~', MDTokenType.Tilde);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- if (this.singleTildeEnabled) {
- if (this.attemptPair(state, pass, tokens, MDStrikethroughNode, MDTokenType.Tilde, 2)) return true;
- }
- if (this.doubleTildeEnabled) {
- if (this.attemptPair(state, pass, tokens, MDStrikethroughNode, MDTokenType.Tilde)) return true;
- }
- return false;
- }
- }
-
- /**
- * Reader for underline syntax. Consists of two underscores on either side of
- * some text. If used with `MDStrongReader` which also looks for double
- * underscores, this reader will take priority.
- */
- class MDUnderlineReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('_')) return new MDToken('_', MDTokenType.Underscore);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- return this.attemptPair(state, pass, tokens, MDUnderlineNode, MDTokenType.Underscore, 2);
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other instanceof MDStrongReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Reader for highlight syntax. Consists of pairs of equal signs on either side
- * of some text.
- */
- class MDHighlightReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('=')) return new MDToken('=', MDTokenType.Equal);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- return this.attemptPair(state, pass, tokens, MDHighlightNode, MDTokenType.Equal, 2);
- }
- }
-
- /**
- * Reader for inline code syntax. Consists of one or two delimiting backticks
- * around text. The contents between the backticks will be rendered verbatim,
- * ignoring any inner markdown syntax. To include a backtick inside, escape it
- * with a backslash.
- */
- class MDCodeSpanReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('`')) return new MDToken('`', MDTokenType.Backtick);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- if (this.attemptPair(state, pass, tokens, MDCodeNode, MDTokenType.Backtick, 2, true)) return true;
- if (this.attemptPair(state, pass, tokens, MDCodeNode, MDTokenType.Backtick, 1, true)) return true;
- }
- }
-
- /**
- * Reader for subscript syntax. Consists of single tildes on either side of
- * some text. If used with `MDStrikethroughReader`, this reader will take
- * precedence, and strikethrough can only be done with double tildes.
- */
- class MDSubscriptReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('~')) return new MDToken('~', MDTokenType.Tilde);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- return this.attemptPair(state, pass, tokens, MDSubscriptNode, MDTokenType.Tilde);
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other instanceof MDStrikethroughReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Reader for superscript syntax. Consists of single caret characters on either
- * side of some text.
- */
- class MDSuperscriptReader extends MDSimplePairInlineReader {
- readToken(state, line) {
- if (line.startsWith('^')) return new MDToken('^', MDTokenType.Caret);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- return this.attemptPair(state, pass, tokens, MDSuperscriptNode, MDTokenType.Caret);
- }
- }
-
- /**
- * Reads a hypertext link. Consists of link text between square brackets
- * followed immediately by a URL in parentheses.
- */
- class MDLinkReader extends MDReader {
- static #simpleEmailRegex = new RegExp("^<(" + MDUtils.baseEmailRegex.source + ")>", "i"); // 1=email
- static #simpleURLRegex = new RegExp("^<(" + MDUtils.baseURLRegex.source + ")>", "i"); // 1=URL
-
- readToken(state, line) {
- var groups;
- if (groups = MDToken.tokenizeLabel(line)) {
- return new MDToken(groups[0], MDTokenType.Label, groups[1]);
- }
- if (groups = MDToken.tokenizeEmail(line)) {
- return new MDToken(groups[0], MDTokenType.Email, groups[1], groups[2]);
- }
- if (groups = MDToken.tokenizeURL(line)) {
- return new MDToken(groups[0], MDTokenType.URL, groups[1], groups[2]);
- }
- if (groups = MDLinkReader.#simpleEmailRegex.exec(line)) {
- return new MDToken(groups[0], MDTokenType.SimpleEmail, groups[1]);
- }
- if (groups = MDLinkReader.#simpleURLRegex.exec(line)) {
- return new MDToken(groups[0], MDTokenType.SimpleLink, groups[1]);
- }
- return null;
- }
-
- substituteTokens(state, pass, 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 MDLinkNode(url, state.inlineMarkdownToNode(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 MDLinkNode(url, state.inlineMarkdownToNodes(text), title));
- return true;
- }
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.SimpleEmail ])) {
- const token = match.tokens[0];
- const link = `mailto:${token.content}`;
- const node = new MDLinkNode(link, new MDObfuscatedTextNode(token.content));
- tokens.splice(match.index, 1, node);
- return true;
- }
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.SimpleLink ])) {
- const token = match.tokens[0];
- const link = token.content;
- const node = new MDLinkNode(link, new MDTextNode(link));
- tokens.splice(match.index, 1, node);
- return true;
- }
- return false;
- }
- }
-
- /**
- * Reader for referential URL definitions. Consists of link text between square
- * brackets followed immediately by a reference symbol also in square brackets.
- * The URL can be defined elsewhere on a line by itself with the symbol in square
- * brackets, colon, and the URL (and optional title in quotes).
- */
- class MDReferencedLinkReader extends MDLinkReader {
- /**
- * @param {MDState} state
- */
- 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 MDNode([]); // empty
- }
-
- substituteTokens(state, pass, tokens) {
- var match;
- 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 MDReferencedLinkNode(ref, state.inlineMarkdownToNodes(text)));
- return true;
- }
- return false;
- }
- }
-
- /**
- * Reader for images. Consists of an exclamation, alt text in square brackets,
- * and image URL in parentheses.
- */
- class MDImageReader extends MDLinkReader {
- readToken(state, line) {
- const s = super.readToken(state, line);
- if (s) return s;
- if (line.startsWith('!')) return new MDToken('!', MDTokenType.Bang);
- return null;
- }
-
- substituteTokens(state, pass, 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;
- const node = new MDImageNode(url, alt);
- if (title !== null) {
- node.attributes['title'] = title;
- }
- tokens.splice(match.index, match.tokens.length, node);
- return true;
- }
- return false;
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other.constructor === MDLinkReader || other.constructor === MDReferencedLinkReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Reader for images with referential URL definitions. Consists of an
- * exclamation, alt text in square brackets, and link symbol in square brackets.
- * URL is defined the same as for `MDReferencedLinkReader`.
- */
- class MDReferencedImageReader extends MDReferencedLinkReader {
- readToken(state, line) {
- const s = super.readToken(state, line);
- if (s) return s;
- if (line.startsWith('!')) return new MDToken('!', MDTokenType.Bang);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- var match;
- 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 MDReferencedImageNode(ref, alt));
- return true;
- }
- return false;
- }
-
- compareSubstituteOrdering(other, pass) {
- if (other.constructor === MDLinkReader || other.constructor === MDReferencedLinkReader) {
- return -1;
- }
- return 0;
- }
- }
-
- /**
- * Converts line breaks within blocks into line breaks in the HTML. Not
- * included in any of the default reader sets since most flavors ignore
- * line breaks within blocks.
- */
- class MDLineBreakReader extends MDReader {
- postProcess(state, blocks) {
- MDNode.replaceNodes(state, blocks, (original) => {
- if (!(original instanceof MDTextNode)) return null;
- const lines = original.text.split("\n");
- if (lines.length == 1) return null;
- var nodes = [];
- for (const [i, line] of lines.entries()) {
- if (i > 0) {
- nodes.push(new MDLineBreakNode());
- }
- nodes.push(new MDTextNode(line));
- }
- return new MDNode(nodes);
- });
- }
- }
-
- /**
- * Reads a verbatim HTML tag, and if it passes validation by `MDState.tagFilter`,
- * will be rendered in the final HTML document. Disallowed tags will be rendered
- * as plain text in the resulting document.
- */
- class MDHTMLTagReader extends MDReader {
- readToken(state, line) {
- const tag = MDHTMLTag.fromLineStart(line, state);
- if (tag === null) return null;
- if (!state.root.tagFilter.isValidTagName(tag.tagName)) return null;
- state.root.tagFilter.scrubTag(tag);
- return new MDToken(tag.original, MDTokenType.HTMLTag, tag);
- }
-
- substituteTokens(state, pass, tokens) {
- var match;
- if (match = MDToken.findFirstTokens(tokens, [ MDTokenType.HTMLTag ])) {
- const tag = match.tokens[0].tag
- tokens.splice(match.index, match.tokens.length, new MDHTMLTagNode(tag))
- return true;
- }
- return false;
- }
- }
-
- /**
- * Reads tag modifiers. Consists of curly braces with one or more CSS classes,
- * IDs, or custom attributes separated by spaces to apply to the preceding
- * node. Validation is performed on modifiers and only acceptable values are
- * applied.
- */
- class MDModifierReader extends MDReader {
- readToken(state, line) {
- var modifier = MDTagModifier.fromStart(line);
- if (modifier) return new MDToken(modifier.original, MDTokenType.Modifier, modifier);
- return null;
- }
-
- substituteTokens(state, pass, tokens) {
- // Modifiers are applied elsewhere, and if they're not it's fine if they're
- // rendered as the original syntax.
- return false;
- }
- }
-
-
- // -- Document nodes --------------------------------------------------------
-
-
- /**
- * Base class for nodes in the assembled document tree.
- */
- class MDNode {
- /**
- * Array of CSS classes to add to the node when rendered as HTML.
- * @type {string[]}
- */
- cssClasses = [];
-
- /** @type {string|null} */
- cssId = null;
-
- /**
- * Mapping of CSS attributes to values.
- * @type {object}
- */
- cssStyles = {};
-
- /**
- * Mapping of arbitrary attributes and values to add to this node's top-level
- * tag when rendered as HTML. For `class`, `id`, and `style` attributes, use
- * `cssClasses`, `cssId`, and `cssStyles` instead.
- * @type {object}
- */
- attributes = {};
-
- /**
- * All child nodes in this node.
- * @type {MDNode[]}
- */
- children;
-
- /**
- * @param {MDNode[]} children
- */
- constructor(children=[]) {
- if (children instanceof Array) {
- for (const elem of children) {
- if (!(elem instanceof MDNode)) {
- throw new Error(`${this.constructor.name} expects children of type MDNode[] or MDNode, got array with ${MDUtils.typename(elem)} element`);
- }
- }
- this.children = children;
- } else if (children instanceof MDNode) {
- this.children = [ children ];
- } else {
- throw new Error(`${this.constructor.name} expects children of type MDNode[] or MDNode, got ${MDUtils.typename(children)}`);
- }
- }
-
- /**
- * Adds a CSS class. If already present it will not be duplicated.
- *
- * @param {string} cssClass
- * @returns {boolean} whether the class was added
- */
- addClass(cssClass) {
- if (this.cssClasses.indexOf(cssClass) >= 0) return false;
- this.cssClasses.push(cssClass);
- return true;
- }
-
- /**
- * Removes a CSS class.
- *
- * @param {string} cssClass
- * @returns {boolean} whether the class was present and removed
- */
- removeClass(cssClass) {
- const beforeLength = this.cssClasses.length;
- this.cssClasses = this.cssClasses.filter((val) => val !== cssClass);
- return this.cssClasses.length != beforeLength;
- }
-
- /**
- * Renders this node and any children as an HTML string. If the node has no
- * content an empty string should be returned.
- *
- * @param {MDState} state
- * @returns {string} HTML string
- */
- toHTML(state) {
- return MDNode.toHTML(this.children, state);
- }
-
- /**
- * Renders this node and any children as a plain text string. The conversion
- * should only render ordinary text, not attempt markdown-like formatting
- * (e.g. list items should not be prefixed with asterisks, only have their
- * content text returned). If the node has no renderable content an empty
- * string should be returned.
- *
- * @param {MDState} state
- * @returns {string} plaintext string
- */
- toPlaintext(state) {
- return MDNode.toPlaintext(this.children, state);
- }
-
- /**
- * Protected helper method that renders an HTML fragment of the attributes
- * to apply to the root HTML tag representation of this node.
- *
- * Example result with a couple `cssClasses`, a `cssId`, and a custom
- * `attributes` key-value pair:
- *
- * ```
- * class="foo bar" id="baz" lang="en"
- * ```
- *
- * The value includes a leading space if it's non-empty so that it can be
- * concatenated directly after the tag name and before the closing `>`.
- *
- * @returns {string} HTML fragment
- */
- _htmlAttributes() {
- var html = '';
- if (this.cssClasses.length > 0) {
- html += ` class="${this.cssClasses.join(' ')}"`;
- }
- if (this.cssId !== null && this.cssId.length > 0) {
- html += ` id="${this.cssId}"`;
- }
- var styles = [];
- for (const key in this.cssStyles) {
- styles.push(`${key}: ${this.cssStyles[key]};`)
- }
- if (styles.length > 0) {
- html += ` style="${MDUtils.escapeHTML(styles.join(' '))}"`;
- }
- for (const key in this.attributes) {
- if (key == 'class' || key == 'id' || key == 'style') continue;
- const value = `${this.attributes[key]}`;
- const cleanKey = MDUtils.scrubAttributeName(key);
- if (cleanKey.length == 0) continue;
- const cleanValue = MDUtils.escapeHTML(value);
- html += ` ${cleanKey}="${cleanValue}"`;
- }
- return html;
- }
-
- /**
- * Protected helper that renders and concatenates the HTML of all children
- * of this node. Mostly for use by subclasses in their `toHTML`
- * implementations.
- *
- * @param {MDState} state
- * @returns {string} concatenated HTML
- */
- _childHTML(state) {
- return MDNode.toHTML(this.children, state);
- }
-
- /**
- * Protected helper that renders and concatenates the plaintext of all
- * children of this node.
- *
- * @param {MDState} state
- * @returns {string} concatenated plaintext
- */
- _childPlaintext(state) {
- return MDNode.toPlaintext(this.children, state);
- }
-
- /**
- * Protected helper for rendering nodes represented by simple paired HTML
- * tags. Custom CSS classes and attributes will be included in the result,
- * and child content will be rendered between the tags.
- *
- * @param {MDState} state
- * @param {string} tagName - HTML tag name, without angle braces
- * @returns {string} HTML string
- */
- _simplePairedTagHTML(state, tagName) {
- const openTagSuffix = this.children[0] instanceof MDBlockNode ? '\n' : ''
- const closeTagPrefix = this.children[this.children.length - 1] instanceof MDBlockNode ? '\n' : '';
- const closeTagSuffix = this instanceof MDBlockNode ? '\n' : '';
- return `<${tagName}${this._htmlAttributes()}>${openTagSuffix}${this._childHTML(state)}${closeTagPrefix}</${tagName}>${closeTagSuffix}`;
- }
-
- /**
- * Calls the given callback function with every child node, recursively.
- * Nodes are visited depth-first.
- *
- * @param {function} fn - callback that accepts one `MDNode` argument
- */
- visitChildren(fn) {
- if (this.children === undefined || !Array.isArray(this.children)) {
- return;
- }
- for (const child of this.children) {
- fn(child);
- child.visitChildren(fn);
- }
- }
-
- /**
- * Helper for rendering and concatenating HTML from an array of `MDNode`s.
- *
- * @param {MDNode[]} nodes
- * @param {MDState} state
- * @returns {string} HTML string
- */
- static toHTML(nodes, state) {
- return nodes.map((node) => node.toHTML(state) + (node instanceof MDBlockNode ? '\n' : '')).join('');
- }
-
- /**
- * Helper for rendering and concatenating plaintext from an array of `MDNode`s.
- *
- * @param {MDNode[]} nodes
- * @param {MDState} state
- * @returns {string} plaintext
- */
- static toPlaintext(nodes, state) {
- return nodes.map((node) => node.toPlaintext(state)).join('');
- }
-
- /**
- * Recursively searches and replaces nodes in a tree. The given `replacer`
- * is passed every node in the tree. If `replacer` returns a new `MDNode`
- * the original will be replaced with it. If the function returns `null` no
- * change will be made to that node. Traversal is depth-first.
- *
- * @param {MDState} state
- * @param {MDNode[]} nodes
- * @param {function} replacer - takes a node as an argument, returns either
- * a new node or `null` to leave it unchanged
- */
- static replaceNodes(state, nodes, replacer) {
- for (var i = 0; i < nodes.length; i++) {
- var originalNode = nodes[i];
- const replacement = replacer(originalNode);
- if (replacement instanceof MDNode) {
- nodes.splice(i, 1, replacement);
- } else {
- this.replaceNodes(state, originalNode.children, replacer);
- }
- }
- }
- }
-
- /**
- * Marker subclass that indicates a node represents block syntax.
- */
- class MDBlockNode extends MDNode {}
-
- /**
- * Paragraph block.
- */
- class MDParagraphNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'p');
- }
- }
-
- /**
- * A heading block with a level from 1 to 6.
- */
- class MDHeadingNode extends MDBlockNode {
- /** @type {number} */
- level;
-
- constructor(level, children) {
- super(children);
- if (typeof level !== 'number' || (level < 1 || level > 6)) {
- throw new Error(`${this.constructor.name} requires heading level 1 to 6`);
- }
- this.level = level;
- }
-
- toHTML(state) {
- return this._simplePairedTagHTML(state, `h${this.level}`);
- }
- }
-
- /**
- * A sub-text block with smaller, less prominent text.
- */
- class MDSubtextNode extends MDBlockNode {
- toHTML(state) {
- this.addClass('subtext');
- return this._simplePairedTagHTML(state, 'div');
- }
- }
-
- /**
- * Node for a horizontal dividing line.
- */
- class MDHorizontalRuleNode extends MDBlockNode {
- toHTML(state) {
- return `<hr${this._htmlAttributes()}>`;
- }
- }
-
- /**
- * A block quote, usually rendered indented from other text.
- */
- class MDBlockquoteNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'blockquote');
- }
- }
-
- /**
- * A bulleted list. Contains `MDListItemNode` children.
- */
- class MDUnorderedListNode extends MDBlockNode {
- /** @type {MDListItemNode[]} children */
-
- /**
- * @param {MDListItemNode[]} children
- */
- constructor(children) {
- super(children);
- }
-
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'ul');
- }
- }
-
- /**
- * A numbered list. Contains `MDListItemNode` children.
- */
- class MDOrderedListNode extends MDBlockNode {
- /** @type {MDListItemNode[]} children */
-
- /** @type {number|null} */
- startOrdinal;
-
- /**
- * @param {MDListItemNode[]} children
- * @param {number|null} startOrdinal
- */
- constructor(children, startOrdinal=null) {
- super(children);
- this.startOrdinal = startOrdinal;
- }
-
- toHTML(state) {
- if (this.startOrdinal !== null && this.startOrdinal != 1) this.attributes['start'] = this.startOrdinal;
- return this._simplePairedTagHTML(state, 'ol');
- }
- }
-
- /**
- * An item in a bulleted or numbered list.
- */
- class MDListItemNode extends MDBlockNode {
- /** @type {number|null} */
- ordinal;
-
- /**
- * @param {MDNode|MDNode[]} children
- * @param {number|null} ordinal
- */
- constructor(children, ordinal=null) {
- super(children);
- this.ordinal = ordinal;
- }
-
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'li');
- }
- }
-
- /**
- * A block of preformatted computer code. Inner markdown is ignored.
- */
- class MDCodeBlockNode extends MDBlockNode {
- /** @type {string} */
- text;
-
- /**
- * The programming language of the content.
- * @type {string|null}
- */
- language;
-
- /**
- * @param {string} text
- * @param {string|null} language
- */
- constructor(text, language=null) {
- super([]);
- this.text = text;
- this.language = language;
- }
-
- toHTML(state) {
- const languageModifier = (this.language !== null) ? ` class="language-${this.language}"` : '';
- return `<pre${this._htmlAttributes()}><code${languageModifier}>` +
- `${MDUtils.escapeHTML(this.text)}</code></pre>\n`;
- }
- }
-
- /**
- * A table node with a single header row and any number of body rows.
- *
- * If modifying the rows, use the `headerRow` and `bodyRows` accessors,
- * otherwise `children` may get out of sync.
- */
- class MDTableNode extends MDBlockNode {
- /** @param {MDTableRowNode[]} children */
-
- /** @type {MDTableRowNode} */
- get headerRow() { return this.#headerRow; }
- set headerRow(newValue) {
- this.#headerRow = newValue;
- this.#recalculateChildren();
- }
- #headerRow;
-
- /** @type {MDTableRowNode[]} */
- get bodyRows() { return this.#bodyRows; }
- set bodyRows(newValue) {
- this.#bodyRows = newValue;
- this.#recalculateChildren();
- }
- #bodyRows;
-
- /**
- * How to align each column. Columns beyond the length of the array or with
- * corresponding `null` elements will have no alignment set. Values should
- * be valid CSS `text-align` values.
- *
- * @type {string[]}
- */
- columnAlignments = [];
-
- /**
- * @param {MDTableRowNode} headerRow
- * @param {MDTableRowNode[]} bodyRows
- */
- constructor(headerRow, bodyRows) {
- super([ headerRow, ...bodyRows ]);
- this.#headerRow = headerRow;
- this.#bodyRows = bodyRows;
- }
-
- #recalculateChildren() {
- this.children = [ this.#headerRow, ...this.#bodyRows ];
- }
-
- #applyAlignments() {
- this.children.forEach((child) => this.#applyAlignmentsToRow(child));
- }
-
- /**
- * @param {MDTableRowNode} row
- */
- #applyAlignmentsToRow(row) {
- for (const [columnIndex, cell] of row.children.entries()) {
- const alignment = columnIndex < this.columnAlignments.length ? this.columnAlignments[columnIndex] : null;
- this.#applyAlignmentToCell(cell, alignment);
- }
- }
-
- /**
- * @param {MDTableCellNode} cell
- * @param {string|null} alignment
- */
- #applyAlignmentToCell(cell, alignment) {
- if (alignment) {
- cell.cssStyles['text-align'] = alignment;
- } else {
- delete cell.cssStyles['text-align'];
- }
- }
-
- toHTML(state) {
- this.#applyAlignments();
- var html = '';
- html += `<table${this._htmlAttributes()}>\n`;
- html += '<thead>\n';
- html += this.headerRow.toHTML(state) + '\n';
- html += '</thead>\n';
- html += '<tbody>\n';
- html += MDNode.toHTML(this.bodyRows, state) + '\n';
- html += '</tbody>\n';
- html += '</table>\n';
- return html;
- }
- }
-
- /**
- * Node for one row (header or body) in a table.
- */
- class MDTableRowNode extends MDBlockNode {
- /** @type {MDTableCellNode[]} children */
-
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'tr');
- }
- }
-
- /**
- * Node for one cell in a table row.
- */
- class MDTableCellNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'td');
- }
- }
-
- /**
- * Node for a header cell in a header table row.
- */
- class MDTableHeaderCellNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'th');
- }
- }
-
- /**
- * Definition list with `MDDefinitionListTermNode` and
- * `MDDefinitionListDefinitionNode` children.
- */
- class MDDefinitionListNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'dl');
- }
- }
-
- /**
- * A word or term in a definition list.
- */
- class MDDefinitionListTermNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'dt');
- }
- }
-
- /**
- * The definition of a word or term in a definition list. Should follow a
- * definition term, or another definition to serve as an alternate.
- */
- class MDDefinitionListDefinitionNode extends MDBlockNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'dd');
- }
- }
-
- /**
- * Block at the bottom of a document listing all the footnotes with their
- * content.
- */
- class MDFootnoteListNode extends MDBlockNode {
- /**
- * @param {MDState} state
- * @param {string} symbol
- * @return {number}
- */
- #footnoteId(state, symbol) {
- const lookup = state.root['footnoteIds'];
- if (!lookup) return null;
- return lookup[symbol] ?? null;
- }
-
- toHTML(state) {
- const footnotes = state.footnotes;
- var symbolOrder = Object.keys(footnotes);
- if (Object.keys(footnotes).length == 0) return '';
- const footnoteUniques = state.root.footnoteInstances;
- var html = '';
- html += '<div class="footnotes">';
- html += '<ol>';
- for (const symbol of symbolOrder) {
- /** @type {MDNode[]} */
- let content = footnotes[symbol];
- if (!content) continue;
- let footnoteId = this.#footnoteId(state, symbol);
- const contentHTML = MDNode.toHTML(content, state);
- html += `<li value="${footnoteId}" id="${state.root.elementIdPrefix}footnote_${footnoteId}">${contentHTML}`;
- const uniques = footnoteUniques[symbol];
- if (uniques) {
- for (const unique of uniques) {
- html += ` <a href="#${state.root.elementIdPrefix}footnoteref_${unique}" class="footnote-backref">↩︎</a>`;
- }
- }
- html += `</li>\n`;
- }
- html += '</ol>';
- html += '</div>';
- return html;
- }
-
- toPlaintext(state) {
- const footnotes = state.footnotes;
- var symbolOrder = Object.keys(footnotes);
- if (Object.keys(footnotes).length == 0) return '';
- var text = '';
- for (const symbol of symbolOrder) {
- let content = footnotes[symbol];
- if (!content) continue;
- text += `${symbol}. ${this._childPlaintext(state)}\n`;
- }
- return text.trim();
- }
- }
-
- /**
- * Marker subclass that indicates a node represents inline syntax.
- */
- class MDInlineNode extends MDNode {}
-
- /**
- * Contains plain text. Special HTML characters are escaped when rendered.
- */
- class MDTextNode extends MDInlineNode {
- text;
-
- constructor(text) {
- super([]);
- this.text = text;
- }
-
- toHTML(state) {
- return MDUtils.escapeHTML(this.text);
- }
-
- toPlaintext(state) {
- return this.text;
- }
- }
-
- /**
- * Contains plain text which is rendered with HTML entities when rendered to
- * be marginally more difficult for web scapers to decipher. Used for
- * semi-sensitive info like email addresses.
- */
- class MDObfuscatedTextNode extends MDTextNode {
- toHTML(state) {
- return MDUtils.escapeObfuscated(this.text);
- }
- }
-
- /**
- * Emphasized (italicized) content.
- */
- class MDEmphasisNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'em');
- }
- }
-
- /**
- * Strong (bold) content.
- */
- class MDStrongNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'strong');
- }
- }
-
- /**
- * Content rendered with a line through it.
- */
- class MDStrikethroughNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 's');
- }
- }
-
- /**
- * Underlined content.
- */
- class MDUnderlineNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'u');
- }
- }
-
- /**
- * Highlighted content. Usually rendered with a bright colored background.
- */
- class MDHighlightNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'mark');
- }
- }
-
- /**
- * Superscripted content.
- */
- class MDSuperscriptNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'sup');
- }
- }
-
- /**
- * Subscripted content.
- */
- class MDSubscriptNode extends MDInlineNode {
- toHTML(state) {
- return this._simplePairedTagHTML(state, 'sub');
- }
- }
-
- /**
- * Inline plaintext indicating computer code.
- */
- class MDCodeNode extends MDInlineNode {
- /** @type {string} */
- text;
-
- constructor(text) {
- super([]);
- this.text = text;
- }
-
- toHTML(state) {
- return `<code${this._htmlAttributes()}>${MDUtils.escapeHTML(this.text)}</code>`;
- }
- }
-
- /**
- * A footnote symbol in a document. Denoted as a superscripted number that can
- * be clicked to go to its content at the bottom of the document.
- */
- class MDFootnoteNode extends MDInlineNode {
- /**
- * Symbol the author used to match up the footnote to its content definition.
- * @type {string}
- */
- symbol;
-
- /**
- * The superscript symbol rendered in HTML. May be the same or different
- * than `symbol`.
- * @type {string} display symbol
- */
- displaySymbol = null;
-
- /**
- * Unique ID for the footnote definition.
- * @type {number|null}
- */
- footnoteId = null;
-
- /**
- * Unique number for backlinking to a footnote occurrence. Populated by
- * `MDFootnoteReader.postProcess`.
- * @type {number|null}
- */
- occurrenceId = null;
-
- /**
- * @param {string} symbol
- * @param {string|null} title
- */
- constructor(symbol, title=null) {
- super([]);
- this.symbol = symbol;
- if (title) this.attributes['title'] = title;
- }
-
- toHTML(state) {
- if (this.differentiator !== null) {
- return `<sup class="footnote" id="${state.root.elementIdPrefix}footnoteref_${this.occurrenceId}"${this._htmlAttributes()}>` +
- `<a href="#${state.root.elementIdPrefix}footnote_${this.footnoteId}">${MDUtils.escapeHTML(this.displaySymbol ?? this.symbol)}</a></sup>`;
- }
- return `<!--FNREF:{${this.symbol}}-->`;
- }
- }
-
- /**
- * A clickable hypertext link.
- */
- class MDLinkNode extends MDInlineNode {
- /** @type {string} */
- href;
-
- /**
- * @param {string} href
- * @param {MDNode[]|MDNode} children
- */
- constructor(href, children, title=null) {
- super(children);
- this.href = href;
- if (title !== null) this.attributes['title'] = title;
- }
-
- toHTML(state) {
- var escapedLink;
- if (this.href.startsWith('mailto:')) {
- escapedLink = MDUtils.escapeObfuscated(this.href);
- } else {
- escapedLink = MDUtils.escapeHTML(this.href);
- }
- return `<a href="${escapedLink}"${this._htmlAttributes()}>${this._childHTML(state)}</a>`;
- }
- }
-
- /**
- * A clickable hypertext link where the URL is defined elsewhere by reference.
- */
- class MDReferencedLinkNode extends MDLinkNode {
- /** @type {string} */
- reference;
-
- constructor(reference, children) {
- super('', children);
- this.reference = reference;
- }
-
- /**
- * @param {MDState} state
- */
- toHTML(state) {
- if (this.href === '') {
- const url = state.urlForReference(this.reference);
- if (url) this.href = url;
- const title = state.urlTitleForReference(this.reference);
- if (title) this.attributes['title'] = title;
- }
- return super.toHTML(state);
- }
- }
-
- /**
- * An inline image.
- */
- class MDImageNode extends MDInlineNode {
- /** @type {string} */
- src;
-
- /** @type {string|null} */
- alt;
-
- /**
- * @param {string} src
- * @param {string|null} alt
- */
- constructor(src, alt) {
- super([]);
- this.src = src;
- this.alt = alt;
- }
-
- toHTML(state) {
- var html = `<img src="${MDUtils.escapeHTML(this.src)}"`;
- if (this.alt) html += ` alt="${MDUtils.escapeHTML(this.alt)}"`;
- html += `${this._htmlAttributes()}>`;
- return html;
- }
- }
-
- /**
- * An inline image where the URL is defined elsewhere by reference.
- */
- class MDReferencedImageNode extends MDImageNode {
- /** @type {string} */
- reference;
-
- /**
- * @param {string} reference
- * @param {string|null} alt
- */
- constructor(reference, alt='') {
- super('', alt, []);
- this.reference = reference;
- }
-
- toHTML(state) {
- if (this.src === '') {
- const url = state.urlForReference(this.reference);
- if (url !== null) this.src = url;
- const title = state.urlTitleForReference(this.reference);
- if (title !== null) this.attributes['title'] = title;
- }
- return super.toHTML(state);
- }
- }
-
- /**
- * An abbreviation that can be hovered over to see its full expansion.
- */
- class MDAbbreviationNode extends MDInlineNode {
- /** @type {string} */
- abbreviation;
-
- /** @type {string} */
- get definition() { return this.attributes['title'] ?? null; }
- set definition(newValue) { this.attributes['title'] = newValue; }
-
- /**
- * @param {string} abbreviation
- * @param {string} definition
- */
- constructor(abbreviation, definition) {
- super([]);
- this.abbreviation = abbreviation;
- this.attributes['title'] = definition;
- }
-
- toHTML(state) {
- return `<abbr${this._htmlAttributes()}>${MDUtils.escapeHTML(this.abbreviation)}</abbr>`;
- }
- }
-
- /**
- * A line break that is preserved when rendered to HTML.
- */
- class MDLineBreakNode extends MDInlineNode {
- toHTML(state) {
- return '<br>';
- }
-
- toPlaintext(state) {
- return '\n';
- }
- }
-
- /**
- * A verbatim HTML tag. May be altered to strip out disallowed attributes or
- * CSS values.
- */
- class MDHTMLTagNode extends MDInlineNode {
- /** @type {MDHTMLTag} */
- tag;
-
- constructor(tag) {
- super([]);
- this.tag = tag;
- }
-
- toHTML(state) {
- return this.tag.toString();
- }
- }
-
-
- // -- Main class ------------------------------------------------------------
-
-
- /**
- * Markdown parser.
- */
- class Markdown {
- /**
- * Set of standard readers to handle common syntax.
- * @type {MDReader[]}
- */
- static standardReaders = [
- new MDUnderlinedHeadingReader(),
- new MDHashHeadingReader(),
- new MDBlockQuoteReader(),
- new MDHorizontalRuleReader(),
- new MDUnorderedListReader(),
- new MDOrderedListReader(),
- new MDFencedCodeBlockReader(),
- new MDIndentedCodeBlockReader(),
- new MDParagraphReader(),
-
- new MDStrongReader(),
- new MDEmphasisReader(),
- new MDCodeSpanReader(),
- new MDImageReader(),
- new MDLinkReader(),
- new MDHTMLTagReader(),
- ];
-
- /**
- * All supported readers except `MDLineBreakReader`.
- * @type {MDReader[]}
- */
- static allReaders = [
- ...this.standardReaders,
- new MDSubtextReader(),
- new MDTableReader(),
- new MDDefinitionListReader(),
- new MDFootnoteReader(),
- new MDAbbreviationReader(),
-
- new MDUnderlineReader(),
- new MDSubscriptReader(),
- new MDStrikethroughReader(),
- new MDHighlightReader(),
- new MDSuperscriptReader(),
- new MDReferencedImageReader(),
- new MDReferencedLinkReader(),
- new MDModifierReader(),
- ];
-
- /**
- * Shared instance of a parser with standard syntax.
- */
- static standardParser = new Markdown(this.standardReaders);
-
- /**
- * Shared instance of a parser with all supported syntax.
- */
- static completeParser = new Markdown(this.allReaders);
-
- /**
- * Filter for what non-markdown HTML is permitted. HTML generated as a
- * result of markdown is unaffected.
- */
- tagFilter = new MDHTMLFilter();
-
- /** @type {MDReader[]} */
- #readers;
-
- /** @type {MDReader[]} */
- #readersByBlockPriority;
- /** @type {MDReader[]} */
- #readersByTokenPriority;
- /** @type {Array} */
- #readersBySubstitutePriority;
-
- /**
- * Creates a Markdown parser with the given syntax readers.
- *
- * @param {MDReader[]} readers
- */
- constructor(readers=Markdown.allReaders) {
- this.#readers = readers;
- this.#readersByBlockPriority = MDReader.sortReaderForBlocks(readers);
- this.#readersByTokenPriority = MDReader.sortReadersForTokenizing(readers);
- this.#readersBySubstitutePriority = MDReader.sortReadersForSubstitution(readers);
- }
-
- /**
- * Converts a markdown string to an HTML string.
- *
- * @param {string} markdown
- * @param {string} elementIdPrefix - Optional prefix for generated element
- * `id`s and links to them. For differentiating multiple markdown docs in
- * the same HTML page.
- * @returns {string} HTML
- */
- toHTML(markdown, elementIdPrefix='') {
- const lines = markdown.split(/(?:\n|\r|\r\n)/);
- try {
- return this.#parse(lines, elementIdPrefix);
- } catch (e) {
- this.#investigateException(lines, elementIdPrefix);
- throw e;
- }
- }
-
- /**
- * @param {string[]} lines
- * @param {string} elementIdPrefix
- */
- #parse(lines, elementIdPrefix) {
- const state = new MDState(lines);
- state.readersByBlockPriority = this.#readersByBlockPriority;
- state.readersByTokenPriority = this.#readersByTokenPriority
- state.readersBySubstitutePriority = this.#readersBySubstitutePriority
- state.tagFilter = this.tagFilter;
- state.elementIdPrefix = elementIdPrefix;
- for (const reader of this.#readers) {
- reader.preProcess(state);
- }
- const nodes = state.readBlocks();
- for (const reader of this.#readers) {
- reader.postProcess(state, nodes);
- }
- return MDNode.toHTML(nodes, state);
- }
-
- /**
- * Keeps removing first and last lines of markdown to locate the source of
- * an exception and prints the minimal snippet to `console.error`.
- *
- * @param {string[]} lines
- * @param {string} elementIdPrefix
- */
- #investigateException(lines, elementIdPrefix) {
- var startIndex = 0;
- var endIndex = lines.length;
- // Keep stripping away first line until an exception stops being thrown
- for (var i = 0; i < lines.length; i++) {
- try {
- this.#parse(lines.slice(i, endIndex), elementIdPrefix);
- break;
- } catch (e0) {
- startIndex = i;
- }
- }
- // Keep stripping away last line until an exception stops being thrown
- for (var i = lines.length; i > startIndex; i--) {
- try {
- this.#parse(lines.slice(startIndex, i), elementIdPrefix);
- break;
- } catch (e0) {
- endIndex = i;
- }
- }
- const problematicMarkdown = lines.slice(startIndex, endIndex).join("\n");
- console.error(`This portion of markdown caused an unexpected exception: ${problematicMarkdown}`);
- }
- }
|