diff --git a/src/lib/utils/formatParse.ts b/src/lib/utils/formatParse.ts new file mode 100644 index 0000000..a0d5302 --- /dev/null +++ b/src/lib/utils/formatParse.ts @@ -0,0 +1,50 @@ +/** + * Formats a date string into a human-readable format. + * @param date - The date string to format. + * @returns A string representing the formatted date, or an empty string if the input is invalid. + */ +export const formatDate = (date: string) => { + try { + const d = new Date(date); + return `${d.toLocaleString('default', { + month: 'long' + })} ${d.getDate()}, ${d.getFullYear()}`; + } catch (e) { + return ''; + } +}; + +/** + * Takes a string and returns a beautified version of it. + * @param str The input string to be beautified. + * @returns The beautified string. + */ +export const stringToBeautifiedFragment = (str = '') => + (str || '').toLocaleLowerCase().replace(/\s/g, '-').replace(/\?/g, '').replace(/,/g, ''); + +/** + * Removes the trailing slash from a given string. + * @param site - The string to remove the trailing slash from. + * @returns The string without the trailing slash. + */ +export const removeTrailingSlash = (site: string) => { + return site.replace(/\/$/, ''); +}; + + +// ======= MARKDOWN PARSER ======== + + +// https://github.com/jonschlinkert/gray-matter +import matter from 'gray-matter'; +// https://github.com/markedjs/marked +import marked from 'marked'; + +export function parseMarkdown(content: string): { frontmatter: T; content: string } { + const { data, content: rawContent } = matter(content); + const markdownContent = marked(rawContent); + return { + frontmatter: data as T, + content: markdownContent, + }; +} diff --git a/src/lib/utils/minToH.cjs b/src/lib/utils/minToH.cjs deleted file mode 100644 index a9a78ca..0000000 --- a/src/lib/utils/minToH.cjs +++ /dev/null @@ -1,20 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -function convertMinutesToHours(minutes) { - try { - var hours = Math.floor(minutes / 60); - var mins = minutes % 60; - if (hours) { - console.log("".concat(hours, "h ").concat(mins, "m")); - return "".concat(hours, "h ").concat(mins, "m"); - } - else - console.log("".concat(mins, "m")); - return "".concat(mins, "m"); - } - catch (error) { - console.error("I don't think that's time: ".concat(error)); - process.exit(1); // Exit with non-zero code to signal failure - } -} -exports.default = convertMinutesToHours; diff --git a/src/lib/utils/scroll.ts b/src/lib/utils/scroll.ts new file mode 100644 index 0000000..c9b6047 --- /dev/null +++ b/src/lib/utils/scroll.ts @@ -0,0 +1,32 @@ +/** + * Scrolls the page to the nearest element matching the given selector. + * @param selector - The CSS selector of the element to scroll to. + */ +export const scrollIntoView = (selector: string) => { + const scrollToElement = document.querySelector(selector); + + if (!scrollToElement) return; + + const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)'); + + scrollToElement.scrollIntoView({ + block: 'nearest', + inline: 'start', + behavior: mediaQuery.matches ? 'auto' : 'smooth' + }); +}; + +/** + * Scrolls to the first element that matches the given selector within the provided element. + * @param element The element to search within. + * @param selector The selector to match against. + */ +export const scrollToElement = async (element: HTMLElement, selector: string) => { + const firstElement: HTMLElement | null = element.querySelector(selector); + if (!firstElement) { + return; + } + firstElement.scrollIntoView({ + behavior: 'smooth' + }); +};