diff --git a/config/jsdoc/api/template/publish.js b/config/jsdoc/api/template/publish.js index e9a479d893..bac92a51a3 100644 --- a/config/jsdoc/api/template/publish.js +++ b/config/jsdoc/api/template/publish.js @@ -17,7 +17,6 @@ const taffy = require('taffydb').taffy; const handle = require('jsdoc/lib/jsdoc/util/error').handle; const helper = require('jsdoc/lib/jsdoc/util/templateHelper'); const htmlsafe = helper.htmlsafe; -const linkto = helper.linkto; const resolveAuthorLinks = helper.resolveAuthorLinks; const outdir = env.opts.destination; @@ -32,6 +31,78 @@ function find(spec) { return helper.find(data, spec); } +function getShortName(longname) { + if (!longname.includes('module:ol/')) { + return longname; + } + if (longname.includes('|')) { + return longname; + } + if (longname.includes('<')) { + return longname; + } + return longname.split(/[\~\.#]/).pop(); +} + +function linkto(longname, linkText, cssClass, fragmentId) { + if (linkText) { + return helper.linkto(longname, linkText, cssClass, fragmentId); + } + + if (!longname.includes('module:ol/')) { + return helper.linkto(longname, linkText, cssClass, fragmentId); + } + + // check for `Array` types (but allow `Array|Array` types) + let openBrackets = 0; + let parseTypes = false; + for (const c of longname) { + if (c === '<') { + openBrackets += 1; + continue; + } + if (c === '>') { + openBrackets -= 1; + continue; + } + if (openBrackets > 0 && c === '|') { + parseTypes = true; + break; + } + } + if (parseTypes) { + // collections or generics with unions get parsed by catharsis and + // will unfortunamely include long module:ol/foo names + return helper.linkto(longname, '', cssClass, fragmentId); + } + + // handle union types + if (longname.includes('|')) { + return longname + .split('|') + .map((part) => linkto(part, '', cssClass, fragmentId)) + .join(' | '); + } + + const match = longname.match(/(.+?)\.?<(.+)>$/); + // handle generics and collections + if (match) { + return ( + linkto(match[1], '', cssClass, fragmentId) + + '<' + + linkto(match[2], '', cssClass, fragmentId) + + '>' + ); + } + + return helper.linkto( + longname, + htmlsafe(getShortName(longname)), + cssClass, + fragmentId + ); +} + function tutoriallink(tutorial) { return helper.toTutorial(tutorial, null, { tag: 'em', @@ -85,8 +156,37 @@ function addSignatureParams(f) { f.signature = (f.signature || '') + '(' + params.join(', ') + ')'; } +/** + * Copied from https://github.com/jsdoc/jsdoc/blob/main/packages/jsdoc/lib/jsdoc/util/templateHelper.js + * Modified to call our own `linkto` to shorten names. + * @param {Object} doclet The doclet. + * @param {Array} [doclet.yields] The returns. + * @param {Array} [doclet.returns] The returns. + * @param {string} cssClass The css class. + * @return {Array} The returns. + */ +function getSignatureReturns({yields, returns}, cssClass) { + let returnTypes = []; + + if (yields || returns) { + (yields || returns).forEach((r) => { + if (r && r.type && r.type.names) { + if (!returnTypes.length) { + returnTypes = r.type.names; + } + } + }); + } + + if (returnTypes && returnTypes.length) { + returnTypes = returnTypes.map((r) => linkto(r, '', cssClass)); + } + + return returnTypes; +} + function addSignatureReturns(f) { - const returnTypes = helper.getSignatureReturns(f); + const returnTypes = getSignatureReturns(f); f.signature = '' + (f.signature || '') + ''; @@ -136,6 +236,13 @@ function getPathFromDoclet(doclet) { return filepath; } +function preprocessLinks(text) { + return text.replaceAll( + /\{@link (module:ol\/\S+?)\}/g, + (match, longname) => `{@link ${longname} ${getShortName(longname)}}` + ); +} + function generate(title, docs, filename, resolveLinks) { resolveLinks = resolveLinks === false ? false : true; @@ -150,7 +257,7 @@ function generate(title, docs, filename, resolveLinks) { let html = view.render('container.tmpl', docData); if (resolveLinks) { - html = helper.resolveLinks(html); // turn {@link foo} into foo + html = helper.resolveLinks(preprocessLinks(html)); // turn {@link foo} into foo } fs.writeFileSync(outpath, html, 'utf8'); diff --git a/config/jsdoc/api/template/tmpl/container.tmpl b/config/jsdoc/api/template/tmpl/container.tmpl index b9ef45b5ae..4a0b1d4b38 100644 --- a/config/jsdoc/api/template/tmpl/container.tmpl +++ b/config/jsdoc/api/template/tmpl/container.tmpl @@ -85,7 +85,7 @@ ?>

Subclasses

    -
  • +
@@ -95,7 +95,7 @@

Extends

    -
  • +
@@ -103,7 +103,7 @@

Mixes In

    -
  • +
@@ -111,7 +111,7 @@

Requires

    -
  • +
diff --git a/config/jsdoc/api/template/tmpl/members.tmpl b/config/jsdoc/api/template/tmpl/members.tmpl index 19f224e3d5..6412895305 100644 --- a/config/jsdoc/api/template/tmpl/members.tmpl +++ b/config/jsdoc/api/template/tmpl/members.tmpl @@ -5,7 +5,7 @@ var typeSignature = ''; if (data.type && data.type.names) { data.type.names.forEach(function (name) { - typeSignature += '{' + self.linkto(name, self.htmlsafe(name)) + '} '; + typeSignature += '{' + self.linkto(name) + '} '; }); } ?> diff --git a/config/jsdoc/api/template/tmpl/method.tmpl b/config/jsdoc/api/template/tmpl/method.tmpl index 4fdd023627..d09e9276f8 100644 --- a/config/jsdoc/api/template/tmpl/method.tmpl +++ b/config/jsdoc/api/template/tmpl/method.tmpl @@ -56,7 +56,7 @@ if (/-dev$/.test(version)) {
This:
-
+
diff --git a/config/jsdoc/api/template/tmpl/returns.tmpl b/config/jsdoc/api/template/tmpl/returns.tmpl index 41a25b788b..e72ce80907 100644 --- a/config/jsdoc/api/template/tmpl/returns.tmpl +++ b/config/jsdoc/api/template/tmpl/returns.tmpl @@ -52,7 +52,7 @@ if (returns.length > 1) { - + | - + | \ No newline at end of file