Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
fix(References): Fix Reference formatting selectors and type castings
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed Feb 11, 2020
1 parent 45ae358 commit b3e23e1
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/shared/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,43 @@
*/

export const formatReferences = (): void => {
const referenceListItemSel = '.references > li'
const titleSel = 'span[itemprop="headline"]'
const referenceListItemSel =
'[data-itemprop="references"] > [itemprop="citation"]'
const titleSel = '[itemprop="headline"]'
const datePublishedSel = '[itemprop="datePublished"]'
const publicationIssueSel = '[itemtype="https://schema.org/PublicationIssue"]'

document.querySelectorAll(referenceListItemSel).forEach(node => {
const datePublished = node.querySelector(datePublishedSel) as HTMLElement
const title = node.querySelector(titleSel) as HTMLElement
const datePublished = node.querySelector(datePublishedSel)
const title = node.querySelector(titleSel)

if (title) {
const titleCopy = title.cloneNode(true) as HTMLElement
if (title !== null) {
const titleCopy = title.cloneNode(true)

// Add title node after original datePublished node
if (datePublished && datePublished.parentNode) {
if (datePublished !== null && datePublished.parentNode !== null) {
datePublished.parentNode.insertBefore(
titleCopy,
datePublished.nextSibling
)
}

// Add datePublished node after PublicationIssue node (if exists)
const publicationIssue = node.querySelector(
publicationIssueSel
) as HTMLElement
const datePublishedCopy = datePublished.cloneNode(true) as HTMLElement
const publicationIssue = node.querySelector(publicationIssueSel)
const datePublishedCopy = datePublished?.cloneNode(true)

if (publicationIssue && publicationIssue.parentNode) {
if (
datePublishedCopy !== undefined &&
publicationIssue !== null &&
publicationIssue.parentNode !== null
) {
publicationIssue.parentNode.insertBefore(
datePublishedCopy,
publicationIssue.nextSibling
)
} else {
} else if (datePublishedCopy !== undefined) {
// Otherwise, add node after titleCopy
if (titleCopy && titleCopy.parentNode) {
if (titleCopy !== null && titleCopy.parentNode !== null) {
titleCopy.parentNode.insertBefore(
datePublishedCopy,
titleCopy.nextSibling
Expand Down

0 comments on commit b3e23e1

Please sign in to comment.