feat: handle links with no protocol from wikipedia
This commit is contained in:
parent
6cfe4df5d5
commit
9196390a18
|
|
@ -31,6 +31,7 @@ const getMeta = ($: cheerio.Root, metaName: string) => {
|
||||||
const parseUrl = (url?: string, baseUrl?: string) => {
|
const parseUrl = (url?: string, baseUrl?: string) => {
|
||||||
if (!url) return undefined;
|
if (!url) return undefined;
|
||||||
if (!baseUrl || url.startsWith('http')) return url;
|
if (!baseUrl || url.startsWith('http')) return url;
|
||||||
|
if (url.startsWith('//')) return `https:${url}`;
|
||||||
|
|
||||||
return `${baseUrl}${url}`;
|
return `${baseUrl}${url}`;
|
||||||
};
|
};
|
||||||
|
|
@ -92,7 +93,7 @@ const getLinkPreview = async (url: string, options?: ILinkPreviewerOptions) => {
|
||||||
: ('error' as 'error'),
|
: ('error' as 'error'),
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await fetch(fetchUrl, fetchOptions).catch(e => {
|
const response = await fetch(fetchUrl, fetchOptions).catch((e) => {
|
||||||
if (e.name === 'AbortError') {
|
if (e.name === 'AbortError') {
|
||||||
throw new Error('Request timeout');
|
throw new Error('Request timeout');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import getLinkPreview, { ILinkPreviewInfo } from '../src';
|
||||||
const youtubeSample: ILinkPreviewInfo = {
|
const youtubeSample: ILinkPreviewInfo = {
|
||||||
description:
|
description:
|
||||||
'How much do we know about the impact of technologies we use everyday? How much the web industry is responsible for carbon emissions? Can we define an ethic d...',
|
'How much do we know about the impact of technologies we use everyday? How much the web industry is responsible for carbon emissions? Can we define an ethic d...',
|
||||||
favicon: 'https://www.youtube.com/s/desktop/ce262d3b/img/favicon.ico',
|
favicon: 'https://www.youtube.com/s/desktop/1422277c/img/favicon.ico',
|
||||||
image: 'https://i.ytimg.com/vi/feH26j3rBz8/maxresdefault.jpg',
|
image: 'https://i.ytimg.com/vi/feH26j3rBz8/maxresdefault.jpg',
|
||||||
imageHeight: 720,
|
imageHeight: 720,
|
||||||
imageWidth: 1280,
|
imageWidth: 1280,
|
||||||
|
|
@ -35,6 +35,30 @@ const rawmaterialSample: ILinkPreviewInfo = {
|
||||||
videos: [],
|
videos: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const wikipediaSample: ILinkPreviewInfo = {
|
||||||
|
description: undefined,
|
||||||
|
favicon: 'https://en.wikipedia.org/static/favicon/wikipedia.ico',
|
||||||
|
image:
|
||||||
|
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Plone_5.2.png/1200px-Plone_5.2.png',
|
||||||
|
imageHeight: 1423,
|
||||||
|
imageWidth: 1200,
|
||||||
|
images: [
|
||||||
|
'https://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Plone-logo.svg/121px-Plone-logo.svg.png',
|
||||||
|
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Plone_5.2.png/300px-Plone_5.2.png',
|
||||||
|
'https://upload.wikimedia.org/wikipedia/en/thumb/8/8a/OOjs_UI_icon_edit-ltr-progressive.svg/10px-OOjs_UI_icon_edit-ltr-progressive.svg.png',
|
||||||
|
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Free_and_open-source_software_logo_%282009%29.svg/28px-Free_and_open-source_software_logo_%282009%29.svg.png',
|
||||||
|
'https://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/30px-Commons-logo.svg.png',
|
||||||
|
'https://upload.wikimedia.org/wikipedia/en/thumb/8/8a/OOjs_UI_icon_edit-ltr-progressive.svg/10px-OOjs_UI_icon_edit-ltr-progressive.svg.png',
|
||||||
|
'https://en.wikipedia.org/static/images/footer/wikimedia-button.png',
|
||||||
|
'https://en.wikipedia.org/static/images/footer/poweredby_mediawiki_88x31.png',
|
||||||
|
],
|
||||||
|
mediaType: 'website',
|
||||||
|
siteName: undefined,
|
||||||
|
title: 'Plone (software) - Wikipedia',
|
||||||
|
video: undefined,
|
||||||
|
videos: [],
|
||||||
|
};
|
||||||
|
|
||||||
it('gets correct info for RawMaterial website', async () => {
|
it('gets correct info for RawMaterial website', async () => {
|
||||||
const info = await getLinkPreview('https://rawmaterial.it/en');
|
const info = await getLinkPreview('https://rawmaterial.it/en');
|
||||||
expect(info).toEqual(rawmaterialSample);
|
expect(info).toEqual(rawmaterialSample);
|
||||||
|
|
@ -46,3 +70,10 @@ it('gets correct info for YouTube video link', async () => {
|
||||||
);
|
);
|
||||||
expect(info).toEqual(youtubeSample);
|
expect(info).toEqual(youtubeSample);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('gets correct info for Wikipedia url', async () => {
|
||||||
|
const info = await getLinkPreview(
|
||||||
|
'https://en.wikipedia.org/wiki/Plone_(software)'
|
||||||
|
);
|
||||||
|
expect(info).toEqual(wikipediaSample);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue