Skip to content

Commit

Permalink
增加字数统计
Browse files Browse the repository at this point in the history
  • Loading branch information
javajianghu committed May 20, 2024
1 parent 48fc572 commit 2740434
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
60 changes: 60 additions & 0 deletions assets/js/countable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//default values
var defaultOptions = {
countable: true,
position: "top",
margin: "10px",
float: "right",
fontsize: "0.9em",
color: "rgb(90,90,90)",
language: "english",
isExpected: true,
}

// Docsify plugin functions
function plugin(hook, vm) {
if (!defaultOptions.countable) {
return
}
let wordsCount
hook.beforeEach(function (content) {
// Match regex every time you start parsing .md
wordsCount = content.match(/([\u4e00-\u9fa5]+?|[a-zA-Z0-9]+)/g).length
return content
})
hook.afterEach(function (html, next) {
let str = wordsCount + " words"
let readTime = Math.ceil(wordsCount / 400) + " min"
//Determine whether to use the Chinese style according to the attribute "language"
if (defaultOptions.language === "chinese") {
str = wordsCount + " 字"
readTime = Math.ceil(wordsCount / 400) + " 分钟"
}

//add html string
next(
`
${defaultOptions.position === "bottom" ? html : ""}
<div style="margin-${defaultOptions.position ? "bottom" : "top"}: ${
defaultOptions.margin
};">
<span style="
float: ${defaultOptions.float === "right" ? "right" : "left"};
font-size: ${defaultOptions.fontsize};
color:${defaultOptions.color};">
${str}
${defaultOptions.isExpected ? `&nbsp; | &nbsp;${readTime}` : ""}
</span>
<div style="clear: both"></div>
</div>
${defaultOptions.position !== "bottom" ? html : ""}
`
)
})
}

// Docsify plugin options
window.$docsify["count"] = Object.assign(
defaultOptions,
window.$docsify["count"]
)
window.$docsify.plugins = [].concat(plugin, window.$docsify.plugins)
1 change: 1 addition & 0 deletions assets/js/docsify-pagination.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions assets/js/docsify-sidebar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,26 @@
// 字段统计
count:{
countable:true,
position: 'top',
margin: '10px',
float: 'left',
fontsize:'0.9em',
color:'rgb(90,90,90)',
language:'chinese',
localization: {
words: "",
minute: ""
},
isExpected: true,
language:'chinese'
},
// 分页导航插件
pagination: {
previousText: '上一页',
nextText: '下一页',
crossChapter: true,
crossChapterText: true
},
toc: {
tocMaxLevel: 2,
target: "h1,h2, h3, h4, h5, h6",
Expand All @@ -59,5 +75,9 @@
<script src="assets/js/emoji.min.js"></script>
<!-- 搜索 -->
<script src="assets/js/search.min.js"></script>
<!-- 字数插件 -->
<script src="assets/js/countable.js"></script>
<!-- 分页导航 -->
<script src="assets/js/docsify-pagination.min.js"></script>
</body>
</html>

0 comments on commit 2740434

Please sign in to comment.