UNPKG

@nolebase/vitepress-plugin-git-changelog

Version:

A VitePress plugin that adds a changelog fetched from git to your documentation.

31 lines (30 loc) 1.62 kB
import { formatDistanceToNow, toDate } from "date-fns"; import { subtle } from "uncrypto"; import * as DateFnsLocales from "date-fns/locale"; export function renderMarkdown(markdownText = "") { const htmlText = markdownText.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/^### (.*$)/gm, "<h3>$1</h3>").replace(/^## (.*$)/gm, "<h2>$1</h2>").replace(/^# (.*$)/gm, "<h1>$1</h1>").replace(/^> (.*$)/gm, "<blockquote>$1</blockquote>").replace(/\*\*(.*)\*\*/g, "<b>$1</b>").replace(/\*(.*)\*/g, "<i>$1</i>").replace(/!\[(.*?)\]\((.*?)\)/g, "<img alt='$1' src='$2' />").replace(/\[(.*?)\]\((.*?)\)/g, `<a class="no-icon" href='$2'>$1</a>`).replace(/`(.*?)`/g, "<code>$1</code>").replace(/\n$/gm, "<br />"); return htmlText.trim(); } export function renderCommitMessage(repoLink, msg) { return renderMarkdown(msg).replace(/#(\d+)/g, `<a class="no-icon" href='${repoLink}/issues/$1'>#$1</a>`); } export function formatDistanceToNowFromValue(value, localeName = "enUS") { try { return formatDistanceToNow(toDate(value), { locale: DateFnsLocales[localeName] || "enUS", addSuffix: true }); } catch { return value.toLocaleDateString(); } } export function isStringArray(value) { return Array.isArray(value) && value.every((item) => typeof item === "string"); } export async function digestStringAsSHA256(message) { const msgUint8 = new TextEncoder().encode(message); const hashBuffer = await subtle.digest("SHA-256", msgUint8); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); return hashHex; }