mirror of
https://github.com/docmost/docmost.git
synced 2026-08-24 08:12:15 +10:00
markdown import
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user