Skip to content

Commit

Permalink
Library Catalog (TIND ILS): Enrich item type from Schema.org
Browse files Browse the repository at this point in the history
Records in the TIND platform may include a Schema.org mapping. This
mapping often has a `@type` property that is more accurate than what
can be obtained purely from the MARC data of the record.
  • Loading branch information
thms-rmb committed Jan 22, 2025
1 parent d4e43e2 commit 9180a24
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Library Catalog (TIND ILS).js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ function doWeb(doc, url) {
}
}

/**
*
* @param {Document} doc The page document
*/
function scrape(doc, _url) {
const schemaOrg = getSchemaOrg(doc);

let marcXMLURL = attr(doc, 'a[href$="/export/xm"], a[download$=".xml"]', 'href');
ZU.doGet(marcXMLURL, function (respText) {
var translator = Zotero.loadTranslator("import");
Expand All @@ -96,13 +102,89 @@ function scrape(doc, _url) {
item.url = erURL;
}

if (schemaOrg) {
enrichItemWithSchemaOrgItemType(item, schemaOrg);
}

item.complete();
});

translator.translate();
});
}

/**
* Enriches the Zotero item with item type found in the Schema.org data.
*
* @param {Z.Item} item The Zotero item
* @param {Object} schemaOrg The parsed Schema.org data
*/
function enrichItemWithSchemaOrgItemType(item, schemaOrg) {
if (!schemaOrg.hasOwnProperty("@type")) {
return;
}

const schemaOrgType = schemaOrg["@type"];

if (!schemaOrgType) {
return;
}

/**
* @type {Map<string, keyof Z.ItemTypes>}
*/
const schemaOrgType2ZoteroType = new Map([
["Thing", "document"],
["CreativeWork", "document"],
["Article", "journalArticle"],
["ScholarlyArticle", "journalArticle"],
["Report", "report"],
["Thesis", "thesis"],
["Manuscript", "manuscript"],
["Dataset", "dataset"],
]);

if (schemaOrgType2ZoteroType.has(schemaOrgType)) {
item.itemType = schemaOrgType2ZoteroType.get(schemaOrgType);
}
}

/**
* Obtains the parsed Schema.org data from the page.
*
* @param {Document} doc The page document
* @returns {Object | null} The schema.org JSON-LD object
*/
function getSchemaOrg(doc) {
const schemaOrgElement = doc.getElementById("detailed-schema-org");

if (schemaOrgElement === null) {
return;
}

let schemaOrg;

try {
schemaOrg = JSON.parse(schemaOrgElement.innerHTML);
} catch (error) {

Check warning on line 169 in Library Catalog (TIND ILS).js

View workflow job for this annotation

GitHub Actions / Lint, Check, Test

Closing curly brace appears on the same line as the subsequent block
return;
}

if (typeof schemaOrg !== "object") {
return;
}

if (!schemaOrg.hasOwnProperty("@context")) {
return;
}

if (schemaOrg["@context"] !== "https://schema.org") {
return;
}

return schemaOrg;

Check failure on line 185 in Library Catalog (TIND ILS).js

View workflow job for this annotation

GitHub Actions / Lint, Check, Test

Function 'getSchemaOrg' expected no return value
}

/** BEGIN TEST CASES **/
var testCases = [
{
Expand Down

0 comments on commit 9180a24

Please sign in to comment.