markdown.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Translate doclet descriptions from Markdown into HTML.
  3. *
  4. * @module plugins/markdown
  5. */
  6. 'use strict';
  7. var env = require('jsdoc/env');
  8. var config = env.conf.markdown || {};
  9. var defaultTags = [
  10. 'author',
  11. 'classdesc',
  12. 'description',
  13. 'exceptions',
  14. 'params',
  15. 'properties',
  16. 'returns',
  17. 'see',
  18. 'summary'
  19. ];
  20. var hasOwnProp = Object.prototype.hasOwnProperty;
  21. var parse = require('jsdoc/util/markdown').getParser();
  22. var tags = [];
  23. var excludeTags = [];
  24. function shouldProcessString(tagName, text) {
  25. var shouldProcess = true;
  26. // we only want to process `@author` and `@see` tags that contain Markdown links
  27. if ( (tagName === 'author' || tagName === 'see') && text.indexOf('[') === -1 ) {
  28. shouldProcess = false;
  29. }
  30. return shouldProcess;
  31. }
  32. /**
  33. * Process the markdown source in a doclet. The properties that should be processed are
  34. * configurable, but always include "author", "classdesc", "description", "exceptions", "params",
  35. * "properties", "returns", and "see". Handled properties can be bare strings, objects, or arrays
  36. * of objects.
  37. */
  38. function process(doclet) {
  39. tags.forEach(function(tag) {
  40. if ( !hasOwnProp.call(doclet, tag) ) {
  41. return;
  42. }
  43. if (typeof doclet[tag] === 'string' && shouldProcessString(tag, doclet[tag]) ) {
  44. doclet[tag] = parse(doclet[tag]);
  45. }
  46. else if ( Array.isArray(doclet[tag]) ) {
  47. doclet[tag].forEach(function(value, index, original) {
  48. var inner = {};
  49. inner[tag] = value;
  50. process(inner);
  51. original[index] = inner[tag];
  52. });
  53. }
  54. else if (doclet[tag]) {
  55. process(doclet[tag]);
  56. }
  57. });
  58. }
  59. // set up the list of "tags" (properties) to process
  60. if (config.tags) {
  61. tags = config.tags.slice();
  62. }
  63. // set up the list of default tags to exclude from processing
  64. if (config.excludeTags) {
  65. excludeTags = config.excludeTags.slice();
  66. }
  67. defaultTags.forEach(function(tag) {
  68. if (excludeTags.indexOf(tag) === -1 && tags.indexOf(tag) === -1) {
  69. tags.push(tag);
  70. }
  71. });
  72. exports.handlers = {
  73. /**
  74. * Translate Markdown syntax in a new doclet's description into HTML. Is run
  75. * by JSDoc 3 whenever a "newDoclet" event fires.
  76. */
  77. newDoclet: function(e) {
  78. process(e.doclet);
  79. }
  80. };