countable.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //default values
  2. var defaultOptions = {
  3. countable: true,
  4. position: "top",
  5. margin: "10px",
  6. float: "right",
  7. fontsize: "0.9em",
  8. color: "rgb(90,90,90)",
  9. language: "english",
  10. isExpected: true,
  11. }
  12. // Docsify plugin functions
  13. function plugin(hook, vm) {
  14. if (!defaultOptions.countable) {
  15. return
  16. }
  17. let wordsCount
  18. hook.beforeEach(function (content) {
  19. // Match regex every time you start parsing .md
  20. wordsCount = content.match(/([\u4e00-\u9fa5]+?|[a-zA-Z0-9]+)/g).length
  21. return content
  22. })
  23. hook.afterEach(function (html, next) {
  24. let str = wordsCount + " words"
  25. let readTime = Math.ceil(wordsCount / 400) + " min"
  26. //Determine whether to use the Chinese style according to the attribute "language"
  27. if (defaultOptions.language === "chinese") {
  28. str = wordsCount + " 字"
  29. readTime = Math.ceil(wordsCount / 400) + " 分钟"
  30. }
  31. //add html string
  32. next(
  33. `
  34. ${defaultOptions.position === "bottom" ? html : ""}
  35. <div style="margin-${defaultOptions.position ? "bottom" : "top"}: ${
  36. defaultOptions.margin
  37. };">
  38. <span style="
  39. float: ${defaultOptions.float === "right" ? "right" : "left"};
  40. font-size: ${defaultOptions.fontsize};
  41. color:${defaultOptions.color};">
  42. ${str}
  43. ${defaultOptions.isExpected ? `&nbsp; | &nbsp;${readTime}` : ""}
  44. </span>
  45. <div style="clear: both"></div>
  46. </div>
  47. ${defaultOptions.position !== "bottom" ? html : ""}
  48. `
  49. )
  50. })
  51. }
  52. // Docsify plugin options
  53. window.$docsify["count"] = Object.assign(
  54. defaultOptions,
  55. window.$docsify["count"]
  56. )
  57. window.$docsify.plugins = [].concat(plugin, window.$docsify.plugins)