Last active
November 20, 2022 23:06
-
-
Save jrson83/7fa97a663b82aeddfdd75c5327c32c11 to your computer and use it in GitHub Desktop.
Lume reading time plugin
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
import { merge } from "lume/core/utils.ts"; | |
import type { Page, Site } from "lume/core.ts"; | |
export interface Options { | |
/** The list extensions this plugin applies to */ | |
extensions: string[]; | |
/** The words per minute a reader can read (default: 275) */ | |
wordsPerMinute: number; | |
} | |
export const defaults: Options = { | |
extensions: [".md"], | |
wordsPerMinute: 275 | |
}; | |
export default function (userOptions?: Partial<Options>) { | |
const options = merge(defaults, userOptions); | |
return (site: Site) => { | |
site.preprocess(options.extensions, readingTime); | |
function readingTime(page: Page) { | |
const content = page.data.content as string; | |
if (!content || typeof content !== "string") { | |
page.data.readingTime = { | |
words: 0, | |
minutes: 0, | |
time: 0, | |
}; | |
} | |
const matches = content.match(/\w+/g); | |
const wordCount = matches ? matches.length : 0; | |
const minutes = wordCount / options.wordsPerMinute; | |
const time = Math.round(minutes * 60 * 1000); | |
const displayTime = Math.ceil(parseFloat(minutes.toFixed(2))); | |
page.data.readingTime = { | |
words: wordCount, | |
minutes: displayTime, | |
time, | |
}; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment