Skip to content
Merged
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
10 changes: 2 additions & 8 deletions src/components/features/newlib/ItemPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ArrowUpRight01Icon,
LicenseIcon,
} from "@hugeicons/core-free-icons";
import { licenceComplete, licenceLabel, licenceTermsUrl, recordLicenceSchema, type RecordLicence } from "@/lib/licences";
import { completeRecordLicence, licenceLabel, licenceTermsUrl, type RecordLicence } from "@/lib/licences";
import { normalizeAccessDate } from "@/lib/admin/field-contract";
import { parseBibliographicDate } from "@/lib/bibliographic-date";
import { escapeHtml } from "@/lib/escape-html";
Expand Down Expand Up @@ -70,7 +70,7 @@
} from "@/context/LibraryConfigProvider";
import { useQueryState } from "nuqs";
import ItemHeader from "@/components/features/newlib/ItemHeader";
import ItemTitle from "@/components/features/newlib/ItemTitle";

Check failure on line 73 in src/components/features/newlib/ItemPage.tsx

View workflow job for this annotation

GitHub Actions / Static checks

Circular dependency

Circular import chain detected: src/components/features/newlib/ItemPage.tsx → src/components/features/newlib/ItemTitle.tsx → src/components/features/newlib/ItemPage.tsx Circular dependencies can cause initialization bugs and make code harder to reason about. Consider extracting shared logic into a separate module.
import { COUNTRIES, getSafeReferrer } from "@/constants/misc";
import { useInstitutionCountryCodes } from "@/lib/use-institution-countries";
import {
Expand Down Expand Up @@ -279,7 +279,7 @@
);
}

function ItemAuthor({

Check failure on line 282 in src/components/features/newlib/ItemPage.tsx

View workflow job for this annotation

GitHub Actions / Static checks

High CRAP score (critical)

Function 'ItemAuthor' has a CRAP score of 172 (threshold: 30). • Severity: critical • Cyclomatic: 26 • Cognitive: 23 • CRAP: 172 (threshold: 30) • Lines: 143 CRAP combines complexity with coverage: high CRAP means changes here carry high risk. Consider adding tests, simplifying the function, or both.
author,
profile,
countryOf,
Expand Down Expand Up @@ -931,7 +931,7 @@
return [...doiLinks, canonicalLink, buildOpenScholarPaperLink(item, openScholar)].filter((link) => link !== null);
}, [faviconUrl, identity.baseUrl, item, openScholar]);

const licence = item ? recordLicence(item.licence) : null;
const licence = item ? completeRecordLicence(item.licence) : null;
if (!links.length && !licence) return null;

return (
Expand All @@ -947,12 +947,6 @@
);
}

/** A record's licence as its DOI carries it: only a whole one, so a half-written custom licence never shows. */
const recordLicence = (value: unknown) => {
const licence = recordLicenceSchema.safeParse(value).data;
return licence && licenceComplete(licence) ? licence : null;
};

/** The licence the record is shared under, beside its DOI, linked to its terms where they have a link. */
function ItemLicenceRow({ licence }: { licence: RecordLicence }) {
const t = useTranslations("ui");
Expand Down Expand Up @@ -1277,7 +1271,7 @@
const hasLibraryLinks = (links: Item["links"] | undefined) =>
Boolean(links && (links.cites.length > 0 || links.citedBy.length > 0 || links.related.length > 0));

function ItemRelations({ item }: { item: Item | undefined }) {

Check failure on line 1274 in src/components/features/newlib/ItemPage.tsx

View workflow job for this annotation

GitHub Actions / Static checks

High CRAP score (high)

Function 'ItemRelations' has a CRAP score of 88 (threshold: 30). • Severity: high • Cyclomatic: 18 • Cognitive: 8 • CRAP: 88 (threshold: 30) • Lines: 50 CRAP combines complexity with coverage: high CRAP means changes here carry high risk. Consider adding tests, simplifying the function, or both.
const t = useTranslations("ui");
// A card's reference count opens this page on its References list, and its citation count on Citations.
const asked = useSearchParams().get("links");
Expand Down Expand Up @@ -1400,7 +1394,7 @@
);
}

export default function ItemPage({

Check failure on line 1397 in src/components/features/newlib/ItemPage.tsx

View workflow job for this annotation

GitHub Actions / Static checks

High CRAP score (high)

Function 'ItemPage' has a CRAP score of 79.4 (threshold: 30). • Severity: high • Cyclomatic: 17 • Cognitive: 14 • CRAP: 79.4 (threshold: 30) • Lines: 111 CRAP combines complexity with coverage: high CRAP means changes here carry high risk. Consider adding tests, simplifying the function, or both.
data,
loading: loadingProp,
}: {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/licences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ export function licenceComplete(licence: RecordLicence | null | undefined): lice
return licence.kind === "zenodo" || (Boolean(licence.title) && /^https?:\/\//u.test(licence.url) && URL.canParse(licence.url));
}

/** A record's saved licence as its DOI carries it: only a whole one, so a half-written custom licence never shows. */
export const completeRecordLicence = (value: unknown) => {
const licence = recordLicenceSchema.safeParse(value).data;
return licence && licenceComplete(licence) ? licence : null;
};

/** A licence as one line: its short name where it has one, else its title. */
export function licenceLabel(licence: RecordLicence) {
return licence.kind === "custom" ? licence.title : (licenceShortName(licence.id) ?? zenodoLicenceTitle(licence.id));
Expand Down
9 changes: 8 additions & 1 deletion src/lib/record-structured-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { creatorDisplayName } from "@/lib/creator-display-name";
import { buildSiteUrl } from "@/lib/site-url";
import { convertToImageURL, getExtraValues } from "@/lib/utils";
import { parseBibliographicDate } from "@/lib/bibliographic-date";
import { completeRecordLicence, licenceLabel, licenceTermsUrl } from "@/lib/licences";

/** The schema.org type for a Zotero item type; anything else is a plain creative work. */
const TYPES: Partial<Record<NonNullable<Item["itemType"]>, string>> = {
Expand Down Expand Up @@ -44,9 +45,11 @@ export function recordStructuredData(item: Item, baseUrl: string) {
const cover = item.attachmentsData?.find((attachment) => attachment.PDFCoverPageImage)?.PDFCoverPageImage;
// Authors are people: records from OpenAlex keep a person's whole name in one field, so the field's shape can't tell an organisation apart.
const authors = (item.creators ?? []).filter((creator) => creator.creatorType === "author" && creatorDisplayName(creator)).map((creator) => named("Person", creatorDisplayName(creator)));
const licence = completeRecordLicence(item.licence);
const type = (item.itemType && TYPES[item.itemType]) ?? "CreativeWork";
const data: Record<string, unknown> = {
"@context": "https://schema.org",
"@type": (item.itemType && TYPES[item.itemType]) ?? "CreativeWork",
"@type": type,
"@id": url,
url,
name: item.title,
Expand All @@ -61,6 +64,10 @@ export function recordStructuredData(item: Item, baseUrl: string) {
keywords: item.tags?.join(", "),
sameAs: doiAddressOf(item),
image: cover && convertToImageURL(cover),
// A licence without a terms address goes by its name, as the page shows it.
license: licence && (licenceTermsUrl(licence) ?? named("CreativeWork", licenceLabel(licence))),
// Google reads a dataset by its description and creator, not an article's abstract and author.
...(type === "Dataset" && { description: item.abstractNote, creator: authors }),
};
// Empty values say nothing, so they're left out.
return Object.fromEntries(Object.entries(data).filter(([, value]) => value != null && value !== "" && !(Array.isArray(value) && !value.length)));
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/record-structured-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ describe("record structured data", () => {
expect(data.author).toEqual([{ "@type": "Person", name: "UNESCO" }]);
expect(recordStructuredData(record({ date: "2021-05" }), "https://aied.openscholar.ai").datePublished).toBe("2021-05");
});

// Google's Dataset rich result needs a description and reads its people from creator (Search Console, 21 November 2026).
it("gives a dataset the description and creator Google reads", () => {
const data = recordStructuredData(record({ itemType: "Dataset", abstractNote: "Transcripts of fifteen interviews.", creators: [{ creatorType: "author", name: "Irina Zlotnikova" }] }), "https://aied.openscholar.ai");
expect(data).toMatchObject({ "@type": "Dataset", description: "Transcripts of fifteen interviews.", creator: [{ "@type": "Person", name: "Irina Zlotnikova" }] });
});

it("links the record's licence to its terms", () => {
const data = recordStructuredData(record({ licence: { kind: "zenodo", id: "cc-by-4.0" } }), "https://aied.openscholar.ai");
expect(data.license).toMatch(/creativecommons\.org\/licenses\/by\/4\.0/);
// Zenodo has no terms address for a closed licence, so it goes by its name.
expect(recordStructuredData(record({ licence: { kind: "zenodo", id: "other-closed" } }), "https://aied.openscholar.ai").license).toEqual({ "@type": "CreativeWork", name: "Other (Not Open)" });
});
});
Loading