如何向JSDoc添加标记

How to add tags to JSDoc?

本文关键字:添加 加标记 JSDoc      更新时间:2023-09-26

我使用的是Meteor,与普通JavaScript相比,它有一些奇怪的警告。我想添加一些标签,以便使文档更明确。

Meteor.methods({
  /**
  * Upgrade a user's role
  *
  * @where Anywhere
  * @rolerequired 'admin'
  *
  * @module Meteor.methods
  * @method Roles.upgrade
  * @param {String|Object} user the userId or the user document to update
  * @param {String} role the role to add the user to
  * @throws Meteor.Error 401 if the user trying to upgrade was not authorized to do so
  * 
  * @example
  * Meteor.call('Roles.upgrade', Meteor.users.findOne(), function (err) {
    if (!err) {
      console.log('User successfully added to role');
    } else {
      Router.error(401);
    }
  })
  */
  'Roles.upgrade': function (user, role) {
    if (Roles.userIsInRole(this.userId, 'admin')) {
      return Roles.addUserToRoles(user, role);
    } else {
      throw new Meteor.Error(401, "Not authorized to upgrade roles")
    }
  }
});

@where@rolerequired更特定于这个基于Meteor的应用程序。@where可以在devdocs.io中看到。

如何添加标签到JSDoc?

是的,可以向JSDoc添加自定义标记。你需要创建一个javascript文件来定义你想要添加的标签:

custom_tags.js:

exports.defineTags = function(dictionary) {
  dictionary.defineTag('where', {
    mustHaveValue: true,
    onTagged : function(doclet, tag) {
      doclet.where = doclet.where || [];
      doclet.where.push(tag.value);
    }
  });
};

然后你需要将位置添加到该javascript文件的conf.json中,该conf.json将你的插件列为路径

的一部分

conf.json:

{
  "plugins": [
    "plugins/test"
  ],
}

最后,您需要更新默认模板的.tmpl文件,以便在生成的文档中呈现您的信息。或者您可以创建自己的模板并定义自己的解析,以便将标记添加到生成的文档中。