Skip to content

Commit cacdc02

Browse files
authored
Merge pull request #12144 from hasezoey/addSee
Add "@see" handling and output to documentation
2 parents a1c63ba + 21b4818 commit cacdc02

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

docs/api_split.pug

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ block content
5353
if prop.deprecated
5454
<span class="deprecated">~DEPRECATED~</span>
5555
if prop.param != null
56-
h5 Parameters
56+
h5 Parameters:
5757
ul.params
5858
each param in prop.param
5959
- if (param.nested)
@@ -77,5 +77,10 @@ block content
7777
h5 Inherits:
7878
ul
7979
li <span class="method-type">&laquo;#{prop.inherits}&raquo;</span>
80+
if prop.see != null
81+
h5 See:
82+
ul
83+
each see in prop.see
84+
li <span class="method-type"><a href="#{see.url}">#{see.text}</a></span>
8085
div
8186
| !{prop.description}

docs/source/api.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ for (const file of files) {
6363

6464
parse();
6565

66+
/**
67+
* @typedef {Object} SeeObject
68+
* @property {String} text The text to display the link as
69+
* @property {String} [url] The link the text should have as href
70+
*/
71+
6672
/**
6773
* @typedef {Object} PropContext
6874
* @property {boolean} [isStatic] Defines wheter the current property is a static property (not mutually exlusive with "isInstance")
@@ -77,6 +83,7 @@ parse();
7783
* @property {string} [anchorId] Defines the Anchor ID to be used for linking
7884
* @property {string} [description] Defines the Description the property will be listed with
7985
* @property {string} [deprecated] Defines wheter the current Property is signaled as deprecated
86+
* @property {SeeObject[]} [see] Defines all "@see" references
8087
*/
8188

8289
function parse() {
@@ -133,6 +140,40 @@ function parse() {
133140

134141
for (const tag of prop.tags) {
135142
switch (tag.type) {
143+
case 'see':
144+
if (!Array.isArray(ctx.see)) {
145+
ctx.see = [];
146+
}
147+
148+
// for this type, it needs to be parsed from the string itself to support more than 1 word
149+
// this is required because "@see" is kinda badly defined and mongoose uses a slightly customized way (longer text and different kinds of links)
150+
151+
// the following regex matches cases of:
152+
// "External Links http://someurl.com/" -> "External Links"
153+
// "External https://someurl.com/" -> "External"
154+
// "Id href #some_Some-method" -> "Id href"
155+
// "Local Absolute /docs/somewhere" -> "Local Absolute"
156+
// "No Href" -> "No Href"
157+
// "https://someurl.com" -> "" (fallback added)
158+
// "Some#Method #something" -> "Some#Method"
159+
// The remainder is simply taken by a call to "slice" (also the text is trimmed later)
160+
const textMatches = /^(.*? (?=#|\/|(?:https?:)|$))/i.exec(tag.string);
161+
162+
let text;
163+
if (textMatches === null || textMatches === undefined) {
164+
// warn because this most likely is a badly defined "@see"
165+
console.warn(`No Text Matches found in @see for "${ctx.constructor}.${ctx.name}"`)
166+
break;
167+
} else {
168+
text = textMatches[1].trim();
169+
}
170+
171+
const url = tag.string.slice(text.length).trim();
172+
ctx.see.push({
173+
text: text || 'No Description', // fallback text, so that the final text does not end up as a empty element that cannot be seen
174+
url: url || undefined, // change to be "undefined" if text is empty or non-valid
175+
});
176+
break;
136177
case 'receiver':
137178
console.warn(`Found "@receiver" tag in ${ctx.constructor} ${ctx.name}`);
138179
break;

lib/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ Schema.prototype.post = function(name) {
16981698
*
16991699
* @param {Function} plugin The Plugin's callback
17001700
* @param {Object} [opts] Options to pass to the plugin
1701-
* @see plugins
1701+
* @see plugins /docs/plugins.html
17021702
* @api public
17031703
*/
17041704

0 commit comments

Comments
 (0)