This commit is contained in:
UltraLionFr
2025-08-20 16:05:24 +02:00
parent f6170fa581
commit 3de3e6f35a
2 changed files with 31 additions and 19 deletions
+30 -18
View File
@@ -4,11 +4,6 @@ import { useRouter } from 'next/navigation';
import Prism from 'prismjs'; import Prism from 'prismjs';
import { use, useEffect, useState } from 'react'; import { use, useEffect, useState } from 'react';
import 'prismjs/components/prism-bash';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-json';
import 'prismjs/components/prism-python';
import 'prismjs/components/prism-typescript';
import 'prismjs/plugins/line-numbers/prism-line-numbers.css'; import 'prismjs/plugins/line-numbers/prism-line-numbers.css';
import 'prismjs/plugins/line-numbers/prism-line-numbers.js'; import 'prismjs/plugins/line-numbers/prism-line-numbers.js';
import 'prismjs/themes/prism-tomorrow.css'; import 'prismjs/themes/prism-tomorrow.css';
@@ -19,6 +14,15 @@ import LoadingPaste from '@/components/paste/LoadingPaste';
import PasswordForm from '@/components/paste/PasswordForm'; import PasswordForm from '@/components/paste/PasswordForm';
import PasteSidebar from '@/components/paste/PasteSidebar'; import PasteSidebar from '@/components/paste/PasteSidebar';
async function loadLanguage(lang: string) {
try {
await import(`prismjs/components/prism-${lang}.js`);
} catch (err) {
console.warn(`[Prism] Language '${lang}' introuvable. Fallback -> plaintext`);
}
}
export default function PastePage({ export default function PastePage({
params, params,
}: { }: {
@@ -54,29 +58,38 @@ export default function PastePage({
const data = await res.json(); const data = await res.json();
setPaste(data); setPaste(data);
if (data.content.includes('function') || data.content.includes('const')) { const content = data.content.toLowerCase();
setLanguage('language-javascript');
} else if (data.content.includes('import') || data.content.includes('export')) { let detected = 'javascript';
setLanguage('language-typescript'); if (content.includes('select ') || content.includes('insert into')) {
} else if (data.content.includes('{') && data.content.includes('}')) { detected = 'sql';
setLanguage('language-json'); } else if (content.includes('function') || content.includes('const')) {
detected = 'javascript';
} else if (content.includes('import ') || content.includes('export ')) {
detected = 'typescript';
} else if (content.includes('{') && content.includes('}')) {
detected = 'json';
} else if (content.includes('#include') || content.includes('int main')) {
detected = 'cpp';
} else if (content.includes('arduino')) {
detected = 'arduino';
} else { } else {
setLanguage('language-bash'); detected = 'bash';
} }
await loadLanguage(detected);
setLanguage(`language-${detected}`);
setLoading(false); setLoading(false);
}; };
useEffect(() => { useEffect(() => {
fetchPaste(); fetchPaste();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id]); }, [id]);
useEffect(() => { useEffect(() => {
if (paste?.content) { if (paste?.content) Prism.highlightAll();
Prism.highlightAll(); }, [paste, language]);
}
}, [paste]);
if (loading) return <LoadingPaste />; if (loading) return <LoadingPaste />;
@@ -91,5 +104,4 @@ export default function PastePage({
<AuthSection /> <AuthSection />
</div> </div>
); );
} }
BIN
View File
Binary file not shown.