3 ms·
That's cool that you've found a way to test the code that's embedded in your docs. Google has infrastructure for inserting code from a GitHub repo into docs [1]
by kaycebasques 8y ago
That's cool that you've found a way to test the code that's embedded in your docs. Google has infrastructure for inserting code from a GitHub repo into docs [1] but I haven't heard of anyone extracting the code from the docs and testing it.
One thing that you might want to look into is conditionally displaying parts of the code. For example, suppose that for a given doc, you only need to display one line of code. But to get the code runnable, you need to put in some boilerplate. There should be a way to only display the one line of relevant code.
// START_EMBED
fetch("example.com");
// END_EMBED
// Boilerplate infrastructure that the user doesn't need to see.
new Test().run();
One advantage of the Google approach is that you write the code once, but can insert code one line at a time into the doc. For example, suppose you start off with the full code sample.
const service = new Service();
service.init();
service.doStuff();
service.terminate();
But then you want to discuss the first line:
const service = new Service();
Then the second, and so on. The Google markup looks something like this:
// START_FULL
// START_CONSTRUCTOR
const service = new Service();
// END_CONSTRUCTOR
// START_INIT
service.init();
// END_INIT
service.doStuff();
service.terminate();
// END_FULL
And then to embed a certain snippet into each doc you just do something like this:
{% includecode "https://github.com/.../code.js" snippet="FULL" %}
[1] https://news.ycombinator.com/item?id=17835045 https://news.ycombinator.com/item?id=17835045
- franciscop 8y agoThat is very nice idea as well. I didn't consider that, but definitely I could pull tests from the test files and display some parts of that. I wouldn't make the full START_FULL bit, it'd be a bit overkill for my use case/resources (or I'd simplify it to something like <test-case from="1" to="4">). I think it is important where you start a project as well. Many times I will write the docs even before writing any code (totally or partially). If I think the docs and snippets are going somewhere I'll continue with the code. So in a way, it makes more sense for me to pull the data like docs -> code -> tests than the other way around. That way I know where I want to go, and then I try to get there. Examples of my projects like this [1] [2]. I'll very likely try it though, so thank you! [1] https://superdom.site/ https://superdom.site/ [2] https://github.com/franciscop/magic-promises https://github.com/franciscop/magic-promises