Fix: Markdown formatter removed insertion points#92
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes a critical bug in the Markdown formatter where insertion point markers were being removed during paragraph merging, which caused crashes during template processing. The fix enhances the child element filtering logic to preserve insertion point markers and marked elements alongside Table and Run elements.
Changes:
- Updated dependency versions for DocumentFormat.OpenXml, Markdig, and test framework packages
- Fixed child element filtering in MarkdownFormatter to preserve insertion points and marked elements during paragraph merging
- Added regression test for the crash scenario reported in GitHub issue #89
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| DocxTemplater/DocxTemplater.csproj | Updated DocumentFormat.OpenXml minimum version from 3.3.0 to 3.4.1 |
| DocxTemplater.Markdown/DocxTemplater.Markdown.csproj | Updated Markdig minimum version from 0.43.0 to 0.44.0 |
| DocxTemplater.Test/DocxTemplater.Test.csproj | Updated Microsoft.NET.Test.Sdk to 18.0.1 and NUnit3TestAdapter to 6.1.0, added markdown-crash-example.docx resource |
| DocxTemplater.Markdown/MarkdownFormatter.cs | Enhanced child element filtering to preserve insertion point markers and marked elements during paragraph merging |
| DocxTemplater.Test/GitHubQuestionsExamples.cs | Added MarkdownCrash regression test and reformatted existing test assertion |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| [Test] | ||
| // Issue: https://github.com/Amberg/DocxTemplater/issues/89 | ||
| public void MarkdownCrash() | ||
| { | ||
| using var fileStream = File.OpenRead("Resources/markdown-crash-example.docx"); | ||
| var docTemplate = new DocxTemplate(fileStream); | ||
| var data = new | ||
| { | ||
| var_hastegenargumenten = true, | ||
| var_tegenargumenten = new[] | ||
| { | ||
| new | ||
| { | ||
| nrq_name = "Tegenargument", | ||
| nrq_argument = "BASIS ARGUMENT", | ||
| nrq_response = "SOME RESPONSE", | ||
| nrq_hidedecisiononletter = new | ||
| { | ||
| Label = "Nee", | ||
| Value = false | ||
| }, | ||
| nrq_decision = new | ||
| { | ||
| Label = "Niet geaccepteerd", | ||
| Value = 875810001 | ||
| }, | ||
| nrq_regarding = new | ||
| { | ||
| Label = "Dossier", | ||
| Value = 875810000 | ||
| }, | ||
| nrq_processphase = new | ||
| { | ||
| Label = "Hoorzitting", | ||
| Value = 875810003 | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| docTemplate.RegisterFormatter(new MarkdownFormatter()); | ||
| docTemplate.BindModel("ds", data); | ||
| var result = docTemplate.Process(); | ||
| docTemplate.Validate(); | ||
| Assert.That(result, Is.Not.Null); | ||
| result.SaveAsFileAndOpenInWord(); | ||
|
|
There was a problem hiding this comment.
The test validates that the process completes without throwing an exception and returns a non-null result, but it doesn't verify the actual correctness of the output document. Consider adding assertions that check the document content or structure to ensure the markdown is processed correctly and insertion points are preserved as intended by the fix.
Fix: Fix for: #89
This pull request includes dependency updates, a bugfix for Markdown formatting, and new test coverage for a previously reported crash scenario. The most notable changes are the updates to package versions for improved compatibility and stability, enhancements to how Markdown content is processed, and the addition of a regression test for a specific Markdown crash case.
Dependency updates:
Markdigpackage inDocxTemplater.Markdown.csprojto version 0.44.0 or higher, improving Markdown parsing capabilities.DocumentFormat.OpenXmlpackage inDocxTemplater.csprojto version 3.4.1 or higher for better document handling and bug fixes.DocxTemplater.Test.csproj, includingMicrosoft.NET.Test.SdkandNUnit3TestAdapter, for improved test reliability and compatibility.Markdown formatting and bugfix:
MarkdownFormatter.csto ensure elements with insertion point markers or marked elements are retained during paragraph merging, addressing a Markdown crash scenario.Test coverage:
MarkdownCrash) inGitHubQuestionsExamples.csto reproduce and guard against the crash described in GitHub issue MarkdownFormatter removes insertion points causing processing failure #89, including the relevant.docxresource file. [1] [2] [3]