From 791245dccdb16d10a3b48ccd5064ad26420c5822 Mon Sep 17 00:00:00 2001 From: giovannimanetti11 Date: Tue, 9 Apr 2024 11:18:58 +0000 Subject: [PATCH] Fix APA citation format in cite popup --- inc/js/single-post.js | 54 ++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/inc/js/single-post.js b/inc/js/single-post.js index 54005d2..0c9afe4 100755 --- a/inc/js/single-post.js +++ b/inc/js/single-post.js @@ -371,6 +371,22 @@ document.addEventListener('DOMContentLoaded', () => { } } + // Formats author names according to APA 7th edition + window.formatAPAAuthors = function(authors) { + const authorsArray = authors.split(', ').map((author) => { + const parts = author.split(' '); + const lastName = parts.pop(); + const initials = parts.map(name => `${name.charAt(0)}.`).join(' '); + return `${lastName}, ${initials}`; + }); + + if (authorsArray.length > 1) { + const lastAuthor = authorsArray.pop(); + return `${authorsArray.join(', ')}, & ${lastAuthor}`; + } else { + return authorsArray[0]; // No need for '&' for a single author + } + } window.generateCitation = function() { var author = articleData.author; @@ -389,37 +405,37 @@ document.addEventListener('DOMContentLoaded', () => { var citation = ""; if (citationStyle === "APA") { - citation = author + ". (" + publicationDate + "). " + title + ". " + siteName + ". " + url; + const formattedAuthors = formatAPAAuthors(author); + citation = `${formattedAuthors} (${formattedPublicationDate}). ${title}. ${siteName}. ${url}`; } else if (citationStyle === "MLA") { - citation = author + ". \"" + title + ".\" " + siteName + ". " + formattedPublicationDate + ". Web. Accessed " + formattedAccessDate + ". " + url + "."; + citation = `${author}. "${title}." ${siteName}, ${formattedPublicationDate}. Web. Accessed ${formattedAccessDate}. ${url}.`; } else if (citationStyle === "Wikipedia") { - citation = "<ref>{{Cita web |url=" + url + " |titolo=" + title + " |accesso=" + formattedAccessDate + "}}</ref>"; + citation = `<ref>{{Cita web |url=${url} |titolo=${title} |accesso=${formattedAccessDate}}}</ref>`; } document.getElementById("citation-text").innerHTML = citation; } - - window.copyCitationToClipboard = function() { - var citation = document.getElementById("citation-text").textContent; - var textarea = document.createElement("textarea"); - textarea.textContent = citation; - document.body.appendChild(textarea); - textarea.select(); - document.execCommand("copy"); - document.body.removeChild(textarea); - - var copyMessage = document.getElementById("citation-copy-message"); - copyMessage.style.display = "block"; - copyMessage.textContent = "Citazione copiata negli appunti!"; - setTimeout(function () { - copyMessage.style.display = "none"; - }, 2000); + var citation = document.getElementById("citation-text").textContent; + var textarea = document.createElement("textarea"); + textarea.textContent = citation; + document.body.appendChild(textarea); + textarea.select(); + document.execCommand("copy"); + document.body.removeChild(textarea); + + var copyMessage = document.getElementById("citation-copy-message"); + copyMessage.style.display = "block"; + copyMessage.textContent = "Citazione copiata negli appunti!"; + setTimeout(function () { + copyMessage.style.display = "none"; + }, 2000); } document.querySelector('.citation-button').addEventListener('click', openCitationPopup); + // Close popup when you click outside of it document.addEventListener('click', function(event) { var popup = document.getElementById('citation-popup');