UNPKG

maz-ui

Version:

A standalone components library for Vue.Js 3 & Nuxt.Js 3

33 lines (32 loc) 1.24 kB
import { computed } from "vue"; function countWords(text) { const words = text.join(", ").match(/\b\w+\b/g); return words ? words.length : 0; } function getReadTimeInMinutes(content, velocity = 150) { const words = countWords([content]); return Math.ceil(words / velocity); } function useReadingTime(options) { const velocity = computed( () => typeof options.velocity == "number" ? options.velocity : options.velocity?.value ?? 150 ), selector = computed(() => typeof options.contentSelector == "string" ? options.contentSelector : options.contentSelector?.value), content = computed(() => { if (typeof options.contentRef?.value == "object") return options.contentRef.value?.textContent; if (selector.value && typeof document < "u") { const contentElement = document.querySelector(selector.value); if (contentElement) return contentElement.textContent; } return typeof options.content == "string" ? options.content : options.content?.value; }), duration = computed(() => getReadTimeInMinutes(content.value, velocity.value)), wordCount = computed(() => countWords([content.value])); return { content, wordCount, velocity, duration }; } export { useReadingTime };