Add link embeds to posts
This commit is contained in:
parent
89638dbd18
commit
f5ff0fd274
8 changed files with 178 additions and 4 deletions
|
@ -1,4 +1,9 @@
|
|||
import {extractHtmlMeta} from './strings'
|
||||
import {
|
||||
extractHtmlMeta,
|
||||
isBskyAppUrl,
|
||||
convertBskyAppUrlIfNeeded,
|
||||
} from './strings'
|
||||
import {match as matchRoute} from '../view/routes'
|
||||
|
||||
export enum LikelyType {
|
||||
HTML,
|
||||
|
@ -6,6 +11,7 @@ export enum LikelyType {
|
|||
Image,
|
||||
Video,
|
||||
Audio,
|
||||
AtpData,
|
||||
Other,
|
||||
}
|
||||
|
||||
|
@ -18,6 +24,18 @@ export interface LinkMeta {
|
|||
}
|
||||
|
||||
export async function getLinkMeta(url: string): Promise<LinkMeta> {
|
||||
if (isBskyAppUrl(url)) {
|
||||
// TODO this could be better
|
||||
url = convertBskyAppUrlIfNeeded(url)
|
||||
const route = matchRoute(url)
|
||||
return {
|
||||
likelyType: LikelyType.AtpData,
|
||||
url,
|
||||
title: route.defaultTitle,
|
||||
// description: ''
|
||||
}
|
||||
}
|
||||
|
||||
let urlp
|
||||
try {
|
||||
urlp = new URL(url)
|
||||
|
@ -53,7 +71,15 @@ export async function getLinkMeta(url: string): Promise<LinkMeta> {
|
|||
return meta
|
||||
}
|
||||
|
||||
function getLikelyType(url: URL): LikelyType {
|
||||
export function getLikelyType(url: URL | string): LikelyType {
|
||||
if (typeof url === 'string') {
|
||||
try {
|
||||
url = new URL(url)
|
||||
} catch (e) {
|
||||
return LikelyType.Other
|
||||
}
|
||||
}
|
||||
|
||||
const ext = url.pathname.split('.').pop() || ''
|
||||
if (ext === 'html' || ext === 'htm') {
|
||||
return LikelyType.HTML
|
||||
|
|
|
@ -209,8 +209,12 @@ export function toShareUrl(url: string): string {
|
|||
return url
|
||||
}
|
||||
|
||||
export function isBskyAppUrl(url: string): boolean {
|
||||
return url.startsWith('https://bsky.app/')
|
||||
}
|
||||
|
||||
export function convertBskyAppUrlIfNeeded(url: string): string {
|
||||
if (url.startsWith('https://bsky.app/')) {
|
||||
if (isBskyAppUrl(url)) {
|
||||
try {
|
||||
const urlp = new URL(url)
|
||||
return urlp.pathname
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue