From bdc244e50a35fffc342968811621fac6720ec461 Mon Sep 17 00:00:00 2001 From: Xavier Rutayisire Date: Wed, 28 Aug 2024 20:24:10 +0200 Subject: [PATCH] feat: support link text --- package-lock.json | 215 ++++------------------------------ package.json | 8 +- src/PrismicNextLink.tsx | 13 +- test/PrismicNextLink.test.tsx | 32 +++++ 4 files changed, 75 insertions(+), 193 deletions(-) diff --git a/package-lock.json b/package-lock.json index be37b54..3286eff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,8 +14,8 @@ "negotiator": "^0.6.3" }, "devDependencies": { - "@prismicio/client": "^7.2.0", - "@prismicio/mock": "^0.3.1", + "@prismicio/client": "7.9.0-alpha.2", + "@prismicio/mock": "0.3.8-alpha.0", "@size-limit/preset-small-lib": "^9.0.0", "@types/negotiator": "^0.6.3", "@types/react-test-renderer": "^18.0.2", @@ -1219,52 +1219,42 @@ } }, "node_modules/@prismicio/client": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/@prismicio/client/-/client-7.2.0.tgz", - "integrity": "sha512-bfbs2ZMLd3ba7Bp1qI4qAz+13FIWE/JDT9+h+3hL/MVpK4iDpjz3vmOlYJs25myhryU9HP8Og7qKoaeE9hPzmA==", + "version": "7.9.0-alpha.2", + "resolved": "https://registry.npmjs.org/@prismicio/client/-/client-7.9.0-alpha.2.tgz", + "integrity": "sha512-Y4Ti7XoJ57Rk2AkhI0UBxg3f2+wGr4ZwGfr82bFbmY9CCAUXQ+aN8cl3nUSCUafhRyJTcqWSty52D8RKvseY4g==", "dev": true, + "license": "Apache-2.0", "dependencies": { - "@prismicio/richtext": "^2.1.5", - "imgix-url-builder": "^0.0.4" + "imgix-url-builder": "^0.0.5" }, "engines": { "node": ">=14.15.0" } }, - "node_modules/@prismicio/mock": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/@prismicio/mock/-/mock-0.3.1.tgz", - "integrity": "sha512-xwwnQ6kKfHMypjao2Q0sdUFuQsdDlif8qmgKoqYv8KlmNxKHzurgfGh9Zl1wHF1ldEJ8C3NVpI4AqdaHjyCHSA==", + "node_modules/@prismicio/client/node_modules/imgix-url-builder": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/imgix-url-builder/-/imgix-url-builder-0.0.5.tgz", + "integrity": "sha512-dCx3UlXghtsjySoqVCcHQHRikqDummwQEfKlxBUK/wrMzd+1ox/XX+zhqVuXKMVuZvCc84pVLEJyntQB0OOi/w==", "dev": true, - "dependencies": { - "change-case": "^4.1.2" - }, + "license": "Apache-2.0", "engines": { "node": ">=12.7.0" - }, - "peerDependencies": { - "@prismicio/client": "^7" } }, - "node_modules/@prismicio/richtext": { - "version": "2.1.5", - "resolved": "https://registry.npmjs.org/@prismicio/richtext/-/richtext-2.1.5.tgz", - "integrity": "sha512-Sf6iCsciPmfK3uQeFmeY9RTRSuhrCVzyU39OkiJ1VJn8O0531pKapGoWS+5WGxouJtE5+jGqV8L+L2mKP7NkXQ==", + "node_modules/@prismicio/mock": { + "version": "0.3.8-alpha.0", + "resolved": "https://registry.npmjs.org/@prismicio/mock/-/mock-0.3.8-alpha.0.tgz", + "integrity": "sha512-w8yGiL6cmapktjeb47IS4zdqFArfo+cC81ahdoDBShOjREqQjzTHECihMudcnePav5Ok11IUHXB0F5Il3aaRjA==", "dev": true, + "license": "Apache-2.0", "dependencies": { - "@prismicio/types": "^0.2.7" + "change-case": "^5.4.4" }, "engines": { - "node": ">=12.7.0" - } - }, - "node_modules/@prismicio/types": { - "version": "0.2.8", - "resolved": "https://registry.npmjs.org/@prismicio/types/-/types-0.2.8.tgz", - "integrity": "sha512-EmuYYil56U+UtEifMD/9TmLzpWliV+X6kypwPq47GNXmIXyFK1JsP3z872fUziXwoBjd2YILj28DNdYXlLOpXg==", - "dev": true, - "engines": { - "node": ">=12.7.0" + "node": ">=18" + }, + "peerDependencies": { + "@prismicio/client": "^7" } }, "node_modules/@rollup/plugin-typescript": { @@ -2267,16 +2257,6 @@ "node": ">=6" } }, - "node_modules/camel-case": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz", - "integrity": "sha512-gxGWBrTT1JuMx6R+o5PTXMmUnhnVzLQ9SNutD4YqKtI6ap897t3tKECYla6gCWEkplXnlNybEkZg9GEGxKFCgw==", - "dev": true, - "dependencies": { - "pascal-case": "^3.1.2", - "tslib": "^2.0.3" - } - }, "node_modules/camelcase": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", @@ -2323,17 +2303,6 @@ } ] }, - "node_modules/capital-case": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/capital-case/-/capital-case-1.0.4.tgz", - "integrity": "sha512-ds37W8CytHgwnhGGTi88pcPyR15qoNkOpYwmMMfnWqqWgESapLqvDx6huFjQ5vqWSn2Z06173XNA7LtMOeUh1A==", - "dev": true, - "dependencies": { - "no-case": "^3.0.4", - "tslib": "^2.0.3", - "upper-case-first": "^2.0.2" - } - }, "node_modules/chai": { "version": "4.3.8", "resolved": "https://registry.npmjs.org/chai/-/chai-4.3.8.tgz", @@ -2367,24 +2336,11 @@ } }, "node_modules/change-case": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/change-case/-/change-case-4.1.2.tgz", - "integrity": "sha512-bSxY2ws9OtviILG1EiY5K7NNxkqg/JnRnFxLtKQ96JaviiIxi7djMrSd0ECT9AC+lttClmYwKw53BWpOMblo7A==", + "version": "5.4.4", + "resolved": "https://registry.npmjs.org/change-case/-/change-case-5.4.4.tgz", + "integrity": "sha512-HRQyTk2/YPEkt9TnUPbOpr64Uw3KOicFWPVBb+xiHvd6eBx/qPr9xqfBFDT8P2vWsvvz4jbEkfDe71W3VyNu2w==", "dev": true, - "dependencies": { - "camel-case": "^4.1.2", - "capital-case": "^1.0.4", - "constant-case": "^3.0.4", - "dot-case": "^3.0.4", - "header-case": "^2.0.4", - "no-case": "^3.0.4", - "param-case": "^3.0.4", - "pascal-case": "^3.1.2", - "path-case": "^3.0.4", - "sentence-case": "^3.0.4", - "snake-case": "^3.0.4", - "tslib": "^2.0.3" - } + "license": "MIT" }, "node_modules/character-entities": { "version": "2.0.2", @@ -2516,17 +2472,6 @@ "typedarray": "^0.0.6" } }, - "node_modules/constant-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/constant-case/-/constant-case-3.0.4.tgz", - "integrity": "sha512-I2hSBi7Vvs7BEuJDr5dDHfzb/Ruj3FyvFyh7KLilAjNQw3Be+xgqUBA2W6scVEcL0hL1dwPRtIqEPVUCKkSsyQ==", - "dev": true, - "dependencies": { - "no-case": "^3.0.4", - "tslib": "^2.0.3", - "upper-case": "^2.0.2" - } - }, "node_modules/conventional-changelog": { "version": "3.1.25", "resolved": "https://registry.npmjs.org/conventional-changelog/-/conventional-changelog-3.1.25.tgz", @@ -3087,16 +3032,6 @@ "node": ">=6.0.0" } }, - "node_modules/dot-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/dot-case/-/dot-case-3.0.4.tgz", - "integrity": "sha512-Kv5nKlh6yRrdrGvxeJ2e5y2eRUpkUosIW4A2AS38zwSz27zu7ufDwQPi5Jhs3XAlGNetl3bmnGhQsMtkKJnj3w==", - "dev": true, - "dependencies": { - "no-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, "node_modules/dot-prop": { "version": "5.3.0", "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-5.3.0.tgz", @@ -4457,16 +4392,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/header-case": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/header-case/-/header-case-2.0.4.tgz", - "integrity": "sha512-H/vuk5TEEVZwrR0lp2zed9OCo1uAILMlx0JEMgC26rzyJJ3N1v6XkwHHXJQdR2doSjcGPM6OKPYoJgf0plJ11Q==", - "dev": true, - "dependencies": { - "capital-case": "^1.0.4", - "tslib": "^2.0.3" - } - }, "node_modules/hosted-git-info": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-4.1.0.tgz", @@ -5491,15 +5416,6 @@ "get-func-name": "^2.0.0" } }, - "node_modules/lower-case": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz", - "integrity": "sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==", - "dev": true, - "dependencies": { - "tslib": "^2.0.3" - } - }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -6449,16 +6365,6 @@ } } }, - "node_modules/no-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/no-case/-/no-case-3.0.4.tgz", - "integrity": "sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg==", - "dev": true, - "dependencies": { - "lower-case": "^2.0.2", - "tslib": "^2.0.3" - } - }, "node_modules/node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -6757,16 +6663,6 @@ "node": ">=6" } }, - "node_modules/param-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/param-case/-/param-case-3.0.4.tgz", - "integrity": "sha512-RXlj7zCYokReqWpOPH9oYivUzLYZ5vAPIfEmCTNViosC78F8F0H9y7T7gG2M39ymgutxF5gcFEsyZQSph9Bp3A==", - "dev": true, - "dependencies": { - "dot-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, "node_modules/parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -6792,26 +6688,6 @@ "node": ">=4" } }, - "node_modules/pascal-case": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/pascal-case/-/pascal-case-3.1.2.tgz", - "integrity": "sha512-uWlGT3YSnK9x3BQJaOdcZwrnV6hPpd8jFH1/ucpiLRPh/2zCVJKS19E4GvYHvaCcACn3foXZ0cLB9Wrx1KGe5g==", - "dev": true, - "dependencies": { - "no-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, - "node_modules/path-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/path-case/-/path-case-3.0.4.tgz", - "integrity": "sha512-qO4qCFjXqVTrcbPt/hQfhTQ+VhFsqNKOPtytgNKkKxSoEp3XPUQ8ObFuePylOIok5gjn69ry8XiULxCwot3Wfg==", - "dev": true, - "dependencies": { - "dot-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, "node_modules/path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", @@ -7745,17 +7621,6 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true }, - "node_modules/sentence-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/sentence-case/-/sentence-case-3.0.4.tgz", - "integrity": "sha512-8LS0JInaQMCRoQ7YUytAo/xUu5W2XnQxV2HI/6uM6U7CITS1RqPElr30V6uIqyMKM9lJGRVFy5/4CuzcixNYSg==", - "dev": true, - "dependencies": { - "no-case": "^3.0.4", - "tslib": "^2.0.3", - "upper-case-first": "^2.0.2" - } - }, "node_modules/set-function-name": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/set-function-name/-/set-function-name-2.0.1.tgz", @@ -7846,16 +7711,6 @@ "node": ">=8" } }, - "node_modules/snake-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/snake-case/-/snake-case-3.0.4.tgz", - "integrity": "sha512-LAOh4z89bGQvl9pFfNF8V146i7o7/CqFPbqzYgP+yYzDIDeS9HaNFtXABamRW+AQzEVODcvE79ljJ+8a9YSdMg==", - "dev": true, - "dependencies": { - "dot-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -8561,24 +8416,6 @@ "browserslist": ">= 4.21.0" } }, - "node_modules/upper-case": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/upper-case/-/upper-case-2.0.2.tgz", - "integrity": "sha512-KgdgDGJt2TpuwBUIjgG6lzw2GWFRCW9Qkfkiv0DxqHHLYJHmtmdUIKcZd8rHgFSjopVTlw6ggzCm1b8MFQwikg==", - "dev": true, - "dependencies": { - "tslib": "^2.0.3" - } - }, - "node_modules/upper-case-first": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/upper-case-first/-/upper-case-first-2.0.2.tgz", - "integrity": "sha512-514ppYHBaKwfJRK/pNC6c/OxfGa0obSnAl106u97Ed0I625Nin96KAjttZF6ZL3e1XLtphxnqrOi9iWgm+u+bg==", - "dev": true, - "dependencies": { - "tslib": "^2.0.3" - } - }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/package.json b/package.json index 9efe8bc..b096b47 100644 --- a/package.json +++ b/package.json @@ -57,8 +57,8 @@ "negotiator": "^0.6.3" }, "devDependencies": { - "@prismicio/client": "^7.2.0", - "@prismicio/mock": "^0.3.1", + "@prismicio/client": "7.9.0-alpha.2", + "@prismicio/mock": "0.3.8-alpha.0", "@size-limit/preset-small-lib": "^9.0.0", "@types/negotiator": "^0.6.3", "@types/react-test-renderer": "^18.0.2", @@ -99,5 +99,9 @@ }, "publishConfig": { "access": "public" + }, + "overrides": { + "@prismicio/client": "7.9.0-alpha.2", + "@prismicio/mock": "0.3.8-alpha.0" } } diff --git a/src/PrismicNextLink.tsx b/src/PrismicNextLink.tsx index 9780c1b..e21dfce 100644 --- a/src/PrismicNextLink.tsx +++ b/src/PrismicNextLink.tsx @@ -32,7 +32,7 @@ export const PrismicNextLink = React.forwardRef< HTMLAnchorElement, PrismicNextLinkProps >(function PrismicNextLink( - { field, document, linkResolver, ...restProps }, + { field, document, linkResolver, children, ...restProps }, ref, ): JSX.Element | null { const { @@ -51,5 +51,14 @@ export const PrismicNextLink = React.forwardRef< rel = restProps.rel; } - return ; + let text: string | undefined; + if (field && "text" in field) { + text = field.text; + } + + return ( + + {children || text} + + ); }); diff --git a/test/PrismicNextLink.test.tsx b/test/PrismicNextLink.test.tsx index 371d88d..1e97427 100644 --- a/test/PrismicNextLink.test.tsx +++ b/test/PrismicNextLink.test.tsx @@ -237,3 +237,35 @@ it("forwards ref", (ctx) => { expect(ref).toHaveBeenCalledWith({ tagName: "a" }); }); + +it("renders a next/link with text and without children", (ctx) => { + const field = ctx.mock.value.link({ + type: "Web", + model: { type: "Link", config: { text: { type: "Text" } } }, + }); + + const actual = renderJSON(); + const expected = renderJSON( + + {field.text} + , + ); + + expectLinkToEqual(actual, expected); +}); + +it("renders a next/link with text and children", (ctx) => { + const field = ctx.mock.value.link({ type: "Web" }); + const children = ctx.mock.value.keyText(); + + const actual = renderJSON( + {children}, + ); + const expected = renderJSON( + + {children} + , + ); + + expectLinkToEqual(actual, expected); +});