Preserve <pre> elements with prism- class and identity them as code blocks

This commit is contained in:
Hongbo Wu 2023-02-14 12:33:59 +08:00
parent 4513b5931b
commit cc8b1cefdb
7 changed files with 3993 additions and 3 deletions

View file

@ -239,7 +239,9 @@ export const parsePreparedContent = async (
// to the handlers, and have some concept of postHandle
if (article?.content) {
const articleDom = parseHTML(article.content).document
const codeBlocks = articleDom.querySelectorAll('code')
const codeBlocks = articleDom.querySelectorAll(
'code, pre[class^="prism-"], pre[class^="language-"]'
)
if (codeBlocks.length > 0) {
codeBlocks.forEach((e) => {
if (e.textContent) {

View file

@ -35,7 +35,7 @@ import { EveryIoHandler } from './newsletters/every-io-handler'
import { EnergyWorldHandler } from './newsletters/energy-world'
import { IndiaTimesHandler } from './newsletters/india-times-handler'
const validateUrlString = (url: string) => {
const validateUrlString = (url: string): boolean => {
const u = new URL(url)
// Make sure the URL is http or https
if (u.protocol !== 'http:' && u.protocol !== 'https:') {
@ -49,6 +49,8 @@ const validateUrlString = (url: string) => {
if (/^(10|172\.16|192\.168)\..*/.test(u.hostname)) {
throw new Error('Invalid URL is private ip')
}
return true
}
const contentHandlers: ContentHandler[] = [

View file

@ -226,7 +226,7 @@ Readability.prototype = {
// These are the classes that readability sets itself.
CLASSES_TO_PRESERVE: [
"page", "twitter-tweet", "tweet-placeholder", "instagram-placeholder", "morning-brew-markets"
"page", "twitter-tweet", "tweet-placeholder", "instagram-placeholder", "morning-brew-markets", "prism-code"
],
// Classes of placeholder elements that can be empty but shouldn't be removed

View file

@ -0,0 +1,12 @@
{
"title": "How to fetch data with React Hooks",
"byline": "Robin Wieruch",
"dir": null,
"excerpt": "A tutorial on how to fetch data in React with Hooks from third-party APIs. You will use state and effect hooks for the data request from a real API ...",
"siteName": null,
"siteIcon": "http://fakehost/favicon-32x32.png?v=9db82c76a9aaf54925ac42d41f3d384c",
"previewImage": "https://www.robinwieruch.de/static/9b13b3546c675d6f1a1e565f5185cab6/9842e/banner.jpg",
"publishedDate": null,
"language": "English",
"readerable": true
}

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
https://www.robinwieruch.de/react-hooks-fetch-data/