Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Convert to String extension

And introduce a TitleRaw property
  • Loading branch information
reakaleek committed Jan 23, 2025
1 parent 2b4e21d commit 7bb6322
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/testing/cross-links.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cross Links
# Cross Links [Testing](testing://index.md) `code`

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

Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Markdown/Helpers/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Elastic.Markdown.Helpers;

public static class Markdown
public static class MarkdownStringExtensions
{
public static string StripMarkdown(string markdown)
public static string StripMarkdown(this string markdown)
{
using var writer = new StringWriter();
Markdig.Markdown.ToPlainText(markdown, writer);
Expand Down
31 changes: 22 additions & 9 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,23 @@ public DocumentationGroup? Parent
public string? UrlPathPrefix { get; }
private MarkdownParser MarkdownParser { get; }
public YamlFrontMatter? YamlFrontMatter { get; private set; }

public string? TitleRaw
{
get => _titleRaw;
private set
{
Title = value?.StripMarkdown();
_titleRaw = value;
}
}

public string? Title { get; private set; }

public string? NavigationTitle
{
get => !string.IsNullOrEmpty(_navigationTitle) ? _navigationTitle : Title;
private set => _navigationTitle = value != null ? Helpers.Markdown.StripMarkdown(value) : null;
private set => _navigationTitle = value?.StripMarkdown();
}

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

private bool _instructionsParsed;
private DocumentationGroup? _parent;
private string? _titleRaw;

public MarkdownFile[] YieldParents()
{
Expand Down Expand Up @@ -98,7 +111,7 @@ public async Task<MarkdownDocument> ParseFullAsync(Cancel ctx)
private void ReadDocumentInstructions(MarkdownDocument document)
{

Title = document
TitleRaw = document
.FirstOrDefault(block => block is HeadingBlock { Level: 1 })?
.GetData("header") as string;

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


// set title on yaml front matter manually.
// frontmatter gets passed around as page information throughout
YamlFrontMatter.Title = Title;
YamlFrontMatter.Title = TitleRaw;

NavigationTitle = YamlFrontMatter.NavigationTitle;
if (!string.IsNullOrEmpty(NavigationTitle))
Expand All @@ -143,11 +156,11 @@ private void ReadDocumentInstructions(MarkdownDocument document)
}
}
else
YamlFrontMatter = new YamlFrontMatter { Title = Title };
YamlFrontMatter = new YamlFrontMatter { Title = TitleRaw };

if (string.IsNullOrEmpty(Title))
if (string.IsNullOrEmpty(TitleRaw))
{
Title = RelativePath;
TitleRaw = RelativePath;
Collector.EmitWarning(FilePath, "Document has no title, using file name as title.");
}

Expand All @@ -159,7 +172,7 @@ private void ReadDocumentInstructions(MarkdownDocument document)
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string))
.Select(h => new PageTocItem
{
Heading = Helpers.Markdown.StripMarkdown(h.Item1!),
Heading = h.Item1!.StripMarkdown(),
Slug = _slugHelper.GenerateSlug(h.Item2 ?? h.Item1)
})
.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
$"'{url}' could not be resolved to a markdown file while creating an auto text link, '{file.FullName}' does not exist.");
}

var title = markdown?.Title;
var title = markdown?.TitleRaw;

if (!string.IsNullOrEmpty(anchor))
{
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public async Task<string> RenderLayout(MarkdownFile markdown, Cancel ctx = defau

var slice = Index.Create(new IndexViewModel
{
TitleRaw = markdown.TitleRaw ?? "[TITLE NOT SET]",
Title = markdown.Title ?? "[TITLE NOT SET]",
MarkdownHtml = html,
PageTocItems = markdown.TableOfContents.Values.ToList(),
Expand Down
3 changes: 2 additions & 1 deletion src/Elastic.Markdown/Slices/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@functions {
public LayoutViewModel LayoutModel => new()
{
TitleRaw = Model.TitleRaw,
Title = $"Elastic Documentation: {Model.Title}",
PageTocItems = Model.PageTocItems,
Tree = Model.Tree,
Expand All @@ -18,7 +19,7 @@
}
<section id="elastic-docs-v3">
@* This way it's correctly rendered as <h1>text</h1> instead of <h1><p>text</p></h1> *@
@(new HtmlString(Markdown.ToHtml("# " + Model.Title)))
@(new HtmlString(Markdown.ToHtml("# " + Model.TitleRaw)))
@if (Model.Applies is not null)
{
await RenderPartialAsync(Applies.Create(Model.Applies));
Expand Down
2 changes: 2 additions & 0 deletions src/Elastic.Markdown/Slices/_ViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Elastic.Markdown.Slices;

public class IndexViewModel
{
public required string TitleRaw { get; init; }
public required string Title { get; init; }
public required string MarkdownHtml { get; init; }
public required DocumentationGroup Tree { get; init; }
Expand All @@ -25,6 +26,7 @@ public class IndexViewModel

public class LayoutViewModel
{
public string TitleRaw { get; set; } = "Elastic Documentation";
public string Title { get; set; } = "Elastic Documentation";
public required IReadOnlyCollection<PageTocItem> PageTocItems { get; init; }
public required DocumentationGroup Tree { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class YamlFrontMatterTests(ITestOutputHelper output) : DirectiveTest(outp
)
{
[Fact]
public void ReadsTitle() => File.Title.Should().Be("Elastic Docs v3");
public void ReadsTitle() => File.TitleRaw.Should().Be("Elastic Docs v3");

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

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

0 comments on commit 7bb6322

Please sign in to comment.