Skip to content

Commit

Permalink
add zotero bibliography features
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-wojahn committed Nov 19, 2024
1 parent 5357f42 commit dc6bf82
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified docs/.DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion docs/admonitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@


??? info "Did you know that..."
Lorem ipsum
Lorem ipsum

<zotero-bibliography user-id="8242679" api-key="wfl1lbslylLV0HFAqm65uYTU"></zotero-bibliography>
122 changes: 122 additions & 0 deletions docs/assets/js/zotero-bibliography.js.js
Original file line number Diff line number Diff line change
@@ -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 = `
<div class="error">
Error loading bibliography. Please check your Zotero settings.
</div>
`;
}
}

renderBibliography(items) {
const styles = `
<style>
.bibliography {
font-family: var(--md-text-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif);
line-height: 1.6;
padding: 1rem;
}
.bib-item {
margin-bottom: 1rem;
padding-bottom: 1rem;
border-bottom: 1px solid var(--md-default-fg-color--lightest);
}
.bib-title {
font-weight: bold;
color: var(--md-typeset-color);
}
.bib-authors {
color: var(--md-default-fg-color--light);
font-style: italic;
}
.bib-meta {
font-size: 0.9em;
color: var(--md-default-fg-color--light);
}
.error {
color: var(--md-code-hl-number-color);
padding: 1rem;
border-left: 4px solid var(--md-code-hl-number-color);
}
</style>
`;

const bibliography = items.map(item => {
const itemData = item.data;
return `
<div class="bib-item">
<div class="bib-title">${itemData.title}</div>
<div class="bib-authors">${this.formatAuthors(itemData.creators)}</div>
<div class="bib-meta">
${itemData.date ? `(${itemData.date.split('-')[0]})` : ''}
${itemData.publicationTitle ? `in ${itemData.publicationTitle}` : ''}
${itemData.DOI ? `<br>DOI: <a href="https://doi.org/${itemData.DOI}" target="_blank">${itemData.DOI}</a>` : ''}
</div>
</div>
`;
}).join('');

this.shadowRoot.innerHTML = `
${styles}
<div class="bibliography">
${bibliography}
</div>
`;
}

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);
4 changes: 3 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ copyright: Copyright &copy; 2024 CSLS, University of Oxford
extra_css:
- assets/css/extra.css
extra_javascript:
- assets/js/iiif-viewer.js
- assets/js/iiif-viewer.js
- assets/js/zotero-bibliography.js

0 comments on commit dc6bf82

Please sign in to comment.