From bbb0bfb2ce898d67a65e8480d6a3e37f7c819bb8 Mon Sep 17 00:00:00 2001 From: Gunnar Velle Date: Tue, 17 Dec 2024 14:24:52 +0100 Subject: [PATCH] Supports linking to new short url in link-plugin. Use pretty urls for preview --- .../plugins/link/__tests__/linkUtils-test.ts | 26 +++++++++++++++++-- .../SlateEditor/plugins/link/utils.ts | 25 ++++++++++++++++++ src/modules/article/articleGqlQueries.ts | 3 +++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/components/SlateEditor/plugins/link/__tests__/linkUtils-test.ts b/src/components/SlateEditor/plugins/link/__tests__/linkUtils-test.ts index 6b23b64d2c..f69d4349ff 100644 --- a/src/components/SlateEditor/plugins/link/__tests__/linkUtils-test.ts +++ b/src/components/SlateEditor/plugins/link/__tests__/linkUtils-test.ts @@ -16,6 +16,7 @@ import { isNDLATaxonomyUrl, isNDLAEdPathUrl, isPlainId, + isNDLATaxonomyContextUrl, } from "../utils"; test("urls are parsed correctly", async () => { @@ -25,7 +26,7 @@ test("urls are parsed correctly", async () => { "http://www.test.ndla.no/en/article/64323", ]; - const subjectUrls = [ + const taxonomyUrls = [ "https://api.test.ndla.no/en/subjects/subject:3/topic:1:2342/resource:1:64323", "https://api.test.ndla.no/subject:3/topic:1:2342/resource:1:64323", "https://ndla-frontend.api.test.ndla.no/sma/subject:3/topic:1:2342/resource:1:64323", @@ -44,6 +45,13 @@ test("urls are parsed correctly", async () => { "https://www.test.ndla.no/subject:3/topic:1:2342/resource:1:64323", ]; + const taxonomyContextUrls = [ + "https://ndla.no/r/06b775d81a", + "https://ndla.no/r/kroppsoving-vg1/kroppslig-laring-og-parkour-okt-1/06b775d81a", + "https://test.ndla.no/f/kroppsoving-vg1/06b775d81a", + "https://ndla.no/e/kroppsoving-vg3/parkour/f9d2bbbb98", + ]; + const learningPathUrls = [ "https://www.test.ndla.no/nn/learningpaths/64323", "https://www.test.ndla.no/learningpaths/64323", @@ -79,14 +87,25 @@ test("urls are parsed correctly", async () => { expect(isNDLATaxonomyUrl(url)).toBe(false); expect(isNDLAEdPathUrl(url)).toBe(false); expect(isPlainId(url)).toBe(false); + expect(isNDLATaxonomyContextUrl(url)).toBe(false); }); - subjectUrls.forEach((url) => { + taxonomyUrls.forEach((url) => { expect(isNDLAArticleUrl(url)).toBe(false); expect(isNDLALearningPathUrl(url)).toBe(false); expect(isNDLATaxonomyUrl(url)).toBe(true); expect(isNDLAEdPathUrl(url)).toBe(false); expect(isPlainId(url)).toBe(false); + expect(isNDLATaxonomyContextUrl(url)).toBe(false); + }); + + taxonomyContextUrls.forEach((url) => { + expect(isNDLAArticleUrl(url)).toBe(false); + expect(isNDLALearningPathUrl(url)).toBe(false); + expect(isNDLATaxonomyUrl(url)).toBe(false); + expect(isNDLAEdPathUrl(url)).toBe(false); + expect(isPlainId(url)).toBe(false); + expect(isNDLATaxonomyContextUrl(url)).toBe(true); }); learningPathUrls.forEach((url) => { @@ -103,6 +122,7 @@ test("urls are parsed correctly", async () => { expect(isNDLATaxonomyUrl(url)).toBe(false); expect(isNDLAEdPathUrl(url)).toBe(true); expect(isPlainId(url)).toBe(false); + expect(isNDLATaxonomyContextUrl(url)).toBe(false); }); plainIds.forEach((url) => { @@ -111,6 +131,7 @@ test("urls are parsed correctly", async () => { expect(isNDLATaxonomyUrl(url)).toBe(false); expect(isNDLAEdPathUrl(url)).toBe(false); expect(isPlainId(url)).toBe(true); + expect(isNDLATaxonomyContextUrl(url)).toBe(false); }); otherUrls.forEach((url) => { @@ -119,6 +140,7 @@ test("urls are parsed correctly", async () => { expect(isNDLATaxonomyUrl(url)).toBe(false); expect(isNDLAEdPathUrl(url)).toBe(false); expect(isPlainId(url)).toBe(false); + expect(isNDLATaxonomyContextUrl(url)).toBe(false); }); }); diff --git a/src/components/SlateEditor/plugins/link/utils.ts b/src/components/SlateEditor/plugins/link/utils.ts index be1f014c97..1bea4dd792 100644 --- a/src/components/SlateEditor/plugins/link/utils.ts +++ b/src/components/SlateEditor/plugins/link/utils.ts @@ -9,6 +9,7 @@ import { Editor, Element, Range, Transforms } from "slate"; import { jsx as slatejsx } from "slate-hyperscript"; import { isValidLocale } from "../../../../i18n"; +import { fetchNodes } from "../../../../modules/nodes/nodeApi"; import { resolveUrls } from "../../../../modules/taxonomy/taxonomyApi"; export const insertLink = (editor: Editor) => { @@ -90,6 +91,26 @@ export const splitTaxonomyUrl = async (href: string) => { return { resourceId, resourceType }; }; +export const splitTaxonomyContextUrl = async (href: string) => { + const { pathname } = new URL(href); + const paths = pathname.split("/"); + const language = isValidLocale(paths[1]) ? paths[1] : "nb"; + const contextId = paths.at(-1); + const nodes = await fetchNodes({ + contextId, + language, + taxonomyVersion: "default", + }); + if (!nodes.length) { + return { resourceId: null, resourceType: null }; + } + + const contentUriSplit = nodes[0].contentUri?.split(":") ?? []; + const resourceId = contentUriSplit.pop(); + const resourceType = contentUriSplit.pop(); + return { resourceId, resourceType }; +}; + export const splitEdPathUrl = (href: string) => { const id = href.split("subject-matter/")[1].split("/")[1]; return { @@ -109,6 +130,8 @@ export const splitEdPreviewUrl = (href: string) => { export const isNDLAArticleUrl = (url: string) => /^http(s)?:\/\/((.*)\.)?ndla.no\/((.*)\/)?article\/\d*/.test(url); export const isNDLATaxonomyUrl = (url: string) => /^http(s)?:\/\/((.*)\.)?ndla.no\/((.*)\/)?subject:(.*)\/topic(.*)/.test(url); +export const isNDLATaxonomyContextUrl = (url: string) => + /^http(s)?:\/\/((.*)\.)?ndla.no\/((.*)\/)?(f|e|r)\/(.*)\/?(.*)\/?[a-z0-9]{10}/.test(url); export const isNDLALearningPathUrl = (url: string) => /^http(s)?:\/\/((.*)\.)?ndla.no\/((.*)\/)?learningpaths\/(.*)/.test(url); export const isNDLAEdPathUrl = (url: string) => @@ -128,6 +151,8 @@ export const getIdAndTypeFromUrl = async (href: string) => { return splitPlainUrl(baseHref); } else if (isNDLATaxonomyUrl(baseHref)) { return await splitTaxonomyUrl(baseHref); + } else if (isNDLATaxonomyContextUrl(baseHref)) { + return await splitTaxonomyContextUrl(baseHref); } else if (isNDLAEdPathUrl(baseHref)) { return splitEdPathUrl(baseHref); } else if (isNDLAEdPreviewUrl(baseHref)) { diff --git a/src/modules/article/articleGqlQueries.ts b/src/modules/article/articleGqlQueries.ts index 5203576db3..8840196535 100644 --- a/src/modules/article/articleGqlQueries.ts +++ b/src/modules/article/articleGqlQueries.ts @@ -45,6 +45,7 @@ export const usePreviewArticle = ( previewH5p: true, draftConcept: useDraftConcepts, absoluteUrl: true, + prettyUrl: true, }, options, ); @@ -58,6 +59,7 @@ const transformArticleMutation = gql` $previewH5p: Boolean $draftConcept: Boolean $absoluteUrl: Boolean + $prettyUrl: Boolean ) { transformArticleContent( content: $content @@ -66,6 +68,7 @@ const transformArticleMutation = gql` previewH5p: $previewH5p draftConcept: $draftConcept absoluteUrl: $absoluteUrl + prettyUrl: $prettyUrl ) } `;