From 373fc86e4770370634893f63267cd8200f4b874e Mon Sep 17 00:00:00 2001
From: Philipinho <16838612+Philipinho@users.noreply.github.com>
Date: Mon, 22 Jul 2024 14:09:52 +0100
Subject: [PATCH] preserve details tag in markdown export
---
.../src/integrations/export/turndown-utils.ts | 39 ++++++++-----------
1 file changed, 16 insertions(+), 23 deletions(-)
diff --git a/apps/server/src/integrations/export/turndown-utils.ts b/apps/server/src/integrations/export/turndown-utils.ts
index a7cf514c..4bb73fd8 100644
--- a/apps/server/src/integrations/export/turndown-utils.ts
+++ b/apps/server/src/integrations/export/turndown-utils.ts
@@ -18,13 +18,12 @@ export function turndown(html: string): string {
highlightedCodeBlock,
taskList,
callout,
- toggleListTitle,
- toggleListBody,
+ preserveDetail,
listParagraph,
mathInline,
mathBlock,
]);
-
+ console.log(turndownService.turndown(html));
return turndownService.turndown(html).replaceAll('
', ' ');
}
@@ -74,29 +73,23 @@ function taskList(turndownService: TurndownService) {
});
}
-function toggleListTitle(turndownService: TurndownService) {
- turndownService.addRule('toggleListTitle', {
+function preserveDetail(turndownService: TurndownService) {
+ turndownService.addRule('preserveDetail', {
filter: function (node: HTMLInputElement) {
- return (
- node.nodeName === 'SUMMARY' && node.parentNode.nodeName === 'DETAILS'
- );
+ return node.nodeName === 'DETAILS';
},
replacement: function (content: any, node: HTMLInputElement) {
- return '- ' + content;
- },
- });
-}
+ // TODO: preserve summary of nested details
+ const summary = node.querySelector(':scope > summary');
+ let detailSummary = '';
-function toggleListBody(turndownService: TurndownService) {
- turndownService.addRule('toggleListContent', {
- filter: function (node: HTMLInputElement) {
- return (
- node.getAttribute('data-type') === 'detailsContent' &&
- node.parentNode.nodeName === 'DETAILS'
- );
- },
- replacement: function (content: any, node: HTMLInputElement) {
- return ` ${content.replace(/\n/g, '\n ')} `;
+ if (summary) {
+ detailSummary = `${turndownService.turndown(summary.innerHTML)}`;
+ summary.remove();
+ }
+
+ const detailsContent = turndownService.turndown(node.innerHTML);
+ return `\n\n${detailSummary}\n\n${detailsContent}\n\n \n`;
},
});
}
@@ -124,7 +117,7 @@ function mathBlock(turndownService: TurndownService) {
);
},
replacement: function (content: any, node: HTMLInputElement) {
- return `$$${content}$$`;
+ return `\n$$${content}$$\n`;
},
});
}