omnivore/packages/web/lib/textFormatting.ts

53 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
export const timeAgo = (date: string | undefined): string => {
if (!date) {
return ''
}
return dayjs(date).fromNow()
}
export const shouldHideUrl = (url: string): boolean => {
try {
const origin = new URL(url).origin
2025-09-24 10:37:39 +00:00
const hideHosts = ['https://storage.googleapis.com', 'https://omnivore.work']
if (hideHosts.indexOf(origin) != -1) {
return true
}
} catch {
console.log('invalid url item', url)
}
return false
}
2024-08-26 07:11:19 +00:00
const shouldHideSiteName = (siteName: string) => {
2025-09-24 10:37:39 +00:00
const hideNames = ['storage.googleapis.com', 'omnivore.work']
2024-08-26 07:46:58 +00:00
if (hideNames.indexOf(siteName) != -1) {
2024-08-26 07:11:19 +00:00
return true
}
return false
}
export const siteName = (
originalArticleUrl: string,
itemUrl: string,
siteName?: string
): string => {
if (siteName) {
2024-08-26 07:11:19 +00:00
return shouldHideSiteName(siteName) ? '' : siteName
}
if (shouldHideUrl(originalArticleUrl)) {
return ''
}
try {
return new URL(originalArticleUrl).hostname.replace(/^www\./, '')
} catch {}
try {
return new URL(itemUrl).hostname.replace(/^www\./, '')
} catch {}
return ''
}