UNPKG

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