[英]ASP.NET Web API Help Page with markdown comments
我使用asp.net WebApi幫助頁面從源代碼的注釋生成文檔。 而且我以前曾使用doxygen生成文檔。 doxygen可以解析注釋中的markdown語法並輸出格式正確的文檔。 但是, WebApi幫助頁面現在無法解析markdown語法。
例如, foo
函數的注釋包含Markdown注釋 ,它將作為### Markdown comments *It will return "foo" *It always returns "foo"
輸出### Markdown comments *It will return "foo" *It always returns "foo"
在WebApi幫助頁面中 ### Markdown comments *It will return "foo" *It always returns "foo"
。
public MyApiController : ApiController {
///<summary>
/// It will return "foo"
/// ### Markdown comments
/// * It will return "foo"
/// * It always returns "foo"
///</summary>
[HttpPost, ActionName("foo")]
public string Foo() {
return "foo";
}
}
感謝您的提示,剛剛修改了HelpPageApiModel.cshtml
1)從NuGet安裝一些Markdown庫,例如MarkdownDeep
2)添加助手功能。 請注意,您應該修剪行,因為<summery>
會按原樣解析,並且在換行符之后的所有尾隨空格都將被解析。 否則,所有降價清單等都將無法正確解析。
@functions {
string ToMarkdown(string str)
{
var lines = str.Split('\n');
var whitespaceCount = 0;
var i = 0; //from 2. Line
var imax = lines.Count();
while (++i < imax)
{
var line = lines[i];
if (whitespaceCount != 0)
{
lines[i] = line.Substring(whitespaceCount);
continue;
}
var trimmed = line.TrimStart();
if (trimmed.Length == 0 || trimmed == line) continue;
whitespaceCount = line.Length - trimmed.Length;
i--;
}
str = string.Join("\n", lines);
var md = new MarkdownDeep.Markdown {ExtraMode = true, SafeMode = false};
return md.Transform(str);
}
}
3)預處理文檔字符串輸出
<p>@Html.Raw(ToMarkdown(description.Documentation))</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.