From 64192c728ce81986f16fc8e8cc659a5f5ac45cdb Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 14 Feb 2025 18:45:10 +0100 Subject: [PATCH 1/5] Remove UseSoftlineBreakAsHardlineBreak from MarkdownParser This change ensures that soft line breaks are no longer treated as hard line breaks during markdown parsing. It enhances compatibility with standard markdown behavior . --- src/Elastic.Markdown/Myst/MarkdownParser.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Elastic.Markdown/Myst/MarkdownParser.cs b/src/Elastic.Markdown/Myst/MarkdownParser.cs index a2e124cfe..72b748461 100644 --- a/src/Elastic.Markdown/Myst/MarkdownParser.cs +++ b/src/Elastic.Markdown/Myst/MarkdownParser.cs @@ -71,7 +71,6 @@ public static MarkdownPipeline Pipeline .UseDiagnosticLinks() .UseHeadingsWithSlugs() .UseEmphasisExtras(EmphasisExtraOptions.Default) - .UseSoftlineBreakAsHardlineBreak() .UseSubstitution() .UseComments() .UseYamlFrontMatter() From 8052d1d7ba1a05f4cd67d085fca47ed6446d615a Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 14 Feb 2025 18:52:07 +0100 Subject: [PATCH 2/5] update tests --- tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs | 6 +++--- tests/authoring/Container/DefinitionLists.fs | 4 ++-- tests/authoring/Inline/Substitutions.fs | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs b/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs index 655582335..14d6cb0d0 100644 --- a/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs +++ b/tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs @@ -23,7 +23,7 @@ not a comment [Fact] public void ReplacesSubsFromFrontMatter() => Html.Should().Contain( - """Hello World!
""" + """Hello World!""" ).And.Contain( """not a comment""" ) @@ -52,7 +52,7 @@ not a {substitution} [Fact] public void PreservesSingleBracket() => Html.Should().Contain( - """Hello World!
""" + """Hello World!""" ).And.Contain( """not a comment""" ) @@ -108,7 +108,7 @@ public class SupportsSubstitutionsFromDocSet(ITestOutputHelper output) : InlineT [Fact] public void EmitsGlobalVariable() => - Html.Should().Contain("Hello World!
") + Html.Should().Contain("Hello World!") .And.NotContain("{{hello-world}}") .And.Contain("A variable from docset.yml") .And.NotContain("{{global-var}}"); diff --git a/tests/authoring/Container/DefinitionLists.fs b/tests/authoring/Container/DefinitionLists.fs index c03c06a9a..0e6b5cf82 100644 --- a/tests/authoring/Container/DefinitionLists.fs +++ b/tests/authoring/Container/DefinitionLists.fs @@ -42,12 +42,12 @@ This is my `definition` """ [] - let ``validate HTML 2`` () = + let ``validate HTML`` () = markdown |> convertsToHtml """
This is my definition
-

And this is the definition body
+

And this is the definition body Which may contain multiple lines

Note

diff --git a/tests/authoring/Inline/Substitutions.fs b/tests/authoring/Inline/Substitutions.fs index ef128c249..d6c53e6d8 100644 --- a/tests/authoring/Inline/Substitutions.fs +++ b/tests/authoring/Inline/Substitutions.fs @@ -19,7 +19,7 @@ not a comment [] let ``validate HTML: replace substitution`` () = markdown |> convertsToHtml """ -

The following should be subbed: Hello World!
+

The following should be subbed: Hello World! not a comment

""" @@ -44,8 +44,8 @@ not a {substitution} [] let ``validate HTML: leaves non subs alone`` () = markdown |> convertsToHtml """ -

The following should be subbed: Hello World!
- not a comment
- not a {{valid-key}}
+

The following should be subbed: Hello World! + not a comment + not a {{valid-key}} not a {substitution}

""" From 8cb22ed8c368b61f076d609492aaebe0d728c26a Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 14 Feb 2025 19:03:23 +0100 Subject: [PATCH 3/5] Add support for soft line endings in Markdown parser Introduces a new `soft_line_endings` configuration option to control line break behavior in Markdown parsing. When enabled, soft line endings are converted to hard HTML breaks (`
`). Updated relevant parser logic and documentation to reflect this addition. --- docs/configure/content-set/navigation.md | 6 ++ .../IO/Configuration/ConfigurationFile.cs | 4 + .../Myst/Directives/DirectiveHtmlRenderer.cs | 4 +- src/Elastic.Markdown/Myst/MarkdownParser.cs | 77 ++++++++++++++----- 4 files changed, 69 insertions(+), 22 deletions(-) diff --git a/docs/configure/content-set/navigation.md b/docs/configure/content-set/navigation.md index 09b841e82..c2bd4ae4b 100644 --- a/docs/configure/content-set/navigation.md +++ b/docs/configure/content-set/navigation.md @@ -8,6 +8,7 @@ Example: ```yaml project: 'PROJECT_NAME' +soft_line_endings: true external_hosts: - EXTERNAL_LINKS_HERE @@ -41,6 +42,11 @@ Example: project: 'APM Java agent reference' ``` +### `soft_line_endings` + +Optional key. Defaults to `false`. When enabled turns soft line endings in the markdown to hard HTML breaks `
`. + + ### `external_hosts` All links to external hosts must be declared in this section of `docset.yml`. diff --git a/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs b/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs index 6e8f24186..a2bd8a5aa 100644 --- a/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs +++ b/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs @@ -18,6 +18,7 @@ public record ConfigurationFile : DocumentationFile private readonly int _depth; public string? Project { get; } public Glob[] Exclude { get; } = []; + public bool SoftLineEndings { get; } public string[] CrossLinkRepositories { get; } = []; @@ -69,6 +70,9 @@ public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildCon case "project": Project = ReadString(entry); break; + case "soft_line_endings": + SoftLineEndings = bool.TryParse(ReadString(entry), out var softLineEndings) && softLineEndings; + break; case "exclude": Exclude = ReadStringArray(entry) .Select(Glob.Parse) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index 9fb5155fc..8ed999bb2 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -230,7 +230,7 @@ private void WriteIncludeBlock(HtmlRenderer renderer, IncludeBlock block) block.Configuration, block.LinksResolver); var file = block.FileSystem.FileInfo.New(block.IncludePath); var document = parser.ParseAsync(file, block.FrontMatter, default).GetAwaiter().GetResult(); - var html = document.ToHtml(MarkdownParser.Pipeline); + var html = document.ToHtml(parser.Pipeline); renderer.Write(html); //var slice = Include.Create(new IncludeViewModel { Html = html }); //RenderRazorSlice(slice, renderer, block); @@ -271,7 +271,7 @@ private void WriteSettingsBlock(HtmlRenderer renderer, SettingsBlock block) RenderMarkdown = s => { var document = parser.Parse(s, block.IncludeFrom, block.FrontMatter); - var html = document.ToHtml(MarkdownParser.Pipeline); + var html = document.ToHtml(parser.Pipeline); return html; } }); diff --git a/src/Elastic.Markdown/Myst/MarkdownParser.cs b/src/Elastic.Markdown/Myst/MarkdownParser.cs index 72b748461..1517b6ad7 100644 --- a/src/Elastic.Markdown/Myst/MarkdownParser.cs +++ b/src/Elastic.Markdown/Myst/MarkdownParser.cs @@ -57,32 +57,69 @@ public static MarkdownPipeline MinimalPipeline } // ReSharper disable once InconsistentNaming - private static MarkdownPipeline? _pipeline; - public static MarkdownPipeline Pipeline + private static MarkdownPipeline? _pipelineSoft; + private static MarkdownPipeline PipelineSoft + { + get + { + if (_pipelineSoft is not null) + return _pipelineSoft; + + var builder = CreateDefaultBuilder() + .UseSoftlineBreakAsHardlineBreak(); + + _pipelineSoft = builder.Build(); + return _pipelineSoft; + } + } + // ReSharper disable once InconsistentNaming + private static MarkdownPipeline? _pipelineHard; + private static MarkdownPipeline PipelineHard + { + get + { + if (_pipelineHard is not null) + return _pipelineHard; + + var builder = CreateDefaultBuilder(); + + _pipelineHard = builder.Build(); + return _pipelineHard; + } + } + + private static MarkdownPipelineBuilder CreateDefaultBuilder() + { + var builder = new MarkdownPipelineBuilder() + .UseInlineAnchors() + .UsePreciseSourceLocation() + .UseDiagnosticLinks() + .UseHeadingsWithSlugs() + .UseEmphasisExtras(EmphasisExtraOptions.Default) + .UseSubstitution() + .UseComments() + .UseYamlFrontMatter() + .UseGridTables() + .UsePipeTables() + .UseDirectives() + .UseDefinitionLists() + .UseEnhancedCodeBlocks() + .DisableHtml() + .UseHardBreaks(); + builder.BlockParsers.TryRemove(); + return builder; + } + + // ReSharper disable once InconsistentNaming + private MarkdownPipeline? _pipeline; + public MarkdownPipeline Pipeline { get { if (_pipeline is not null) return _pipeline; - var builder = new MarkdownPipelineBuilder() - .UseInlineAnchors() - .UsePreciseSourceLocation() - .UseDiagnosticLinks() - .UseHeadingsWithSlugs() - .UseEmphasisExtras(EmphasisExtraOptions.Default) - .UseSubstitution() - .UseComments() - .UseYamlFrontMatter() - .UseGridTables() - .UsePipeTables() - .UseDirectives() - .UseDefinitionLists() - .UseEnhancedCodeBlocks() - .DisableHtml() - .UseHardBreaks(); - builder.BlockParsers.TryRemove(); - _pipeline = builder.Build(); + _pipeline = Configuration.SoftLineEndings ? PipelineSoft : PipelineHard; return _pipeline; } } From 3528a6b885ec9e1a4aaf4f9798a475395c5888b0 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 14 Feb 2025 19:07:27 +0100 Subject: [PATCH 4/5] dotnet format --- src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs b/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs index a2bd8a5aa..7c3403d52 100644 --- a/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs +++ b/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs @@ -18,7 +18,7 @@ public record ConfigurationFile : DocumentationFile private readonly int _depth; public string? Project { get; } public Glob[] Exclude { get; } = []; - public bool SoftLineEndings { get; } + public bool SoftLineEndings { get; } public string[] CrossLinkRepositories { get; } = []; From 0ec2eb4aea5b7c2ea493743c03fe427b7063ee2e Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Mon, 10 Mar 2025 09:46:49 +0100 Subject: [PATCH 5/5] Fix tests post merging --- src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs | 2 +- tests/authoring/Container/DefinitionLists.fs | 1 - tests/authoring/Inline/CrossLinksRedirects.fs | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs b/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs index 3fa55bed4..b6984b10c 100644 --- a/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs +++ b/src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs @@ -70,7 +70,7 @@ public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildCon Project = reader.ReadString(entry.Entry); break; case "soft_line_endings": - SoftLineEndings = bool.TryParse(ReadString(entry), out var softLineEndings) && softLineEndings; + SoftLineEndings = bool.TryParse(reader.ReadString(entry.Entry), out var softLineEndings) && softLineEndings; break; case "exclude": Exclude = [.. YamlStreamReader.ReadStringArray(entry.Entry).Select(Glob.Parse)]; diff --git a/tests/authoring/Container/DefinitionLists.fs b/tests/authoring/Container/DefinitionLists.fs index 63fbc0edb..dd8cee990 100644 --- a/tests/authoring/Container/DefinitionLists.fs +++ b/tests/authoring/Container/DefinitionLists.fs @@ -51,7 +51,6 @@ This is my `definition`

And this is the definition body -
Which may contain multiple lines

diff --git a/tests/authoring/Inline/CrossLinksRedirects.fs b/tests/authoring/Inline/CrossLinksRedirects.fs index b6f2ccce4..c7c1b5f22 100644 --- a/tests/authoring/Inline/CrossLinksRedirects.fs +++ b/tests/authoring/Inline/CrossLinksRedirects.fs @@ -90,7 +90,7 @@ type ``Scenario 2: Splitting a page into multiple smaller pages`` () = [] let ``validate HTML`` () = markdown |> convertsToHtml $""" -

Scenario 2
+

Scenario 2 Scenario 2

"""