Skip to content

Commit

Permalink
feat(reading-time): allow to exclude code and tex blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Renovamen committed Feb 4, 2021
1 parent 5fad11c commit b8f7c78
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 52 deletions.
44 changes: 43 additions & 1 deletion packages/plugins/reading-time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,48 @@ plugins: [

 

### excludeCodeBlock

Exclude all content inside code blocks or not.

- Type: `boolean`
- Default: `false`

Example:

```js
plugins: [
[
'@renovamen/vuepress-plugin-reading-time', {
excludeCodeBlock: true
}
]
]
```

 

### excludeTexBlock

Exclude all content inside tex blocks or not.

- Type: `boolean`
- Default: `false`

Example:

```js
plugins: [
[
'@renovamen/vuepress-plugin-reading-time', {
excludeTexBlock: true
}
]
]
```

 

## License

[MIT](LICENSE)
[MIT](LICENSE)
43 changes: 19 additions & 24 deletions packages/plugins/reading-time/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
const readingTime = require('./reading-time')
const readingTime = require("./reading-time");

module.exports = (options = {}) => ({
extendPageData ($page) {
const {
regularPath,
path,
frontmatter,
_strippedContent
} = $page
extendPageData($page) {
const { regularPath, path, frontmatter, _strippedContent } = $page;

if (!_strippedContent){
return $page
if(!_strippedContent) {
return $page;
}

if (frontmatter && frontmatter.readingTime) {
$page.readingTime = frontmatter.readingTime
return $page
if(frontmatter && frontmatter.readingTime) {
$page.readingTime = frontmatter.readingTime;
return $page;
}

const excludePage = options.excludes && options.excludes.some(p => {
const testRegex = new RegExp(p)
return testRegex.test(path) || testRegex.test(regularPath)
})
const testRegex = new RegExp(p);
return testRegex.test(path) || testRegex.test(regularPath);
});

if (excludePage) {
return $page
if(excludePage) {
return $page;
}
var stats = readingTime(_strippedContent, options)
$page.readingTime = stats
return $page

var stats = readingTime(_strippedContent, options);
$page.readingTime = stats;

return $page;
}
})
});
10 changes: 5 additions & 5 deletions packages/plugins/reading-time/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"name": "@renovamen/vuepress-plugin-reading-time",
"version": "0.1.1",
"version": "0.1.2",
"description": "A plugin for displaying the number of words and how long a post takes to read in VuePress.",
"author": "Renovamen <renovamenzxh@gmail.com>",
"main": "index.js",
"keywords": [
"vue",
"vuepress",
Expand All @@ -24,5 +22,7 @@
"url": "git+https://github.com/Renovamen/vuepress-theme-gungnir.git",
"directory": "packages/plugins/reading-time"
},
"license": "MIT"
}
"license": "MIT",
"author": "Renovamen <renovamenzxh@gmail.com>",
"main": "index.js"
}
46 changes: 33 additions & 13 deletions packages/plugins/reading-time/reading-time.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
const getNumCN = function(text) {
return (text.match(/[\u4E00-\u9FA5]/g) || []).length
}
return (text.match(/[\u4E00-\u9FA5]/g) || []).length;
};

const getNumEN = function(text) {
return (text.replace(/[\u4E00-\u9FA5]/g, '').match(/[a-zA-Z0-9_\u0392-\u03c9\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af\u0400-\u04FF]+|[\u00E4\u00C4\u00E5\u00C5\u00F6\u00D6]+|\w+/g) || []).length
}
return (
text
.replace(/[\u4E00-\u9FA5]/g, "")
.match(
/[a-zA-Z0-9_\u0392-\u03c9\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af\u0400-\u04FF]+|[\u00E4\u00C4\u00E5\u00C5\u00F6\u00D6]+|\w+/g
) || []
).length;
};

const readingTime = function(text, options) {
const cntCN = getNumCN(text || ''), cntEN = getNumEN(text || '')
const excludeCodeBlock = function(text) {
return text.replace(/```[\s\S]*?```/g, '');
};

const excludeTexBlock = function(text) {
return text.replace(/\$\$[\s\S]*?\$\$/g, '');
};

options = options || {}
const readingTime = function(text, options) {
options = options || {};

// use default values if necessary
options.wordsPerMinuteCN = options.wordsPerMinuteCN || 300
options.wordsPerMinuteEN = options.wordsPerMinuteEN || 160
options.wordsPerMinuteCN = options.wordsPerMinuteCN || 300;
options.wordsPerMinuteEN = options.wordsPerMinuteEN || 160;

// exclude all content inside code blocks
if(options.excludeCodeBlock) text = excludeCodeBlock(text)
// exclude all content inside tex blocks
if(options.excludeTexBlock) text = excludeTexBlock(text)

// number of chinese words and english words
const cntCN = getNumCN(text || ""), cntEN = getNumEN(text || "");

// compute reading time
var minutes = cntCN / options.wordsPerMinuteCN + cntEN / options.wordsPerMinuteEN
minutes = minutes < 1 ? 1 : Math.ceil(minutes.toFixed(2))
var minutes = cntCN / options.wordsPerMinuteCN + cntEN / options.wordsPerMinuteEN;
minutes = minutes < 1 ? 1 : Math.ceil(minutes.toFixed(2));

return {
minutes: minutes,
words: cntCN + cntEN
}
}
};
};

module.exports = readingTime;
4 changes: 2 additions & 2 deletions packages/theme-gungnir/components/ArticleHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{ subItem }}
</span>
</div>

<h1 class="title">
{{ articleInfo.title }}
</h1>
Expand Down Expand Up @@ -66,7 +66,7 @@ export default {
// to: 2019-09-20 18:22:30
formatDateValue (value) {
if (!value) return ''
value = value.replace('T', ' ').slice(0, value.lastIndexOf('.'))
const h = Number(value.substr(11, 2)) // hours
const m = Number(value.substr(14, 2)) // minutes
Expand Down
5 changes: 3 additions & 2 deletions packages/theme-gungnir/components/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
v-if="($page.id == 'posts') && getPostIndex != -1"
:data="getPostPager"
/>
<!--
<!--
Use prop `key` to update vssue when navigating directly from one
<Page> to another.
See here: https://github.com/meteorlxy/vssue/issues/87
Expand Down Expand Up @@ -104,6 +104,7 @@ export default {
},
mounted() {
console.log(this.$page.readingTime)
this.codeFullScreen()
},
methods: {
Expand Down Expand Up @@ -168,7 +169,7 @@ export default {
.page
padding-top 3rem
margin-left 0
@media (max-width: $MQMobileNarrow)
.page
.vssue-comment-wrapper
Expand Down
10 changes: 6 additions & 4 deletions packages/theme-gungnir/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ module.exports = (options, ctx) => {
chartjs: false,
roughviz: false,
mdPlus: false,
readingTime: {},
personalInfo: {},
homeHeaderImages: {},
pages: {},
footer: ''
}, options))

const { comment, analytics } = options
const { comment, analytics, readingTime } = options

return {
name: 'vuepress-theme-gungnir',
Expand Down Expand Up @@ -111,9 +112,10 @@ module.exports = (options, ctx) => {
}, comment) : false
],
[
'@renovamen/vuepress-plugin-reading-time', {
excludes: ['/about', '/tags/.*', '/links']
}
'@renovamen/vuepress-plugin-reading-time',
readingTime ? Object.assign({
excludes: ['/tags/.*', '/links']
}, readingTime) : false
],
[
'@vuepress/google-analytics',
Expand Down
2 changes: 1 addition & 1 deletion packages/theme-gungnir/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@renovamen/vuepress-plugin-katex": "^0.1.1",
"@renovamen/vuepress-plugin-md-plus": "^0.1.1",
"@renovamen/vuepress-plugin-mermaid": "^0.1.0",
"@renovamen/vuepress-plugin-reading-time": "^0.1.1",
"@renovamen/vuepress-plugin-reading-time": "^0.1.2",
"@renovamen/vuepress-plugin-rss": "^0.1.1",
"@vssue/api-github-v3": "^1.4.7",
"@vssue/vuepress-plugin-vssue": "^1.4.7",
Expand Down

1 comment on commit b8f7c78

@vercel
Copy link

@vercel vercel bot commented on b8f7c78 Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.