您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

anchor_headings.html 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {% capture headingsWorkspace %}
  2. {% comment %}
  3. Version 1.0.3
  4. https://github.com/allejo/jekyll-anchor-headings
  5. "Be the pull request you wish to see in the world." ~Ben Balter
  6. Usage:
  7. {% include anchor_headings.html html=content %}
  8. Parameters:
  9. * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
  10. Optional Parameters:
  11. * beforeHeading (bool) : false - Set to true if the anchor should be placed _before_ the heading's content
  12. * anchorBody (string) : '' - The content that will be placed inside the anchor; the `%heading%` placeholder is available
  13. * anchorClass (string) : '' - The class(es) that will be used for each anchor. Separate multiple classes with a space
  14. * anchorTitle (string) : '' - The `title` attribute that will be used for anchors
  15. * h_min (int) : 1 - The minimum header level to build an anchor for; any header lower than this value will be ignored
  16. * h_max (int) : 6 - The maximum header level to build an anchor for; any header greater than this value will be ignored
  17. * bodyPrefix (string) : '' - Anything that should be inserted inside of the heading tag _before_ its anchor and content
  18. * bodySuffix (string) : '' - Anything that should be inserted inside of the heading tag _after_ its anchor and content
  19. Output:
  20. The original HTML with the addition of anchors inside of all of the h1-h6 headings.
  21. {% endcomment %}
  22. {% assign minHeader = include.h_min | default: 1 %}
  23. {% assign maxHeader = include.h_max | default: 6 %}
  24. {% assign beforeHeading = include.beforeHeading %}
  25. {% assign nodes = include.html | split: '<h' %}
  26. {% capture edited_headings %}{% endcapture %}
  27. {% for _node in nodes %}
  28. {% capture node %}{{ _node | strip }}{% endcapture %}
  29. {% if node == "" %}
  30. {% continue %}
  31. {% endif %}
  32. {% assign nextChar = node | replace: '"', '' | strip | slice: 0, 1 %}
  33. {% assign headerLevel = nextChar | times: 1 %}
  34. <!-- If the level is cast to 0, it means it's not a h1-h6 tag, so let's try to fix it -->
  35. {% if headerLevel == 0 %}
  36. {% if nextChar != '<' and nextChar != '' %}
  37. {% capture node %}<h{{ node }}{% endcapture %}
  38. {% endif %}
  39. {% capture edited_headings %}{{ edited_headings }}{{ node }}{% endcapture %}
  40. {% continue %}
  41. {% endif %}
  42. {% assign _workspace = node | split: '</h' %}
  43. {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
  44. {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
  45. {% assign html_id = _idWorkspace[0] %}
  46. {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
  47. {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
  48. <!-- Build the anchor to inject for our heading -->
  49. {% capture anchor %}{% endcapture %}
  50. {% if html_id and headerLevel >= minHeader and headerLevel <= maxHeader %}
  51. {% capture anchor %}href="#{{ html_id}}"{% endcapture %}
  52. {% if include.anchorClass %}
  53. {% capture anchor %}{{ anchor }} class="{{ include.anchorClass }}"{% endcapture %}
  54. {% endif %}
  55. {% if include.anchorTitle %}
  56. {% capture anchor %}{{ anchor }} title="{{ include.anchorTitle | replace: '%heading%', header }}"{% endcapture %}
  57. {% endif %}
  58. {% capture anchor %}<a {{ anchor }}>{{ include.anchorBody | replace: '%heading%', header | default: '' }}</a>{% endcapture %}
  59. <!-- In order to prevent adding extra space after a heading, we'll let the 'anchor' value contain it -->
  60. {% if beforeHeading %}
  61. {% capture anchor %}{{ anchor }} {% endcapture %}
  62. {% else %}
  63. {% capture anchor %} {{ anchor }}{% endcapture %}
  64. {% endif %}
  65. {% endif %}
  66. {% capture new_heading %}
  67. <h{{ _hAttrToStrip }}
  68. {{ include.bodyPrefix }}
  69. {% if beforeHeading %}
  70. {{ anchor }}{{ header }}
  71. {% else %}
  72. {{ header }}{{ anchor }}
  73. {% endif %}
  74. {{ include.bodySuffix }}
  75. </h{{ _workspace | last }}
  76. {% endcapture %}
  77. {% capture edited_headings %}{{ edited_headings }}{{ new_heading }}{% endcapture %}
  78. {% endfor %}
  79. {% endcapture %}{% assign headingsWorkspace = '' %}{{ edited_headings | strip }}