UNPKG

2.39 kBJavaScriptView Raw
1/**
2 * Translate doclet descriptions from Markdown into HTML.
3 *
4 * @module plugins/markdown
5 */
6'use strict';
7
8var env = require('jsdoc/env');
9
10var config = env.conf.markdown || {};
11var defaultTags = [
12 'author',
13 'classdesc',
14 'description',
15 'exceptions',
16 'params',
17 'properties',
18 'returns',
19 'see',
20 'summary'
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 processed are
40 * configurable, but always include "author", "classdesc", "description", "exceptions", "params",
41 * "properties", "returns", and "see". Handled properties can be bare strings, objects, or arrays
42 * 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
57 inner[tag] = value;
58 process(inner);
59 original[index] = inner[tag];
60 });
61 }
62 else if (doclet[tag]) {
63 process(doclet[tag]);
64 }
65 });
66}
67
68// set up the list of "tags" (properties) to process
69if (config.tags) {
70 tags = config.tags.slice();
71}
72// set up the list of default tags to exclude from processing
73if (config.excludeTags) {
74 excludeTags = config.excludeTags.slice();
75}
76defaultTags.forEach(function(tag) {
77 if (excludeTags.indexOf(tag) === -1 && tags.indexOf(tag) === -1) {
78 tags.push(tag);
79 }
80});
81
82exports.handlers = {
83 /**
84 * Translate Markdown syntax in a new doclet's description into HTML. Is run
85 * by JSDoc 3 whenever a "newDoclet" event fires.
86 */
87 newDoclet: function(e) {
88 process(e.doclet);
89 }
90};