Question: Is it possible to extract code for use in the examples in API documentation
I have seen that it is possible to extract code snippets from code files for use in the general documentation. What I would really like is to be able to do this in API documentation as well. I know I can add examples in the xml documentation sections but those examples are encoded in the XML so they cannot automatically be compiled and tested as part of the solution. What I'm after is to:
- Have a project that contains all the example code. This code can be compiled and tested so that I know the examples work.
- Have some way of indicating to DocFX that it should extract a given part of a specific file as an example for the documentation of a class, method, property etc.. Essentially the same as what DocFX can do with code snippets but in API documentation.
So the question is what is the best way of achieving this with the current version of DocFX.
Hi @pvandervelde , you can try Overwrite Files: uid to indicate the API, and put code snippets in Markdown content.
We can handle one example in overwrite files, as following example:
---
uid: Your.Uid
example: [*content]
---
[!Code-csharp[any title(your code file?options)]
I'd love to see an example of this, as I'm finding it hard to get working. I thought maybe I didn't understand overwrites in general, but I was able to overwrite the remarks and summary for my method. So I thought it might be that I didn't understand the syntax for including the sample code, but no experimentation with that would work. Even using hardcoded text in the overwrite file didn't effect the example node. I'm guessing, but it's purely a guess, that the issue here has something to do with example being an array? Anybody that has gotten this to work can point me at some sample code, please.
I'd also like to say that while this could work, it's awkward in comparison to the way this was done in Sandcastle. Including a "source" attribute on <code> tags in the doc comment just makes more sense to me. Yet another feature of Sandcastle that we're missing that I wish just worked.
+1 here
I'm trying to include code samples in API documentation but no matter what I tried to put to
We've fixed an issue which causes overwriting example failing in dev branch. Here is an example repo.