Skip to content

Commit

Permalink
Library Catalog (TIND ILS): Use schema.org in detectWeb, address vari…
Browse files Browse the repository at this point in the history
…ous other feedback
  • Loading branch information
thms-rmb committed Jan 23, 2025
1 parent b7685d0 commit 56ac415
Showing 1 changed file with 47 additions and 32 deletions.
79 changes: 47 additions & 32 deletions Library Catalog (TIND ILS).js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2025-01-22 10:38:00"
"lastUpdated": "2025-01-23 07:55:06"
}

/*
Expand All @@ -35,13 +35,38 @@
***** END LICENSE BLOCK *****
*/

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

/**
* @param {Document} doc The page document
*/
function detectWeb(doc, url) {
if (!doc.querySelector('#tindfooter')) {
return false;
}

if (url.includes('/record/')) {
const schemaOrg = getSchemaOrg(doc);

if (schemaOrg) {
const zoteroType = getZoteroTypeFromSchemaOrg(schemaOrg);

if (zoteroType) {
return zoteroType;
}
}
return "book";
}
else if (getSearchResults(doc, true)) {
Expand Down Expand Up @@ -114,38 +139,32 @@ function scrape(doc, _url) {
}

/**
* 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
* @param {Object} schemaOrg
* @returns {?keyof Z.ItemTypes}
*/
function enrichItemWithSchemaOrgItemType(item, schemaOrg) {
if (!schemaOrg.hasOwnProperty("@type")) {
return;
}

function getZoteroTypeFromSchemaOrg(schemaOrg) {

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

View workflow job for this annotation

GitHub Actions / Lint, Check, Test

Expected to return a value at the end of function 'getZoteroTypeFromSchemaOrg'
const schemaOrgType = schemaOrg["@type"];

if (!schemaOrgType) {
return;
return null;
}

if (SCHEMA_ORG_TYPE_2_ZOTERO_TYPE.has(schemaOrgType)) {
return SCHEMA_ORG_TYPE_2_ZOTERO_TYPE.get(schemaOrgType);
}
}

/**
* @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"],
]);
/**
* 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) {
const zoteroType = getZoteroTypeFromSchemaOrg(schemaOrg);

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

Expand All @@ -165,7 +184,7 @@ function getSchemaOrg(doc) {
let schemaOrg;

try {
schemaOrg = JSON.parse(schemaOrgElement.innerHTML);
schemaOrg = JSON.parse(schemaOrgElement.textContent);
}
catch (error) {
return null;
Expand All @@ -175,10 +194,6 @@ function getSchemaOrg(doc) {
return null;
}

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

if (schemaOrg["@context"] !== "https://schema.org") {
return null;
}
Expand Down Expand Up @@ -457,7 +472,7 @@ var testCases = [
{
"type": "web",
"url": "https://socialmediaarchive.org/record/60",
"detectedItemType": "book",
"detectedItemType": "dataset",
"items": [
{
"itemType": "dataset",
Expand Down

0 comments on commit 56ac415

Please sign in to comment.