Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { themes as prismThemes } from "prism-react-renderer";

const remarkMath = require("remark-math");
const rehypeKatex = require("rehype-katex");
const readingTimePlugin = require("./plugins/remark-reading-time");

const path = require("path");

Expand Down Expand Up @@ -56,7 +57,7 @@ const config = {
},
editUrl:
"https://github.com/codeharborhub/codeharborhub.github.io/edit/main/",
remarkPlugins: [remarkMath],
remarkPlugins: [readingTimePlugin, remarkMath],
rehypePlugins: [rehypeKatex],
},
pages: {
Expand Down
46 changes: 46 additions & 0 deletions plugins/remark-reading-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Remark plugin to calculate and inject reading time into frontmatter
* Estimates reading time based on word count (average 200 words per minute)
* Code blocks are weighted at 0.5x since code is read differently than prose
*/

const { visit } = require('unist-util-visit');

module.exports = function readingTimePlugin() {
return (tree, vfile) => {
let wordCount = 0;
let codeWordCount = 0;

// Count words in text nodes
visit(tree, 'text', (node) => {
wordCount += node.value
.split(/\s+/)
.filter((word) => word.length > 0).length;
});

// Count words in code blocks (weighted at 0.5x)
visit(tree, 'code', (node) => {
codeWordCount += node.value
.split(/\s+/)
.filter((word) => word.length > 0).length;
});

visit(tree, 'inlineCode', (node) => {
codeWordCount += node.value
.split(/\s+/)
.filter((word) => word.length > 0).length;
});

// Calculate total words with code weighted at 0.5x
const totalWords = wordCount + codeWordCount * 0.5;

// Estimate reading time (200 words per minute, minimum 1 minute)
const readingTime = Math.max(1, Math.round(totalWords / 200));

// Inject into frontmatter
if (!vfile.data.frontMatter) {
vfile.data.frontMatter = {};
}
vfile.data.frontMatter.readingTime = readingTime;
};
};
Loading