From c34b56bc50977ea7bc5e38965485d57713c08ebf Mon Sep 17 00:00:00 2001 From: Miodec Date: Tue, 15 Nov 2022 13:01:19 +0100 Subject: [PATCH] added a function to convert html to text --- frontend/src/ts/test/wikipedia.ts | 2 +- frontend/src/ts/utils/misc.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/frontend/src/ts/test/wikipedia.ts b/frontend/src/ts/test/wikipedia.ts index cff00fb62..af993ad41 100644 --- a/frontend/src/ts/test/wikipedia.ts +++ b/frontend/src/ts/test/wikipedia.ts @@ -105,7 +105,7 @@ export async function getSection(language: string): Promise
{ sectionText = sectionText.replace(/<\/p>

+/g, " "); // Convert HTML to text - sectionText = $("

").html(sectionText).text(); + sectionText = Misc.htmlToText(sectionText); // Remove reference links sectionText = sectionText.replace(/\[\d+\]/gi, ""); diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index 50945bc6c..8d6cd25a4 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -1300,3 +1300,9 @@ export function areUnsortedArraysEqual(a: unknown[], b: unknown[]): boolean { export function areSortedArraysEqual(a: unknown[], b: unknown[]): boolean { return a.length === b.length && a.every((v, i) => v === b[i]); } + +export function htmlToText(html: string): string { + const el = document.createElement("div"); + el.innerHTML = html; + return el.textContent || el.innerText || ""; +}