chore(copyright): 2024
[curben-blog.git] / themes / chameleon / scripts / heading-link.js
blob7e7182367f325229ce06daaf345cfc0175f86fdf
1 'use strict'
2 /* global hexo */
4 /*
5 * Add Link button next to a heading
6 */
8 const { slugize, stripHTML } = require('hexo-util')
10 const anchorId = (str, transformOption) => {
11   return slugize(str.trim(), { transform: transformOption })
14 hexo.extend.filter.register('marked:renderer', function (renderer) {
15   const { config } = this
16   renderer.heading = function (text, level) {
17     const transformOption = config.marked.modifyAnchors
18     let id = anchorId(stripHTML(text), transformOption)
19     const headingId = this._headingId
21     // Add a number after id if repeated
22     if (headingId[id]) {
23       id += `-${headingId[id]++}`
24     } else {
25       headingId[id] = 1
26     }
28     // add headerlink
29     return `<h${level} id="${id}">${text} <a href="#${id}" class="headerlink" title="${stripHTML(text)}">ยง</a></h${level}>`
30   }