From d9fcb4c2173a13888ebf61c8fe035c839a78c930 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 24 Oct 2024 10:18:13 -0500 Subject: [PATCH] Add GitHub Gist embed support --- src/alf/fonts.ts | 4 + src/lib/strings/embed-player.ts | 12 ++ .../util/post-embeds/ExternalLinkEmbed.tsx | 12 ++ src/view/com/util/post-embeds/GithubGist.tsx | 154 ++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 src/view/com/util/post-embeds/GithubGist.tsx diff --git a/src/alf/fonts.ts b/src/alf/fonts.ts index 7869167212..9380b5c9e0 100644 --- a/src/alf/fonts.ts +++ b/src/alf/fonts.ts @@ -38,6 +38,10 @@ export function setFontFamily(fontFamily: Device['fontFamily']) { * Unused fonts are commented out, but the files are there if we need them. */ export function applyFonts(style: TextStyle, fontFamily: 'system' | 'theme') { + if (style.fontFamily === 'monospace') { + return + } + if (fontFamily === 'theme') { if (isAndroid) { style.fontFamily = diff --git a/src/lib/strings/embed-player.ts b/src/lib/strings/embed-player.ts index d0d8277c86..8b84277fbe 100644 --- a/src/lib/strings/embed-player.ts +++ b/src/lib/strings/embed-player.ts @@ -25,6 +25,7 @@ export const embedPlayerSources = [ 'giphy', 'tenor', 'flickr', + 'github', ] as const export type EmbedPlayerSource = (typeof embedPlayerSources)[number] @@ -45,6 +46,7 @@ export type EmbedPlayerType = | 'giphy_gif' | 'tenor_gif' | 'flickr_album' + | 'github_gist' export const externalEmbedLabels: Record = { youtube: 'YouTube', @@ -57,6 +59,7 @@ export const externalEmbedLabels: Record = { appleMusic: 'Apple Music', soundcloud: 'SoundCloud', flickr: 'Flickr', + github: 'GitHub', } export interface EmbedPlayerParams { @@ -448,6 +451,15 @@ export function parseEmbedPlayerFromUrl( return undefined } } + + if (urlp.hostname === 'gist.github.com') { + const id = urlp.pathname.replace(/^\//, '').split('/')[1] + return { + type: 'github_gist', + source: 'github', + playerUri: id, + } + } } export function getPlayerAspect({ diff --git a/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx b/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx index 0fe3084178..8053d89a65 100644 --- a/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx +++ b/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx @@ -14,6 +14,7 @@ import {useExternalEmbedsPrefs} from '#/state/preferences' import {ExternalGifEmbed} from '#/view/com/util/post-embeds/ExternalGifEmbed' import {ExternalPlayer} from '#/view/com/util/post-embeds/ExternalPlayerEmbed' import {GifEmbed} from '#/view/com/util/post-embeds/GifEmbed' +import {GithubGist} from '#/view/com/util/post-embeds/GithubGist' import {atoms as a, useTheme} from '#/alf' import {Divider} from '#/components/Divider' import {Earth_Stroke2_Corner0_Rounded as Globe} from '#/components/icons/Globe' @@ -66,6 +67,17 @@ export const ExternalLinkEmbed = ({ ) } + if ( + embedPlayerParams?.source === 'github' && + embedPlayerParams?.type === 'github_gist' + ) { + return ( + + + + ) + } + return ( res.json()) + const files = Object.values(gist.files) as { + filename: string + type: 'text/markdown' | string + language: 'Markdown' | string + raw_url: string + size: number + truncated: boolean + content: string + }[] + return files + }, + }) + + const onShareExternal = React.useCallback(() => { + if (info.uri && isNative) { + shareUrl(info.uri) + } + }, [info.uri]) + + return ( + + {({hovered}) => ( + + + {isLoading || !files ? ( + + ) : ( + + + {files[0].filename} + + + {files[0].content} + + + )} + + + + + + {info.title || info.uri} + + + + + + + + {toNiceDomain(info.uri)} + + + + + + )} + + ) +}