Skip to content

Commit fe7d941

Browse files
author
win
committed
Merge branch 'master' of github.com:winhtaikaung/react-link-preview
2 parents cdac9bd + 0611d7a commit fe7d941

File tree

7 files changed

+13
-20
lines changed

7 files changed

+13
-20
lines changed

lib/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/Amazon/ScrapAmazon.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { ReactTinyLinkType } from '../../ReactTinyLinkTypes'
2-
import { isEmpty, getTitleOfDoc, getAttrOfDocElement, fixRelativeUrls } from '../utils'
2+
import { isEmpty, getTitleOfDoc, getAttrOfDocElement, fixRelativeUrls, getBaseUrl } from '../utils'
33

44
export default async (url, htmlDoc, defaultMedia) => {
5-
let baseUrl = getAttrOfDocElement(htmlDoc, 'base', 'href')
6-
if (!baseUrl) {
7-
baseUrl = url
8-
}
5+
let baseUrl = getBaseUrl(htmlDoc, url)
6+
97
const image = [
108
getAttrOfDocElement(htmlDoc, '.a-dynamic-image', 'data-old-hires'),
119
getAttrOfDocElement(htmlDoc, '.a-dynamic-image', 'src'),

src/rules/Default/ScrapDefault.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { ReactTinyLinkType } from '../../ReactTinyLinkTypes'
2-
import { isEmpty, getTitleOfDoc, getAttrOfDocElement, fixRelativeUrls } from '../utils'
2+
import { isEmpty, getTitleOfDoc, getAttrOfDocElement, fixRelativeUrls, getBaseUrl } from '../utils'
33

44
export default async (url, htmlDoc, defaultMedia) => {
5-
let baseUrl = getAttrOfDocElement(htmlDoc, 'base', 'href')
6-
if (!baseUrl) {
7-
baseUrl = url
8-
}
5+
let baseUrl = getBaseUrl(htmlDoc, url)
96

107
const image = [
118
getAttrOfDocElement(htmlDoc, 'meta[property="og:logo"]', 'content'),

src/rules/Instagram/ScrapInstagram.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import { ReactTinyLinkType } from '../../ReactTinyLinkTypes'
22
import { getTitleOfDoc, getAttrOfDocElement, getInstagramVideo, isEmpty } from '../utils'
33

44
export default async (url: string, htmlDoc, data, defaultMedia) => {
5-
let baseUrl = getAttrOfDocElement(htmlDoc, 'base', 'href')
6-
if (!baseUrl) {
7-
baseUrl = url
8-
}
9-
105
return {
116
title: getTitleOfDoc(htmlDoc),
127
content: getAttrOfDocElement(htmlDoc, "meta[name='description']", 'content'),

src/rules/utils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export declare const getAttrOfDocElement: (htmlDoc: any, query: string, attr: st
1010
export declare const getYoutTubeVideoId: (url: string) => string;
1111
export declare const getInstagramVideo: (htmlDoc: any) => string;
1212
export declare const fixRelativeUrls: (baseUrl: string, itemUrl: string) => string;
13+
export declare const getBaseUrl: (htmlDoc: any, url: string) => any;

src/rules/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,7 @@ export const fixRelativeUrls = (baseUrl: string, itemUrl: string) => {
7777
if (normalizedUrl.startsWith('http://') || normalizedUrl.startsWith('https://')) {
7878
return itemUrl
7979
}
80-
return baseUrl + itemUrl
80+
return new URL(itemUrl, baseUrl).href
8181
}
82+
83+
export const getBaseUrl = (htmlDoc: any, url: string) => getAttrOfDocElement(htmlDoc, 'base', 'href') || new URL(url).origin

0 commit comments

Comments
 (0)