mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00

* Adding a dark mode * Adding reference for types to summary pages * Adding API Reference * Adding modifiers to methods/fields/etc * Minimizing files input * Caching a lot of the output pages * Cache only for each execution * Adding API references to existing docs
43 lines
1.6 KiB
C#
43 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Docs.Extensions;
|
|
using Docs.Utilities;
|
|
using Microsoft.CodeAnalysis;
|
|
using Statiq.CodeAnalysis;
|
|
using Statiq.Common;
|
|
|
|
namespace Docs.Shortcodes;
|
|
|
|
public class ExampleSnippet : Shortcode
|
|
{
|
|
protected const string Solution = nameof(Solution);
|
|
protected const string Project = nameof(Project);
|
|
protected const string Symbol = nameof(Symbol);
|
|
protected const string BodyOnly = nameof(BodyOnly);
|
|
|
|
public override async Task<ShortcodeResult> ExecuteAsync(KeyValuePair<string, string>[] args, string content,
|
|
IDocument document, IExecutionContext context)
|
|
{
|
|
var props = args.ToDictionary(Solution, Project, Symbol, BodyOnly);
|
|
var symbolName = props.GetString(Symbol);
|
|
var bodyOnly = props.Get<bool?>(BodyOnly) ?? symbolName.StartsWith("m:", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
if (!context.TryGetCommentIdDocument(symbolName, out var apiDocument, out _))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var options = HighlightService.HighlightOption.All;
|
|
if (bodyOnly)
|
|
{
|
|
options = HighlightService.HighlightOption.Body;
|
|
}
|
|
|
|
var comp = apiDocument.Get<Compilation>(CodeAnalysisKeys.Compilation);
|
|
var symbol = apiDocument.Get<ISymbol>(CodeAnalysisKeys.Symbol);
|
|
var highlightElement = await HighlightService.Highlight(comp, symbol, options);
|
|
ShortcodeResult shortcodeResult = $"<pre><code>{highlightElement}</code></pre>";
|
|
return shortcodeResult;
|
|
}
|
|
} |