如何在jsdoc的属性默认值中写space

How to write space in default values of properties in jsdoc

在 JSDOC 中记录属性默认值时,我找不到打印 space 的方法。例子: /** * @prop {string} str='String with space' - The string. */

这将记录为:

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

有什么正确的建议吗?

我不太清楚解决方案,但今天我遇到了同样的问题并找到了解决方法。 :) 问题是当前的 jsdoc 解析器(和正则表达式)只有在我们使用可选括号时才能正确处理。在这种情况下,我们希望默认值不是可选的。

在模板的 publish.js 内添加此内容。我使用“-”作为分隔符,所以让它工作,在您的描述中使用“-”,这将post-正确处理解析器。

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
            }  
        });                  
    }
});

至少从 jsdoc 3.3.0 开始你可以这样做。

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