| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226 |
- // FIXME: Nested blockquotes require blank line
- // TODO: HTML tags probably need better handling. Consider whether interior of matched tags should be interpreted as markdown.
- // TODO: Test broken/incomplete syntax thoroughly
- // TODO: Sanity checks on loops/recursion?
- // TODO: Tolerate whitespace between tokens (e.g. [click here] [urlref])
- // TODO: Spreadsheet functions in tables
-
- class _MDHAlign {
- static Left = new _MDHAlign('Left');
- static Center = new _MDHAlign('Center');
- static Right = new _MDHAlign('Right');
-
- /** @var {String} */
- name;
-
- constructor(name) {
- this.name = name;
- }
-
- toString() {
- return `${this.constructor.name}.${this.name}`;
- }
-
- static toHTMLAttribute(align) {
- switch (align) {
- case _MDHAlign.Left: return ' align="left"';
- case _MDHAlign.Center: return ' align="center"';
- case _MDHAlign.Right: return ' align="right"';
- }
- return '';
- }
- }
-
- class _MDTokenType {
- static Text = new _MDTokenType('Text');
- static Whitespace = new _MDTokenType('Whitespace');
-
- static Underscore = new _MDTokenType('Underscore');
- static Asterisk = new _MDTokenType('Asterisk');
- static Slash = new _MDTokenType('Slash');
- static Tilde = new _MDTokenType('Tilde');
- static Bang = new _MDTokenType('Bang');
- static Backtick = new _MDTokenType('Backtick');
-
- static Label = new _MDTokenType('Label'); // content=label
- static URL = new _MDTokenType('URL'); // content=URL, extra=title
- static Email = new _MDTokenType('Email'); // content=email address, extra=title
- static SimpleLink = new _MDTokenType('SimpleLink'); // content=URL
- static SimpleEmail = new _MDTokenType('SimpleEmail'); // content=email address
- static Footnote = new _MDTokenType('Footnote'); // content=symbol
- static Modifier = new _MDTokenType('Modifier'); // content
-
- static HTMLTag = new _MDTokenType('HTMLTag'); // content=tag string, tag=_MDHTMLTag
-
- static META_AnyNonWhitespace = new _MDTokenType('METAAnyNonWhitespace');
-
- /** @var {String} */
- name;
-
- constructor(name) {
- this.name = name;
- }
-
- toString() {
- return `${this.constructor.name}.${this.name}`;
- }
- }
-
- class _MDToken {
- /**
- * The original token string.
- * @var {String}
- */
- original;
- /** @var {_MDTokenType} */
- type;
- /** @var {String|null} */
- content;
- /** @var {String|null} */
- extra;
- /** @var {_MDHTMLTag|null} */
- tag;
- /** @var {_MDTagModifier|null} */
- modifier;
-
- /**
- * @param {String} original
- * @param {_MDTokenType} type
- * @param {String|_MDTagModifier|null} content
- * @param {String|null} extra
- * @param {_MDHTMLTag|null} tag
- */
- constructor(original, type, content=null, extra=null, tag=null) {
- this.original = original;
- this.type = type;
- if (content instanceof _MDTagModifier) {
- this.content = null;
- this.modifier = content;
- } else {
- this.content = content;
- this.modifier = null;
- }
- this.extra = extra;
- this.tag = tag;
- }
- }
-
- class _MDUtils {
- /**
- * @param {String} str
- * @returns {String}
- */
- static escapeHTML(str) {
- return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
- }
-
- /**
- * @param {String} email
- */
- static escapeObfuscated(text) {
- var html = '';
- for (var p = 0; p < text.length; p++) {
- const cp = text.codePointAt(p);
- html += `&#${cp};`;
- }
- return html;
- }
- }
-
-
- // -- Spans -----------------------------------------------------------------
-
-
- class _MDSpan {
- /** @var {String[]} */
- cssClasses = [];
- /** @var {String|null} */
- cssId = null;
- /** @var {Object} */
- attributes = {};
-
- /**
- * @param {_MDState} state
- * @returns {String} HTML
- */
- toHTML(state) {
- throw new Error(self.constructor.name + ".toHTML not implemented");
- }
-
- htmlAttributes() {
- var html = '';
- if (this.cssClasses.length > 0) {
- html += ` class="${this.cssClasses.join(' ')}"`;
- }
- if (this.cssId !== null) {
- html += ` id="${this.cssId}"`;
- }
- for (const name in this.attributes) {
- let value = this.attributes[name];
- html += ` ${name}="${_MDUtils.escapeHTML(value)}"`;
- }
- return html;
- }
-
- /**
- * @param {_MDSpan[]} spans
- * @param {_MDState} state
- */
- static toHTML(spans, state) {
- return spans.map((span) => span.toHTML(state)).join("");
- }
- }
-
- class _MDMultiSpan extends _MDSpan {
- /** @var {_MDSpan[]} */
- content;
-
- /**
- * @param {_MDSpan[]} content
- */
- constructor(content) {
- super();
- this.content = content;
- }
-
- toHTML(state) {
- return _MDSpan.toHTML(this.content, state);
- }
- }
-
- class _MDTextSpan extends _MDSpan {
- /** @param {String} text */
- text;
-
- /**
- * @param {String} text
- */
- constructor(text) {
- super();
- this.text = text;
- }
-
- toHTML(state) {
- let html = this.text.replace('<', '<');
- let abbrevs = state.abbreviations;
- let regexes = state.abbreviationRegexes;
- for (const abbrev in abbrevs) {
- let def = abbrevs[abbrev];
- let regex = regexes[abbrev];
- let escapedDef = _MDUtils.escapeHTML(def);
- html = html.replace(regex, `<abbr title="${escapedDef}">$1</abbr>`);
- }
- return html;
- }
- }
-
- class _MDHTMLSpan extends _MDSpan {
- /** @param {String} html */
- html;
-
- /**
- * @param {String} html
- */
- constructor(html) {
- super();
- this.html = html;
- }
-
- toHTML(state) {
- return this.html;
- }
- }
-
- class _MDObfuscatedTextSpan extends _MDSpan {
- /** @param {String} text */
- text;
-
- /**
- * @param {String} text
- */
- constructor(text) {
- super();
- this.text = text;
- }
-
- toHTML(state) {
- return _MDUtils.escapeObfuscated(this.text);
- }
- }
-
- class _MDLinkSpan extends _MDSpan {
- /** @var {String} */
- link;
- /** @var {String|null} */
- target = null;
- /** @var {_MDSpan} */
- content;
- /** @var {String|null} */
- title = null;
-
- /**
- * @param {String} link
- * @param {_MDSpan} content
- */
- constructor(link, content, title=null) {
- super();
- this.link = link;
- this.content = content;
- this.title = title;
- }
-
- toHTML(state) {
- var escapedLink;
- if (this.link.startsWith('mailto:')) {
- escapedLink = 'mailto:' + _MDUtils.escapeObfuscated(this.link.substring(7));
- } else {
- escapedLink = _MDUtils.escapeHTML(this.link);
- }
- var html = `<a href="${escapedLink}"`;
- if (this.target) {
- html += ` target="${_MDUtils.escapeHTML(this.target)}"`;
- }
- if (this.title) {
- html += ` title="${_MDUtils.escapeHTML(this.title)}"`;
- }
- html += this.htmlAttributes();
- html += '>' + this.content.toHTML(state) + '</a>';
- return html;
- }
- }
-
- class _MDReferencedLinkSpan extends _MDLinkSpan {
- /** @var {String} */
- ref;
-
- constructor(ref, content) {
- super(null, content);
- this.ref = ref;
- }
-
- /**
- * @param {_MDState} state
- */
- toHTML(state) {
- if (!this.link) {
- let url = state.urls[this.ref.toLowerCase()];
- let title = state.urlTitles[this.ref.toLowerCase()];
- this.link = url;
- this.title = title || this.title;
- }
- if (this.link) {
- return super.toHTML(state);
- } else {
- let contentHTML = this.content.toHTML(state);
- return `[${contentHTML}][${this.ref}]`;
- }
- }
- }
-
- class _MDEmphasisSpan extends _MDSpan {
- /** @var {_MDSpan} */
- #content;
-
- /**
- * @param {_MDSpan} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<em${this.htmlAttributes()}>${contentHTML}</em>`;
- }
- }
-
- class _MDStrongSpan extends _MDSpan {
- /** @var {_MDSpan} content */
- #content;
-
- /**
- * @param {_MDSpan} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<strong${this.htmlAttributes()}>${contentHTML}</strong>`;
- }
- }
-
- class _MDStrikethroughSpan extends _MDSpan {
- /** @var {_MDSpan} content */
- #content;
-
- /**
- * @param {_MDSpan} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<strike${this.htmlAttributes()}>${contentHTML}</strike>`;
- }
- }
-
- class _MDCodeSpan extends _MDSpan {
- /** @var {String} content */
- #content;
-
- /**
- * @param {String} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- return `<code${this.htmlAttributes()}>${_MDUtils.escapeHTML(this.#content)}</code>`;
- }
- }
-
- class _MDImageSpan extends _MDSpan {
- /** @var {String} */
- source;
- /** @var {String|null} */
- alt;
- /** @var {String|null} */
- title;
-
- /**
- * @param {String} source
- */
- constructor(source, alt, title=null) {
- super();
- this.source = source;
- this.alt = alt;
- this.title = title;
- }
-
- toHTML(state) {
- let html = `<img src="${_MDUtils.escapeHTML(this.source)}"`;
- if (this.alt) {
- html += ` alt="${_MDUtils.escapeHTML(this.alt)}"`;
- }
- if (this.title) {
- html += ` title="${_MDUtils.escapeHTML(this.title)}"`;
- }
- html += this.htmlAttributes();
- html += '>';
- return html;
- }
- }
-
- class _MDReferencedImageSpan extends _MDImageSpan {
- /** @var {String} */
- ref;
-
- /**
- * @param {String} ref
- * @param {String|null} alt
- */
- constructor(ref, alt) {
- super(null, alt);
- this.ref = ref;
- }
-
- toHTML(state) {
- if (!this.source) {
- let url = state.urls[this.ref.toLowerCase()];
- let title = state.urlTitles[this.ref.toLowerCase()];
- this.source = url;
- this.title = title || this.title;
- }
- if (this.source) {
- return super.toHTML(state);
- } else {
- return `![${_MDUtils.escapeHTML(this.alt)}][${_MDUtils.escapeHTML(this.ref)}]`;
- }
- }
- }
-
- class _MDFootnoteReferenceSpan extends _MDSpan {
- /** @var {String} */
- symbol;
-
- /**
- * @param {String} symbol
- */
- constructor(symbol) {
- super();
- this.symbol = symbol;
- }
-
- toHTML(state) {
- return `<!--FNREF:{${this.symbol}}-->`;
- }
- }
-
-
- // -- Blocks ----------------------------------------------------------------
-
-
- class _MDBlock {
- /** @var {String[]} */
- cssClasses = [];
- /** @var {String|null} */
- cssId = null;
- /** @var {Object} */
- attributes = {};
-
- /**
- * @param {_MDState} state
- */
- toHTML(state) {
- throw new Error(self.constructor.name + ".toHTML not implemented");
- }
-
- htmlAttributes() {
- var html = '';
- if (this.cssClasses.length > 0) {
- html += ` class="${this.cssClasses.join(' ')}"`;
- }
- if (this.cssId !== null) {
- html += ` id="${this.cssId}"`;
- }
- for (const name in this.attributes) {
- let value = this.attributes[name];
- html += ` ${name}="${_MDUtils.escapeHTML(value)}"`;
- }
- return html;
- }
-
- /**
- * @param {_MDBlock[]} blocks
- * @param {_MDState} state
- * @returns {String}
- */
- static toHTML(blocks, state) {
- return blocks.map((block) => block.toHTML(state)).join("\n");
- }
- }
-
- class _MDMultiBlock extends _MDBlock {
- /** @var {_MDBlock[]} */
- #blocks;
- /**
- * @param {_MDBlock[]} blocks
- */
- constructor(blocks) {
- super();
- this.#blocks = blocks;
- }
- toHTML(state) {
- return _MDBlock.toHTML(this.#blocks, state);
- }
- }
-
- class _MDParagraphBlock extends _MDBlock {
- /** @var {_MDBlock} */
- content;
-
- /**
- * @param {_MDBlock} content
- */
- constructor(content) {
- super();
- this.content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.content.toHTML(state);
- return `<p${this.htmlAttributes()}>${contentHTML}</p>\n`;
- }
- }
-
- class _MDHeaderBlock extends _MDBlock {
- /** @var {number} */
- level;
- /** @var {_MDBlock} */
- content;
-
- /**
- * @param {number} level
- * @param {_MDBlock} content
- */
- constructor(level, content) {
- super();
- this.level = level;
- this.content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.content.toHTML(state);
- return `<h${this.level}${this.htmlAttributes()}>${contentHTML}</h${this.level}>\n`;
- }
- }
-
- class _MDBlockquoteBlock extends _MDBlock {
- /** @var {_MDBlock[]} */
- content;
-
- /**
- * @param {_MDBlock[]} content
- */
- constructor(content) {
- super();
- this.content = content;
- }
-
- toHTML(state) {
- let contentHTML = _MDBlock.toHTML(this.content, state);
- return `<blockquote${this.htmlAttributes()}>\n${contentHTML}\n</blockquote>`;
- }
- }
-
- class _MDUnorderedListBlock extends _MDBlock {
- /** @var {_MDListItemBlock[]} */
- items;
-
- /**
- * @param {_MDListItemBlock[]} items
- */
- constructor(items) {
- super();
- this.items = items;
- }
-
- toHTML(state) {
- let contentHTML = _MDBlock.toHTML(this.items, state);
- return `<ul${this.htmlAttributes()}>\n${contentHTML}\n</ul>`;
- }
- }
-
- class _MDOrderedListBlock extends _MDBlock {
- /** @var {_MDListItemBlock[]} */
- items;
-
- /** @var {Number|null} */
- startOrdinal;
-
- /**
- * @param {_MDListItemBlock[]} items
- */
- constructor(items, startOrdinal=null) {
- super();
- this.items = items;
- this.startOrdinal = startOrdinal;
- }
-
- htmlAttributes() {
- var html = super.htmlAttributes();
- if (this.startOrdinal !== null) {
- html += ` start="${this.startOrdinal}"`;
- }
- return html;
- }
-
- toHTML(state) {
- let contentHTML = _MDBlock.toHTML(this.items, state);
- return `<ol${this.htmlAttributes()}>\n${contentHTML}\n</ol>`;
- }
- }
-
- class _MDListItemBlock extends _MDBlock {
- /** @var {_MDBlock} */
- content;
- /** @var {Number|null} */
- ordinal;
-
- /**
- * @param {_MDBlock} content
- */
- constructor(content, ordinal=null) {
- super();
- this.content = content;
- this.ordinal = ordinal;
- }
-
- toHTML(state) {
- let contentHTML = this.content.toHTML(state);
- return `<li${this.htmlAttributes()}>${contentHTML}</li>`;
- }
- }
-
- class _MDCodeBlock extends _MDBlock {
- /** @var {String} */
- #code;
-
- /**
- * @param {String} code
- */
- constructor(code) {
- super();
- this.#code = code;
- }
-
- toHTML(state) {
- return `<pre${this.htmlAttributes()}><code>${this.#code}</code></pre>`;
- }
- }
-
- class _MDHorizontalRuleBlock extends _MDBlock {
- toHTML(state) {
- return `<hr${this.htmlAttributes()}>\n`;
- }
- }
-
- class _MDTableCellBlock extends _MDBlock {
- /** @var {_MDBlock} */
- #content;
- /** @var {_MDHAlign|null} */
- align = null;
-
- /**
- * @param {_MDBlock} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- htmlAttributes() {
- var html = super.htmlAttributes();
- html += _MDHAlign.toHTMLAttribute(this.align);
- return html;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<td${this.htmlAttributes()}>${contentHTML}</td>`;
- }
- }
-
- class _MDTableHeaderCellBlock extends _MDTableCellBlock {
- toHTML(state) {
- let html = super.toHTML(state);
- let groups = /^<td(.*)td>$/.exec(html);
- return `<th${groups[1]}th>`;
- }
- }
-
- class _MDTableRowBlock extends _MDBlock {
- /** @var {_MDTableCellBlock[]|_MDTableHeaderCellBlock[]} */
- #cells;
-
- /**
- * @param {_MDTableCellBlock[]|_MDTableHeaderCellBlock[]} cells
- */
- constructor(cells) {
- super();
- this.#cells = cells;
- }
-
- /**
- * @param {_MDHAlign[]} alignments
- */
- applyAlignments(alignments) {
- for (var i = 0; i < this.#cells.length; i++) {
- let cell = this.#cells[i];
- let align = i < alignments.length ? alignments[i] : null;
- cell.align = align;
- }
- }
-
- toHTML(state) {
- let cellsHTML = _MDBlock.toHTML(this.#cells, state);
- return `<tr${this.htmlAttributes()}>\n${cellsHTML}\n</tr>`;
- }
- }
-
- class _MDTableBlock extends _MDBlock {
- /** @var {_MDTableRowBlock} */
- #headerRow;
- /** @var {_MDTableRowBlock[]} */
- #bodyRows;
-
- /**
- * @param {_MDTableRowBlock} headerRow
- * @param {_MDTableRowBlock[]} bodyRows
- */
- constructor(headerRow, bodyRows) {
- super();
- this.#headerRow = headerRow;
- this.#bodyRows = bodyRows;
- }
-
- toHTML(state) {
- let headerRowHTML = this.#headerRow.toHTML(state);
- let bodyRowsHTML = _MDBlock.toHTML(this.#bodyRows, state);
- return `<table${this.htmlAttributes()}>\n<thead>\n${headerRowHTML}\n</thead>\n<tbody>\n${bodyRowsHTML}\n</tbody>\n</table>`;
- }
- }
-
- class _MDDefinitionListBlock extends _MDBlock {
- /** @var {_MDBlock[]} */
- #content;
-
- /**
- * @param {_MDBlock[]} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = _MDBlock.toHTML(this.#content, state);
- return `<dl${this.htmlAttributes()}>\n${contentHTML}\n</dl>`;
- }
- }
-
- class _MDDefinitionTermBlock extends _MDBlock {
- /** @var {_MDBlock} */
- #content;
-
- /**
- * @param {_MDBlock} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<dt${this.htmlAttributes()}>${contentHTML}</dt>`;
- }
- }
-
- class _MDDefinitionDefinitionBlock extends _MDBlock {
- /** @var {_MDBlock} */
- #content;
-
- /**
- * @param {_MDBlock} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- let contentHTML = this.#content.toHTML(state);
- return `<dd${this.htmlAttributes()}>${contentHTML}</dd>`;
- }
- }
-
- class _MDInlineBlock extends _MDBlock {
- /** @var {_MDSpan[]} */
- #content;
-
- /**
- * @param {_MDSpan[]} content
- */
- constructor(content) {
- super();
- this.#content = content;
- }
-
- toHTML(state) {
- return _MDSpan.toHTML(this.#content, state);
- }
- }
-
- class _MDHTMLTag {
- /** @var {String} */
- fullTag;
- /** @var {String} */
- tagName;
- /** @var {Boolean} */
- isCloser;
- /** @var {Object} */
- attributes;
-
- /**
- * @param {String} fullTag
- * @param {String} tagName
- * @param {Boolean} isCloser
- * @param {Object} attributes
- */
- constructor(fullTag, tagName, isCloser, attributes) {
- this.fullTag = fullTag;
- this.tagName = tagName;
- this.isCloser = isCloser;
- this.attributes = attributes;
- }
- }
-
- class _MDState {
- /** @var {String[]} */
- lines = [];
-
- /** @var {Object} */
- #abbreviations = {};
-
- /** @var {Object} */
- #abbreviationRegexes = {};
-
- /** @var {Object} */
- #footnotes = {};
-
- /** @var {Object} */
- #urlDefinitions = {};
-
- /** @var {Object} */
- #urlTitles = {};
-
- /** @var {number} */
- p = 0;
-
- /** @var {_MDState|null} */
- #parent = null;
-
- /** @var {Object} */
- get abbreviations() {
- return (this.#parent) ? this.#parent.abbreviations : this.#abbreviations;
- }
-
- /** @var {Object} */
- get abbreviationRegexes() {
- return (this.#parent) ? this.#parent.abbreviationRegexes : this.#abbreviationRegexes;
- }
-
- /** @var {Object} */
- get footnotes() {
- return (this.#parent) ? this.#parent.footnotes : this.#footnotes;
- }
-
- /** @var {Object} */
- get urls() {
- return (this.#parent) ? this.#parent.urls : this.#urlDefinitions;
- }
-
- /** @var {Object} */
- get urlTitles() {
- return (this.#parent) ? this.#parent.urlTitles : this.#urlTitles;
- }
-
- /**
- * @param {String[]} lines
- */
- copy(lines) {
- let cp = new _MDState();
- cp.#parent = this;
- cp.lines = lines;
- cp.p = 0;
- return cp;
- }
-
- /**
- * @param {String} abbreviation
- * @param {String} definition
- */
- defineAbbreviation(abbreviation, definition) {
- if (this.#parent) {
- this.#parent.defineAbbreviation(abbreviation, definition);
- return;
- }
- this.#abbreviations[abbreviation] = definition;
- let regex = new RegExp("\\b(" + abbreviation + ")\\b", "ig");
- this.#abbreviationRegexes[abbreviation] = regex;
- }
-
- /**
- * @param {String} symbol
- * @param {_MDBlock} footnote
- */
- defineFootnote(symbol, footnote) {
- if (this.#parent) {
- this.#parent.defineFootnote(symbol, footnote);
- } else {
- this.#footnotes[symbol] = footnote;
- }
- }
-
- defineURL(symbol, url, title=null) {
- if (this.#parent) {
- this.#parent.defineURL(symbol, url, title);
- } else {
- this.#urlDefinitions[symbol.toLowerCase()] = url;
- if (title !== null) {
- this.#urlTitles[symbol.toLowerCase()] = title;
- }
- }
- }
-
- hasLines(minCount, p=-1) {
- let relativeTo = (p < 0) ? this.p : p;
- return relativeTo + minCount <= this.lines.length;
- }
- }
-
- class _MDTagModifier {
- /** @var {String} */
- original;
- /** @var {String[]} */
- cssClasses = [];
- /** @var {String|null} */
- cssId = null;
- /** @var {Object} */
- attributes = {};
-
- static #baseClassRegex = /\.([a-z_\-][a-z0-9_\-]*?)/i;
- static #baseIdRegex = /#([a-z_\-][a-z0-9_\-]*?)/i;
- static #baseAttributeRegex = /([a-z0-9]+?)=([^\s\}]+?)/i;
- static #baseRegex = /\{([^}]+?)}/i;
- static #leadingClassRegex = new RegExp('^' + this.#baseRegex.source, 'i');
- static #trailingClassRegex = new RegExp('^(.*?)\\s*' + this.#baseRegex.source + '\\s*$', 'i');
- static #classRegex = new RegExp('^' + this.#baseClassRegex.source + '$', 'i'); // 1=classname
- static #idRegex = new RegExp('^' + this.#baseIdRegex.source + '$', 'i'); // 1=id
- static #attributeRegex = new RegExp('^' + this.#baseAttributeRegex.source + '$', 'i'); // 1=attribute name, 2=attribute value
-
- /**
- * @param {_MDBlock|_MDSpan} elem
- */
- applyTo(elem) {
- if (elem instanceof _MDBlock || elem instanceof _MDSpan) {
- elem.cssClasses = elem.cssClasses.concat(this.cssClasses);
- if (this.cssId) elem.cssId = this.cssId;
- for (const name in this.attributes) {
- elem.attributes[name] = this.attributes[name];
- }
- }
- }
-
- static #fromContents(contents) {
- let modifierTokens = contents.split(/\s+/);
- let mod = new _MDTagModifier();
- mod.original = `{${contents}}`;
- var groups;
- for (const token of modifierTokens) {
- if (token.trim() == '') continue;
- if (groups = this.#classRegex.exec(token)) {
- mod.cssClasses.push(groups[1]);
- } else if (groups = this.#idRegex.exec(token)) {
- mod.cssId = groups[1];
- } else if (groups = this.#attributeRegex.exec(token)) {
- mod.attributes[groups[1]] = groups[2];
- } else {
- return null;
- }
- }
- return mod;
- }
-
- /**
- * Extracts modifier from line.
- * @param {String} line
- * @returns {Array} Tuple with remaining line and _MDTagModifier.
- */
- static fromLine(line) {
- let groups = this.#trailingClassRegex.exec(line);
- if (groups === null) return [ line, null ];
- let bareLine = groups[1];
- let mod = this.#fromContents(groups[2]);
- return [ bareLine, mod ];
- }
-
- /**
- * Extracts modifier from head of string.
- * @param {String} line
- * @returns {_MDTagModifier}
- */
- static fromStart(line) {
- let groups = this.#leadingClassRegex.exec(line);
- if (groups === null) return null;
- return this.#fromContents(groups[1]);
- }
-
- /**
- * @param {String} line
- * @returns {String}
- */
- static strip(line) {
- let groups = this.#trailingClassRegex.exec(line);
- if (groups === null) return line;
- return groups[1];
- }
- }
-
- class Markdown {
- /**
- * @param {String} line
- */
- static #stripIndent(line, count=1) {
- let regex = new RegExp(`^(?: {1,4}|\\t){${count}}`);
- return line.replace(regex, '');
- }
-
- /**
- * @param {String} line
- * @param {Boolean} fullIndentsOnly
- * @returns {Number} indent count
- */
- static #countIndents(line, fullIndentsOnly=false) {
- var count = 0;
- var lastLine = line;
- while (line.length > 0) {
- line = (fullIndentsOnly)
- ? line.replace(/^(?: {4}|\t)/, '')
- : line.replace(/^(?: {1,4}|\t)/, '');
- if (line != lastLine) {
- count++;
- } else {
- break;
- }
- lastLine = line;
- }
- return count;
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock[]}
- */
- static #readBlocks(state) {
- var blocks = [];
- while (state.hasLines(1)) {
- let block = this.#readNextBlock(state);
- if (block) {
- blocks.push(block);
- } else {
- break;
- }
- }
- return blocks;
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock}
- */
- static #readNextBlock(state) {
- while (state.hasLines(1) && state.lines[state.p].trim().length == 0) {
- state.p++;
- }
- var block;
- block = this.#readUnderlineHeader(state); if (block) return block;
- block = this.#readHashHeader(state); if (block) return block;
- block = this.#readBlockQuote(state); if (block) return block;
- block = this.#readUnorderedList(state); if (block) return block;
- block = this.#readOrderedList(state); if (block) return block;
- block = this.#readFencedCodeBlock(state); if (block) return block;
- block = this.#readIndentedCodeBlock(state); if (block) return block;
- block = this.#readHorizontalRule(state); if (block) return block;
- block = this.#readTable(state); if (block) return block;
- block = this.#readFootnoteDef(state); if (block) return block;
- block = this.#readAbbreviationDef(state); if (block) return block;
- block = this.#readURLDef(state); if (block) return block;
- block = this.#readDefinitionList(state); if (block) return block;
- block = this.#readParagraph(state); if (block) return block;
- return null;
- }
-
- static #htmlTagNameFirstRegex = /[a-z]/i;
- static #htmlTagNameMedialRegex = /[a-z0-9]/i;
- static #htmlAttributeNameFirstRegex = /[a-z]/i;
- static #htmlAttributeNameMedialRegex = /[a-z0-9-]/i;
- static #whitespaceCharRegex = /\s/;
-
- /**
- * @param {String} line
- * @returns {_MDHTMLTag|null} HTML tag if possible
- */
- static #htmlTag(line) {
- let expectOpenBracket = 0;
- let expectCloserOrName = 1;
- let expectName = 2;
- let expectAttributeNameOrEnd = 3;
- let expectEqualsOrAttributeOrEnd = 4;
- let expectAttributeValue = 5;
- let expectCloseBracket = 6;
-
- var isCloser = false;
- var tagName = '';
- var attributeName = '';
- var attributeValue = '';
- var attributeQuote = null;
- var attributes = {};
- var fullTag = null;
- let endAttribute = function() {
- if (attributeName.length > 0) {
- if (attributeValue.length > 0 || attributeQuote) {
- attributes[attributeName] = attributeValue;
- } else {
- attributes[attributeName] = true;
- }
- }
- attributeName = '';
- attributeValue = '';
- attributeQuote = null;
- };
-
- var expect = expectOpenBracket;
- for (var p = 0; p < line.length && fullTag === null; p++) {
- let ch = line.substring(p, p + 1);
- let isWhitespace = this.#whitespaceCharRegex.exec(ch) !== null;
- switch (expect) {
- case expectOpenBracket:
- if (ch != '<') return null;
- expect = expectCloserOrName;
- break;
- case expectCloserOrName:
- if (ch == '/') {
- isCloser = true;
- } else {
- p--;
- }
- expect = expectName;
- break;
- case expectName:
- if (tagName.length == 0) {
- if (this.#htmlTagNameFirstRegex.exec(ch) === null) return null;
- tagName += ch;
- } else {
- if (this.#htmlTagNameMedialRegex.exec(ch)) {
- tagName += ch;
- } else {
- p--;
- expect = (isCloser) ? expectCloseBracket : expectAttributeNameOrEnd;
- }
- }
- break;
- case expectAttributeNameOrEnd:
- if (attributeName.length == 0) {
- if (isWhitespace) {
- // skip whitespace
- } else if (ch == '/') {
- expect = expectCloseBracket;
- } else if (ch == '>') {
- fullTag = line.substring(0, p + 1);
- break;
- } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
- attributeName += ch;
- } else {
- return null;
- }
- } else if (isWhitespace) {
- expect = expectEqualsOrAttributeOrEnd;
- } else if (ch == '/') {
- endAttribute();
- expect = expectCloseBracket;
- } else if (ch == '>') {
- endAttribute();
- fullTag = line.substring(0, p + 1);
- break;
- } else if (ch == '=') {
- expect = expectAttributeValue;
- } else if (this.#htmlAttributeNameMedialRegex.exec(ch)) {
- attributeName += ch;
- } else {
- return null;
- }
- break;
- case expectEqualsOrAttributeOrEnd:
- if (ch == '=') {
- expect = expectAttributeValue;
- } else if (isWhitespace) {
- // skip whitespace
- } else if (ch == '/') {
- expect = expectCloseBracket;
- } else if (ch == '>') {
- fullTag = line.substring(0, p + 1);
- break;
- } else if (this.#htmlAttributeNameFirstRegex.exec(ch)) {
- endAttribute();
- expect = expectAttributeNameOrEnd;
- p--;
- }
- break;
- case expectAttributeValue:
- if (attributeValue.length == 0) {
- if (attributeQuote === null) {
- if (isWhitespace) {
- // skip whitespace
- } else if (ch == '"' || ch == "'") {
- attributeQuote = ch;
- } else {
- attributeQuote = ''; // explicitly unquoted
- p--;
- }
- } else {
- if (ch === attributeQuote) {
- // Empty string
- endAttribute();
- expect = expectAttributeNameOrEnd;
- } else if (attributeQuote === '' && (ch == '/' || ch == '>')) {
- return null;
- } else {
- attributeValue += ch;
- }
- }
- } else {
- if (ch === attributeQuote) {
- endAttribute();
- expect = expectAttributeNameOrEnd;
- } else if (attributeQuote === '' && isWhitespace) {
- endAttribute();
- expect = expectAttributeNameOrEnd;
- } else {
- attributeValue += ch;
- }
- }
- break;
- case expectCloseBracket:
- if (isWhitespace) {
- // ignore whitespace
- } else if (ch == '>') {
- fullTag = line.substring(0, p + 1);
- break;
- }
- break;
- }
- }
- if (fullTag === null) return null;
- endAttribute();
- return new _MDHTMLTag(fullTag, tagName, isCloser, attributes);
- }
-
- static #textWhitespaceRegex = /^(\s*)(?:(\S|\S.*\S)(\s*?))?$/; // 1=leading WS, 2=text, 3=trailing WS
-
- // 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;
-
- static #footnoteWithTitleRegex = /^\[\^(\d+?)\s+"(.*?)"\]/; // 1=symbol, 2=title
- static #footnoteRegex = /^\[\^(\d+?)\]/; // 1=symbol
- static #urlWithTitleRegex = /^\((\S+?)\s+"(.*?)"\)/i; // 1=URL, 2=title
- static #urlRegex = /^\((\S+?)\)/i; // 1=URL
- static #emailWithTitleRegex = new RegExp("^\\(\\s*(" + this.#baseEmailRegex.source + ")\\s+\"(.*?)\"\\s*\\)", "i"); // 1=email, 2=title
- static #emailRegex = new RegExp("^\\(\\s*(" + this.#baseEmailRegex.source + ")\\s*\\)", "i"); // 1=email
- static #simpleURLRegex = new RegExp("^<(" + this.#baseURLRegex.source + ")>", "i"); // 1=URL
- static #simpleEmailRegex = new RegExp("^<(" + this.#baseEmailRegex.source + ")>", "i"); // 1=email
-
- /**
- * Finds a `[label]` at the start of a string. Regex was too inefficient for this.
- * @param {String} line
- * @returns {String[]}
- */
- static #matchLabel(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;
- }
-
- /**
- * @param {String} line
- * @returns {_MDToken[]} tokens
- */
- static #tokenize(line) {
- var tokens = [];
- var text = '';
- var expectLiteral = false;
- var groups = null;
- var tag = null;
- var modifier = null;
- const endText = function() {
- if (text.length == 0) return;
- let textGroups = Markdown.#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++) {
- let ch = line.substring(p, p + 1);
- let remainder = line.substring(p);
- if (expectLiteral) {
- text += ch;
- expectLiteral = false;
- continue;
- }
- if (ch == '\\') {
- expectLiteral = true;
- } else if (ch == '*') {
- endText();
- tokens.push(new _MDToken(ch, _MDTokenType.Asterisk));
- } else if (ch == '_') {
- endText();
- tokens.push(new _MDToken(ch, _MDTokenType.Underscore));
- } else if (ch == '`') {
- endText();
- tokens.push(new _MDToken(ch, _MDTokenType.Backtick));
- } else if (ch == '~') {
- endText();
- tokens.push(new _MDToken(ch, _MDTokenType.Tilde));
- } else if (ch == '!') {
- endText();
- tokens.push(new _MDToken(ch, _MDTokenType.Bang));
-
- } else if (groups = this.#footnoteWithTitleRegex.exec(remainder)) {
- // Footnote with title [^1 "Foo"]
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.Footnote, groups[1], groups[2]));
- p += groups[0].length - 1;
- } else if (groups = this.#footnoteRegex.exec(remainder)) {
- // Footnote without title [^1]
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.Footnote, groups[1]));
- p += groups[0].length - 1;
- } else if (groups = this.#matchLabel(remainder)) {
- // Label/ref for link/image [Foo]
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.Label, groups[1]));
- p += groups[0].length - 1;
- } else if (groups = this.#emailWithTitleRegex.exec(remainder)) {
- // Email address with title (user@example.com "Foo")
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.Email, groups[1], groups[2]));
- p += groups[0].length - 1;
- } else if (groups = this.#urlWithTitleRegex.exec(remainder)) {
- // URL with title (https://foo "Bar")
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.URL, groups[1], groups[2]));
- p += groups[0].length - 1;
- } else if (groups = this.#emailRegex.exec(remainder)) {
- // Email (user@example.com)
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.Email, groups[1]));
- p += groups[0].length - 1;
- } else if (groups = this.#urlRegex.exec(remainder)) {
- // URL (https://example.com)
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.URL, groups[1]));
- p += groups[0].length - 1;
- } else if (groups = this.#simpleEmailRegex.exec(remainder)) {
- // Simple email <user@example.com>
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.SimpleEmail, groups[1]));
- p += groups[0].length - 1;
- } else if (groups = this.#simpleURLRegex.exec(remainder)) {
- // Simple URL <https://example.com>
- endText();
- tokens.push(new _MDToken(groups[0], _MDTokenType.SimpleLink, groups[1]));
- p += groups[0].length - 1;
- } else if (tag = this.#htmlTag(remainder)) {
- endText();
- tokens.push(new _MDToken(tag.fullTag, _MDTokenType.HTMLTag, tag.fullTag, null, tag));
- p += tag.fullTag.length - 1;
- } else if (modifier = _MDTagModifier.fromStart(remainder)) {
- endText();
- tokens.push(new _MDToken(modifier.original, _MDTokenType.Modifier, modifier));
- p += modifier.original.length - 1;
- } else {
- text += ch;
- }
- }
- endText();
- return tokens;
- }
-
- static #firstTokenIndex(tokens, pattern, startIndex=0) {
- for (var t = startIndex; t < tokens.length; t++) {
- var matchedAll = true;
- for (var p = 0; p < pattern.length; p++) {
- var t0 = t + p;
- if (t0 >= tokens.length) return null;
- let token = tokens[t0];
- let elem = pattern[p];
- if (elem == _MDTokenType.META_AnyNonWhitespace) {
- if (token instanceof _MDToken && token.type == _MDTokenType.Whitespace) {
- matchedAll = false;
- break;
- }
- } else {
- if (!(token instanceof _MDToken) || token.type != elem) {
- matchedAll = false;
- break;
- }
- }
- }
- if (matchedAll) {
- return t;
- }
- }
- return null;
- }
-
- /**
- * @param {_MDState} state
- * @param {String} line
- * @returns {_MDBlock|null}
- */
- static #readInline(state, line) {
- var tokens = this.#tokenize(line);
- return new _MDInlineBlock(this.#tokensToSpans(tokens, state));
- }
-
- /**
- * @param {Array} tokens
- * @returns {_MDSpan[]} spans
- */
- static #tokensToSpans(tokens, state) {
- var spans = tokens.slice(0, tokens.length);
- var anyChanges = false;
- var index, index0;
- // First pass - contiguous constructs
- do {
- anyChanges = false;
- // <https://example.com>
- if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.SimpleLink,
- ])) !== null) {
- let url = spans[index].content;
- spans.splice(index, 1, new _MDLinkSpan(url, new _MDTextSpan(url)));
- anyChanges = true;
- }
-
- // <user@example.com>
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.SimpleEmail,
- ])) !== null) {
- let email = spans[index].content;
- spans.splice(index, 1, new _MDLinkSpan(`mailto:${email}`, new _MDObfuscatedTextSpan(email)));
- anyChanges = true;
- }
-
- // 
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.Bang,
- _MDTokenType.Label,
- _MDTokenType.URL,
- ])) !== null) {
- let alt = spans[index + 1].content;
- let url = spans[index + 2].content;
- let title = spans[index + 2].extra;
- spans.splice(index, 3, new _MDImageSpan(url, alt, title));
- anyChanges = true;
- }
-
- // ![alt][ref]
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.Bang,
- _MDTokenType.Label,
- _MDTokenType.Label,
- ])) !== null) {
- let alt = spans[index + 1].content;
- let ref = spans[index + 2].content;
- spans.splice(index, 3, new _MDReferencedImageSpan(ref, alt));
- anyChanges = true;
- }
-
- // [text](link.html "title")
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.Label,
- _MDTokenType.URL,
- ])) !== null) {
- let text = spans[index + 0].content;
- let url = spans[index + 1].content;
- let title = spans[index + 1].extra;
- spans.splice(index, 2, new _MDLinkSpan(url, this.#readInline(state, text), title));
- anyChanges = true;
- }
-
- // [text](user@example.com "title")
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.Label,
- _MDTokenType.Email,
- ])) !== null) {
- let text = spans[index + 0].content;
- let email = spans[index + 1].content;
- let title = spans[index + 1].extra;
- spans.splice(index, 2, new _MDLinkSpan(`mailto:${email}`, this.#readInline(state, text), title));
- anyChanges = true;
- }
-
- // [text][ref]
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.Label,
- _MDTokenType.Label,
- ])) !== null) {
- let text = spans[index + 0].content;
- let ref = spans[index + 1].content;
- spans.splice(index, 2, new _MDReferencedLinkSpan(ref, this.#readInline(state, text)));
- anyChanges = true;
- }
-
- // [^1]
- else if ((index = this.#firstTokenIndex(spans, [
- _MDTokenType.Footnote,
- ])) !== null) {
- let symbol = spans[index].content;
- spans.splice(index, 1, new _MDFootnoteReferenceSpan(symbol));
- anyChanges = true;
- }
- } while (anyChanges);
-
- /**
- * @param {_MDTokenType[]} delimiter
- * @param {Set<_MDTokenType>} disallowedInnerTokens
- */
- const matchPair = function(delimiter, disallowedInnerTokens=new Set()) {
- var searchStart = 0;
- var hasNewStart = false;
- do {
- hasNewStart = false;
- let startIndex = Markdown.#firstTokenIndex(spans, delimiter.concat(_MDTokenType.META_AnyNonWhitespace), searchStart);
- if (startIndex === null) return null;
- let endIndex = Markdown.#firstTokenIndex(spans, [_MDTokenType.META_AnyNonWhitespace].concat(delimiter), startIndex + delimiter.length);
- if (endIndex === null) return null;
- let contentTokens = spans.slice(startIndex + delimiter.length, endIndex + 1);
- if (disallowedInnerTokens.size > 0) {
- for (const token of contentTokens) {
- if (token instanceof _MDToken && disallowedInnerTokens.has(token.type)) {
- searchStart = startIndex + 1;
- hasNewStart = true;
- break;
- }
- }
- if (hasNewStart) continue;
- }
- let contentSpans = Markdown.#tokensToSpans(contentTokens, state);
- return {
- startIndex: startIndex,
- toDelete: endIndex - startIndex + delimiter.length + 1,
- content: new _MDMultiSpan(contentSpans),
- rawContent: contentTokens.map((token) => token.original).join(''),
- };
- } while (hasNewStart);
- return null;
- };
- var spanMatch = null;
-
- // Second pass - paired constructs. Prioritize pairs with no other paired tokens inside.
- const delimiterTokens = new Set([
- _MDTokenType.Tilde,
- _MDTokenType.Asterisk,
- _MDTokenType.Underscore
- ]);
- for (let disallowed of [ delimiterTokens, new Set() ]) {
- do {
- anyChanges = false;
- // ``code``
- if (spanMatch = matchPair([ _MDTokenType.Backtick, _MDTokenType.Backtick ], disallowed)) {
- spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDCodeSpan(spanMatch.rawContent));
- anyChanges = true;
- }
-
- // ~~strike~~
- else if (spanMatch = matchPair([ _MDTokenType.Tilde, _MDTokenType.Tilde ], disallowed)) {
- spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDStrikethroughSpan(spanMatch.content));
- anyChanges = true;
- }
-
- // **strong** __strong__
- else if (spanMatch = (matchPair([ _MDTokenType.Asterisk, _MDTokenType.Asterisk ], disallowed) ||
- matchPair([ _MDTokenType.Underscore, _MDTokenType.Underscore ], disallowed))) {
- spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDStrongSpan(spanMatch.content));
- anyChanges = true;
- }
-
- // `code`
- else if (spanMatch = matchPair([ _MDTokenType.Backtick ], disallowed)) {
- spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDCodeSpan(spanMatch.rawContent));
- anyChanges = true;
- }
-
- // ~strike~
- else if (spanMatch = matchPair([ _MDTokenType.Tilde ], disallowed)) {
- spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDStrikethroughSpan(spanMatch.content));
- anyChanges = true;
- }
-
- // *strong* _strong_
- else if (spanMatch = (matchPair([ _MDTokenType.Asterisk ], disallowed) ||
- matchPair([ _MDTokenType.Underscore ], disallowed))) {
- spans.splice(spanMatch.startIndex, spanMatch.toDelete, new _MDEmphasisSpan(spanMatch.content));
- anyChanges = true;
- }
- } while (anyChanges);
- }
- var lastSpan = null;
- spans = spans.map(function(span) {
- if (span instanceof _MDToken) {
- if (span.type == _MDTokenType.Modifier && lastSpan) {
- span.modifier.applyTo(lastSpan);
- lastSpan = null;
- return new _MDTextSpan('');
- }
- lastSpan = null;
- return new _MDTextSpan(span.original);
- } else if (span instanceof _MDSpan) {
- lastSpan = (span instanceof _MDTextSpan) ? null : span;
- return span;
- } else {
- throw new Error(`Unexpected span type ${span.constructor.name}`);
- }
- });
- return spans;
- }
-
- /**
- * Reads the contents of something like a list item
- * @param {_MDState} state
- * @param {number} firstLineStartPos
- * @param {RegExp} stopRegex
- * @param {Boolean} inList
- * @returns {_MDBlock}
- */
- static #readInteriorContent(state, firstLineStartPos, stopRegex, inList=false) {
- var p = state.p;
- var seenBlankLine = false;
- var needsBlocks = false;
- var lines = [];
- var hasNestedList = false;
- var firstNestedListLine = -1;
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (p == state.p + 1) {
- line = line.substring(firstLineStartPos);
- }
- let isBlank = line.trim().length == 0;
- let isIndented = /^\s+/.exec(line) !== null;
- if (isBlank) {
- seenBlankLine = true;
- lines.push(line.trim());
- } else if (stopRegex && stopRegex.exec(line)) {
- p--;
- break;
- } else if (isIndented) {
- if (seenBlankLine) {
- needsBlocks = true;
- }
- if (inList && /^\s*(?:\*|\+|\-|\d+\.)\s+/.exec(line)) {
- hasNestedList = true;
- if (firstNestedListLine < 0) {
- firstNestedListLine = lines.length;
- }
- }
- lines.push(this.#stripIndent(line));
- } else {
- if (seenBlankLine) {
- p--;
- break;
- }
- lines.push(this.#stripIndent(line));
- }
- }
- while (lines.length > 0 && lines[lines.length - 1].trim().length == 0) {
- lines.pop();
- }
- if (inList && hasNestedList) {
- let parentLines = lines.slice(0, firstNestedListLine);
- let parentContent = this.#readInline(state, parentLines.join("\n"));
- let nestedLines = lines.slice(firstNestedListLine);
- let substate = state.copy(nestedLines);
- let nestedContent = this.#readBlocks(substate);
- state.p = p;
- return new _MDMultiBlock([parentContent].concat(nestedContent));
- }
- if (needsBlocks) {
- let substate = state.copy(lines);
- let blocks = this.#readBlocks(substate);
- state.p = p;
- return new _MDMultiBlock(blocks);
- } else {
- state.p = p;
- return this.#readInline(state, lines.join("\n"));
- }
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readUnderlineHeader(state) {
- var p = state.p;
- if (!state.hasLines(2)) return null;
- var modifier;
- let contentLine = state.lines[p++].trim();
- [contentLine, modifier] = _MDTagModifier.fromLine(contentLine);
- let underLine = state.lines[p++].trim();
- if (contentLine == '') return null;
- if (/^=+$/.exec(underLine)) {
- state.p = p;
- let block = new _MDHeaderBlock(1, this.#readInline(state, contentLine));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- if (/^\-+$/.exec(underLine)) {
- state.p = p;
- let block = new _MDHeaderBlock(2, this.#readInline(state, contentLine));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- return null;
- }
-
- static #hashHeaderRegex = /^(#{1,6})\s*([^#].*?)\s*\#*\s*$/; // 1=hashes, 2=content
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readHashHeader(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = _MDTagModifier.fromLine(line);
- var groups = this.#hashHeaderRegex.exec(line);
- if (groups === null) return null;
- state.p = p;
- let block = new _MDHeaderBlock(groups[1].length, this.#readInline(state, groups[2]));
- if (modifier) modifier.applyTo(block);
- return block;
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readBlockQuote(state) {
- var blockquoteLines = [];
- var p = state.p;
- while (p < state.lines.length) {
- let line = state.lines[p++];
- if (line.startsWith(">")) {
- blockquoteLines.push(line);
- } else {
- break;
- }
- }
- if (blockquoteLines.length > 0) {
- let contentLines = blockquoteLines.map(function(line) {
- return line.substring(1).replace(/^ {0,3}\t?/, '');
- });
- let substate = state.copy(contentLines);
- let quotedBlocks = this.#readBlocks(substate);
- state.p = p;
- return new _MDBlockquoteBlock(quotedBlocks);
- }
- return null;
- }
-
- static #unorderedListRegex = /^([\*\+\-]\s+)(.*)$/; // 1=bullet, 2=content
- static #unorderedListItemRegex = /^[\*\+\-]\s+/;
-
- /**
- * @param {_MDState} state
- * @returns {_MDListItemBlock|null}
- */
- static #readUnorderedListItem(state) {
- var p = state.p;
- let line = state.lines[p];
- let groups = this.#unorderedListRegex.exec(line);
- if (groups === null) return null;
- return new _MDListItemBlock(this.#readInteriorContent(state, groups[1].length, this.#unorderedListItemRegex, true));
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readUnorderedList(state) {
- var items = [];
- var item = null;
- do {
- item = this.#readUnorderedListItem(state);
- if (item) items.push(item);
- } while (item);
- if (items.length == 0) return null;
- return new _MDUnorderedListBlock(items);
- }
-
- static #orderedListRegex = /^(\d+)(\.\s+)(.*)$/; // 1=number, 2=dot, 3=content
- static #orderedListItemRegex = /^\d+\.\s+/;
-
- /**
- * @param {_MDState} state
- * @returns {_MDListItemBlock|null}
- */
- static #readOrderedListItem(state) {
- var p = state.p;
- let line = state.lines[p];
- let groups = this.#orderedListRegex.exec(line);
- if (groups === null) return null;
- let ordinal = parseInt(groups[1]);
- let content = this.#readInteriorContent(state, groups[1].length + groups[2].length, this.#orderedListItemRegex, true);
- return new _MDListItemBlock(content, ordinal);
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readOrderedList(state) {
- var items = [];
- var item = null;
- do {
- item = this.#readOrderedListItem(state);
- if (item) items.push(item);
- } while (item);
- if (items.length == 0) return null;
- return new _MDOrderedListBlock(items, items[0].ordinal);
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readFencedCodeBlock(state) {
- if (!state.hasLines(2)) return null;
- var p = state.p;
- let openFenceLine = state.lines[p++];
- var modifier;
- [openFenceLine, modifier] = _MDTagModifier.fromLine(openFenceLine);
- if (openFenceLine.trim() != '```') return null;
- var codeLines = [];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (line.trim() == '```') {
- state.p = p;
- let block = new _MDCodeBlock(codeLines.join("\n"));
- if (modifier) modifier.applyTo(block);
- return block;
- }
- codeLines.push(line);
- }
- return null;
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readIndentedCodeBlock(state) {
- var p = state.p;
- var codeLines = [];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (this.#countIndents(line, true) < 1) {
- p--;
- break;
- }
- codeLines.push(this.#stripIndent(line));
- }
- if (codeLines.length == 0) return null;
- state.p = p;
- return new _MDCodeBlock(codeLines.join("\n"));
- }
-
- static #horizontalRuleRegex = /^\s*(?:\-(?:\s*\-){2,}|\*(?:\s*\*){2,})\s*$/;
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readHorizontalRule(state) {
- var p = state.p;
- let line = state.lines[p++];
- var modifier;
- [line, modifier] = _MDTagModifier.fromLine(line);
- if (this.#horizontalRuleRegex.exec(line)) {
- state.p = p;
- let block = new _MDHorizontalRuleBlock();
- if (modifier) modifier.applyTo(block);
- return block;
- }
- return null;
- }
-
- /**
- * @param {_MDState} state
- * @param {Boolean} isHeader
- * @return {_MDTableRowBlock|null}
- */
- static #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 = Markdown.#readInline(state, token);
- return isHeader ? new _MDTableHeaderCellBlock(content) : new _MDTableCellBlock(content);
- });
- state.p = p;
- return new _MDTableRowBlock(cells);
- }
-
- /**
- * @param {String} line
- * @returns {_MDHAlign[]}
- */
- static #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('|').map(function(token) {
- token = token.trim();
- if (token.startsWith(':')) {
- if (token.endsWith(':')) {
- return _MDHAlign.Center;
- }
- return _MDHAlign.Left;
- } else if (token.endsWith(':')) {
- return _MDHAlign.Right;
- }
- return null;
- });
- }
-
- static #tableDividerRegex = /^\s*[|]?(?:\s*[:]?-+[:]?\s*\|)(?:\s*[:]?-+[:]?\s*)[|]?\s*$/;
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readTable(state) {
- if (!state.hasLines(2)) return null;
- let startP = state.p;
- let firstLine = state.lines[startP];
- var ignore, modifier;
- [ignore, modifier] = _MDTagModifier.fromLine(firstLine);
- let headerRow = this.#readTableRow(state, true);
- if (headerRow === null) {
- state.p = startP;
- return null;
- }
- let dividerLine = state.lines[state.p++];
- let dividerGroups = this.#tableDividerRegex.exec(dividerLine);
- if (dividerGroups === null) {
- state.p = startP;
- return null;
- }
- let columnAlignments = this.#parseColumnAlignments(dividerLine);
- headerRow.applyAlignments(columnAlignments);
- var bodyRows = [];
- while (state.hasLines(1)) {
- let row = this.#readTableRow(state, false);
- if (row === null) break;
- row.applyAlignments(columnAlignments);
- bodyRows.push(row);
- }
- let table = new _MDTableBlock(headerRow, bodyRows);
- if (modifier) modifier.applyTo(table);
- return table;
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readDefinitionList(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) {
- p--;
- 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+(.*)$/.exec(line)) {
- return new _MDDefinitionDefinitionBlock(Markdown.#readInline(state, groups[1]));
- } else {
- return new _MDDefinitionTermBlock(Markdown.#readInline(state, line));
- }
- });
- state.p = p;
- return new _MDDefinitionListBlock(blocks);
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readFootnoteDef(state) {
- var p = state.p;
- let groups = /^\s*\[\^\s*([^\]]+)\s*\]:\s+(.*)\s*$/.exec(state.lines[p++]);
- if (groups === null) return null;
- let symbol = groups[1];
- let def = groups[2];
- while (state.hasLines(1, p)) {
- let line = state.lines[p++];
- if (/^\s+/.exec(line)) {
- def += "\n" + line;
- } else {
- p--;
- break;
- }
- }
- state.p = p;
- let content = this.#readInline(state, def);
- state.defineFootnote(symbol, content);
- state.p = p;
- return new _MDMultiBlock([]);
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readAbbreviationDef(state) {
- var p = state.p;
- let line = state.lines[p++];
- let groups = /^\s*\*\[([^\]]+?)\]:\s+(.*?)\s*$/.exec(line);
- if (groups === null) return null;
- let abbrev = groups[1];
- let def = groups[2];
- state.defineAbbreviation(abbrev, def);
- state.p = p;
- return new _MDMultiBlock([]);
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readURLDef(state) {
- var p = state.p;
- let line = state.lines[p++];
- var symbol;
- var url;
- var title = null;
- let groups = /^\s*\[(.+?)]:\s*(\S+)\s+"(.*?)"\s*$/.exec(line);
- if (groups) {
- symbol = groups[1];
- url = groups[2];
- title = groups[3];
- } else {
- groups = /^\s*\[(.+?)]:\s*(\S+)\s*$/.exec(line);
- if (groups) {
- symbol = groups[1];
- url = groups[2];
- } else {
- return null;
- }
- }
- state.defineURL(symbol, url, title);
- state.p = p;
- return new _MDInlineBlock([]);
- }
-
- /**
- * @param {_MDState} state
- * @returns {_MDBlock|null}
- */
- static #readParagraph(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 (paragraphLines.length > 0) {
- state.p = p;
- let content = paragraphLines.join("\n");
- return new _MDParagraphBlock(this.#readInline(state, content));
- }
- return null;
- }
-
- /**
- * @param {String} html
- * @param {_MDState} state
- * @returns {String}
- */
- static #postProcessFootnotes(html, state) {
- let footnotes = state.footnotes;
- if (Object.keys(footnotes).length == 0) return html;
- var symbolOrder = [];
- var footnoteOccurrences = {};
- var footnoteIndex = 0;
- html = html.replace(/<!--FNREF:{(\d+)}-->/g, function(match, symbol) {
- footnoteIndex++;
- symbol = symbol.toLowerCase();
- if (!symbolOrder.includes(symbol)) {
- symbolOrder.push(symbol);
- }
- var occurrences = footnoteOccurrences[symbol] || [];
- occurrences.push(footnoteIndex);
- footnoteOccurrences[symbol] = occurrences;
- return `<sup id="footnoteref_${footnoteIndex}"><a href="#footnote_${symbol}">${symbol}</a></sup>`;
- });
- if (footnoteIndex == 0) return html;
- html += '<div class="footnotes"><hr/>';
- html += '<ol>';
- for (const symbol of symbolOrder) {
- let content = state.footnotes[symbol];
- if (!content) continue;
- html += `<li value="${symbol}" id="footnote_${symbol}">${content.toHTML(state)}`;
- for (const ref of footnoteOccurrences[symbol]) {
- html += ` <a href="#footnoteref_${ref}" class="footnote-backref" role="doc-backlink">↩︎</a>`;
- }
- html += `</li>\n`;
- }
- html += '</ol>';
- html += '</div>';
- return html;
- }
-
- /**
- * @param {String} markdown
- * @returns {String} HTML
- */
- static toHTML(markdown) {
- var state = new _MDState();
- let lines = markdown.split(/(?:\n|\r|\r\n)/);
- state.lines = lines;
- let blocks = this.#readBlocks(state);
- let html = _MDBlock.toHTML(blocks, state);
- html = this.#postProcessFootnotes(html, state);
- return html;
- }
- }
|