From 1a480666848c1c6cd778b9535b5610fb8cd64240 Mon Sep 17 00:00:00 2001 From: Artemy Date: Tue, 14 May 2024 13:11:59 +0300 Subject: [PATCH] fix(server): title in text output --- packages/server/src/distributor.ts | 12 ++++++++---- packages/server/src/utils/html2text.ts | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/server/src/distributor.ts b/packages/server/src/distributor.ts index e92d11f..68065a3 100644 --- a/packages/server/src/distributor.ts +++ b/packages/server/src/distributor.ts @@ -75,13 +75,17 @@ export class Distributor { const purify = DOMPurify(dom); const content = purify.sanitize(dom.document.toString()); + const title = output.title || dom.document.title; + const lang = output.lang || dom.document.documentElement.lang; + const textContent = + html2text(output, dom.document, title) || + 'Text output cannot be generated.'; return { content, - textContent: - html2text(output, dom.document) || 'Text output cannot be generated.', - title: output.title || dom.document.title, - lang: output.lang || dom.document.documentElement.lang, + textContent, + title, + lang, }; } diff --git a/packages/server/src/utils/html2text.ts b/packages/server/src/utils/html2text.ts index 4f2471b..270e279 100644 --- a/packages/server/src/utils/html2text.ts +++ b/packages/server/src/utils/html2text.ts @@ -1,9 +1,14 @@ import { EngineOutput } from '@txtdot/sdk/dist/types/handler'; import config from '../config'; -export function html2text(output: EngineOutput, doc: Document) { +function setTitle(body: string | null, title: string) { + if (!body) return null; + return `${title.toUpperCase()}\n${'='.repeat(title.length)}\n\n${body}`; +} + +export function html2text(output: EngineOutput, doc: Document, title: string) { if (output.textContent) return output.textContent; else if (config.plugin.html2text) - return config.plugin.html2text(output.content); - else return doc.documentElement.textContent; + return setTitle(config.plugin.html2text(output.content), title); + else return setTitle(doc.documentElement.textContent, title); }