-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9266ab
commit 08a69ed
Showing
6 changed files
with
272 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { badRequest, createApiRoute } from "@/lib/api-route"; | ||
import articleTitle from "@/lib/title" | ||
export const GET = createApiRoute(async (request) => { | ||
const url = new URL(request.url); | ||
const remote_url = url.searchParams.get("url")!; | ||
const response = await fetch(remote_url) | ||
const body = await response.text() | ||
const title: string = articleTitle(body) | ||
return title | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// https://github.com/sindresorhus/article-title/blob/main/index.js | ||
// slightly modified because import style | ||
// MIT license | ||
import * as cheerio from 'cheerio'; | ||
|
||
const matchers = [ | ||
'.instapaper_title', | ||
'article h1', | ||
'.entry-content h1', | ||
'.markdown-body h1', | ||
'.entry-title', | ||
'.post-title', | ||
'.pageTitle', | ||
'.post_title', | ||
'.headline h1', | ||
'.headline', | ||
'.story h1', | ||
'.entry-header h1', | ||
'.news_title', | ||
'#page-post h1', | ||
'.postheader h1', | ||
'.postheader h2', | ||
'.type-post h1', | ||
]; | ||
|
||
const clean = string => string.replace(/\r?\n/g, '').replace(/\s+/g, ' ').trim(); | ||
|
||
const findSelectorMatch = $ => { | ||
for (const matcher of matchers) { | ||
const element = $(matcher).first().text().trim(); | ||
|
||
if (element && element.length > 0) { | ||
return element; | ||
} | ||
} | ||
}; | ||
|
||
export default function articleTitle(html) { | ||
const $ = cheerio.load(html); | ||
|
||
let documentTitle = $('title').text().replace(/\r?\n/g, ''); | ||
documentTitle = (/^[^|\-/•—]+/.exec(documentTitle) || [])[0] || documentTitle; | ||
documentTitle = (((documentTitle || '').match(/:(?<documentTitle>.*)/) || []).groups || '').documentTitle || documentTitle; | ||
documentTitle = (documentTitle || '').trim(); | ||
|
||
let title = documentTitle; | ||
const heading = findSelectorMatch($); | ||
|
||
if (heading && heading.length > 5 && heading.length < 100) { | ||
title = heading; | ||
} | ||
|
||
return clean(title); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.