generated from cp-20/nextjs-with-mantine-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #42
- Loading branch information
Showing
3 changed files
with
63 additions
and
16 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 |
---|---|---|
@@ -1,29 +1,72 @@ | ||
import { Readability } from '@mozilla/readability'; | ||
import { JSDOM } from 'jsdom'; | ||
import { decode } from 'iconv-lite'; | ||
|
||
import type { ArticleResponse } from '@/crawler'; | ||
|
||
export const fetchUsingReadability = async ( | ||
url: string, | ||
): Promise<ArticleResponse | null> => { | ||
const response = await fetch(url); | ||
const html = await response.text(); | ||
const additionalCharset = ['shift_jis', 'euc-jp']; | ||
const additionalCharsetRegex = additionalCharset.map((c) => new RegExp(c, 'i')); | ||
|
||
const dom = new JSDOM(html); | ||
const reader = new Readability(dom.window.document); | ||
const article = reader.parse(); | ||
const detectCharset = (document: Document) => { | ||
const html4 = document | ||
.querySelector('meta[http-equiv="content-type"]') | ||
?.getAttribute('content') | ||
?.match(/charset=(?<charset>.+)/)?.[1]; | ||
const html5 = document | ||
.querySelector('meta[charset]') | ||
?.getAttribute('charset'); | ||
const charset = html4 ?? html5 ?? null; | ||
const isAdditionalCharset = additionalCharsetRegex.map( | ||
(r) => charset !== null && r.test(charset), | ||
); | ||
|
||
const ogImageUrl = | ||
dom.window.document.querySelector<HTMLMetaElement>( | ||
'meta[property="og:image"]', | ||
)?.content ?? null; | ||
return isAdditionalCharset.reduce( | ||
(prev, current, i) => (current ? additionalCharset[i] : prev), | ||
'utf-8', | ||
); | ||
}; | ||
|
||
const parseArticle = ( | ||
url: string, | ||
document: Document, | ||
): ArticleResponse | null => { | ||
const reader = new Readability(document); | ||
const article = reader.parse(); | ||
|
||
if (article === null) return null; | ||
|
||
const ogImageUrl = | ||
document.querySelector<HTMLMetaElement>('meta[property="og:image"]') | ||
?.content ?? null; | ||
|
||
return { | ||
title: article.title, | ||
body: article.textContent, | ||
url, | ||
title: article.title, | ||
body: article.content, | ||
ogImageUrl, | ||
}; | ||
}; | ||
|
||
const decoder = new TextDecoder(); | ||
|
||
export const fetchUsingReadability = async ( | ||
url: string, | ||
): Promise<ArticleResponse | null> => { | ||
const response = await fetch(url); | ||
const arrayBuffer = await response.arrayBuffer(); | ||
const html = decoder.decode(arrayBuffer); | ||
|
||
const dom = new JSDOM(html); | ||
const document = dom.window.document; | ||
|
||
const charset = detectCharset(document); | ||
if (charset !== 'utf-8') { | ||
const html2 = decode(Buffer.from(arrayBuffer), charset); | ||
const dom2 = new JSDOM(html2); | ||
const document2 = dom2.window.document; | ||
|
||
return parseArticle(url, document2); | ||
} | ||
|
||
return parseArticle(url, document); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.