Skip to content

Commit

Permalink
fix: resolve links with story.full_slug (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedebock committed Jun 3, 2024
1 parent d631acd commit f5160a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-windows-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@labdigital/storyblok-graphql-codegen-terraform': patch
---

Fix bug in link resolver for internal links without context.links
13 changes: 13 additions & 0 deletions src/lib/resolvers/linkResolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ describe('linkResolvers', () => {
expect(result?.newTab).toBe(false)
})

it('resolves an internal url without `links` in context', () => {
const result = resolvers.Article.url(
{ url: { id: '1', linktype: 'story', anchor: 'test-hash', story: { full_slug: '/article/test-article-story' } } },
null,
{}
)
expect(result?.url).toBe('/article/test-article-story#test-hash')
expect(result?.hash).toBe('test-hash')
expect(result?.pathname).toBe('/article/test-article-story')
expect(result?.type).toBe('internal')
expect(result?.newTab).toBe(false)
})

it('ignores an internal url with `links` in context if the link does not exist', () => {
const result = resolvers.Article.url(
{ url: { id: '1', linktype: 'story', anchor: 'test-hash' } },
Expand Down
12 changes: 12 additions & 0 deletions src/lib/resolvers/linkResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ export const toStoryblokLink = (
return undefined
}

return {
type: 'internal',
hash: link.anchor,
newTab: link.target === '_blank', // internal links are not newTab by default
url:
urlResolver(fullSlug, context) +
(link.anchor ? `#${link.anchor}` : ''),
pathname: urlResolver(fullSlug, context),
}
} else if (context && link.story?.full_slug) {
const fullSlug = link.story.full_slug

return {
type: 'internal',
hash: link.anchor,
Expand Down

0 comments on commit f5160a7

Please sign in to comment.