mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-06-05 21:07:26 +09:00
Refactoring
This commit is contained in:
parent
c0de302d85
commit
fc7ef6d143
@ -75,12 +75,13 @@ export default async (url: URL.Url): Promise<Summary> => {
|
|||||||
$('link[rel="icon"]').attr('href') ||
|
$('link[rel="icon"]').attr('href') ||
|
||||||
'/favicon.ico';
|
'/favicon.ico';
|
||||||
|
|
||||||
const checkExistence = (checkURL: string): Promise<string> => new Promise(done => {
|
const find = (path: string) => new Promise<string>(done => {
|
||||||
request.head(checkURL, (err, res) => {
|
const target = URL.resolve(url.href, path);
|
||||||
|
request.head(target, (err, res) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
done(null);
|
done(null);
|
||||||
} else if (res.statusCode == 200) {
|
} else if (res.statusCode == 200) {
|
||||||
done(checkURL);
|
done(target);
|
||||||
} else {
|
} else {
|
||||||
done(null);
|
done(null);
|
||||||
}
|
}
|
||||||
@ -90,18 +91,19 @@ export default async (url: URL.Url): Promise<Summary> => {
|
|||||||
// 相対的なURL (ex. test) を絶対的 (ex. /test) に変換
|
// 相対的なURL (ex. test) を絶対的 (ex. /test) に変換
|
||||||
const toAbsolute = (relativeURLString: string): string => {
|
const toAbsolute = (relativeURLString: string): string => {
|
||||||
const relativeURL = URL.parse(relativeURLString);
|
const relativeURL = URL.parse(relativeURLString);
|
||||||
const isAbsolute = relativeURL.slashes || relativeURL.path.indexOf("/") === 0;
|
const isAbsolute = relativeURL.slashes || relativeURL.path[0] === '/';
|
||||||
// 既に絶対的なら、即座に値を返却
|
// 既に絶対的なら、即座に値を返却
|
||||||
if (isAbsolute) return relativeURLString;
|
if (isAbsolute) return relativeURLString;
|
||||||
// スラッシュを付けて返却
|
// スラッシュを付けて返却
|
||||||
return "/" + relativeURLString;
|
return '/' + relativeURLString;
|
||||||
};
|
};
|
||||||
|
|
||||||
const icon = await checkExistence(URL.resolve(url.href, favicon)) ||
|
const icon = await find(favicon) ||
|
||||||
// 相対指定を絶対指定に変換し再試行
|
// 相対指定を絶対指定に変換し再試行
|
||||||
await checkExistence(URL.resolve(url.href, toAbsolute(favicon))) ||
|
await find(toAbsolute(favicon)) ||
|
||||||
null
|
null;
|
||||||
|
|
||||||
|
// Clean up the title
|
||||||
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();
|
||||||
title = title.replace(/[\-—\|:]$/, '').trim();
|
title = title.replace(/[\-—\|:]$/, '').trim();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user