Skip to content

Commit 262f41b

Browse files
LLMO-2804: [Readability] Detect phase: exclude unwanted content
1 parent 6fafd33 commit 262f41b

2 files changed

Lines changed: 16 additions & 23 deletions

File tree

src/readability/preflight/handler.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,8 @@ export default async function readability(context, auditContext) {
201201
// Helper function to calculate readability score and create audit opportunity
202202
const analyzeReadability = async (text, element, elementIndex) => {
203203
try {
204-
const normalized = normalizeReadabilityText(text);
205-
206204
// Check if text is in a supported language before analyzing readability
207-
const detectedLanguage = getSupportedLanguage(normalized);
205+
const detectedLanguage = getSupportedLanguage(text);
208206
if (!detectedLanguage) {
209207
return; // Skip unsupported languages
210208
}
@@ -215,18 +213,18 @@ export default async function readability(context, auditContext) {
215213
// Use text-readability library for English, custom function for other languages
216214
let readabilityScore;
217215
if (detectedLanguage === 'english') {
218-
readabilityScore = rs.fleschReadingEase(normalized);
216+
readabilityScore = rs.fleschReadingEase(text);
219217
} else {
220-
readabilityScore = await calculateReadabilityScore(normalized, detectedLanguage);
218+
readabilityScore = await calculateReadabilityScore(text, detectedLanguage);
221219
}
222220

223221
if (readabilityScore < TARGET_READABILITY_SCORE) {
224222
poorReadabilityCount += 1;
225223

226224
// Truncate text for display
227-
const displayText = normalized.length > MAX_CHARACTERS_DISPLAY
228-
? `${normalized.substring(0, MAX_CHARACTERS_DISPLAY)}...`
229-
: normalized;
225+
const displayText = text.length > MAX_CHARACTERS_DISPLAY
226+
? `${text.substring(0, MAX_CHARACTERS_DISPLAY)}...`
227+
: text;
230228

231229
const issueText = `Text element is difficult to read: "${displayText}"`;
232230

@@ -238,7 +236,7 @@ export default async function readability(context, auditContext) {
238236
fleschReadingEase: readabilityScore,
239237
language: detectedLanguage,
240238
seoRecommendation: 'Improve readability by using shorter sentences, simpler words, and clearer structure',
241-
textContent: normalized, // Store normalized text for AI processing
239+
textContent: text, // Store normalized text for AI processing
242240
...toElementTargets(selector),
243241
});
244242
}
@@ -302,7 +300,7 @@ export default async function readability(context, auditContext) {
302300
return tempDiv.text();
303301
})
304302
.map((p) => normalizeReadabilityText(p))
305-
.filter((p) => p.length >= MIN_TEXT_LENGTH);
303+
.filter((p) => p.length >= MIN_TEXT_LENGTH && /\s/.test(p));
306304

307305
// Add promises for each paragraph
308306
paragraphs.forEach((paragraph) => {

src/readability/shared/analysis-utils.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,8 @@ async function analyzeTextReadability(
124124
scrapedAt,
125125
) {
126126
try {
127-
const normalized = normalizeReadabilityText(text);
128-
129127
// Check if text is in a supported language
130-
const detectedLanguage = getSupportedLanguage(normalized);
128+
const detectedLanguage = getSupportedLanguage(text);
131129
if (!detectedLanguage) {
132130
return null; // Skip unsupported languages
133131
}
@@ -138,30 +136,30 @@ async function analyzeTextReadability(
138136
// Calculate readability score
139137
let readabilityScore;
140138
if (detectedLanguage === 'english') {
141-
readabilityScore = rs.fleschReadingEase(normalized);
139+
readabilityScore = rs.fleschReadingEase(text);
142140
} else {
143-
readabilityScore = await calculateReadabilityScore(normalized, detectedLanguage);
141+
readabilityScore = await calculateReadabilityScore(text, detectedLanguage);
144142
}
145143

146144
// Check if readability is poor
147145
if (readabilityScore < TARGET_READABILITY_SCORE) {
148146
// Truncate text for display
149-
const displayText = normalized.length > MAX_CHARACTERS_DISPLAY
150-
? `${normalized.substring(0, MAX_CHARACTERS_DISPLAY)}...`
151-
: normalized;
147+
const displayText = text.length > MAX_CHARACTERS_DISPLAY
148+
? `${text.substring(0, MAX_CHARACTERS_DISPLAY)}...`
149+
: text;
152150

153151
// Calculate priority rank
154152
const trafficWeight = traffic || 0;
155153
const readabilityWeight = TARGET_READABILITY_SCORE - readabilityScore;
156-
const contentLengthWeight = Math.min(normalized.length, 1000) / 1000;
154+
const contentLengthWeight = Math.min(text.length, 1000) / 1000;
157155
const rank = (readabilityWeight * 0.5) + (trafficWeight * 0.0001)
158156
+ (contentLengthWeight * 0.1);
159157

160158
return {
161159
pageUrl,
162160
scrapedAt,
163161
selector,
164-
textContent: normalized,
162+
textContent: text,
165163
displayText,
166164
fleschReadingEase: Math.round(readabilityScore * 100) / 100,
167165
language: detectedLanguage,
@@ -201,9 +199,6 @@ const getMeaningfulElementsForReadability = ($) => {
201199
});
202200
};
203201

204-
/**
205-
* Analyzes readability for a single page's content
206-
*/
207202
/**
208203
* Analyzes the readability of HTML page content and returns an array of readability issue objects
209204
* for text elements with poor readability.

0 commit comments

Comments
 (0)