sw/qa/filter/md/md.cxx        |   22 ++++++++++++++++++++++
 sw/source/filter/md/wrtmd.cxx |    4 +++-
 2 files changed, 25 insertions(+), 1 deletion(-)

New commits:
commit f63c14c9e45d795c6d5927103181c8d0ede8a34b
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Mon Sep 15 08:49:00 2025 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Mon Sep 15 12:44:01 2025 +0200

    sw markdown export: handle line breaks
    
    Export the bugdoc as markdown, the line break is lost when opening the
    result with a markdown viewer.
    
    <https://spec.commonmark.org/0.31.2/#hard-line-breaks> says the markup
    to be used is a normal line break that "is preceded by two or more
    spaces", which explains why just writing the newline character as-is
    wasn't enough.
    
    Fix the problem in OutEscapedChars() where we can emit the "escaped"
    line break as a special case.
    
    The alternative markup would be a backslash at the end of the line,
    which is more readable, but seems that's less supported by parsers (e.g.
    okular and https://markdowntohtml.com/ doesn't support it).
    
    Change-Id: Iedabe7ef2b42027f90595bf76e952253141d9b3f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190951
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/sw/qa/filter/md/md.cxx b/sw/qa/filter/md/md.cxx
index 1de912e55ed8..1290fc0786b1 100644
--- a/sw/qa/filter/md/md.cxx
+++ b/sw/qa/filter/md/md.cxx
@@ -566,6 +566,28 @@ CPPUNIT_TEST_FIXTURE(Test, testImageLinkMdExport)
     CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testNewlineMdExport)
+{
+    // Given a document with a line break:
+    createSwDoc();
+    SwDocShell* pDocShell = getSwDocShell();
+    SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
+    pWrtShell->Insert(u"A"_ustr);
+    pWrtShell->InsertLineBreak();
+    pWrtShell->Insert(u"B"_ustr);
+
+    // When saving that to markdown:
+    save(mpFilter);
+
+    std::string aActual = TempFileToString();
+    std::string aExpected("A  " SAL_NEWLINE_STRING "B" SAL_NEWLINE_STRING);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: A  
B
+    // - Actual  : A
B
+    // i.e. the line break was lost.
+    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/sw/source/filter/md/wrtmd.cxx b/sw/source/filter/md/wrtmd.cxx
index d8764a9aa283..623422c5e524 100644
--- a/sw/source/filter/md/wrtmd.cxx
+++ b/sw/source/filter/md/wrtmd.cxx
@@ -445,7 +445,9 @@ void OutEscapedChars(SwMDWriter& rWrt, std::u16string_view 
chars)
             case CH_TXT_TRACKED_DUMMY_CHAR:
                 break;
 
-                // TODO: line breaks
+            case '
':
+                rWrt.Strm().WriteUnicodeOrByteText(u"  " SAL_NEWLINE_STRING);
+                break;
 
             case '\':
             case '`':

Reply via email to