Skip to content

Commit c5d3a7b

Browse files
esmacikegilmorez
authored andcommitted
Linking to external types - docgen (#549)
* First version of addTypeAliasLinks that will scan TypeDoc HTML output and insert links to external library documentation. * Updated link for UserRecord and UserInfo * Resolving a few of Hiranya's comments. Removing unecessary debugging console log, JSDOM is now imported with one line, and Map of types and links is now created from a JSON file, Still need to resolve a couple of Hiranya's comments. * Ran npm run format:fix * Resolving more of Hiranya's comments. Loading JSON file uses 'require' rather than 'read file'. JSON file is now simpler. Script only replaces fully qualified names rather than aliases in document. * Adding comment as requested by thechenky. * Updating package.json to include JSDom module required by doc generation script.
1 parent 3018b60 commit c5d3a7b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

docgen/generate-docs.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const contentPath = path.resolve(`${__dirname}/content-sources`);
3939
const tempHomePath = path.resolve(`${contentPath}/HOME_TEMP.md`);
4040
const devsitePath = `/docs/reference/functions/`;
4141

42+
const { JSDOM } = require("jsdom");
43+
44+
const typeMap = require('./type-aliases.json');
45+
4246
/**
4347
* Strips path prefix and returns only filename.
4448
* @param {string} path
@@ -103,6 +107,7 @@ function renameFiles() {
103107
*/
104108
function fixLinks(file) {
105109
return fs.readFile(file, 'utf8').then(data => {
110+
data = addTypeAliasLinks(data);
106111
const flattenedLinks = data
107112
.replace(/\.\.\//g, '')
108113
.replace(/(modules|interfaces|classes)\//g, '')
@@ -116,6 +121,35 @@ function fixLinks(file) {
116121
});
117122
}
118123

124+
/**
125+
* Adds links to external documentation for type aliases that
126+
* reference an external library.
127+
*
128+
* @param data File data to add external library links to.
129+
*/
130+
function addTypeAliasLinks(data) {
131+
const htmlDom = new JSDOM(data);
132+
/**
133+
* Select .tsd-signature-type because all potential external
134+
* links will have this identifier.
135+
*/
136+
const fileTags = htmlDom.window.document.querySelectorAll(".tsd-signature-type");
137+
fileTags.forEach(tag => {
138+
const mapping = typeMap[tag.textContent];
139+
if (mapping) {
140+
console.log('Adding link to '+tag.textContent+" documentation.");
141+
142+
// Add the corresponding document link to this type
143+
const linkChild = htmlDom.window.document.createElement('a');
144+
linkChild.setAttribute('href', mapping);
145+
linkChild.textContent = tag.textContent;
146+
tag.textContent = null;
147+
tag.appendChild(linkChild);
148+
}
149+
});
150+
return htmlDom.serialize();
151+
}
152+
119153
let tocText = '';
120154

121155
/**

docgen/type-aliases.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"firebase.firestore.DocumentSnapshot": "https://googleapis.dev/nodejs/firestore/latest/DocumentSnapshot.html",
3+
"firebase.auth.UserRecord": "https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord.html",
4+
"firebase.auth.UserInfo": "https://firebase.google.com/docs/reference/admin/node/admin.auth.UserInfo.html"
5+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"firebase-admin": "^8.2.0",
5959
"istanbul": "^0.4.5",
6060
"js-yaml": "^3.13.1",
61+
"jsdom": "^15.2.0",
6162
"mocha": "^6.1.4",
6263
"mock-require": "^3.0.3",
6364
"mz": "^2.7.0",

0 commit comments

Comments
 (0)