mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-24 13:53:50 +00:00

* perf: read file worker * fix: Http node url input * fix: htm2md * fix: html2md * fix: ts * perf: Problem classification increases the matching order * feat: tool response answer
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import TurndownService from 'turndown';
|
|
const domino = require('domino-ext');
|
|
const turndownPluginGfm = require('joplin-turndown-plugin-gfm');
|
|
|
|
export const html2md = (html: string): string => {
|
|
const turndownService = new TurndownService({
|
|
headingStyle: 'atx',
|
|
bulletListMarker: '-',
|
|
codeBlockStyle: 'fenced',
|
|
fence: '```',
|
|
emDelimiter: '_',
|
|
strongDelimiter: '**',
|
|
linkStyle: 'inlined',
|
|
linkReferenceStyle: 'full'
|
|
});
|
|
|
|
try {
|
|
const window = domino.createWindow(html);
|
|
const document = window.document;
|
|
|
|
turndownService.remove(['i', 'script', 'iframe']);
|
|
turndownService.addRule('codeBlock', {
|
|
filter: 'pre',
|
|
replacement(_, node) {
|
|
const content = node.textContent?.trim() || '';
|
|
// @ts-ignore
|
|
const codeName = node?._attrsByQName?.class?.data?.trim() || '';
|
|
|
|
return `\n\`\`\`${codeName}\n${content}\n\`\`\`\n`;
|
|
}
|
|
});
|
|
|
|
turndownService.use(turndownPluginGfm.gfm);
|
|
|
|
return turndownService.turndown(document);
|
|
} catch (error) {
|
|
console.log('html 2 markdown error', error);
|
|
return '';
|
|
}
|
|
};
|