|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using Elastic.Markdown.Diagnostics; |
| 6 | +using Elastic.Markdown.Myst.Directives; |
| 7 | +using Markdig; |
| 8 | +using Markdig.Helpers; |
| 9 | +using Markdig.Parsers; |
| 10 | +using Markdig.Parsers.Inlines; |
| 11 | +using Markdig.Renderers; |
| 12 | +using Markdig.Syntax.Inlines; |
| 13 | + |
| 14 | +namespace Elastic.Markdown.Myst.InlineParsers; |
| 15 | + |
| 16 | +public static class DirectiveMarkdownBuilderExtensions |
| 17 | +{ |
| 18 | + public static MarkdownPipelineBuilder UseDiagnosticLinks(this MarkdownPipelineBuilder pipeline) |
| 19 | + { |
| 20 | + pipeline.Extensions.AddIfNotAlready<DiagnosticLinkInlineExtensions>(); |
| 21 | + return pipeline; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +public class DiagnosticLinkInlineExtensions : IMarkdownExtension |
| 26 | +{ |
| 27 | + public void Setup(MarkdownPipelineBuilder pipeline) => |
| 28 | + pipeline.InlineParsers.Replace<LinkInlineParser>(new DiagnosticLinkInlineParser()); |
| 29 | + |
| 30 | + public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { } |
| 31 | +} |
| 32 | + |
| 33 | +public class DiagnosticLinkInlineParser : LinkInlineParser |
| 34 | +{ |
| 35 | + public override bool Match(InlineProcessor processor, ref StringSlice slice) |
| 36 | + { |
| 37 | + var match = base.Match(processor, ref slice); |
| 38 | + if (!match) return false; |
| 39 | + |
| 40 | + if (processor.Inline is not LinkInline link) |
| 41 | + return match; |
| 42 | + |
| 43 | + var url = link.Url; |
| 44 | + var line = link.Line + 1; |
| 45 | + var column = link.Column; |
| 46 | + var length = url?.Length ?? 1; |
| 47 | + |
| 48 | + var context = processor.GetContext(); |
| 49 | + if (processor.GetContext().SkipValidation) |
| 50 | + return match; |
| 51 | + |
| 52 | + if (string.IsNullOrEmpty(url)) |
| 53 | + { |
| 54 | + processor.EmitWarning(line, column, length, $"Found empty url"); |
| 55 | + return match; |
| 56 | + } |
| 57 | + |
| 58 | + if (Uri.TryCreate(url, UriKind.Absolute, out var uri) && uri.Scheme.StartsWith("http")) |
| 59 | + { |
| 60 | + processor.EmitWarning(line, column, length, $"external URI: {uri} "); |
| 61 | + return match; |
| 62 | + } |
| 63 | + |
| 64 | + var includeFrom = context.Path.Directory!.FullName; |
| 65 | + if (url.StartsWith('/')) |
| 66 | + includeFrom = context.Parser.SourcePath.FullName; |
| 67 | + |
| 68 | + var pathOnDisk = Path.Combine(includeFrom, url.TrimStart('/')); |
| 69 | + if (!context.Build.ReadFileSystem.File.Exists(pathOnDisk)) |
| 70 | + processor.EmitError(line, column, length, $"`{url}` does not exist. resolved to `{pathOnDisk}"); |
| 71 | + |
| 72 | + if (link.FirstChild == null) |
| 73 | + { |
| 74 | + var title = context.GetTitle?.Invoke(url); |
| 75 | + if (!string.IsNullOrEmpty(title)) |
| 76 | + link.AppendChild(new LiteralInline(title)); |
| 77 | + } |
| 78 | + |
| 79 | + if (url.EndsWith(".md")) |
| 80 | + link.Url = Path.ChangeExtension(url, ".html"); |
| 81 | + |
| 82 | + return match; |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + } |
| 87 | +} |
0 commit comments