Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for text in links for #351

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
"imgix-url-builder": "^0.0.5"
},
"devDependencies": {
"@prismicio/mock": "^0.3.7",
"@prismicio/types-internal": "^2.6.0",
"@prismicio/mock": "^0.3.8-alpha.1",
"@prismicio/types-internal": "2.7.0-alpha.2",
"@size-limit/preset-small-lib": "^11.1.4",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
Expand Down Expand Up @@ -111,5 +111,10 @@
},
"publishConfig": {
"access": "public"
},
"overrides": {
"@prismicio/mock": {
"@prismicio/client": "7.9.0-alpha.2"
}
}
}
11 changes: 7 additions & 4 deletions src/helpers/asLinkAttrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type AsLinkAttrsConfigRelArgs<
| undefined
isExternal: boolean
target?: string
text?: string
}

export type AsLinkAttrsConfig<
Expand Down Expand Up @@ -58,11 +59,13 @@ type AsLinkAttrsReturnType<
| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>
| undefined
target?: string
text?: string
rel?: string
}
: {
href?: undefined
target?: undefined
text?: undefined
rel?: undefined
}

Expand Down Expand Up @@ -101,15 +104,11 @@ export const asLinkAttrs = <
): AsLinkAttrsReturnType<LinkResolverFunctionReturnType> => {
if (
linkFieldOrDocument &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501
("link_type" in linkFieldOrDocument
? isFilledLink(linkFieldOrDocument)
: linkFieldOrDocument)
) {
const target =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501
"target" in linkFieldOrDocument ? linkFieldOrDocument.target : undefined

const rawHref = asLink(linkFieldOrDocument, config.linkResolver)
Expand All @@ -124,10 +123,14 @@ export const asLinkAttrs = <
? "noreferrer"
: undefined

const text =
"text" in linkFieldOrDocument ? linkFieldOrDocument.text : undefined

return {
href,
target,
rel: rel == null ? undefined : rel,
text,
}
}

Expand Down
80 changes: 80 additions & 0 deletions test/helpers-asLinkAttrs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ it("resolves a link to document field with route resolver", (ctx) => {
href: field.url,
target: undefined,
rel: undefined,
text: undefined,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: "/linkResolver",
target: undefined,
rel: undefined,
text: undefined,
})
})

Expand All @@ -55,11 +57,13 @@ it("resolves a link to document field without route resolver", (ctx) => {
href: undefined,
target: undefined,
rel: undefined,
text: undefined,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: "/linkResolver",
target: undefined,
rel: undefined,
text: undefined,
})
})

Expand All @@ -70,11 +74,13 @@ it("resolves a link to web field", (ctx) => {
href: field.url,
target: undefined,
rel: "noreferrer",
text: undefined,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: undefined,
})
})

Expand All @@ -85,11 +91,30 @@ it("returns correct target when field has a target", (ctx) => {
href: field.url,
target: field.target,
rel: "noreferrer",
text: undefined,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: field.url,
target: field.target,
rel: "noreferrer",
text: undefined,
})
})

it("returns correct text when field has text", (ctx) => {
const field = ctx.mock.value.link({ type: "Web", withText: true })

expect(asLinkAttrs(field)).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: field.text,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: field.text,
})
})

Expand All @@ -100,11 +125,64 @@ it("resolves a link to media field", (ctx) => {
href: field.url,
target: undefined,
rel: "noreferrer",
text: undefined,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: undefined,
})
})

it("returns correct text when a link to media field has display text", (ctx) => {
const field = ctx.mock.value.link({ type: "Media", withText: true })

expect(asLinkAttrs(field)).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: field.text,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: field.text,
})
})

it("resolves a content relationship field", (ctx) => {
const field = ctx.mock.value.link({ type: "Document" })

expect(asLinkAttrs(field)).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: undefined,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: "/linkResolver",
target: undefined,
rel: undefined,
text: undefined,
})
})

it("returns correct text when a content relationship field has display text", (ctx) => {
const field = ctx.mock.value.link({ type: "Document", withText: true })

expect(asLinkAttrs(field)).toEqual({
href: field.url,
target: undefined,
rel: "noreferrer",
text: field.text,
})
expect(asLinkAttrs(field, { linkResolver: () => "/linkResolver" })).toEqual({
href: "/linkResolver",
target: undefined,
rel: undefined,
text: field.text,
})
})

Expand All @@ -116,11 +194,13 @@ it("resolves a document", (ctx) => {
href: doc.url,
target: undefined,
rel: undefined,
text: undefined,
})
expect(asLinkAttrs(doc, { linkResolver: () => "/linkResolver" })).toEqual({
href: "/linkResolver",
target: undefined,
rel: undefined,
text: undefined,
})
})

Expand Down
Loading