Skip to content

Commit

Permalink
Merge pull request #695 from storybookjs/support-named-anchors
Browse files Browse the repository at this point in the history
Add support for named anchors
  • Loading branch information
kylegach authored May 13, 2024
2 parents 73df8a7 + 8bc8566 commit aa8ec6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/util/relative-to-root-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function removeTrailingIndex(path) {
* /addons remains untouched
*/
function relativeToRootLinks(href, path = '', isIndexPage) {
if (!href) {
// Accomodating `<a id="previous-heading-id" />`
return href;
}

const relativeUrlRegex = /^(?!\.\.\/\.\.\/)(\.\/)(.*)$/;
const multiLevelRelativeUrlRegex = /^(?!\.\.\/\.\.\/)(\.\.\/)(.*)$/;

Expand Down
5 changes: 5 additions & 0 deletions src/util/relative-to-root-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ it('does not transform non-relative links', () => {
const rootUrl = relativeToRootLinks('/foo', '/docs/writing-stories/args');
expect(rootUrl).toEqual('/foo');
});

it('does nothing to named anchors', () => {
const rootUrl = relativeToRootLinks();
expect(rootUrl).toBeUndefined();
});

0 comments on commit aa8ec6e

Please sign in to comment.