From dc6bf8231ade8b47d7621e1b7698b27615f3825b Mon Sep 17 00:00:00 2001 From: Daniel Wojahn Date: Tue, 19 Nov 2024 13:40:02 +0100 Subject: [PATCH] add zotero bibliography features --- .DS_Store | Bin 6148 -> 6148 bytes docs/.DS_Store | Bin 6148 -> 6148 bytes docs/admonitions.md | 4 +- docs/assets/js/zotero-bibliography.js.js | 122 +++++++++++++++++++++++ mkdocs.yml | 4 +- 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 docs/assets/js/zotero-bibliography.js.js diff --git a/.DS_Store b/.DS_Store index 43bf7248804ff9847294286d6371185053bce7e6..f0048177d84e69a1c62829171ebf350e0f4e0c5c 100644 GIT binary patch delta 101 zcmZoMXfc?O%(!E-AoFs@$=pm5!aI@*ax#lc3=FO@GBLBTvaz#sPBvqT*5=@h7m%o~ zHnOnPQ7|?zsMS%Zwlp%(Q7|z!tF7ha5LMQ<4vNpt$<52}n!J}up0RuLT_!Eo&1@Y1 F_yJ}n8?^uc delta 102 zcmZoMXfc?O%(!#2AoFs@$=pm5!aI`+ax#lc3=FO@GBLBTvaz$XPc~zU*5Txg7m%#3 zHZ-y@&`~g`)lq;l4NVM9Yil_S`kkOC1Gc1A|%} vg=$M9104kuW3$>?P7YCJee0n3?3~=Z{I1CdndBL}H$P-jW8KWg@sA$>K?WGZ delta 92 zcmZoMXffEJ&Lq4usURn_xWvHV8Y2@k3o9EtJNslSrf3~b&UgXI>S{wH3j-YmgIXO0 rDAUly(6qLelS5Ql-#REhJ0~|UzkBilCV9r5%@3H=SU0nC{N)D#Q~Vg{ diff --git a/docs/admonitions.md b/docs/admonitions.md index 8808b6b..fc9dbd5 100644 --- a/docs/admonitions.md +++ b/docs/admonitions.md @@ -3,4 +3,6 @@ ??? info "Did you know that..." - Lorem ipsum \ No newline at end of file + Lorem ipsum + + diff --git a/docs/assets/js/zotero-bibliography.js.js b/docs/assets/js/zotero-bibliography.js.js new file mode 100644 index 0000000..c8d16b6 --- /dev/null +++ b/docs/assets/js/zotero-bibliography.js.js @@ -0,0 +1,122 @@ +// zotero-bibliography.js +class ZoteroBibliography extends HTMLElement { + constructor() { + super(); + this.attachShadow({ mode: 'open' }); + } + + async connectedCallback() { + const userId = this.getAttribute('user-id'); + const collectionKey = this.getAttribute('collection-key') || null; + const apiKey = this.getAttribute('api-key') || null; + + await this.loadBibliography(userId, collectionKey, apiKey); + } + + async loadBibliography(userId, collectionKey, apiKey) { + try { + let apiUrl = `https://api.zotero.org/users/${userId}/items`; + if (collectionKey) { + apiUrl = `https://api.zotero.org/users/${userId}/collections/${collectionKey}/items`; + } + + const headers = { + 'Content-Type': 'application/json' + }; + if (apiKey) { + headers['Zotero-API-Key'] = apiKey; + } + + const response = await fetch(apiUrl, { + headers, + params: { + format: 'json', + include: 'data,bib', + sort: 'dateModified', + direction: 'desc' + } + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data = await response.json(); + this.renderBibliography(data); + } catch (error) { + console.error('Error loading Zotero bibliography:', error); + this.shadowRoot.innerHTML = ` +
+ Error loading bibliography. Please check your Zotero settings. +
+ `; + } + } + + renderBibliography(items) { + const styles = ` + + `; + + const bibliography = items.map(item => { + const itemData = item.data; + return ` +
+
${itemData.title}
+
${this.formatAuthors(itemData.creators)}
+
+ ${itemData.date ? `(${itemData.date.split('-')[0]})` : ''} + ${itemData.publicationTitle ? `in ${itemData.publicationTitle}` : ''} + ${itemData.DOI ? `
DOI: ${itemData.DOI}` : ''} +
+
+ `; + }).join(''); + + this.shadowRoot.innerHTML = ` + ${styles} +
+ ${bibliography} +
+ `; + } + + formatAuthors(creators) { + if (!creators || creators.length === 0) return ''; + + return creators + .filter(creator => creator.creatorType === 'author') + .map(author => `${author.lastName}, ${author.firstName}`) + .join('; '); + } +} + +customElements.define('zotero-bibliography', ZoteroBibliography); diff --git a/mkdocs.yml b/mkdocs.yml index 34cd6b8..c988564 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -44,4 +44,6 @@ copyright: Copyright © 2024 CSLS, University of Oxford extra_css: - assets/css/extra.css extra_javascript: - - assets/js/iiif-viewer.js \ No newline at end of file + - assets/js/iiif-viewer.js + - assets/js/zotero-bibliography.js +