From de842e51d9a0e133ad08b12276b3f68eef1f261d Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Wed, 1 Nov 2023 19:21:22 -0400 Subject: [PATCH] Convert preformatted blocks My wordpress blog had a lot of
 sections. The conversion to
markdown ignored those. This change did what I wanted, which was to
turn all of these into markdown code blocks. In my case, there was no
language set on any of my sections to carry over into markdown, so
that isn't handled at all.
---
 src/translator.js | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/translator.js b/src/translator.js
index 615781a..dadc0ba 100644
--- a/src/translator.js
+++ b/src/translator.js
@@ -79,6 +79,20 @@ function initTurndownService() {
 		}
 	});
 
+	turndownService.addRule("pre", {
+		filter: (node, options) => {
+			return (
+				options.codeBlockStyle === "fenced" &&
+				node.nodeName === "PRE" &&
+				node.firstChild
+			);
+		},
+		replacement: (content, node) => {
+			const code = node.textContent;
+			return "\n" + "```" + "\n" + code + "\n" + "```" + "\n";
+		}
+	});
+
 	return turndownService;
 }