UNPKG

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