plugin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (function () {
  2. var myPlugin = function (hook, vm) {
  3. // Invoked one time when the docsify instance has mounted on the DOM
  4. hook.mounted(function () {
  5. console.log(window.Docsify,vm);
  6. });
  7. // hook.beforeEach(function (markdown) {
  8. // const tag =" [-FAQ-]";
  9. // console.log(markdown);
  10. // let tagPosition = markdown.indexOf(tag);
  11. // if(tagPosition===-1) return;
  12. // let toc = '> table of content : '+"\n";
  13. // toc += '* Title 1 '+"\n";
  14. // regex = new RegExp(tag,"i");
  15. // markdown = markdown.replace(regex,toc);
  16. // return markdown;
  17. // });
  18. hook.afterEach(function (html) {
  19. const tag ="[-FAQ-]";
  20. let tagPosition = html.indexOf(tag);
  21. if(tagPosition===-1) return;
  22. let questionBlock = html.substr(tagPosition);
  23. let toc = "<ul style='list-style-type:none;margin:0;padding:0'>";
  24. const dom = document.createElement("div");
  25. dom.innerHTML = questionBlock;
  26. let titles = dom.querySelectorAll('h2[id]');
  27. for(var k in titles){
  28. let title = titles[k];
  29. if(!title.innerText || !title.id) continue;
  30. let link = document.location.hash;
  31. link = link.replace(/\?id=/,'');
  32. link +='?id='+title.id;
  33. toc += '<li>'+k+'. <a href="'+link+'">'+title.innerText+"</a></li>";
  34. }
  35. toc += "</ul>";
  36. regex = new RegExp(tag,"i");
  37. console.log(toc);
  38. html = html.replace("\[\-FAQ\-\]",toc);
  39. return html + 'Powered by redok';
  40. });
  41. };
  42. // Add plugin to docsify's plugin array
  43. $docsify = $docsify || {};
  44. $docsify.plugins = [].concat($docsify.plugins || [], myPlugin);
  45. })();