mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-08-02 22:36:34 +09:00
🍕
This commit is contained in:
@ -1,3 +1,7 @@
|
||||
2.0.3 / unreleased
|
||||
------------------
|
||||
* Improve title cleanuping
|
||||
|
||||
2.0.2 / 2017-05-04
|
||||
------------------
|
||||
* Support more favicon cases #64
|
||||
|
@ -2,8 +2,7 @@ import * as URL from 'url';
|
||||
import * as request from 'request';
|
||||
import nullOrEmpty from './utils/null-or-empty';
|
||||
import clip from './utils/clip';
|
||||
|
||||
const escapeRegExp = require('escape-regexp');
|
||||
import cleanupTitle from './utils/cleanup-title';
|
||||
|
||||
import { AllHtmlEntities } from 'html-entities';
|
||||
const entities = new AllHtmlEntities();
|
||||
@ -104,10 +103,7 @@ export default async (url: URL.Url): Promise<Summary> => {
|
||||
null;
|
||||
|
||||
// Clean up the title
|
||||
if (/[\-—\|:]$/.test(title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim())) {
|
||||
title = title.replace(new RegExp(`${escapeRegExp(siteName)}$`), '').trim();
|
||||
title = title.replace(/[\-—\|:]$/, '').trim();
|
||||
}
|
||||
title = cleanupTitle(title, siteName);
|
||||
|
||||
if (title === '') {
|
||||
title = siteName;
|
||||
|
22
src/utils/cleanup-title.ts
Normal file
22
src/utils/cleanup-title.ts
Normal file
@ -0,0 +1,22 @@
|
||||
const escapeRegExp = require('escape-regexp');
|
||||
|
||||
export default function(title: string, siteName?: string): string {
|
||||
title = title.trim();
|
||||
|
||||
if (siteName) {
|
||||
siteName = siteName.trim();
|
||||
|
||||
const x = escapeRegExp(siteName);
|
||||
|
||||
const patterns = [
|
||||
`^(.+?)\s?[\-\|:・]\s?${x}$`
|
||||
].map(p => new RegExp(p));
|
||||
|
||||
for (let i = 0; i < patterns.length; i++) {
|
||||
const [, match] = patterns[i].exec(title);
|
||||
if (match) return match;
|
||||
}
|
||||
}
|
||||
|
||||
return title;
|
||||
}
|
9
test/htmls/dirty-title.html
Normal file
9
test/htmls/dirty-title.html
Normal file
@ -0,0 +1,9 @@
|
||||
<!doctype html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta property="og:site_name" content="Alice's Site">
|
||||
<title>Strawberry Pasta | Alice's Site</title>
|
||||
</head>
|
||||
</html>
|
@ -23,6 +23,12 @@ process.on('unhandledRejection', console.dir);
|
||||
const port = 3000;
|
||||
const host = `http://localhost:${port}`;
|
||||
|
||||
let server;
|
||||
|
||||
afterEach(() => {
|
||||
server.close();
|
||||
});
|
||||
|
||||
/* tests below */
|
||||
|
||||
it('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', done => {
|
||||
@ -31,10 +37,9 @@ it('faviconがHTML上で指定されていないが、ルートに存在する
|
||||
res.sendFile(__dirname + '/htmls/no-favicon.html');
|
||||
});
|
||||
app.get('/favicon.ico', (_, res) => res.sendStatus(200));
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.icon, `${host}/favicon.ico`);
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -44,10 +49,21 @@ it('faviconがHTML上で指定されていなくて、ルートにも存在し
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/no-favicon.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.icon, null);
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('titleがcleanupされる', done => {
|
||||
const app = express();
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/dirty-title.html');
|
||||
});
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.title, 'Strawberry Pasta');
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -58,10 +74,9 @@ describe('OGP', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/og-title.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.title, 'Strawberry Pasta');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -71,10 +86,9 @@ describe('OGP', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/og-description.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.description, 'Strawberry Pasta');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -84,10 +98,9 @@ describe('OGP', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/og-site_name.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.sitename, 'Strawberry Pasta');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -97,10 +110,9 @@ describe('OGP', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/og-image.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.thumbnail, 'https://himasaku.net/himasaku.png');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -112,10 +124,9 @@ describe('TwitterCard', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/twitter-title.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.title, 'Strawberry Pasta');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -125,10 +136,9 @@ describe('TwitterCard', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/twitter-description.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.description, 'Strawberry Pasta');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -138,10 +148,9 @@ describe('TwitterCard', () => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/htmls/twitter-image.html');
|
||||
});
|
||||
const server = app.listen(port, async () => {
|
||||
server = app.listen(port, async () => {
|
||||
const summary = await summaly(host);
|
||||
assert.equal(summary.thumbnail, 'https://himasaku.net/himasaku.png');
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user