Skip to content

Commit

Permalink
Escape some characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Jan 8, 2025
1 parent 99d43e7 commit 8557a96
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/routes/reddit/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
function formatTitle(post) {
let title = post.title
.replace(/\\\"/g, "\"")
.replace(/"/g, '"')
.replace(/&/g, "&")
.replace(/'/g, "'")
Expand Down
26 changes: 15 additions & 11 deletions src/routes/reddit/PostDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@
function renderMarkdown(text) {
if (!text) return "";
// Replace \n with actual newlines before parsing markdown
const processedText = text.replace(/\\n/g, "\n");
const processedText = text
.replace(/\\n/g, "\n")
.replace(/\\\"/g, "\"")
.replace(/"/g, '"')
.replace(/&/g, "&")
.replace(/'/g, "'")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">");
// Convert markdown to HTML and sanitize it
const html = marked.parse(processedText);
return DOMPurify.sanitize(html);
}
function isMediaUrl(url) {
if (!url) return false;
return /\.(jpg|jpeg|png|gif|webp|mp4|webm)$/i.test(url) ||
url.includes('i.imgur.com') ||
url.includes('i.redd.it');
return (
/\.(jpg|jpeg|png|gif|webp|mp4|webm)$/i.test(url) ||
url.includes("i.imgur.com") ||
url.includes("i.redd.it")
);
}
// Configure marked options for Reddit-style markdown
Expand All @@ -34,7 +43,7 @@
<!-- Title and metadata -->
<div class="space-y-3">
<div class="text-2xl font-bold">{post.title}</div>

<div class="flex justify-between items-center gap-x-4 gap-y-2 text-sm">
<div class="flex items-center gap-1.5">
<span role="img" aria-label="fire" class="text-orange-500">🔥</span>
Expand Down Expand Up @@ -63,7 +72,6 @@
class="inline-flex items-center gap-1.5 text-gray-600 hover:text-blue-600"
>
<span>View on Reddit <span class="text-xs">↗</span></span>

</a>
{#if post.url && !post.url.includes("reddit.com")}
<a
Expand Down Expand Up @@ -91,11 +99,7 @@
Your browser does not support the video tag.
</video>
{:else}
<img
src={post.url}
alt="Post content"
class="w-full h-auto"
/>
<img src={post.url} alt="Post content" class="w-full h-auto" />
{/if}
</div>
</div>
Expand Down

0 comments on commit 8557a96

Please sign in to comment.