Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Elastic.Markdown/IO/DocumentationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,10 @@ void ValidateExists(string from, string to, IReadOnlyDictionary<string, string?>

public FrozenDictionary<int, MarkdownFile> MarkdownFiles { get; }

public MarkdownFile? DocumentationFileLookup(IFileInfo sourceFile)
public DocumentationFile? DocumentationFileLookup(IFileInfo sourceFile)
{
var relativePath = Path.GetRelativePath(SourceDirectory.FullName, sourceFile.FullName);
if (FlatMappedFiles.TryGetValue(relativePath, out var file) && file is MarkdownFile markdownFile)
return markdownFile;
return null;
return FlatMappedFiles.GetValueOrDefault(relativePath);
}

public MarkdownFile? GetPrevious(MarkdownFile current)
Expand Down
4 changes: 4 additions & 0 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ DocumentationSet set
{
FileName = sourceFile.Name;
FilePath = sourceFile.FullName;
IsIndex = FileName == "index.md";

UrlPathPrefix = build.UrlPathPrefix;
MarkdownParser = parser;
Collector = build.Collector;
Expand Down Expand Up @@ -76,6 +78,8 @@ public DocumentationGroup? Parent
public YamlFrontMatter? YamlFrontMatter { get; private set; }
public string? TitleRaw { get; protected set; }

public bool IsIndex { get; internal set; }

public string? Title
{
get => _title;
Expand Down
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ .. documentationFiles
}
}

if (indexFile is not null)
indexFile.IsIndex = true;

return indexFile ?? files.FirstOrDefault();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ private static void WriteIncludeBlock(HtmlRenderer renderer, IncludeBlock block,
return;

var snippet = block.Build.ReadFileSystem.FileInfo.New(block.IncludePath);
var parentPath = block.Context.MarkdownSourcePath;

var parentPath = block.Context.MarkdownParentPath ?? block.Context.MarkdownSourcePath;
var document = parser.ParseSnippetAsync(snippet, parentPath, block.Context.YamlFrontMatter, default).GetAwaiter().GetResult();

var html = document.ToHtml(parser.Pipeline);
_ = renderer.Write(html);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,14 @@ private static IFileInfo ResolveFile(ParserContext context, string url) =>
private static void ValidateAnchor(InlineProcessor processor, MarkdownFile markdown, string anchor, LinkInline link)
{
if (!markdown.Anchors.Contains(anchor))
processor.EmitError(link, $"`{anchor}` does not exist in {markdown.FileName}.");
processor.EmitError(link, $"`{anchor}` does not exist in {markdown.RelativePath}.");
}

private static void UpdateLinkUrl(LinkInline link, string url, ParserContext context, string? anchor)
{
// TODO revisit when we refactor our documentation set graph
// This method grew too complex, we need to revisit our documentation set graph generation so we can ask these questions
// on `DocumentationFile` that are mostly precomputed
var urlPathPrefix = context.Build.UrlPathPrefix ?? string.Empty;

if (!url.StartsWith('/') && !string.IsNullOrEmpty(url))
Expand All @@ -303,16 +306,19 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
? context.CurrentUrlPath[urlPathPrefix.Length..]
: urlPathPrefix;

var markdownPath = context.MarkdownSourcePath.Name;
// if we are trying to resolve a relative url from a _snippet folder ensure we eat the _snippet folder
// as it's not part of url by chopping of the extra parent navigation
if (url.StartsWith("../") && context.DocumentationFileLookup(context.MarkdownSourcePath) is SnippetFile snippetFile)
url = url.Substring(3);

// TODO check through context.DocumentationFileLookup if file is index vs "index.md" check
var markdownPath = context.MarkdownSourcePath;
// if the current path is an index e.g /reference/cloud-k8s/
// './' current path lookups should be relative to sub-path.
// If it's not e.g /reference/cloud-k8s/api-docs/ these links should resolve on folder up.
var siblingsGoToCurrent = url.StartsWith("./") && markdownPath == "index.md";
var lastIndexPath = subPrefix.LastIndexOf('/');
if (lastIndexPath >= 0 && !siblingsGoToCurrent)
if (lastIndexPath >= 0 && markdownPath.Name != "index.md")
subPrefix = subPrefix[..lastIndexPath];

var combined = '/' + Path.Combine(subPrefix, url).TrimStart('/');
url = Path.GetFullPath(combined);
}
Expand Down Expand Up @@ -341,6 +347,7 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
url = url[..^5];

link.Url = string.IsNullOrEmpty(anchor) ? url : $"{url}#{anchor}";

}

private static bool IsCrossLink([NotNullWhen(true)] Uri? uri) =>
Expand Down
6 changes: 4 additions & 2 deletions src/Elastic.Markdown/Myst/ParserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class ParserContext : MarkdownParserContext, IParserResolvers
public ConfigurationFile Configuration { get; }
public ICrossLinkResolver CrossLinkResolver { get; }
public IFileInfo MarkdownSourcePath { get; }
public IFileInfo? MarkdownParentPath { get; }
public string CurrentUrlPath { get; }
public YamlFrontMatter? YamlFrontMatter { get; }
public BuildContext Build { get; }
Expand All @@ -67,15 +68,16 @@ public ParserContext(ParserState state)
Configuration = state.Configuration;
YamlFrontMatter = state.YamlFrontMatter;
SkipValidation = state.SkipValidation;
MarkdownParentPath = state.ParentMarkdownPath;

CrossLinkResolver = state.CrossLinkResolver;
MarkdownSourcePath = state.MarkdownSourcePath;
DocumentationFileLookup = state.DocumentationFileLookup;
var parentPath = state.ParentMarkdownPath;

CurrentUrlPath = DocumentationFileLookup(parentPath ?? MarkdownSourcePath) is MarkdownFile md
CurrentUrlPath = DocumentationFileLookup(state.ParentMarkdownPath ?? MarkdownSourcePath) is MarkdownFile md
? md.Url
: string.Empty;

if (SkipValidation && string.IsNullOrEmpty(CurrentUrlPath))
{
//TODO investigate this deeper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected DirectiveTest(ITestOutputHelper output, [LanguageInjection("markdown")
var context = new BuildContext(Collector, FileSystem);
var linkResolver = new TestCrossLinkResolver();
Set = new DocumentationSet(context, logger, linkResolver);
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) ?? throw new NullReferenceException();
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) as MarkdownFile ?? throw new NullReferenceException();
Html = default!; //assigned later
Document = default!;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected InlineTest(
};
var linkResolver = new TestCrossLinkResolver();
Set = new DocumentationSet(context, logger, linkResolver);
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) ?? throw new NullReferenceException();
File = Set.DocumentationFileLookup(FileSystem.FileInfo.New("docs/index.md")) as MarkdownFile ?? throw new NullReferenceException();
Html = default!; //assigned later
Document = default!;
}
Expand Down
Loading