Skip to content

Instantly share code, notes, and snippets.

@tintoy
Last active January 14, 2018 04:16
Show Gist options
  • Save tintoy/9a86e4185b24cc74f3feda50ee58f715 to your computer and use it in GitHub Desktop.
Save tintoy/9a86e4185b24cc74f3feda50ee58f715 to your computer and use it in GitHub Desktop.
DocFX template to produce JSON for consumption in Razor views

DocFX Razor

This is how you get the DocFX view model in JSON form. Now you can create classes to represent it, and deserialise using Newtonsoft.Json. Make that the model for your Razor views, and have an MVC action that can select the appropriate JSON file, deserialise it, and pass that to View() as the model.

var common = require('./common.js');
var extension = require('./conceptual.extension.js')
var util = require('./statictoc.util.js');
exports.transform = function (model) {
if (extension && extension.preTransform) {
model = extension.preTransform(model);
}
model._disableToc = model._disableToc || !model._tocPath || (model._navPath === model._tocPath);
model.docurl = model.docurl || common.getImproveTheDocHref(model, model._gitContribute, model._gitUrlPattern);
model = util.setToc(model);
if (extension && extension.postTransform) {
model = extension.postTransform(model);
}
// AF: yesyesyes, super-hacky; I should feel dirty, etc. BUT...
// The TOC is actually a DynamicExpandoObject from DocFX managed code; it contains cycles and so JSON.stringify(model) fails; but not anymore (muhahaha!)
model._toc = JSON.parse(JSON.stringify(model._toc));
model._nav = JSON.parse(JSON.stringify(model._nav));
// Our template just spits out this property.
model.rawJson = JSON.stringify(model, null, ' ');
return model;
}
{{!master(_raw.tmpl)}}
{{{rawJson}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment