-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@import url('https://fonts.googleapis.com/css2?family=Sevillana&family=Seymour+One&family=Teko:[email protected]&display=swap'); | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect, useState, useRef } from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
import axios, { AxiosError } from 'axios'; | ||
import { IPost } from '../types'; | ||
import DOMPurify from 'dompurify'; | ||
|
||
const Post = () => { | ||
const { id } = useParams<{ id: string }>(); | ||
|
@@ -19,6 +20,14 @@ const Post = () => { | |
}); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<string | null>(null); | ||
const [isPreview, setIsPreview] = useState(false); | ||
const ref = useRef<HTMLIFrameElement>(null); | ||
const [height, setHeight] = useState('0px'); | ||
|
||
const onLoad = () => { | ||
setHeight(ref.current?.contentWindow?.document.body.scrollHeight + 'px'); | ||
console.log(ref.current?.contentWindow?.document.body.scrollHeight); | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchPost = async () => { | ||
|
@@ -39,11 +48,19 @@ const Post = () => { | |
fetchPost(); | ||
}, [id]); | ||
|
||
useEffect(() => { | ||
onLoad(); | ||
}, [isPreview, post.codeSnippet]); | ||
|
||
const handleCopy = () => { | ||
navigator.clipboard.writeText(post.codeSnippet); | ||
alert('Code snippet copied to clipboard'); | ||
}; | ||
|
||
const togglePreview = () => { | ||
setIsPreview(!isPreview); | ||
}; | ||
|
||
if (loading) { | ||
return <div className="text-white">Loading...</div>; | ||
} | ||
|
@@ -52,22 +69,80 @@ const Post = () => { | |
return <div className="text-red-500 text-lg w-full text-center mt-5">{error}</div>; | ||
} | ||
|
||
DOMPurify.addHook('uponSanitizeElement', (node, data) => { | ||
if (data.tagName === 'img' || data.tagName === 'div') { | ||
const src = node.getAttribute('src'); | ||
const style = node.getAttribute('style'); | ||
if (src && src.startsWith('http')) { | ||
node.setAttribute('src', src); | ||
} | ||
if (style && style.includes('url(')) { | ||
node.setAttribute('style', style); | ||
} | ||
} | ||
}); | ||
|
||
const sanitizedSnippet = DOMPurify.sanitize(post?.codeSnippet || '', { | ||
ADD_ATTR: ['style', 'background'], | ||
}); | ||
|
||
return ( | ||
<div className="p-6 text-white max-w-screen-xl mx-auto"> | ||
{post && ( | ||
<> | ||
<h2 className="text-2xl font-semibold mb-4">{post.title}</h2> | ||
<p className="mb-4">{post.description}</p> | ||
<div className="relative mb-4"> | ||
<pre className="p-4 bg-gray-800 border border-gray-700 rounded overflow-auto"> | ||
<code>{post.codeSnippet}</code> | ||
</pre> | ||
<button | ||
onClick={handleCopy} | ||
className="absolute top-2 right-2 px-2 py-1 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded" | ||
> | ||
Copy | ||
</button> | ||
{isPreview ? ( | ||
<div className="p-4 bg-gray-800 z-0 h-full overflow-hidden rounded border border-gray-700"> | ||
<iframe | ||
ref={ref} | ||
onLoad={onLoad} | ||
className="w-full h-full border-0" | ||
srcDoc={ | ||
`<html class='flex w-full h-full'> | ||
<head> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet"> | ||
<script> | ||
document.addEventListener('DOMContentLoaded', function() { | ||
document.querySelectorAll('a[href="#"]').forEach(function(anchor) { | ||
anchor.addEventListener('click', function(e) { | ||
e.preventDefault(); | ||
window.top.location.reload(); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
</head> | ||
<body class='w-full h-full flex items-center justify-center minw-full min-h-full'> | ||
<div class='w-full h-full p-6'>${sanitizedSnippet}</div> | ||
</body> | ||
</html>`} | ||
title="Preview" | ||
sandbox="allow-scripts allow-same-origin" | ||
style={{ minHeight: height, maxWidth: '100%' }} | ||
/> | ||
</div> | ||
) : ( | ||
<pre className="p-4 bg-gray-800 border border-gray-700 rounded overflow-auto max-h-96 line-numbers language-html"> | ||
<code>{post.codeSnippet}</code> | ||
</pre> | ||
)} | ||
<div className="absolute top-2 right-3 flex space-x-2"> | ||
{isPreview ? null : | ||
<button | ||
onClick={handleCopy} | ||
className="px-2 py-1 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded" | ||
> | ||
Copy | ||
</button>} | ||
<button | ||
onClick={togglePreview} | ||
className="px-2 py-1 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded" | ||
> | ||
{isPreview ? 'Show Code' : 'Preview'} | ||
</button> | ||
</div> | ||
</div> | ||
<div className="mb-4"> | ||
<h3 className="text-xl font-semibold mb-2">Tags</h3> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
export default { | ||
content: [ | ||
"./index.html", | ||
"./src/**/*.{js,ts,jsx,tsx}", | ||
], | ||
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"], | ||
theme: { | ||
extend: {}, | ||
extend: { | ||
fontFamily: { | ||
teko: ["Teko", "sans-serif"], | ||
}, | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
|
||
}; |