Skip to content

Commit e9a4305

Browse files
Add explicit imgur domain handler
1 parent d3cc595 commit e9a4305

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/embeds/image_host.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { HTMLElement } from 'node-html-parser';
2+
import { RedditPost } from '../reddit/types';
3+
import { isDefined } from 'remeda';
4+
5+
export async function externalImageEmbed(post: RedditPost, _link: string, head: HTMLElement) {
6+
if (isDefined(post.preview_image_url)) {
7+
head.image(post.preview_image_url, post.resolution?.width, post.resolution?.height, 'large');
8+
}
9+
}

src/html.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare module 'node-html-parser' {
44
interface HTMLElement {
55
meta(propertyName: string, content?: string): HTMLElement;
66

7-
image(url: string, width?: number, height?: number): HTMLElement;
7+
image(url: string, width?: number, height?: number, type?: 'large' | 'thumbnail'): HTMLElement;
88

99
video(url: string, width?: number, height?: number, type?: string): HTMLElement;
1010
}
@@ -17,13 +17,16 @@ HTMLElement.prototype.meta = function (propertyName: string, content?: string):
1717
return node;
1818
};
1919

20-
HTMLElement.prototype.image = function (url: string, width?: number, height?: number): HTMLElement {
20+
HTMLElement.prototype.image = function (url: string, width?: number, height?: number, type?: 'large' | 'thumbnail'): HTMLElement {
2121
this.meta('twitter:image:src', url);
2222
this.meta('og:image', url);
2323
if (width && height) {
2424
this.meta('og:image:width', width.toString());
2525
this.meta('og:image:height', height.toString());
2626
}
27+
if (type === 'large') {
28+
this.meta('twitter:card', 'summary_large_image');
29+
}
2730
return this;
2831
};
2932

src/reddit/compile.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { twitterLinkEmbed } from '../embeds/twitter';
66
import '../html';
77
import { get_packaged_video } from '../util';
88
import { isNonNullish } from 'remeda';
9+
import { externalImageEmbed } from '../embeds/image_host';
910

1011
const imageExtensions = [
1112
'png',
@@ -38,6 +39,11 @@ function getDomainHandler(domain?: string) {
3839
handler: twitterLinkEmbed,
3940
type: 'summary',
4041
};
42+
case 'imgur.com':
43+
return {
44+
handler: externalImageEmbed,
45+
type: 'article',
46+
};
4147
default:
4248
return null;
4349
}
@@ -62,8 +68,7 @@ export async function postToHtml(post: RedditPost): Promise<HTMLElement> {
6268
switch (post.post_hint) {
6369
case 'image':
6470
type = 'photo';
65-
head.meta('twitter:card', 'summary_large_image');
66-
head.image(post.url, post.resolution?.width, post.resolution?.height);
71+
head.image(post.url, post.resolution?.width, post.resolution?.height, 'large');
6772
break;
6873
// case 'rich:video':
6974
case 'hosted:video': {
@@ -115,8 +120,7 @@ export async function postToHtml(post: RedditPost): Promise<HTMLElement> {
115120
} else if (post.url) {
116121
const url = new URL(post.url);
117122
if (isImageUrl(url)) {
118-
head.meta('twitter:card', 'summary_large_image');
119-
head.image(post.url);
123+
head.image(post.url, post.resolution?.width, post.resolution?.height, 'large');
120124
} else if (url.pathname.endsWith('.mp4')) {
121125
head.video(post.url);
122126
}

0 commit comments

Comments
 (0)