-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reading-time): allow to exclude code and tex blocks
- Loading branch information
Showing
8 changed files
with
112 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b8f7c78
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: