-
-
Notifications
You must be signed in to change notification settings - Fork 26
Snippets in JSX static site generators #745
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,20 +181,20 @@ Action<string> CreateIndentedAppendLine(string indent) => s => | |
| continue; | ||
| } | ||
|
|
||
| void AppendSnippet(string key1) | ||
| void AppendSnippet(string key1, bool useLinkRefFormat = false) | ||
| { | ||
| builder.Clear(); | ||
| var indentedAppendLine = CreateIndentedAppendLine(line.LeadingWhitespace); | ||
| ProcessSnippetLine(indentedAppendLine, missingSnippets, usedSnippets, key1, relativePath, line); | ||
| ProcessSnippetLine(indentedAppendLine, missingSnippets, usedSnippets, key1, relativePath, line, useLinkRefFormat); | ||
| builder.TrimEnd(); | ||
| line.Current = builder.ToString(); | ||
| } | ||
|
|
||
| void AppendWebSnippet(string url, string snippetKey, string? viewUrl = null) | ||
| void AppendWebSnippet(string url, string snippetKey, string? viewUrl = null, bool useLinkRefFormat = false) | ||
| { | ||
| builder.Clear(); | ||
| var indentedAppendLine = CreateIndentedAppendLine(line.LeadingWhitespace); | ||
| ProcessWebSnippetLine(indentedAppendLine, missingSnippets, usedSnippets, url, snippetKey, viewUrl, line); | ||
| ProcessWebSnippetLine(indentedAppendLine, missingSnippets, usedSnippets, url, snippetKey, viewUrl, line, useLinkRefFormat); | ||
| builder.TrimEnd(); | ||
| line.Current = builder.ToString(); | ||
| } | ||
|
|
@@ -244,6 +244,34 @@ void AppendWebSnippet(string url, string snippetKey, string? viewUrl = null) | |
| continue; | ||
| } | ||
|
|
||
| if (SnippetKey.ExtractLinkRefCommentSnippet(line, out key)) | ||
| { | ||
| AppendSnippet(key, useLinkRefFormat: true); | ||
|
|
||
| index++; | ||
|
|
||
| lines.RemoveUntil( | ||
| index, | ||
| "[//]: # (endSnippet)", | ||
| relativePath, | ||
| line); | ||
| continue; | ||
| } | ||
|
|
||
| if (SnippetKey.ExtractLinkRefCommentWebSnippet(line, out url, out snippetKey, out viewUrl)) | ||
| { | ||
| AppendWebSnippet(url, snippetKey, viewUrl, useLinkRefFormat: true); | ||
|
|
||
| index++; | ||
|
|
||
| lines.RemoveUntil( | ||
| index, | ||
| "[//]: # (endSnippet)", | ||
| relativePath, | ||
| line); | ||
| continue; | ||
| } | ||
|
Comment on lines
+261
to
+273
|
||
|
|
||
| if (line.Current.TrimStart() == "<!-- toc -->") | ||
| { | ||
| tocLine = line; | ||
|
|
@@ -292,14 +320,16 @@ bool ValidateContent(string? relativePath, Line line, List<ValidationError> vali | |
| return true; | ||
| } | ||
|
|
||
| void ProcessSnippetLine(Action<string> appendLine, List<MissingSnippet> missings, List<Snippet> used, string key, string? relativePath, Line line) | ||
| void ProcessSnippetLine(Action<string> appendLine, List<MissingSnippet> missings, List<Snippet> used, string key, string? relativePath, Line line, bool useLinkRefFormat = false) | ||
| { | ||
| appendLine($"<!-- snippet: {key} -->"); | ||
| var startMarker = useLinkRefFormat ? $"[//]: # (snippet: {key})" : $"<!-- snippet: {key} -->"; | ||
| var endMarker = useLinkRefFormat ? "[//]: # (endSnippet)" : "<!-- endSnippet -->"; | ||
| appendLine(startMarker); | ||
|
|
||
| if (TryGetSnippets(key, relativePath, line.Path, out var snippetsForKey)) | ||
| { | ||
| appendSnippets(key, snippetsForKey, appendLine); | ||
| appendLine("<!-- endSnippet -->"); | ||
| appendLine(endMarker); | ||
| used.AddRange(snippetsForKey); | ||
| return; | ||
| } | ||
|
|
@@ -309,14 +339,19 @@ void ProcessSnippetLine(Action<string> appendLine, List<MissingSnippet> missings | |
| appendLine("```"); | ||
| appendLine($"** Could not find snippet '{key}' **"); | ||
| appendLine("```"); | ||
| appendLine("<!-- endSnippet -->"); | ||
| appendLine(endMarker); | ||
| } | ||
|
|
||
| void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missings, List<Snippet> used, string url, string snippetKey, string? viewUrl, Line line) | ||
| void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missings, List<Snippet> used, string url, string snippetKey, string? viewUrl, Line line, bool useLinkRefFormat = false) | ||
| { | ||
| var commentText = viewUrl == null | ||
| ? $"<!-- web-snippet: {url}#{snippetKey} -->" | ||
| : $"<!-- web-snippet: {url}#{snippetKey} {viewUrl} -->"; | ||
| var endMarker = useLinkRefFormat ? "[//]: # (endSnippet)" : "<!-- endSnippet -->"; | ||
| var commentText = useLinkRefFormat | ||
| ? (viewUrl == null | ||
| ? $"[//]: # (web-snippet: {url}#{snippetKey})" | ||
| : $"[//]: # (web-snippet: {url}#{snippetKey} {viewUrl})") | ||
| : (viewUrl == null | ||
| ? $"<!-- web-snippet: {url}#{snippetKey} -->" | ||
| : $"<!-- web-snippet: {url}#{snippetKey} {viewUrl} -->"); | ||
| appendLine(commentText); | ||
| // Download file content | ||
| try | ||
|
|
@@ -329,7 +364,7 @@ void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missi | |
| appendLine("```"); | ||
| appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **"); | ||
| appendLine("```"); | ||
| appendLine("<!-- endSnippet -->"); | ||
| appendLine(endMarker); | ||
| return; | ||
| } | ||
| // Extract snippets from content | ||
|
|
@@ -343,7 +378,7 @@ void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missi | |
| appendLine("```"); | ||
| appendLine($"** Could not find snippet '{snippetKey}' in '{url}' **"); | ||
| appendLine("```"); | ||
| appendLine("<!-- endSnippet -->"); | ||
| appendLine(endMarker); | ||
| return; | ||
| } | ||
| // Create new snippet with viewUrl if provided | ||
|
|
@@ -359,7 +394,7 @@ void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missi | |
| expressiveCode: found.ExpressiveCode, | ||
| viewUrl: viewUrl); | ||
| appendSnippets(snippetKey, [snippetToAppend], appendLine); | ||
| appendLine("<!-- endSnippet -->"); | ||
| appendLine(endMarker); | ||
| used.Add(snippetToAppend); | ||
| } | ||
| catch | ||
|
|
@@ -369,7 +404,7 @@ void ProcessWebSnippetLine(Action<string> appendLine, List<MissingSnippet> missi | |
| appendLine("```"); | ||
| appendLine($"** Could not fetch or parse web-snippet '{url}#{snippetKey}' **"); | ||
| appendLine("```"); | ||
| appendLine("<!-- endSnippet -->"); | ||
| appendLine(endMarker); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -158,4 +158,86 @@ public static bool IsStartCommentWebSnippetLine(string line) => | |||||||||
|
|
||||||||||
| public static bool IsStartCommentWebSnippetLine(CharSpan line) => | ||||||||||
| line.StartsWith("<!-- web-snippet:", StringComparison.OrdinalIgnoreCase); | ||||||||||
|
|
||||||||||
| // [//]: # (snippet: key) format — link reference comment syntax for MDX compatibility | ||||||||||
|
|
||||||||||
| public static bool ExtractLinkRefCommentSnippet(Line line, [NotNullWhen(true)] out string? key) | ||||||||||
| { | ||||||||||
| var lineCurrent = line.Current.AsSpan().TrimStart(); | ||||||||||
| if (!IsLinkRefCommentSnippetLine(lineCurrent)) | ||||||||||
| { | ||||||||||
| key = null; | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // after "[//]: # (snippet: " = 18 chars | ||||||||||
| var substring = lineCurrent[18..]; | ||||||||||
| var indexOf = substring.IndexOf(")", StringComparison.Ordinal); | ||||||||||
|
Comment on lines
+164
to
+175
|
||||||||||
| if (indexOf < 0) | ||||||||||
| { | ||||||||||
| throw new SnippetException($"Could not find closing ')' in: {line.Original}. Path: {line.Path}. Line: {line.LineNumber}"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| key = substring[..indexOf].Trim().ToString(); | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public static bool ExtractLinkRefCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey) => | ||||||||||
| ExtractLinkRefCommentWebSnippet(line, out url, out snippetKey, out _); | ||||||||||
|
|
||||||||||
| public static bool ExtractLinkRefCommentWebSnippet(Line line, [NotNullWhen(true)] out string? url, [NotNullWhen(true)] out string? snippetKey, out string? viewUrl) | ||||||||||
| { | ||||||||||
| var lineCurrent = line.Current.AsSpan().TrimStart(); | ||||||||||
| if (!IsLinkRefCommentWebSnippetLine(lineCurrent)) | ||||||||||
| { | ||||||||||
| url = null; | ||||||||||
| snippetKey = null; | ||||||||||
| viewUrl = null; | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // after "[//]: # (web-snippet: " = 22 chars | ||||||||||
| var substring = lineCurrent[22..]; | ||||||||||
|
Comment on lines
+199
to
+200
|
||||||||||
| // after "[//]: # (web-snippet: " = 22 chars | |
| var substring = lineCurrent[22..]; | |
| const string prefix = "[//]: # (web-snippet:"; | |
| var substring = lineCurrent[prefix.Length..].TrimStart(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| UsedSnippets: [ | ||
| { | ||
| Key: snippet1, | ||
| Language: cs, | ||
| Value: Snippet, | ||
| Error: , | ||
| FileLocation: thePath(1-2), | ||
| IsInError: false | ||
| }, | ||
| { | ||
| Key: snippet2, | ||
| Language: cs, | ||
| Value: Snippet, | ||
| Error: , | ||
| FileLocation: thePath(1-2), | ||
| IsInError: false | ||
| } | ||
| ], | ||
| result: | ||
| [//]: # (snippet: snippet1) | ||
| ```cs | ||
| Snippet | ||
| ``` | ||
| [//]: # (endSnippet) | ||
|
|
||
| some text | ||
|
|
||
| [//]: # (snippet: snippet2) | ||
| ```cs | ||
| Snippet | ||
| ``` | ||
| [//]: # (endSnippet) | ||
|
|
||
| some other text | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| MissingSnippets: [ | ||
| { | ||
| Key: missingKey, | ||
| LineNumber: 2 | ||
| } | ||
| ], | ||
| result: | ||
| [//]: # (snippet: missingKey) | ||
| ``` | ||
| ** Could not find snippet 'missingKey' ** | ||
| ``` | ||
| [//]: # (endSnippet) | ||
| } |
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.
ShouldWriteIncludeOnDiffLinechecks snippet/end markers withStartsWith(...)on the raw line. Since snippet detection elsewhere trims leading whitespace, indented markers like[//]: # (endSnippet)(or<!-- endSnippet -->) won't match here and may cause include markers to be appended to the same line, potentially breaking subsequent snippet processing. Consider applyingTrimStart()before theseStartsWithchecks (including the newly-added link-ref endSnippet check) to align behavior withSnippetKey.Extract*parsing.