Skip to content

Commit

Permalink
Merge pull request #13113 from hasezoey/changeDocsHeaderId
Browse files Browse the repository at this point in the history
Change API documentation headers to be more unique
  • Loading branch information
vkarpov15 committed Mar 22, 2023
2 parents f42df47 + 068d4ff commit 0ddb0f6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/api_split.pug
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends layout
append style
link(rel="stylesheet", href="/docs/css/api.css")
script(src="/docs/js/api-bold-current-nav.js")
script(src="/docs/js/convert-old-anchorid.js")

block content
<a class="edit-docs-link" href="#{editLink}" target="_blank">
Expand Down
71 changes: 71 additions & 0 deletions docs/js/convert-old-anchorid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

window.addEventListener('DOMContentLoaded', () => {
const anchor = window.location.hash;

// only operate on the old id's
if (!/^#\w+_\w+(?:-\w+)?$/i.test(anchor)) {
return;
}

// in case there is no anchor, return without modifying the anchor
if (!anchor) {
return;
}

const splits = anchor.split('_');

// dont modify anything if the splits are not "2"
if (splits.length !== 2) {
return;
}

const secondSplits = splits[1].split('-');

let mainName;
let propName = '';

// there are 2 possibilities:
// "mongoose_Mongoose-property"
// "mongoose_property"
if (secondSplits.length === 2) {
mainName = secondSplits[0];
propName = secondSplits[1];
} else {
// use the part after the "_" directly, because the before is just a lower-cased version
mainName = splits[1];
propName = '';
}

// check to ensure those properties are not empty
if (!mainName) {
return;
}

let tests = [];

// have to use multiple tests, because the old version did not differentiate between functions and properties
if (mainName && propName) {
// have to double the tests here, because the old version did not differentiate between static and instance properties
tests = [
`${mainName}.${propName}`,
`${mainName}.${propName}()`,

`${mainName}.prototype.${propName}`,
`${mainName}.prototype.${propName}()`
];
} else {
tests = [
mainName,
`${mainName}()`
];
}

for (const test of tests) {
// have to use the "[id=]" selector because "#Something()" is not a valid selector (the "()" part)
const header = document.querySelector(`h3[id="${test}"]`);
if (header) {
window.location.hash = `#${test}`;
}
}
}, { once: true });
9 changes: 1 addition & 8 deletions docs/source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,7 @@ function parse() {
ctx.string = ctx.string + '()';
}

// Backwards compat anchors
if (typeof ctx.constructor === 'string') {
ctx.anchorId = `${ctx.constructor.toLowerCase()}_${ctx.constructor}-${ctx.name}`;
} else if (typeof ctx.receiver === 'string') {
ctx.anchorId = `${ctx.receiver.toLowerCase()}_${ctx.receiver}.${ctx.name}`;
} else {
ctx.anchorId = `${ctx.name.toLowerCase()}_${ctx.name}`;
}
ctx.anchorId = ctx.string;

ctx.description = prop.description.full.
replace(/<br \/>/ig, ' ').
Expand Down

0 comments on commit 0ddb0f6

Please sign in to comment.