From 4e0df6c74032b8ae4e747d889be6d5af25ada0a0 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Mon, 30 Jun 2025 15:48:33 +0000 Subject: [PATCH] add prettier --- reader/src/ResourcePage.jsx | 17 +++++++++++------ reader/src/utils.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/reader/src/ResourcePage.jsx b/reader/src/ResourcePage.jsx index b3aef72..69c0509 100644 --- a/reader/src/ResourcePage.jsx +++ b/reader/src/ResourcePage.jsx @@ -3,6 +3,7 @@ import { marked } from "marked"; import { toast } from "react-toastify"; import { useStore } from "./store.js"; import { resourcesInstance, apiInstance } from "./api.js"; +import { prettifyMarkdown } from "./utils.js"; function LoadingWrapper() { const isLoading = useStore((state) => state.isLoading); @@ -204,11 +205,9 @@ const ResourcePage = ({ topics }) => { } }; - const handleCancel = () => { - if (window.confirm("Сигурни ли сте, че искате да затворите страницата?")) { - window.close(); - } - }; + function handlePrettify() { + setContent((prev) => prettifyMarkdown(prev)); + } const handleCopyContent = async () => { try { @@ -318,7 +317,7 @@ const ResourcePage = ({ topics }) => { onClick={handleDeleteAll} className="bg-red-600 text-sm text-white px-2 py-1 rounded-md hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 transition-colors" > - Изтрий всичко + Изтрий + {isSavingLoading && Saving...} diff --git a/reader/src/utils.js b/reader/src/utils.js index 59e925d..ad51c9e 100644 --- a/reader/src/utils.js +++ b/reader/src/utils.js @@ -8,3 +8,37 @@ export function getIndexFromTopicId(topicId) { return [Number(match[1]) - 1]; } + +export function prettifyMarkdown(markdown) { + if (!markdown || typeof markdown !== "string") { + return ""; + } + + let text = markdown; + + // 1. Trim lines + text = text + .split("\n") + .map((line) => line.trim()) + .join("\n"); + + // 2. Fix whitespace (normalize multiple spaces to single space) + text = text.replace(/[ \t]+/g, " "); + + // 3. Convert bold from __ to ** + text = text.replace(/__/g, "**"); + + // 4. Convert list markers from * to - and fix spacing + text = text.replace(/^\* /gm, "- "); + text = text.replace(/^-([^ ])/gm, "- $1"); + + // 5. Add empty lines before and after headers + text = text.replace(/^(#{1,6} .+)$/gm, "\n$1\n"); + + // 6. Clean up excessive newlines + text = text.replace(/\n{3,}/g, "\n\n"); + text = text.replace(/^\n+/, ""); + text = text.replace(/\n+$/, ""); + + return text; +}