diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/extractHtmlMeta.ts | 10 | ||||
-rw-r--r-- | src/lib/extractTwitterMeta.ts | 20 | ||||
-rw-r--r-- | src/lib/link-meta.ts | 1 |
3 files changed, 29 insertions, 2 deletions
diff --git a/src/lib/extractHtmlMeta.ts b/src/lib/extractHtmlMeta.ts index 2517262be..038ca81c6 100644 --- a/src/lib/extractHtmlMeta.ts +++ b/src/lib/extractHtmlMeta.ts @@ -1,15 +1,18 @@ +import {extractTwitterMeta} from './extractTwitterMeta' import {extractYoutubeMeta} from './extractYoutubeMeta' interface ExtractHtmlMetaInput { html: string hostname?: string + pathname?: string } export const extractHtmlMeta = ({ html, hostname, + pathname, }: ExtractHtmlMetaInput): Record<string, string> => { - const htmlTitleRegex = /<title>([^<]+)<\/title>/i + const htmlTitleRegex = /<title.*>([^<]+)<\/title>/i let res: Record<string, string> = {} @@ -56,9 +59,12 @@ export const extractHtmlMeta = ({ const isYoutubeUrl = hostname?.includes('youtube.') || hostname?.includes('youtu.be') + const isTwitterUrl = hostname?.includes('twitter.') + // Workaround for some websites not having a title or description in the meta tags in the initial serve if (isYoutubeUrl) { - // Workaround for Youtube not having a title in the meta tags res = {...res, ...extractYoutubeMeta(html)} + } else if (isTwitterUrl) { + res = {...extractTwitterMeta({pathname})} } return res diff --git a/src/lib/extractTwitterMeta.ts b/src/lib/extractTwitterMeta.ts new file mode 100644 index 000000000..d785903c0 --- /dev/null +++ b/src/lib/extractTwitterMeta.ts @@ -0,0 +1,20 @@ +export const extractTwitterMeta = ({ + pathname, +}: { + pathname: string +}): Record<string, string> => { + const res = {title: 'Twitter'} + const parsedPathname = pathname.split('/') + if (parsedPathname.length <= 1 || parsedPathname[1].length <= 1) { + // Excluding one letter usernames as they're reserved by twitter for things like cases like twitter.com/i/articles/follows/-1675653703 + return res + } + const username = parsedPathname?.[1] + const isUserProfile = parsedPathname?.length === 2 + + res.title = isUserProfile + ? `@${username} on Twitter` + : `Tweet by @${username}` + + return res +} diff --git a/src/lib/link-meta.ts b/src/lib/link-meta.ts index 7e0964c17..2826e969a 100644 --- a/src/lib/link-meta.ts +++ b/src/lib/link-meta.ts @@ -63,6 +63,7 @@ export async function getLinkMeta( const httpResMeta = extractHtmlMeta({ html: httpResBody, hostname: urlp?.hostname, + pathname: urlp?.pathname, }) meta.title = httpResMeta.title ? he.decode(httpResMeta.title) : undefined meta.description = httpResMeta.description |