in src/Elastic.Markdown/Myst/MarkdownParser.cs [23:165]
public class MarkdownParser(BuildContext build, IParserResolvers resolvers)
{
private BuildContext Build { get; } = build;
private IParserResolvers Resolvers { get; } = resolvers;
public Task<MarkdownDocument> MinimalParseAsync(IFileInfo path, Cancel ctx)
{
var state = new ParserState(Build)
{
MarkdownSourcePath = path,
YamlFrontMatter = null,
DocumentationFileLookup = Resolvers.DocumentationFileLookup,
CrossLinkResolver = Resolvers.CrossLinkResolver,
SkipValidation = true
};
var context = new ParserContext(state);
return ParseAsync(path, context, MinimalPipeline, ctx);
}
public Task<MarkdownDocument> ParseAsync(IFileInfo path, YamlFrontMatter? matter, Cancel ctx)
{
var state = new ParserState(Build)
{
MarkdownSourcePath = path,
YamlFrontMatter = matter,
DocumentationFileLookup = Resolvers.DocumentationFileLookup,
CrossLinkResolver = Resolvers.CrossLinkResolver
};
var context = new ParserContext(state);
return ParseAsync(path, context, Pipeline, ctx);
}
public Task<MarkdownDocument> ParseSnippetAsync(IFileInfo path, IFileInfo parentPath, YamlFrontMatter? matter, Cancel ctx)
{
var state = new ParserState(Build)
{
MarkdownSourcePath = path,
YamlFrontMatter = matter,
DocumentationFileLookup = Resolvers.DocumentationFileLookup,
CrossLinkResolver = Resolvers.CrossLinkResolver,
ParentMarkdownPath = parentPath
};
var context = new ParserContext(state);
return ParseAsync(path, context, Pipeline, ctx);
}
public MarkdownDocument ParseStringAsync(string markdown, IFileInfo path, YamlFrontMatter? matter) =>
ParseMarkdownStringAsync(markdown, path, matter, Pipeline);
public MarkdownDocument MinimalParseStringAsync(string markdown, IFileInfo path, YamlFrontMatter? matter) =>
ParseMarkdownStringAsync(markdown, path, matter, MinimalPipeline);
private MarkdownDocument ParseMarkdownStringAsync(string markdown, IFileInfo path, YamlFrontMatter? matter, MarkdownPipeline pipeline)
{
var state = new ParserState(Build)
{
MarkdownSourcePath = path,
YamlFrontMatter = matter,
DocumentationFileLookup = Resolvers.DocumentationFileLookup,
CrossLinkResolver = Resolvers.CrossLinkResolver
};
var context = new ParserContext(state);
var markdownDocument = Markdig.Markdown.Parse(markdown, pipeline, context);
return markdownDocument;
}
private static async Task<MarkdownDocument> ParseAsync(
IFileInfo path,
MarkdownParserContext context,
MarkdownPipeline pipeline,
Cancel ctx)
{
if (path.FileSystem is FileSystem)
{
//real IO optimize through UTF8 stream reader.
await using var streamReader = new Utf8StreamReader(path.FullName, fileOpenMode: FileOpenMode.Throughput);
var inputMarkdown = await streamReader.AsTextReader().ReadToEndAsync(ctx);
var markdownDocument = Markdig.Markdown.Parse(inputMarkdown, pipeline, context);
return markdownDocument;
}
else
{
var inputMarkdown = await path.FileSystem.File.ReadAllTextAsync(path.FullName, ctx);
var markdownDocument = Markdig.Markdown.Parse(inputMarkdown, pipeline, context);
return markdownDocument;
}
}
// ReSharper disable once InconsistentNaming
private MarkdownPipeline? _minimalPipelineCached;
private MarkdownPipeline MinimalPipeline
{
get
{
if (_minimalPipelineCached is not null)
return _minimalPipelineCached;
var builder = new MarkdownPipelineBuilder()
.UseYamlFrontMatter()
.UseInlineAnchors()
.UseHeadingsWithSlugs()
.UseDirectives(this);
_ = builder.BlockParsers.TryRemove<IndentedCodeBlockParser>();
_minimalPipelineCached = builder.Build();
return _minimalPipelineCached;
}
}
// ReSharper disable once InconsistentNaming
private MarkdownPipeline? _pipelineCached;
public MarkdownPipeline Pipeline
{
get
{
if (_pipelineCached is not null)
return _pipelineCached;
var builder = new MarkdownPipelineBuilder()
.UseInlineAnchors()
.UsePreciseSourceLocation()
.UseDiagnosticLinks()
.UseHeadingsWithSlugs()
.UseEmphasisExtras(EmphasisExtraOptions.Default)
.UseInlineAppliesTo()
.UseSubstitution()
.UseComments()
.UseYamlFrontMatter()
.UseGridTables()
.UsePipeTables()
.UseDirectives(this)
.UseDefinitionLists()
.UseEnhancedCodeBlocks()
.UseHtmxLinkInlineRenderer()
.DisableHtml()
.UseHardBreaks();
_ = builder.BlockParsers.TryRemove<IndentedCodeBlockParser>();
_pipelineCached = builder.Build();
return _pipelineCached;
}
}
}