Question

I use asp.net WebApi Help Page to generate the document from comments of source code. And I have used doxygen to generate the document before. The doxygen can parse markdown syntax in the comments and output the well formatted documents. But the WebApi Help Page could not parse markdown syntax now.

For example, the foo function's comments contain Markdown comments, and it will be output as ### Markdown comments *It will return "foo" *It always returns "foo" in WebApi Help Page.

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";
     }
}
Was it helpful?

Solution

Thanks for the hints, have just modified HelpPageApiModel.cshtml

1) Install some Markdown library from NuGet, like MarkdownDeep

2) Add Helper function. Notice, you should trim lines, as <summery> is parsed as-is with all trailing whitespaces after newlines. Otherwise all markdown lists etc, wont be correctly parsed.

@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) Preprocess the documentation string output

<p>@Html.Raw(ToMarkdown(description.Documentation))</p>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top