From 04a43423d35981587371078400b80c15d8c17854 Mon Sep 17 00:00:00 2001 From: as-op Date: Mon, 16 Sep 2024 16:49:26 +0200 Subject: [PATCH] workaround for missing multiple blank lines support in markdown --- src/commonmark/commonmarkdataprocessor.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/commonmark/commonmarkdataprocessor.js b/src/commonmark/commonmarkdataprocessor.js index 051592c..366ec8b 100644 --- a/src/commonmark/commonmarkdataprocessor.js +++ b/src/commonmark/commonmarkdataprocessor.js @@ -48,7 +48,12 @@ export default class CommonMarkDataProcessor { let taskLists = require('markdown-it-task-lists'); let parser = md.use(taskLists, {label: true}); - const html = parser.render( data ); + const markdown = data + // _htmlDP creates from `
/n` a superfluous blank element `



/n` + // so we create the empty paragraph ourselves + .replace(/
\n/g, '

'); + + const html = parser.render( markdown ) // Convert input HTML data to DOM DocumentFragment. const domFragment = this._htmlDP._toDom( html ); @@ -97,6 +102,8 @@ export default class CommonMarkDataProcessor { codeBlockStyle: 'fenced' } ); + turndownService.keep(['br']) + turndownService.use([ highlightedCodeBlock, ]); @@ -198,11 +205,16 @@ export default class CommonMarkDataProcessor { return (node.nodeName === 'BR') || (node.nodeName === 'P' && node.childNodes.length === 1 && node.childNodes[0].nodeName === 'BR'); }, - replacement: ( _content, node ) => "
", + replacement: ( _content, node ) => '
', }); let turndown = turndownService.turndown( domFragment ); - // Escape non-breaking space characters - return turndown.replace(/\u00A0/, ' '); + return turndown + // Escape non-breaking space characters + .replace(/\u00A0/, ' ') + // turndown compacts `



` to `
\n
\n\n` + // which is not what we need for two (or more) empty lines + .replace(/

/g, '
\n\n
') + } }