{"version":3,"file":"markdownItAnchor.umd.js","sources":["../index.js"],"sourcesContent":["const slugify = (s) => encodeURIComponent(String(s).trim().toLowerCase().replace(/\\s+/g, '-'))\n\nconst position = {\n false: 'push',\n true: 'unshift'\n}\n\nconst hasProp = Object.prototype.hasOwnProperty\n\nconst permalinkHref = slug => `#${slug}`\nconst permalinkAttrs = slug => ({})\n\nconst renderPermalink = (slug, opts, state, idx) => {\n const space = () => Object.assign(new state.Token('text', '', 0), { content: ' ' })\n\n const linkTokens = [\n Object.assign(new state.Token('link_open', 'a', 1), {\n attrs: [\n ['class', opts.permalinkClass],\n ['href', opts.permalinkHref(slug, state)],\n ...Object.entries(opts.permalinkAttrs(slug, state))\n ]\n }),\n Object.assign(new state.Token('html_block', '', 0), { content: opts.permalinkSymbol }),\n new state.Token('link_close', 'a', -1)\n ]\n\n // `push` or `unshift` according to position option.\n // Space is at the opposite side.\n if (opts.permalinkSpace) {\n linkTokens[position[!opts.permalinkBefore]](space())\n }\n state.tokens[idx + 1].children[position[opts.permalinkBefore]](...linkTokens)\n}\n\nconst uniqueSlug = (slug, slugs) => {\n let uniq = slug\n let i = 2\n while (hasProp.call(slugs, uniq)) uniq = `${slug}-${i++}`\n slugs[uniq] = true\n return uniq\n}\n\nconst isLevelSelectedNumber = selection => level => level >= selection\nconst isLevelSelectedArray = selection => level => selection.includes(level)\n\nconst anchor = (md, opts) => {\n opts = Object.assign({}, anchor.defaults, opts)\n\n md.core.ruler.push('anchor', state => {\n const slugs = {}\n const tokens = state.tokens\n\n const isLevelSelected = Array.isArray(opts.level)\n ? isLevelSelectedArray(opts.level)\n : isLevelSelectedNumber(opts.level)\n\n tokens\n .filter(token => token.type === 'heading_open')\n .filter(token => isLevelSelected(Number(token.tag.substr(1))))\n .forEach(token => {\n // Aggregate the next token children text.\n const title = tokens[tokens.indexOf(token) + 1]\n .children\n .filter(token => token.type === 'text' || token.type === 'code_inline')\n .reduce((acc, t) => acc + t.content, '')\n\n let slug = token.attrGet('id')\n\n if (slug == null) {\n slug = uniqueSlug(opts.slugify(title), slugs)\n token.attrPush(['id', slug])\n }\n\n if (opts.permalink) {\n opts.renderPermalink(slug, opts, state, tokens.indexOf(token))\n }\n\n if (opts.callback) {\n opts.callback(token, { slug, title })\n }\n })\n })\n}\n\nanchor.defaults = {\n level: 1,\n slugify,\n permalink: false,\n renderPermalink,\n permalinkClass: 'header-anchor',\n permalinkSpace: true,\n permalinkSymbol: 'ΒΆ',\n permalinkBefore: false,\n permalinkHref,\n permalinkAttrs\n}\n\nexport default anchor\n"],"names":["const","position","false","true","hasProp","Object","prototype","hasOwnProperty","anchor","md","opts","assign","defaults","core","ruler","push","state","selection","slugs","tokens","isLevelSelected","Array","isArray","level","includes","isLevelSelectedNumber","filter","token","type","Number","tag","substr","forEach","title","indexOf","children","reduce","acc","t","content","slug","attrGet","uniq","i","call","uniqueSlug","slugify","attrPush","permalink","renderPermalink","callback","s","encodeURIComponent","String","trim","toLowerCase","replace","idx","linkTokens","Token","attrs","permalinkClass","permalinkHref","entries","permalinkAttrs","permalinkSymbol","permalinkSpace","permalinkBefore","ref"],"mappings":"+KAAAA,IAEMC,EAAW,CACfC,MAAO,OACPC,KAAM,WAGFC,EAAUC,OAAOC,UAAUC,eAuC3BC,WAAUC,EAAIC,GAClBA,EAAOL,OAAOM,OAAO,GAAIH,EAAOI,SAAUF,GAE1CD,EAAGI,KAAKC,MAAMC,KAAK,kBAAUC,OALFC,EAMnBC,EAAQ,GACRC,EAASH,EAAMG,OAEfC,EAAkBC,MAAMC,QAAQZ,EAAKa,QATlBN,EAUAP,EAAKa,eAVQA,UAASN,EAAUO,SAASD,cADxCN,mBAAaM,UAASA,GAASN,GAYrDQ,CAAsBf,EAAKa,OAE/BJ,EACGO,gBAAOC,SAAwB,iBAAfA,EAAMC,OACtBF,gBAAOC,UAASP,EAAgBS,OAAOF,EAAMG,IAAIC,OAAO,OACxDC,iBAAQL,OAEDM,EAAQd,EAAOA,EAAOe,QAAQP,GAAS,GAC1C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