Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/lib/components/lemmy/post/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export const optimizeImageURL = (
}
}

const HARDGIF_REGEX = /^(?:https?:\/\/)?(?:www\.)?(?:hardgif\.com\/(?:gif\/))(\w+)(#.*)?$/

export const isHardgifLink = (url?: string): RegExpMatchArray | null => {
if (!url) return null

return url?.match?.(HARDGIF_REGEX)
}

const YOUTUBE_REGEX =
/^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|shorts\/|live\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/

Expand All @@ -65,13 +73,14 @@ export const postLink = (post: Post) =>
`/post/${encodeURIComponent(instance.data)}/${post.id}`

export type MediaType = 'video' | 'image' | 'iframe' | 'embed' | 'none'
export type IframeType = 'youtube' | 'video' | 'none'
export type IframeType = 'youtube' | 'hardgif' | 'video' | 'none'

export const mediaType = (url?: string): MediaType => {
if (url) {
if (isImage(url)) return 'image'
if (isVideo(url)) return 'iframe'
if (isYoutubeLink(url)) return 'iframe'
if (isHardgifLink(url)) return 'iframe'
if (canParseUrl(url)) return 'embed'
return 'none'
}
Expand All @@ -81,6 +90,7 @@ export const mediaType = (url?: string): MediaType => {
export const iframeType = (url: string): IframeType => {
if (isVideo(url)) return 'video'
if (isYoutubeLink(url)) return 'youtube'
if (isHardgifLink(url)) return 'hardgif'
return 'none'
}

Expand Down
24 changes: 24 additions & 0 deletions src/lib/components/lemmy/post/media/PostIframe.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<script lang="ts" module>
function hardgifVideoId(url: string): string | null {
const regex =
/^(?:https?:\/\/)?(?:www\.)?(?:hardgif\.com\/(?:gif\/))(\w+)(#.*)?$/
const match = url.match(regex)

if (match && match[1]) {
return match[1]
}

return null
}

const youtubeDomain = (place: 'youtube' | 'invidious' | 'piped') => {
switch (place) {
case 'youtube': {
Expand Down Expand Up @@ -53,6 +65,12 @@
)}/embed/${videoID}?${url.searchParams.toString()}`
}

if (type == 'hardgif') {
const videoID = hardgifVideoId(inputUrl)

return `https://hardgif.com/embedPlayer.php?post_id=${videoID}`
}

return ''
}

Expand All @@ -75,6 +93,12 @@
text: 'Video',
}
}
case 'hardgif': {
return {
icon: VideoCamera,
text: 'Video Hardgif',
}
}
default: {
return {
icon: PuzzlePiece,
Expand Down