-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(aio): move link disambiguation from
getLinkInfo
to `getDocFro…
…mAlias` (#22494) The disambiguation needs to be done earlier so that the auto-link-code post-processor can benefit from it. PR Close #22494
- Loading branch information
1 parent
94707fe
commit 997b30a
Showing
10 changed files
with
100 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
aio/tools/transforms/links-package/services/deprecatedDocsLinkDisambiguator.js
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
aio/tools/transforms/links-package/services/disambiguators/disambiguateByDeprecated.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function disambiguateByDeprecated() { | ||
return (alias, originatingDoc, docs) => docs.filter(doc => doc.deprecated === undefined); | ||
}; |
17 changes: 17 additions & 0 deletions
17
aio/tools/transforms/links-package/services/disambiguators/disambiguateByDeprecated.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const disambiguateByDeprecated = require('./disambiguateByDeprecated')(); | ||
const docs = [ | ||
{ id: 'doc1' }, | ||
{ id: 'doc2', deprecated: true }, | ||
{ id: 'doc3', deprecated: '' }, | ||
{ id: 'doc4' }, | ||
{ id: 'doc5', deprecated: 'Some text' }, | ||
]; | ||
|
||
describe('disambiguateByDeprecated', () => { | ||
it('should filter out docs whose `deprecated` property is defined', () => { | ||
expect(disambiguateByDeprecated('alias', {}, docs)).toEqual([ | ||
{ id: 'doc1' }, | ||
{ id: 'doc4' }, | ||
]); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
aio/tools/transforms/links-package/services/disambiguators/disambiguateByModule.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = function disambiguateByModule() { | ||
return (alias, originatingDoc, docs) => { | ||
const originatingModule = originatingDoc && originatingDoc.moduleDoc; | ||
if (originatingModule) { | ||
const filteredDocs = docs.filter(doc => doc.moduleDoc === originatingModule); | ||
if (filteredDocs.length > 0) { | ||
return filteredDocs; | ||
} | ||
} | ||
return docs; | ||
}; | ||
}; |
29 changes: 29 additions & 0 deletions
29
aio/tools/transforms/links-package/services/disambiguators/disambiguateByModule.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const disambiguateByModule = require('./disambiguateByModule')(); | ||
const moduleA = { name: 'a' }; | ||
const moduleB = { name: 'b' }; | ||
const docs = [ | ||
{ id: 'doc1', moduleDoc: moduleA }, | ||
{ id: 'doc2', moduleDoc: moduleA }, | ||
{ id: 'doc3', moduleDoc: moduleB }, | ||
]; | ||
|
||
describe('disambiguateByModule', () => { | ||
it('should return all docs if the originating doc has no moduleDoc', () => { | ||
expect(disambiguateByModule('alias', { }, docs)).toEqual(docs); | ||
}); | ||
|
||
it('should return all docs if no docs match the originating doc moduleDoc', () => { | ||
expect(disambiguateByModule('alias', { moduleDoc: { name: 'c' } }, docs)).toEqual(docs); | ||
}); | ||
|
||
it('should return only docs that match the moduleDoc of the originating doc', () => { | ||
expect(disambiguateByModule('alias', { moduleDoc: moduleA }, docs)).toEqual([ | ||
{ id: 'doc1', moduleDoc: moduleA }, | ||
{ id: 'doc2', moduleDoc: moduleA }, | ||
]); | ||
|
||
expect(disambiguateByModule('alias', { moduleDoc: moduleB }, docs)).toEqual([ | ||
{ id: 'doc3', moduleDoc: moduleB }, | ||
]); | ||
}); | ||
}); |
38 changes: 15 additions & 23 deletions
38
aio/tools/transforms/links-package/services/getDocFromAlias.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,23 @@ | ||
var _ = require('lodash'); | ||
|
||
/** | ||
* @dgService getDocFromAlias | ||
* @description Get an array of docs that match this alias, relative to the originating doc. | ||
* | ||
* @property {Array<(alias: string, originatingDoc: Doc, ambiguousDocs: Doc[]) => Doc[]>} disambiguators | ||
* a collection of functions that attempt to resolve ambiguous links. Each disambiguator returns | ||
* a new collection of docs with unwanted ambiguous docs removed (see links-package/service/disambiguators | ||
* for examples). | ||
*/ | ||
module.exports = function getDocFromAlias(aliasMap) { | ||
|
||
return function getDocFromAlias(alias, originatingDoc) { | ||
var docs = aliasMap.getDocs(alias); | ||
|
||
// If there is more than one item with this name then try to filter them by the originatingDoc's | ||
// area | ||
if (docs.length > 1 && originatingDoc && originatingDoc.area) { | ||
docs = _.filter(docs, function(doc) { return doc.area === originatingDoc.area; }); | ||
} | ||
|
||
// If filtering by area left us with none then let's start again | ||
if (docs.length === 0) { | ||
docs = aliasMap.getDocs(alias); | ||
} | ||
|
||
// If there is more than one item with this name then try to filter them by the originatingDoc's | ||
// module | ||
if (docs.length > 1 && originatingDoc && originatingDoc.module) { | ||
docs = _.filter(docs, function(doc) { return doc.module === originatingDoc.module; }); | ||
} | ||
getDocFromAlias.disambiguators = []; | ||
return getDocFromAlias; | ||
|
||
return docs; | ||
}; | ||
function getDocFromAlias(alias, originatingDoc) { | ||
return getDocFromAlias.disambiguators.reduce( | ||
// Run the disambiguators while there is more than 1 doc found | ||
(docs, disambiguater) => docs.length > 1 ? disambiguater(alias, originatingDoc, docs) : docs, | ||
// Start with the docs that match the alias | ||
aliasMap.getDocs(alias) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
aio/tools/transforms/links-package/services/moduleScopeLinkDisambiguator.js
This file was deleted.
Oops, something went wrong.