sw/qa/filter/md/md.cxx | 22 ++++++++++++++++++++++ sw/source/filter/md/wrtmd.cxx | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-)
New commits: commit 681781bf5d99ebcbbd90418582eacb0648c5f970 Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Mon Sep 15 08:49:00 2025 +0200 Commit: Caolán McNamara <caolan.mcnam...@collabora.com> CommitDate: Mon Sep 15 09:42:39 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/+/190940 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com> diff --git a/sw/qa/filter/md/md.cxx b/sw/qa/filter/md/md.cxx index 5123f9172ecd..bb6210cb7039 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 36a28b24289d..38959584adf5 100644 --- a/sw/source/filter/md/wrtmd.cxx +++ b/sw/source/filter/md/wrtmd.cxx @@ -426,7 +426,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 '`':