mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-19 10:07:24 +00:00
v4.6.5 (#620)
This commit is contained in:
41
worker/html2md.js
Normal file
41
worker/html2md.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const { parentPort } = require('worker_threads');
|
||||
const TurndownService = require('turndown');
|
||||
const domino = require('domino-ext');
|
||||
const turndownPluginGfm = require('joplin-turndown-plugin-gfm');
|
||||
|
||||
const turndownService = new TurndownService({
|
||||
headingStyle: 'atx',
|
||||
bulletListMarker: '-',
|
||||
codeBlockStyle: 'fenced',
|
||||
fence: '```',
|
||||
emDelimiter: '_',
|
||||
strongDelimiter: '**',
|
||||
linkStyle: 'inlined',
|
||||
linkReferenceStyle: 'full'
|
||||
});
|
||||
parentPort?.on('message', (html) => {
|
||||
const md = html2md(html);
|
||||
|
||||
parentPort.postMessage(md);
|
||||
});
|
||||
|
||||
const html2md = (html) => {
|
||||
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);
|
||||
};
|
Reference in New Issue
Block a user