markdown import

This commit is contained in:
Salihu
2026-08-09 12:16:48 +01:00
parent add1d7bf61
commit ea012f9430
6 changed files with 240 additions and 20 deletions
@@ -24,6 +24,7 @@ export function htmlToMarkdown(html: string): string {
TurndownPluginGfm.tables,
TurndownPluginGfm.strikethrough,
TurndownPluginGfm.highlightedCodeBlock,
tabs,
taskList,
callout,
preserveDetail,
@@ -38,6 +39,37 @@ export function htmlToMarkdown(html: string): string {
return turndownService.turndown(html).replaceAll('<br>', ' ');
}
function tabs(turndownService: _TurndownService) {
turndownService.addRule('tabs', {
filter: (node: HTMLInputElement) =>
node.nodeName === 'DIV' && node.getAttribute('data-type') === 'tabs',
replacement: (content: string, node: HTMLInputElement) => {
const tabNodes = Array.from(
node.querySelectorAll(':scope > div[data-type="tab"]'),
);
if (tabNodes.length === 0) return content;
const tabBlocks = tabNodes.map((tabNode) => {
const labelNode = tabNode.querySelector(
':scope > div[data-type="tabLabel"]',
);
const panelNode = tabNode.querySelector(
':scope > div[data-type="tabPanel"]',
);
const label = sanitizeTabLabel(labelNode?.textContent || 'Tab');
const panelMarkdown = panelNode
? turndownService.turndown(panelNode.innerHTML).trim()
: '';
return `=== "${label}"\n${indentMarkdownBlock(panelMarkdown)}`;
});
return `\n\n${tabBlocks.join('\n\n')}\n\n`;
},
});
}
function listParagraph(turndownService: _TurndownService) {
turndownService.addRule('paragraph', {
filter: ['p'],
@@ -218,3 +250,19 @@ function video(turndownService: _TurndownService) {
},
});
}
function sanitizeTabLabel(value: string): string {
return value
.replace(/[\r\n]+/g, ' ')
.replace(/"/g, '\\"')
.trim();
}
function indentMarkdownBlock(content: string): string {
if (!content) return ' ';
return content
.split('\n')
.map((line) => (line.trim() ? ` ${line}` : ''))
.join('\n');
}