Skip to content

Commit afa922b

Browse files
authored
Merge pull request #378 from Dharmendra205111014/bugfix/dynamic-spec-url-change
Bugfix/dynamic spec url change
2 parents 2bd2d6a + 4d4c4d0 commit afa922b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/rapidoc.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import NavStyles from '@/styles/nav-styles';
2323
import InfoStyles from '@/styles/info-styles';
2424
import CustomStyles from '@/styles/custom-styles';
2525
import {
26-
pathIsInSearch, invalidCharsRegEx, sleep, rapidocApiKey, advanceSearch,
26+
pathIsInSearch, invalidCharsRegEx, sleep, rapidocApiKey, advanceSearch, hasValidPathInUrlHash,
2727
} from '@/utils/common-utils';
2828
import ProcessSpec from '@/utils/spec-parser';
2929
import mainBodyTemplate from '@/templates/main-body-template';
@@ -599,6 +599,27 @@ export default class RapiDoc extends LitElement {
599599
}
600600
}
601601

602+
resetSelectedContentId() {
603+
// No content is selected at start
604+
this.selectedContentId = '';
605+
// If there is hash in url then check if hash belong to any of the path in spec
606+
if (window.location.hash) {
607+
this.selectedContentId = window.location.hash.substring(1).startsWith('overview--')
608+
? 'overview' : hasValidPathInUrlHash(this.resolvedSpec.tags)
609+
? window.location.hash.substring(1) : '';
610+
}
611+
// If there is no matching hash to path, check if there is sufficient data to display overview otherwise just display first path from first tag
612+
if (!this.selectedContentId) {
613+
if (this.showInfo === 'true' && (this.resolvedSpec.info?.description || this.resolvedSpec.info?.title)) {
614+
this.selectedContentId = 'overview';
615+
} else {
616+
this.selectedContentId = `${this.resolvedSpec.tags[0]?.paths[0]?.method}-${this.resolvedSpec.tags[0]?.paths[0]?.path}`;
617+
}
618+
}
619+
// Set url back in address bar
620+
window.location.hash = `${this.selectedContentId}`;
621+
}
622+
602623
async afterSpecParsedAndValidated(spec) {
603624
this.resolvedSpec = spec;
604625
if (this.defaultApiServerUrl) {
@@ -616,13 +637,7 @@ export default class RapiDoc extends LitElement {
616637
this.selectedServer = this.resolvedSpec.servers[0];
617638
}
618639
}
619-
if (this.showInfo === 'true' && !window.location.hash && (this.resolvedSpec.info?.description || this.resolvedSpec.info?.title)) {
620-
this.selectedContentId = 'overview';
621-
} else if (window.location.hash) {
622-
this.selectedContentId = window.location.hash.substring(1).startsWith('overview--') ? 'overview' : window.location.hash.substring(1);
623-
} else {
624-
this.selectedContentId = `${this.resolvedSpec.tags[0]?.paths[0]?.method}-${this.resolvedSpec.tags[0]?.paths[0]?.path}`;
625-
}
640+
this.resetSelectedContentId();
626641
this.requestUpdate();
627642
const specLoadedEvent = new CustomEvent('spec-loaded', { detail: spec });
628643
this.dispatchEvent(specLoadedEvent);
@@ -751,7 +766,9 @@ export default class RapiDoc extends LitElement {
751766

752767
// Public Method (scrolls to a given path and highlights the left-nav selection)
753768
async scrollTo(path, expandPath = true) {
754-
this.selectedContentId = path.startsWith('overview--') ? 'overview' : path;
769+
if (path) {
770+
this.selectedContentId = path.startsWith('overview--') ? 'overview' : path;
771+
}
755772
await sleep(0);
756773
const gotoEl = this.shadowRoot.getElementById(path);
757774
if (gotoEl) {

src/utils/common-utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,8 @@ export function advanceSearch(searchVal, allSpecTags, searchOptions = []) {
113113
});
114114
return pathsMatched;
115115
}
116+
117+
export function hasValidPathInUrlHash(tags) {
118+
const filterTags = tags.filter((tag) => tag.paths.filter((path) => window.location.hash.substring(1) === `${path.method}-${path.path.replace(invalidCharsRegEx, '-')}`).length > 0);
119+
return filterTags.length > 0;
120+
}

0 commit comments

Comments
 (0)