Skip to content

Commit 41b54f9

Browse files
committed
Support headers with multiple invalid characters in link. #288
1 parent 6717904 commit 41b54f9

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

src/openapi-explorer.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -428,26 +428,6 @@ export default class OpenApiExplorer extends LitElement {
428428
}, isExpandingNeeded ? 150 : 0);
429429
}
430430

431-
isValidTopId(id) {
432-
return (id.startsWith('overview') || id === 'servers' || id === 'auth');
433-
}
434-
435-
isValidPathId(id) {
436-
if (id === 'overview' && !this.hideInfo) {
437-
return true;
438-
}
439-
if (id === 'servers' && !this.hideServerSelection) {
440-
return true;
441-
}
442-
if (id === 'auth' && !this.hideAuthentication) {
443-
return true;
444-
}
445-
if (id.startsWith('tag--')) {
446-
return this.resolvedSpec.tags && this.resolvedSpec.tags.find((tag) => tag.elementId === id);
447-
}
448-
return this.resolvedSpec.tags && this.resolvedSpec.tags.find((tag) => tag.paths.find((path) => path.elementId === id));
449-
}
450-
451431
onIntersect(entries) {
452432
if (this.isIntersectionObserverActive === false) {
453433
return;
@@ -576,7 +556,14 @@ export default class OpenApiExplorer extends LitElement {
576556
await sleep(0);
577557

578558
// In the case of section scrolling, these are hard swaps, so just load "section". In the case of `tags` the headers have the element html Id in the last `--id`, so split that off and check for it
579-
const contentEl = this.shadowRoot.getElementById(elementId?.startsWith('section') ? 'section' : elementId) || this.shadowRoot.getElementById(elementId.split('--').slice(-1)[0]);
559+
// NOTE: Really this whole nonsense is because Marked, inserts -- between the prefix and the type and we cannot control it at all. When upgrading to Node 20, marked 16+, we will have to change this and we might even be able to make it work correctly. The biggest problem is that both the separator `--` and invalid character replacement `-`, can stack up.
560+
const contentEl = this.shadowRoot.getElementById(elementId?.startsWith('section') ? 'section' : elementId)
561+
// Remove the prefix of the section as headers in sub sections are not prefixed with the type.
562+
|| elementId.split('--').length > 1 && this.shadowRoot.getElementById(elementId.split('--').slice(1).join('--'))
563+
// Remove the prefix of the operation (tag--) and the tag name (tag--NAME--) from header, as headers in sub sections are not prefixed with the type.
564+
|| elementId.split('--').length > 2 && this.shadowRoot.getElementById(elementId.split('--').slice(2).join('--'))
565+
|| this.shadowRoot.getElementById(elementId.split('--').slice(-1)[0]);
566+
580567
if (!contentEl) {
581568
return;
582569
}
@@ -620,14 +607,14 @@ export default class OpenApiExplorer extends LitElement {
620607
// Update Location Hash
621608
replaceState(elementId);
622609
newNavEl = this.shadowRoot.getElementById(`link-${elementId}`);
623-
} else if (!elementId.match('cmp--') && !elementId.match('tag--') && !elementId.match(/--h[12]$/)) {
624-
this.shadowRoot.getElementById('operations-root').scrollIntoView({ behavior: 'auto', block: 'start' });
610+
} else if (elementId.match('cmp--') || elementId.match('tag--') || elementId.match('overview--') || elementId.match('auth--') || elementId.match('servers--')) {
611+
contentEl.scrollIntoView({ behavior: 'auto', block: 'start' });
625612

626613
// Update Location Hash
627614
replaceState(elementId);
628615
newNavEl = this.shadowRoot.getElementById(`link-${elementId}`);
629616
} else {
630-
contentEl.scrollIntoView({ behavior: 'auto', block: 'start' });
617+
this.shadowRoot.getElementById('operations-root').scrollIntoView({ behavior: 'auto', block: 'start' });
631618

632619
// Update Location Hash
633620
replaceState(elementId);

src/utils/common-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function debounce(fn, delay) {
1414
};
1515
}
1616

17-
export const invalidCharsRegEx = new RegExp(/[\s#:?&={}]/, 'g'); // used for generating valid html element ids by replacing the invalid chars with hyphen (-)
17+
export const invalidCharsRegEx = new RegExp(/[\s#:?&={}-]+/, 'g'); // used for generating valid html element ids by replacing the invalid chars with hyphen (-)
1818

1919
export function sleep(ms) {
2020
return new Promise((resolve) => setTimeout(resolve, ms));

0 commit comments

Comments
 (0)