Ardit Meti Ardit Meti - 4 months ago 28
Javascript Question

How to write space in default values of properties in jsdoc

I can't find a way to print space when documenting properties default values in JSDOC. Example:

/**
* @prop {string} str='String with space' - The string.
*/


This will be documented as:

Name Type Default Description
str string 'String with space' - The string


Any suggestion how to do the proper one?

Answer

I don't quite have the solution, but today I was facing the same issue and found a work-around. :) The problem is that the current jsdoc parser (and regex) only handles this correctly if we use the optional brackets. And on this case, we want a default not being optional.

Inside publish.js of your template add this. I use the " - " as separator, so make this work, use " - " on your descriptions and this will post-process the parser correctly.

data().each(function(doclet) {
    if (doclet.properties) {
        doclet.properties = doclet.properties.map(function(property) {
            var separator = " - ",
                separatorLength = separator.length;

            var defaultvalue = property.defaultvalue;
            var description = property.description;

            if( property.defaultvalue !== 'undefined' && !property.optional && description.indexOf(separator) > 0) {
                var index = description.indexOf(separator);
                defaultvalue += " " + description.substr(separatorLength, index-separatorLength);
                description = "<p>" + description.substr(index + separatorLength, description.length);
            }

            return {
                defaultvalue: defaultvalue,
                description: description,
                type: property.type,
                name: property.name
            }  
        });                  
    }
});
Comments