Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docgen/generate-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const contentPath = path.resolve(`${__dirname}/content-sources`);
const tempHomePath = path.resolve(`${contentPath}/HOME_TEMP.md`);
const devsitePath = `/docs/reference/functions/`;

const { JSDOM } = require("jsdom");

const typeMap = require('./type-aliases.json');

/**
* Strips path prefix and returns only filename.
* @param {string} path
Expand Down Expand Up @@ -103,6 +107,7 @@ function renameFiles() {
*/
function fixLinks(file) {
return fs.readFile(file, 'utf8').then(data => {
data = addTypeAliasLinks(data);
const flattenedLinks = data
.replace(/\.\.\//g, '')
.replace(/(modules|interfaces|classes)\//g, '')
Expand All @@ -116,6 +121,35 @@ function fixLinks(file) {
});
}

/**
* Adds links to external documentation for type aliases that
* reference an external library.
*
* @param data File data to add external library links to.
*/
function addTypeAliasLinks(data) {
const htmlDom = new JSDOM(data);
/**
* Select .tsd-signature-type because all potential external
* links will have this identifier.
*/
const fileTags = htmlDom.window.document.querySelectorAll(".tsd-signature-type");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not immediately clear why we're selecting .tsd-signature-type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment here explaining why this tag, so that the next contributor knows the context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding what I think Erik was doing -- just picking an ID to iterate through that would always be associated with potential external types in TypeDoc.

fileTags.forEach(tag => {
const mapping = typeMap[tag.textContent];
if (mapping) {
console.log('Adding link to '+tag.textContent+" documentation.");

// Add the corresponding document link to this type
const linkChild = htmlDom.window.document.createElement('a');
linkChild.setAttribute('href', mapping);
linkChild.textContent = tag.textContent;
tag.textContent = null;
tag.appendChild(linkChild);
}
});
return htmlDom.serialize();
}

let tocText = '';

/**
Expand Down
5 changes: 5 additions & 0 deletions docgen/type-aliases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"firebase.firestore.DocumentSnapshot": "https://googleapis.dev/nodejs/firestore/latest/DocumentSnapshot.html",
"firebase.auth.UserRecord": "https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord.html",
"firebase.auth.UserInfo": "https://firebase.google.com/docs/reference/admin/node/admin.auth.UserInfo.html"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"firebase-admin": "^8.2.0",
"istanbul": "^0.4.5",
"js-yaml": "^3.13.1",
"jsdom": "^15.2.0",
"mocha": "^6.1.4",
"mock-require": "^3.0.3",
"mz": "^2.7.0",
Expand Down