omnivore/packages/api/src/utils/substack-handler.ts

34 lines
1 KiB
TypeScript
Raw Normal View History

2022-02-11 17:24:33 +00:00
export class SubstackHandler {
name = 'substack'
shouldPrehandle = (url: URL, dom: Document): boolean => {
2022-02-11 17:24:33 +00:00
const host = this.name + '.com'
// check if url ends with substack.com
// or has a profile image hosted at substack.com
return (
url.hostname.endsWith(host) ||
!!dom
2022-02-11 17:24:33 +00:00
.querySelector('.email-body img')
?.getAttribute('src')
?.includes(host)
)
}
prehandle = (url: URL, dom: Document): Promise<Document> => {
const body = dom.querySelector('.email-body-container')
2022-02-11 17:24:33 +00:00
// this removes header and profile avatar
body?.querySelector('.header')?.remove()
body?.querySelector('.preamble')?.remove()
body?.querySelector('.meta-author-wrap')?.remove()
// this removes meta button
body?.querySelector('.post-meta')?.remove()
// this removes footer
body?.querySelector('.post-cta')?.remove()
body?.querySelector('.container-border')?.remove()
body?.querySelector('.footer')?.remove()
return Promise.resolve(dom)
}
}