-
Notifications
You must be signed in to change notification settings - Fork 34
Add autolinks rendering #2541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theletterf
wants to merge
8
commits into
main
Choose a base branch
from
add-autolinks-rendering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add autolinks rendering #2541
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6bc94c
Add automatic links from URLs
theletterf 165a076
Update documentation
theletterf 6ea9b36
Merge branch 'main' into add-autolinks-rendering
theletterf aeed112
Fix test
theletterf c84e9a9
Merge branch 'add-autolinks-rendering' of github.com:elastic/docs-bui…
theletterf 6e6a0e3
Fix docs
theletterf 9948e5d
Fix elastic.co/docs hint
theletterf 4d14d25
Merge branch 'main' into add-autolinks-rendering
theletterf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/Elastic.Markdown/Myst/InlineParsers/AutoLinkInlineParser.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using Elastic.Markdown.Diagnostics; | ||
| using Markdig; | ||
| using Markdig.Helpers; | ||
| using Markdig.Parsers; | ||
| using Markdig.Parsers.Inlines; | ||
| using Markdig.Syntax; | ||
| using Markdig.Syntax.Inlines; | ||
|
|
||
| namespace Elastic.Markdown.Myst.InlineParsers; | ||
|
|
||
| public static class AutoLinkBuilderExtensions | ||
| { | ||
| public static MarkdownPipelineBuilder UseAutoLinks(this MarkdownPipelineBuilder pipeline) | ||
| { | ||
| pipeline.Extensions.AddIfNotAlready<AutoLinkBuilderExtension>(); | ||
| return pipeline; | ||
| } | ||
| } | ||
|
|
||
| public class AutoLinkBuilderExtension : IMarkdownExtension | ||
| { | ||
| public void Setup(MarkdownPipelineBuilder pipeline) => | ||
| pipeline.InlineParsers.InsertBefore<LinkInlineParser>(new AutoLinkInlineParser()); | ||
|
|
||
| public void Setup(MarkdownPipeline pipeline, Markdig.Renderers.IMarkdownRenderer renderer) | ||
| { | ||
| // No custom renderer needed - we create standard LinkInline objects | ||
| // that are rendered by HtmxLinkInlineRenderer | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parses bare https:// URLs and converts them to clickable links. | ||
| /// URLs containing elastic.co/docs emit a hint suggesting crosslinks or relative links. | ||
| /// </summary> | ||
| public class AutoLinkInlineParser : InlineParser | ||
| { | ||
| public AutoLinkInlineParser() => OpeningCharacters = ['h']; | ||
|
|
||
| public override bool Match(InlineProcessor processor, ref StringSlice slice) | ||
| { | ||
| // Must start with https:// | ||
| var span = slice.AsSpan(); | ||
| if (!span.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) | ||
| return false; | ||
|
|
||
| // Find the end of the URL | ||
| var urlLength = FindUrlEnd(span); | ||
| if (urlLength <= "https://".Length) | ||
| return false; // Just "https://" with nothing after is not valid | ||
|
|
||
| var url = span[..urlLength].ToString(); | ||
|
|
||
| // Get source position for proper diagnostics | ||
| var startPosition = slice.Start; | ||
| var start = processor.GetSourcePosition(startPosition, out var line, out var column); | ||
| var spanEnd = start + urlLength - 1; | ||
|
|
||
| // Create a LinkInline with the URL as both href and text | ||
| var linkInline = new LinkInline(url, string.Empty) | ||
| { | ||
| IsClosed = true, | ||
| IsAutoLink = true, | ||
| Span = new SourceSpan(start, spanEnd), | ||
| Line = line, | ||
| Column = column | ||
| }; | ||
| _ = linkInline.AppendChild(new LiteralInline(url)); | ||
|
|
||
| // Store context data for the renderer (same pattern as DiagnosticLinkInlineParser) | ||
| var context = processor.GetContext(); | ||
| linkInline.SetData(nameof(context.CurrentUrlPath), context.CurrentUrlPath); | ||
| linkInline.SetData("isCrossLink", false); | ||
|
|
||
| processor.Inline = linkInline; | ||
|
|
||
| // Emit hint for elastic.co/docs URLs (after setting Inline so position is correct) | ||
| if (url.Contains("elastic.co/docs", StringComparison.OrdinalIgnoreCase)) | ||
| processor.EmitHint(linkInline, "Autolink points to elastic.co/docs. Consider using a crosslink or relative link instead."); | ||
|
|
||
| // Advance the slice past the URL | ||
| var end = slice.Start + urlLength; | ||
| while (slice.Start < end) | ||
| slice.SkipChar(); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Finds the end of a URL in the given span, handling trailing punctuation correctly. | ||
| /// </summary> | ||
| private static int FindUrlEnd(ReadOnlySpan<char> span) | ||
| { | ||
| var length = 0; | ||
| var parenDepth = 0; | ||
| var bracketDepth = 0; | ||
|
|
||
| for (var i = 0; i < span.Length; i++) | ||
| { | ||
| var c = span[i]; | ||
|
|
||
| // URL terminates at whitespace or control characters | ||
| if (char.IsWhiteSpace(c) || char.IsControl(c)) | ||
| break; | ||
|
|
||
| // Track balanced parentheses (common in Wikipedia URLs) | ||
| if (c == '(') | ||
| parenDepth++; | ||
| else if (c == ')') | ||
| { | ||
| if (parenDepth > 0) | ||
| parenDepth--; | ||
| else | ||
| break; // Unbalanced closing paren - not part of URL | ||
| } | ||
|
|
||
| // Track balanced brackets | ||
| if (c == '[') | ||
| bracketDepth++; | ||
| else if (c == ']') | ||
| { | ||
| if (bracketDepth > 0) | ||
| bracketDepth--; | ||
| else | ||
| break; // Unbalanced closing bracket - not part of URL | ||
| } | ||
|
|
||
| // These characters end the URL (Markdown syntax) | ||
| if (c is '<' or '>') | ||
| break; | ||
|
|
||
| length = i + 1; | ||
| } | ||
|
|
||
| // Remove trailing punctuation that's likely sentence punctuation, not part of URL | ||
| while (length > 0 && span[length - 1] is '.' or ',' or ';' or ':' or '!' or '?' or '\'' or '"') | ||
| length--; | ||
|
|
||
| return length; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we exempt
/docs/api? we might want to add an exlusion for.zipgiven we want to expose llms.zip and elasticsearch-data.zip soon.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you suggest proceeding here? We could add a filtering system (potentially fragile, hard to maintain, etc.), launch Autolinks anyway despite some broken URLs and let teams fix this, or hold / cancel. See thread in Slack.