Merge pull request #65 from armchair-philosophy/resolve/#64

[general] support more icon pattern
This commit is contained in:
syuilo⭐️ 2017-05-01 16:50:41 +09:00 committed by GitHub
commit 97fc68fdd8

View File

@ -70,31 +70,37 @@ export default async (url: URL.Url): Promise<Summary> => {
siteName = siteName ? entities.decode(siteName) : null; siteName = siteName ? entities.decode(siteName) : null;
let icon = const favicon =
$('link[rel="shortcut icon"]').attr('href') || $('link[rel="shortcut icon"]').attr('href') ||
$('link[rel="icon"]').attr('href'); $('link[rel="icon"]').attr('href') ||
'/favicon.ico';
if (icon == null) { const checkExistence = (checkURL: string): Promise<string> => new Promise(done => {
const favicon = '/favicon.ico'; request.head(checkURL, (err, res) => {
if (err) {
const foundFavicon = await new Promise(done => { done(null);
request.head(URL.resolve(url.href, favicon), (err, res) => { } else if (res.statusCode == 200) {
if (err) { done(checkURL);
done(false); } else {
} else if (res.statusCode == 200) { done(null);
done(true); }
} else {
done(false);
}
});
}); });
});
if (foundFavicon) { // 相対的なURL (ex. test) を絶対的 (ex. /test) に変換
icon = favicon; 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())) { if (/[\-—\|:]$/.test(title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim())) {
title = title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim(); title = title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim();