Skip to content

Commit 7bb6322

Browse files
committed
Refactor
Convert to String extension And introduce a TitleRaw property
1 parent 2b4e21d commit 7bb6322

File tree

8 files changed

+33
-16
lines changed

8 files changed

+33
-16
lines changed

docs/testing/cross-links.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cross Links
1+
# Cross Links [Testing](testing://index.md) `code`
22

33
[Elasticsearch](elasticsearch://index.md)
44

src/Elastic.Markdown/Helpers/Markdown.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Elastic.Markdown.Helpers;
66

7-
public static class Markdown
7+
public static class MarkdownStringExtensions
88
{
9-
public static string StripMarkdown(string markdown)
9+
public static string StripMarkdown(this string markdown)
1010
{
1111
using var writer = new StringWriter();
1212
Markdig.Markdown.ToPlainText(markdown, writer);

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,23 @@ public DocumentationGroup? Parent
4343
public string? UrlPathPrefix { get; }
4444
private MarkdownParser MarkdownParser { get; }
4545
public YamlFrontMatter? YamlFrontMatter { get; private set; }
46+
47+
public string? TitleRaw
48+
{
49+
get => _titleRaw;
50+
private set
51+
{
52+
Title = value?.StripMarkdown();
53+
_titleRaw = value;
54+
}
55+
}
56+
4657
public string? Title { get; private set; }
58+
4759
public string? NavigationTitle
4860
{
4961
get => !string.IsNullOrEmpty(_navigationTitle) ? _navigationTitle : Title;
50-
private set => _navigationTitle = value != null ? Helpers.Markdown.StripMarkdown(value) : null;
62+
private set => _navigationTitle = value?.StripMarkdown();
5163
}
5264

5365
//indexed by slug
@@ -65,6 +77,7 @@ public string? NavigationTitle
6577

6678
private bool _instructionsParsed;
6779
private DocumentationGroup? _parent;
80+
private string? _titleRaw;
6881

6982
public MarkdownFile[] YieldParents()
7083
{
@@ -98,7 +111,7 @@ public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
98111
private void ReadDocumentInstructions(MarkdownDocument document)
99112
{
100113

101-
Title = document
114+
TitleRaw = document
102115
.FirstOrDefault(block => block is HeadingBlock { Level: 1 })?
103116
.GetData("header") as string;
104117

@@ -113,14 +126,14 @@ private void ReadDocumentInstructions(MarkdownDocument document)
113126
{
114127
Collector.EmitWarning(FilePath, "'title' is no longer supported in yaml frontmatter please use a level 1 header instead.");
115128
// TODO remove fallback once migration is over and we fully deprecate front matter titles
116-
if (string.IsNullOrEmpty(Title))
117-
Title = deprecatedTitle;
129+
if (string.IsNullOrEmpty(TitleRaw))
130+
TitleRaw = deprecatedTitle;
118131
}
119132

120133

121134
// set title on yaml front matter manually.
122135
// frontmatter gets passed around as page information throughout
123-
YamlFrontMatter.Title = Title;
136+
YamlFrontMatter.Title = TitleRaw;
124137

125138
NavigationTitle = YamlFrontMatter.NavigationTitle;
126139
if (!string.IsNullOrEmpty(NavigationTitle))
@@ -143,11 +156,11 @@ private void ReadDocumentInstructions(MarkdownDocument document)
143156
}
144157
}
145158
else
146-
YamlFrontMatter = new YamlFrontMatter { Title = Title };
159+
YamlFrontMatter = new YamlFrontMatter { Title = TitleRaw };
147160

148-
if (string.IsNullOrEmpty(Title))
161+
if (string.IsNullOrEmpty(TitleRaw))
149162
{
150-
Title = RelativePath;
163+
TitleRaw = RelativePath;
151164
Collector.EmitWarning(FilePath, "Document has no title, using file name as title.");
152165
}
153166

@@ -159,7 +172,7 @@ private void ReadDocumentInstructions(MarkdownDocument document)
159172
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string))
160173
.Select(h => new PageTocItem
161174
{
162-
Heading = Helpers.Markdown.StripMarkdown(h.Item1!),
175+
Heading = h.Item1!.StripMarkdown(),
163176
Slug = _slugHelper.GenerateSlug(h.Item2 ?? h.Item1)
164177
})
165178
.ToList();

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
131131
$"'{url}' could not be resolved to a markdown file while creating an auto text link, '{file.FullName}' does not exist.");
132132
}
133133

134-
var title = markdown?.Title;
134+
var title = markdown?.TitleRaw;
135135

136136
if (!string.IsNullOrEmpty(anchor))
137137
{

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public async Task<string> RenderLayout(MarkdownFile markdown, Cancel ctx = defau
5555

5656
var slice = Index.Create(new IndexViewModel
5757
{
58+
TitleRaw = markdown.TitleRaw ?? "[TITLE NOT SET]",
5859
Title = markdown.Title ?? "[TITLE NOT SET]",
5960
MarkdownHtml = html,
6061
PageTocItems = markdown.TableOfContents.Values.ToList(),

src/Elastic.Markdown/Slices/Index.cshtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@functions {
55
public LayoutViewModel LayoutModel => new()
66
{
7+
TitleRaw = Model.TitleRaw,
78
Title = $"Elastic Documentation: {Model.Title}",
89
PageTocItems = Model.PageTocItems,
910
Tree = Model.Tree,
@@ -18,7 +19,7 @@
1819
}
1920
<section id="elastic-docs-v3">
2021
@* This way it's correctly rendered as <h1>text</h1> instead of <h1><p>text</p></h1> *@
21-
@(new HtmlString(Markdown.ToHtml("# " + Model.Title)))
22+
@(new HtmlString(Markdown.ToHtml("# " + Model.TitleRaw)))
2223
@if (Model.Applies is not null)
2324
{
2425
await RenderPartialAsync(Applies.Create(Model.Applies));

src/Elastic.Markdown/Slices/_ViewModels.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Elastic.Markdown.Slices;
99

1010
public class IndexViewModel
1111
{
12+
public required string TitleRaw { get; init; }
1213
public required string Title { get; init; }
1314
public required string MarkdownHtml { get; init; }
1415
public required DocumentationGroup Tree { get; init; }
@@ -25,6 +26,7 @@ public class IndexViewModel
2526

2627
public class LayoutViewModel
2728
{
29+
public string TitleRaw { get; set; } = "Elastic Documentation";
2830
public string Title { get; set; } = "Elastic Documentation";
2931
public required IReadOnlyCollection<PageTocItem> PageTocItems { get; init; }
3032
public required DocumentationGroup Tree { get; init; }

tests/Elastic.Markdown.Tests/FrontMatter/YamlFrontMatterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class YamlFrontMatterTests(ITestOutputHelper output) : DirectiveTest(outp
2121
)
2222
{
2323
[Fact]
24-
public void ReadsTitle() => File.Title.Should().Be("Elastic Docs v3");
24+
public void ReadsTitle() => File.TitleRaw.Should().Be("Elastic Docs v3");
2525

2626
[Fact]
2727
public void ReadsNavigationTitle() => File.NavigationTitle.Should().Be("Documentation Guide");
@@ -39,7 +39,7 @@ public void ReadsSubstitutions()
3939
public class EmptyFileWarnsNeedingATitle(ITestOutputHelper output) : DirectiveTest(output, "")
4040
{
4141
[Fact]
42-
public void ReadsTitle() => File.Title.Should().Be("index.md");
42+
public void ReadsTitle() => File.TitleRaw.Should().Be("index.md");
4343

4444
[Fact]
4545
public void ReadsNavigationTitle() => File.NavigationTitle.Should().Be("index.md");

0 commit comments

Comments
 (0)