add prettier

This commit is contained in:
Tomas Mirchev 2025-06-30 15:48:33 +00:00
parent 82982910ca
commit 4e0df6c740
2 changed files with 45 additions and 6 deletions

View File

@ -3,6 +3,7 @@ import { marked } from "marked";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { useStore } from "./store.js"; import { useStore } from "./store.js";
import { resourcesInstance, apiInstance } from "./api.js"; import { resourcesInstance, apiInstance } from "./api.js";
import { prettifyMarkdown } from "./utils.js";
function LoadingWrapper() { function LoadingWrapper() {
const isLoading = useStore((state) => state.isLoading); const isLoading = useStore((state) => state.isLoading);
@ -204,11 +205,9 @@ const ResourcePage = ({ topics }) => {
} }
}; };
const handleCancel = () => { function handlePrettify() {
if (window.confirm("Сигурни ли сте, че искате да затворите страницата?")) { setContent((prev) => prettifyMarkdown(prev));
window.close();
} }
};
const handleCopyContent = async () => { const handleCopyContent = async () => {
try { try {
@ -318,7 +317,7 @@ const ResourcePage = ({ topics }) => {
onClick={handleDeleteAll} 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" 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"
> >
Изтрий всичко Изтрий
</button> </button>
<button <button
@ -327,6 +326,12 @@ const ResourcePage = ({ topics }) => {
> >
Възстанови оригинал Възстанови оригинал
</button> </button>
<button
onClick={handlePrettify}
className="bg-blue-600 text-sm text-white px-2 py-1 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors"
>
Оправи текст
</button>
</div> </div>
{isSavingLoading && <span className="flex justify-end mr-8">Saving...</span>} {isSavingLoading && <span className="flex justify-end mr-8">Saving...</span>}

View File

@ -8,3 +8,37 @@ export function getIndexFromTopicId(topicId) {
return [Number(match[1]) - 1]; 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;
}