Type directives represent every possible javascript construct you might want to document.
All you have to do is create a file describing what your new directive tag looks like and does, and just drop it into the "tags/types" directory - it's that easy.
A common documentation need in JavaScript projects is to document classes with non-standard syntax. Popular frameworks handle class creation using the following pattern:
/*
* we want to document this as being a class
*/
var Person = makeClass(
{
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + " says: " + message;
}
});
Documentjs is flexible enough to let you do this with minimal effort. All you have to do is to add a new type to the existing types folder (documentjs/types). Let's name it make_class.js:
DocumentJS.Type("MakeClass",
/**
* @Static
*/
{
codeMatch: /(\w+)\s*[:=]\s*makeClass\(([^\)]*)/),
/*
* Parses the code to get the class data.
* @param {String} code
* @return {Object} class data
*/
code: function( code ) {
var parts = code.match(this.codeMatch);
if ( parts ) {
return {
name: parts[2],
inherits: parts[1].replace("$.", "jQuery.")
type: "class"
}
}
}
}
There's one final step you must follow to make your custom type work: add it to the list of loaded types in documentjs/types/types.js.
...
'//documentjs/types/function', '//documentjs/types/page', '//documentjs/types/prototype',
'//documentjs/types/script', '//documentjs/types/static', '//documentjs/types/make_class');
And that's it! Now you can write your code using your favorite framework and know that all your classes will be documented correctly for you.