Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlockEnhancer.cs
More file actions
Latest commit
41 lines (35 loc) · 1.78 KB
/
Copy pathCodeBlockEnhancer.cs
File metadata and controls
41 lines (35 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
usingSystem.Text.RegularExpressions;
namespaceShellDocs.Markdown;
publicstaticclassCodeBlockEnhancer
{
privatestaticreadonlyRegexCodeBlock=new(
@"<pre>\s*<code(?<attrs>[^>]*)>(?<code>[\s\S]*?)</code>\s*</pre>",
RegexOptions.Compiled);
privatestaticreadonlyRegexLanguageClass=new(
@"language-(?<lang>[a-zA-Z0-9+#-]+)",
RegexOptions.Compiled);
publicstaticstringEnhance(stringhtml)
{
if(string.IsNullOrEmpty(html))returnhtml;
returnCodeBlock.Replace(html, m =>
{
varattrs=m.Groups["attrs"].Value;
varcode=m.Groups["code"].Value;
varlangMatch=LanguageClass.Match(attrs);
varlangName=langMatch.Success?langMatch.Groups["lang"].Value:"";
varlangBadge=string.IsNullOrEmpty(langName)
?""
:$"<span class=\"shelldocs-code-lang\">{langName}</span>";
return$@"<div class=""shelldocs-codeblock"">
<div class=""shelldocs-codeblock-header"">
{langBadge}
<button type=""button"" class=""shelldocs-codeblock-copy"" onclick=""shelldocsCopyCode(this)"" aria-label=""Copy code"">
<svg class=""icon-copy"" viewBox=""0 0 24 24"" fill=""none"" stroke=""currentColor"" stroke-width=""2"" stroke-linecap=""round"" stroke-linejoin=""round""><rect x=""9"" y=""9"" width=""13"" height=""13"" rx=""2""/><path d=""M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1""/></svg>
<svg class=""icon-check"" viewBox=""0 0 24 24"" fill=""none"" stroke=""currentColor"" stroke-width=""2"" stroke-linecap=""round"" stroke-linejoin=""round""><polyline points=""20 6 9 17 4 12""/></svg>
</button>
</div>
<pre><code{attrs}>{code}</code></pre>
</div>";
});
}
}