Skip to content

Commit

Permalink
Supports linking to new short url in link-plugin. Use pretty urls for…
Browse files Browse the repository at this point in the history
… preview
  • Loading branch information
gunnarvelle committed Dec 17, 2024
1 parent aff892a commit bbb0bfb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isNDLATaxonomyUrl,
isNDLAEdPathUrl,
isPlainId,
isNDLATaxonomyContextUrl,
} from "../utils";

test("urls are parsed correctly", async () => {
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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);
});
});

Expand Down
25 changes: 25 additions & 0 deletions src/components/SlateEditor/plugins/link/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) =>
Expand All @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions src/modules/article/articleGqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const usePreviewArticle = (
previewH5p: true,
draftConcept: useDraftConcepts,
absoluteUrl: true,
prettyUrl: true,
},
options,
);
Expand All @@ -58,6 +59,7 @@ const transformArticleMutation = gql`
$previewH5p: Boolean
$draftConcept: Boolean
$absoluteUrl: Boolean
$prettyUrl: Boolean
) {
transformArticleContent(
content: $content
Expand All @@ -66,6 +68,7 @@ const transformArticleMutation = gql`
previewH5p: $previewH5p
draftConcept: $draftConcept
absoluteUrl: $absoluteUrl
prettyUrl: $prettyUrl
)
}
`;
Expand Down

0 comments on commit bbb0bfb

Please sign in to comment.