Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 285ecf4

Browse files
petebacondarwinwardbell
authored andcommitted
chore(api-builder): add configurable link disambuators & put to work
closes #1852 Add configurable link disambuators Add a service to disambiguate docs by module Add a service to disambiguate docs that are deprecated
1 parent 4da23bf commit 285ecf4

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

tools/api-builder/links-package/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ module.exports = new Package('links', [])
1010
.factory(require('dgeni-packages/links/services/getDocFromAlias'))
1111
.factory(require('./services/getLinkInfo'))
1212
.factory(require('./services/parseArgString'))
13+
.factory(require('./services/moduleScopeLinkDisambiguator'))
14+
.factory(require('./services/deprecatedDocsLinkDisambiguator'))
1315
.factory(require('./services/getApiFragmentFileName'))
1416

1517
.config(function(inlineTagProcessor, linkInlineTagDef, linkDocsInlineTagDef, exampleInlineTagDef, exampleTabsInlineTagDef) {
1618
inlineTagProcessor.inlineTagDefinitions.push(linkInlineTagDef);
1719
inlineTagProcessor.inlineTagDefinitions.push(linkDocsInlineTagDef);
1820
inlineTagProcessor.inlineTagDefinitions.push(exampleInlineTagDef);
1921
inlineTagProcessor.inlineTagDefinitions.push(exampleTabsInlineTagDef);
22+
})
23+
24+
.config(function(getLinkInfo, moduleScopeLinkDisambiguator, deprecatedDocsLinkDisambiguator) {
25+
getLinkInfo.disambiguators.push(moduleScopeLinkDisambiguator);
26+
getLinkInfo.disambiguators.push(deprecatedDocsLinkDisambiguator);
2027
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var _ = require('lodash');
2+
3+
module.exports = function deprecatedDocsLinkDisambiguator() {
4+
return function(url, title, currentDoc, docs) {
5+
if (docs.length != 2) return docs;
6+
7+
var filteredDocs = _.filter(docs, function(doc) {
8+
return !doc.fileInfo.relativePath.match(/\/(\w+)-deprecated\//);
9+
});
10+
11+
return filteredDocs.length > 0 ? filteredDocs : docs;
12+
};
13+
};

tools/api-builder/links-package/services/getLinkInfo.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ var path = require('canonical-path');
1010
* @return {Object} The link information
1111
*
1212
* @property {boolean} relativeLinks Whether we expect the links to be relative to the originating doc
13+
* @property {array<function(url, title, currentDoc, ambiguousDocs) : array} disambiguators a collection of functions
14+
* that attempt to resolve ambiguous links. Each disambiguator returns a new collection of docs with
15+
* unwanted ambiguous docs removed (see moduleScopeLinkDisambiguator service for an example).
1316
*/
1417
module.exports = function getLinkInfo(getDocFromAlias, encodeCodeBlock, log) {
1518

16-
return function getLinkInfoImpl(url, title, currentDoc) {
19+
getLinkInfoImpl.disambiguators = [];
20+
21+
return getLinkInfoImpl;
22+
23+
function getLinkInfoImpl(url, title, currentDoc) {
1724
var linkInfo = {
1825
url: url,
1926
type: 'url',
@@ -27,6 +34,11 @@ module.exports = function getLinkInfo(getDocFromAlias, encodeCodeBlock, log) {
2734

2835
var docs = getDocFromAlias(url, currentDoc);
2936

37+
// Give each disambiguator a chance to reduce the number of ambiguous docs
38+
docs = getLinkInfoImpl.disambiguators.reduce(function(docs, disambiguator) {
39+
return disambiguator(url, title, currentDoc, docs);
40+
}, docs);
41+
3042
if ( !getLinkInfoImpl.useFirstAmbiguousLink && docs.length > 1 ) {
3143

3244
linkInfo.valid = false;
@@ -68,4 +80,5 @@ module.exports = function getLinkInfo(getDocFromAlias, encodeCodeBlock, log) {
6880

6981
return linkInfo;
7082
};
83+
7184
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var _ = require('lodash');
2+
3+
module.exports = function moduleScopeLinkDisambiguator() {
4+
return function(url, title, currentDoc, docs) {
5+
if (docs.length > 1) {
6+
// filter out target docs that are not in the same module as the source doc
7+
var filteredDocs = _.filter(docs, function(doc) {
8+
return doc.moduleDoc === currentDoc.moduleDoc;
9+
});
10+
// if all target docs are in a different module then just return the full collection of ambiguous docs
11+
return filteredDocs.length > 0 ? filteredDocs : docs;
12+
}
13+
return docs;
14+
};
15+
};

0 commit comments

Comments
 (0)