@mr-hope/vuepress-plugin-reading-time
Version:
Reading time plugin for vuepress
30 lines • 1.13 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReadingTime = exports.getWordNumber = exports.getChinese = exports.getWords = void 0;
/**
* Extract Latin words from content
*/
const getWords = (content) => content.match(/[\w\d\s,.\u00C0-\u024F]+/giu) || [];
exports.getWords = getWords;
/**
* Extract Chinese Characters from content
*/
const getChinese = (content) => content.match(/[\u4E00-\u9FD5]/gu) || [];
exports.getChinese = getChinese;
/**
* Get word number of given string
*/
const getWordNumber = (content) => (0, exports.getWords)(content).reduce((accumulator, word) => accumulator + (word.trim() === "" ? 0 : word.trim().split(/\s+/u).length), 0) + (0, exports.getChinese)(content).length;
exports.getWordNumber = getWordNumber;
/**
* Get reading time info
*/
const getReadingTime = (content, wordsPerMinute = 300) => {
const words = (0, exports.getWordNumber)(content || "");
return {
minutes: Math.round((words / wordsPerMinute) * 100) / 100,
words,
};
};
exports.getReadingTime = getReadingTime;
//# sourceMappingURL=reading-time.js.map
;