helper scripts

This commit is contained in:
matthieu42morin 2024-03-22 15:18:46 +01:00
parent 06d84f6d6e
commit 753fbc76b7
3 changed files with 82 additions and 20 deletions

View File

@ -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<T>(content: string): { frontmatter: T; content: string } {
const { data, content: rawContent } = matter(content);
const markdownContent = marked(rawContent);
return {
frontmatter: data as T,
content: markdownContent,
};
}

View File

@ -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;

32
src/lib/utils/scroll.ts Normal file
View File

@ -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'
});
};