fix(server): title in text output
This commit is contained in:
parent
dba42c64b4
commit
1a48066684
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user