Skip to content

Commit

Permalink
Add support for named anchors
Browse files Browse the repository at this point in the history
- Useful for preserving links to prior headings
  • Loading branch information
kylegach committed May 13, 2024
1 parent 077554e commit 8bc8566
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 8bc8566

Please sign in to comment.