1 line
5.1 KiB
Plaintext
1 line
5.1 KiB
Plaintext
|
{"version":3,"file":"markdownItAnchor.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":"AAAAA,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,EAAM
|