xaoxuu / hexo-theme-stellar

内置文档系统的简约商务风Hexo主题,支持大量的标签组件和动态数据组件。
https://xaoxuu.com/wiki/stellar/
MIT License
1.28k stars 317 forks source link

为folding等容器标签添加id访问锚点 #327

Closed wjsw3369 closed 7 months ago

wjsw3369 commented 9 months ago

为folding等容器标签添加可设置的id访问锚点 /scripts/tags/lib/folding.js

 * folding.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
 * 格式与官方标签插件一致使用空格分隔,中括号内的是可选参数(中括号不需要写出来)
 *
 * {% folding [color:yellow] [child:codeblock] [open:false] [id:titleid] title %}
 * body
 * {% endfolding %}
 */

'use strict'

module.exports = ctx => function(args, content) {
  args = ctx.args.map(args, ['color', 'child', 'open', 'id'], ['title'])
  var el = ''
  // header
  el += '<details id="' + (args.id || args.title || '') + '" class="tag-plugin colorful folding"'
  el += ' ' + ctx.args.joinTags(args, ['color', 'child']).join(' ')
  if (args.open && args.open == 'true') {
    el += ' open'
  }
  el += '>'
  // summary
  el += '<summary><span>' + (args.title || '') + '</span></summary>'
  // content
  el += '<div class="body">'
  el += ctx.render.renderSync({text: content, engine: 'markdown'}).split('\n').join('')
  el += '</div></details>'

  return el
}

且自动打开这个容器 /_partial/head.ejs

...
<script>
  window.onload = function() {
    var hash = window.location.hash;
    if (hash) {
      var element = document.querySelector(hash);
      if (element && element.tagName === 'DETAILS') {
        element.setAttribute('open', '');
      }
    }
  };
</script>
wjsw3369 commented 9 months ago

访问后打开容器还不行,有中文等就会失效,

wjsw3369 commented 9 months ago

且只能是打开新页面才有能展开,不知道有没有什么优化的方式

xaoxuu commented 9 months ago

感觉不是很有必要,因为 folding 容器就是放不是很重要但是提供给读者可选阅读的东西,如果需要设置锚点就说明很重要,前后矛盾了。

wjsw3369 commented 9 months ago

哦,好的,就我需要就自己用吧,那这还有需要改善的地方吗

wjsw3369 commented 9 months ago

我整的东西比较多,用折叠容器分内容好快速浏览,有时候需要指定跳转,