UNPKG

marked-footnote

Version:
18 lines (16 loc) 2.75 kB
(function(h,p){typeof exports=="object"&&typeof module<"u"?module.exports=p():typeof define=="function"&&define.amd?define(p):(h=typeof globalThis<"u"?globalThis:h||self,h.markedFootnote=p())})(this,function(){"use strict";function h(t,l){const n={type:"footnotes",raw:l,rawItems:[],items:[]};return{name:"footnote",level:"block",childTokens:["content"],tokenizer(r){t.hasFootnotes||(this.lexer.tokens.push(n),t.tokens=this.lexer.tokens,t.hasFootnotes=!0,n.rawItems=[],n.items=[]);const s=/^\[\^([^\]\n]+)\]:(?:[ \t]+|[\n]*?|$)([^\n]*?(?:\n|$)(?:\n*?[ ]{4,}[^\n]*)*)/.exec(r);if(s){const[a,d,i=""]=s;let f=i.split(` `).reduce((c,u)=>c+` `+u.replace(/^(?:[ ]{4}|[\t])/,""),"");const e=f.trimEnd().split(` `).pop();f+=e&&/^[ \t]*?[>\-*][ ]|[`]{3,}$|^[ \t]*?[|].+[|]$/.test(e)?` `:"";const o={type:"footnote",raw:a,label:d,refs:[],content:this.lexer.blockTokens(f)};return n.rawItems.push(o),o}},renderer(){return""}}}function p(t,l=!1){let n=0;return{name:"footnoteRef",level:"inline",tokenizer(r){const s=/^\[\^([^\]\n]+)\]/.exec(r);if(s){const[a,d]=s,i=this.lexer.tokens[0],f=i.rawItems.filter(u=>u.label===d);if(!f.length)return;const e=f[0],o=i.items.filter(u=>u.label===d)[0],c={type:"footnoteRef",raw:a,id:"",label:d};return o?(c.id=o.refs[0].id,o.refs.push(c)):(n++,c.id=String(n),e.refs.push(c),i.items.push(e)),c}},renderer({id:r,label:s}){n=0;const a=encodeURIComponent(s);return`<sup><a id="${t}ref-${a}" href="#${t+a}" data-${t}ref aria-describedby="${t}label">${l?`[${r}]`:r}</a></sup>`}}}function y(t,l,n,r,s,a){return{name:"footnotes",renderer({raw:d,items:i=[]}){if(i.length===0)return"";const f=i.reduce((u,{label:k,content:R,refs:T})=>{const b=encodeURIComponent(k),m=this.parser.parse(R).trimEnd(),F=m.endsWith("</p>");let $=`<li id="${t+b}"> `;return $+=F?m.replace(/<\/p>$/,""):m,T.forEach((x,w)=>{const g=a.replace("{0}",k);$+=` <a href="#${t}ref-${b}" data-${t}backref aria-label="${g}">${w>0?`↩<sup>${w+1}</sup>`:"↩"}</a>`}),$+=F?`</p> `:` `,$+=`</li> `,u+$},"");let e="";n&&(e+=`<hr data-${l}footnotes> `);let o="";r&&(o=` class="${r}"`);let c="";return s&&(c=` class="${s}"`),e+=`<section${o} data-${l}footnotes> `,e+=`<h2 id="${t}label"${c}>${d.trimEnd()}</h2> `,e+=`<ol> ${f}</ol> `,e+=`</section> `,e}}}function L(t={}){const{prefixId:l="footnote-",prefixData:n="",description:r="Footnotes",refMarkers:s=!1,footnoteDivider:a=!1,sectionClass:d="footnotes",headingClass:i="sr-only",backRefLabel:f="Back to reference {0}"}=t,e={hasFootnotes:!1,tokens:[]};return{extensions:[h(e,r),p(l,s),y(l,n,a,d,i,f)],walkTokens(o){o.type==="footnotes"&&e.tokens.indexOf(o)===0&&o.items.length&&(e.tokens[0]={type:"space",raw:""},e.tokens.push(o)),e.hasFootnotes&&(e.hasFootnotes=!1)}}}return L}); //# sourceMappingURL=index.umd.cjs.map