markdown自动生成简书目录

给简书增加目录功能

目前为 2019-08-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name markdown自动生成简书目录
  3. // @namespace create-jianshuList
  4. // @version 1.0
  5. // @author YIN
  6. // @description 给简书增加目录功能
  7. // @match http://www.jianshu.com/p/*
  8. // @match https://www.jianshu.com/p/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. //去除字符串所有空格
  13. function trim (str, is_global) {
  14. var result;
  15. result = str.replace(/(^\s+)|(\s+$)/g, "");
  16. if (is_global&&is_global.toLowerCase() == "g") {
  17. result = result.replace(/\s/g, "");
  18. }
  19. return result;
  20. }
  21.  
  22. //转义尖括号
  23. function toTxt(str) {
  24. var RexStr = /\<|\>/g
  25. str = str.replace(RexStr, function(MatchStr) {
  26. switch (MatchStr) {
  27. case "<":
  28. return "&lt;";
  29. break;
  30. case ">":
  31. return "&gt;";
  32. break;
  33. default:
  34. break;
  35. }
  36. })
  37. return str;
  38. }
  39. var menuIndex = 0; //初始化标题索引
  40.  
  41. // 在侧边栏中添加目录项
  42. function appendMenuItem(tagName,id,content) {
  43. console.log(tagName+" "+tagName.substring(1));
  44. let paddingLeft = tagName.substring(1) * 20; //添加标题缩进
  45. $('#menu_nav_ol').append('<li class=' + id + ' style="padding-left: '+ paddingLeft +'px;line-height:40px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><a href=javascript:; title='+toTxt(trim(content,"g"))+' style="color:#e1e3e4">' + content + '</a></li>');
  46. }
  47.  
  48. (function() {
  49. // 获取标题元素
  50. let titles = $('body').find('h1,h2,h3,h4,h5,h6');
  51. if(titles.length === 0) {
  52. return;
  53. }
  54. // 将文章内容右移
  55. $('.post').css({padding:'0 150px 0 400px',width:'100%'});
  56. // 在 body 标签内部添加 aside 侧边栏,用于显示文档目录
  57. let asideContent = '<aside id="sideMenu" style="font-size:15px;color:#fff;position: fixed;padding: 70px 15px 20px 0;top: 0;left: 0;margin-bottom:20px;background-color: #282c34;z-index: 810;overflow: auto;height:100%;width:320px;"></aside>';
  58. $('.show-content').prepend(asideContent);
  59. $('#sideMenu').append('<ol id="menu_nav_ol" style="list-style:none;margin:0px;padding:0px;">');// 不显示 li 项前面默认的点标志, 也不使用默认缩进
  60.  
  61. // 遍历文章中的所有标题行, 按需添加id值, 并增加记录到目录列表中
  62. titles.each(function() {
  63. let tagName = $(this)[0].tagName.toLocaleLowerCase();
  64. let content = $(this).text();
  65. // 若标题的id不存在,则使用新id
  66. let newTagId =$(this).attr('id');
  67. if(!$(this).attr('id')) {
  68. newTagId = 'id_'+menuIndex;
  69. $(this).attr('id',newTagId);
  70. menuIndex++;
  71. }
  72. if(newTagId !=='id_0') //忽略标题
  73. appendMenuItem(tagName,newTagId,content);
  74. });
  75.  
  76. $('#sideMenu').append('</ol>');
  77. // 绑定目录li点击事件,点击时跳转到对应的位置
  78. $('#menu_nav_ol li').on('click',function() {
  79. let targetId = $(this).attr('class');
  80. var _top=$("#"+targetId).offset().top-$("#"+targetId).outerHeight(true)-10
  81. $('html,body').animate({
  82. scrollTop:_top
  83. }, 300);
  84.  
  85. });
  86. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址