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 purify = DOMPurify(dom);
|
||||||
const content = purify.sanitize(dom.document.toString());
|
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 {
|
return {
|
||||||
content,
|
content,
|
||||||
textContent:
|
textContent,
|
||||||
html2text(output, dom.document) || 'Text output cannot be generated.',
|
title,
|
||||||
title: output.title || dom.document.title,
|
lang,
|
||||||
lang: output.lang || dom.document.documentElement.lang,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
import { EngineOutput } from '@txtdot/sdk/dist/types/handler';
|
import { EngineOutput } from '@txtdot/sdk/dist/types/handler';
|
||||||
import config from '../config';
|
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;
|
if (output.textContent) return output.textContent;
|
||||||
else if (config.plugin.html2text)
|
else if (config.plugin.html2text)
|
||||||
return config.plugin.html2text(output.content);
|
return setTitle(config.plugin.html2text(output.content), title);
|
||||||
else return doc.documentElement.textContent;
|
else return setTitle(doc.documentElement.textContent, title);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user