Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions phpmyfaq/admin/assets/src/utils/flesch-reading-ease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,32 @@ export type SupportedLanguage =

/**
* Strips HTML tags from content to get plain text
*
* This implementation avoids parsing the input as HTML to prevent
* reinterpreting arbitrary text as markup. It removes tag-like
* structures and decodes a small set of common HTML entities.
*/
export const stripHtml = (html: string): string => {
const doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || '';
if (!html) {
return '';
}

// Remove HTML tags
let text = html.replace(/<[^>]*>/g, ' ');

// Decode a few common HTML entities needed for readability
text = text.replace(/&nbsp;/gi, ' ');
text = text.replace(/&lt;/gi, '<');
text = text.replace(/&gt;/gi, '>');
text = text.replace(/&quot;/gi, '"');
text = text.replace(/&#39;/g, "'");
// Decode ampersand last to avoid double-unescaping sequences like "&amp;lt;"
text = text.replace(/&amp;/gi, '&');

// Normalize whitespace
text = text.replace(/\s+/g, ' ').trim();

return text;
};

/**
Expand Down