feat: add baseURL for redoc

This commit is contained in:
xiaoyi.gao 2022-12-02 15:52:10 +08:00
parent 3d410b6002
commit a978927903
4 changed files with 28 additions and 3 deletions

View File

@ -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);

View File

@ -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 {

View File

@ -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) {

View File

@ -56,6 +56,8 @@ export interface RedocRawOptions {
hideFab?: boolean;
minCharacterLengthToInitSearch?: number;
showWebhookVerb?: boolean;
baseURL?: string;
}
export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {