From 71bb0e382abe470736081933a9c7186a3ce16e11 Mon Sep 17 00:00:00 2001 From: otofune Date: Mon, 1 May 2017 15:27:52 +0900 Subject: [PATCH 1/2] [general] support more icon pattern resolve #64. --- src/general.ts | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/general.ts b/src/general.ts index 67be1cf..84f8c85 100644 --- a/src/general.ts +++ b/src/general.ts @@ -70,31 +70,37 @@ export default async (url: URL.Url): Promise => { siteName = siteName ? entities.decode(siteName) : null; - let icon = + const favicon = $('link[rel="shortcut icon"]').attr('href') || - $('link[rel="icon"]').attr('href'); + $('link[rel="icon"]').attr('href') || + '/favicon.ico' - if (icon == null) { - const favicon = '/favicon.ico'; - - const foundFavicon = await new Promise(done => { - request.head(URL.resolve(url.href, favicon), (err, res) => { - if (err) { - done(false); - } else if (res.statusCode == 200) { - done(true); - } else { - done(false); - } - }); + const checkExistence = (checkURL: string): Promise => new Promise(done => { + request.head(checkURL, (err, res) => { + if (err) { + done(null); + } else if (res.statusCode == 200) { + done(checkURL); + } else { + done(null); + } }); + }); - if (foundFavicon) { - icon = favicon; - } + // 相対的なURL (ex. test) を絶対的 (ex. /test) に変換 + const toAbsolute = (relativeURLString: string): string => { + const relativeURL = URL.parse(relativeURLString); + const isAbsolute = relativeURL.slashes || relativeURL.path.indexOf("/") === 0; + // 既に絶対的なら、即座に値を返却 + if (isAbsolute) return relativeURLString; + // スラッシュを付けて返却 + return "/" + relativeURLString; } - icon = icon ? URL.resolve(url.href, icon) : null; + const icon = await checkExistence(URL.resolve(url.href, favicon)) || + // 相対指定を絶対指定に変換し再試行 + await checkExistence(URL.resolve(url.href, toAbsolute(favicon))) || + null if (/[\-—\|:]$/.test(title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim())) { title = title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim(); From 84f8b3a812db45ace79b7792c26a755dbd03b8dc Mon Sep 17 00:00:00 2001 From: otofune Date: Mon, 1 May 2017 16:45:34 +0900 Subject: [PATCH 2/2] [general] missing semicoron --- src/general.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/general.ts b/src/general.ts index 84f8c85..94cb41a 100644 --- a/src/general.ts +++ b/src/general.ts @@ -73,7 +73,7 @@ export default async (url: URL.Url): Promise => { const favicon = $('link[rel="shortcut icon"]').attr('href') || $('link[rel="icon"]').attr('href') || - '/favicon.ico' + '/favicon.ico'; const checkExistence = (checkURL: string): Promise => new Promise(done => { request.head(checkURL, (err, res) => { @@ -95,7 +95,7 @@ export default async (url: URL.Url): Promise => { if (isAbsolute) return relativeURLString; // スラッシュを付けて返却 return "/" + relativeURLString; - } + }; const icon = await checkExistence(URL.resolve(url.href, favicon)) || // 相対指定を絶対指定に変換し再試行