diff --git a/src/services/AppStore.ts b/src/services/AppStore.ts index 082557f3..be73344b 100644 --- a/src/services/AppStore.ts +++ b/src/services/AppStore.ts @@ -70,6 +70,10 @@ export class AppStore { // update position statically based on hash (in case of SSR) MenuStore.updateOnHistory(history.currentId, this.scroll); + if (options.baseURL) { + history.setBaseURL(options.baseURL); + } + this.spec = new SpecStore(spec, specUrl, this.options); this.menu = new MenuStore(this.spec, this.scroll, history); diff --git a/src/services/HistoryService.ts b/src/services/HistoryService.ts index 1270fb89..ec966f41 100644 --- a/src/services/HistoryService.ts +++ b/src/services/HistoryService.ts @@ -6,21 +6,35 @@ const EVENT = 'hashchange'; export class HistoryService { private _emiter; + private _baseURL: string | undefined; constructor() { this._emiter = new EventEmitter(); this.bind(); } + setBaseURL(baseURL: string) { + this._baseURL = baseURL; + } + + get baseURL(): string | undefined { + return this._baseURL; + } + get currentId(): string { return IS_BROWSER ? decodeURIComponent(window.location.hash.substring(1)) : ''; } linkForId(id: string) { - if (!id) { - return ''; + const links: string[] = []; + if (this._baseURL) { + links.push(this._baseURL); } - return '#' + id; + if (id) { + links.push(id); + } + const link = links.join('/'); + return link === '' ? '' : '#' + link; } subscribe(cb): () => void { diff --git a/src/services/MenuStore.ts b/src/services/MenuStore.ts index 34fb5e08..11c25c39 100644 --- a/src/services/MenuStore.ts +++ b/src/services/MenuStore.ts @@ -123,6 +123,11 @@ export class MenuStore { } let item: IMenuItem | undefined; + const baseURL = this.history.baseURL; + if (baseURL && id.startsWith(baseURL + '/')) { + id = id.substring(baseURL.length + 1); + } + item = this.flatItems.find(i => i.id === id); if (item) { diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 4a219eef..c6889029 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -56,6 +56,8 @@ export interface RedocRawOptions { hideFab?: boolean; minCharacterLengthToInitSearch?: number; showWebhookVerb?: boolean; + + baseURL?: string; } export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {