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

Commit

Permalink
feat(Elife): Add oa and cc icon links
Browse files Browse the repository at this point in the history
  • Loading branch information
nlisgo committed Jul 21, 2020
1 parent 605ac7e commit c6fcdcf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/themes/elife/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { first, ready, select } from '../../util'
import * as dateFormatter from './lib/dateFormatter'
import * as dataProvider from './lib/dataProvider'
import * as downloads from './lib/downloads'
import * as icons from './lib/icons'
import * as socialSharers from './lib/socialSharers'
import * as referenceFormatter from './lib/referencesFormatter'

ready((): void => {
const articleTitle = dataProvider.getArticleTitle()
icons.build(dataProvider.getArticleId())
downloads.build(articleTitle, dataProvider.getArticleId())

try {
Expand Down
17 changes: 16 additions & 1 deletion src/themes/elife/lib/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import { first, text } from '../../../util'

interface Response {
ok: boolean
articleData: { pdf: string; figuresPdf: string }
articleData: {
pdf: string
figuresPdf: string
copyright: { license: string }
}
}

interface PdfUrlGetter {
(id: string, pdfType: string): Promise<string>
}

interface CopyrightLicenseGetter {
(id: string): Promise<string>
}

const normaliseWhitespace = (txt: string): string => {
return txt.replace(/\n/, ' ').replace(/ \s+|\n+/g, ' ')
}
Expand Down Expand Up @@ -73,6 +81,13 @@ export const getFiguresPdfUrl = async (
return pdfUrlGetter(id, 'figures')
}

export const getCopyrightLicense: CopyrightLicenseGetter = async (
id: string
): Promise<string> => {
const response = await query(id, window.fetch)
return Promise.resolve(response.articleData.copyright.license)
}

export const query = async (
id: string,
fetcher: WindowOrWorkerGlobalScope['fetch']
Expand Down
73 changes: 73 additions & 0 deletions src/themes/elife/lib/icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { before, create, first } from '../../../util'
import { getCopyrightLicense } from './dataProvider'

const deriveUrl = (id: string): string => {
switch (id) {
case 'CC0-1.0':
return 'https://creativecommons.org/publicdomain/zero/1.0/'
case 'CC-BY-1.0':
return 'https://creativecommons.org/licenses/by/1.0/'
case 'CC-BY-2.0':
return 'https://creativecommons.org/licenses/by/2.0/'
case 'CC-BY-2.5':
return 'https://creativecommons.org/licenses/by/2.5/'
case 'CC-BY-3.0':
return 'https://creativecommons.org/licenses/by/3.0/'
case 'CC-BY-4.0':
return 'https://creativecommons.org/licenses/by/4.0/'
}
return ''
}

const buildMenu = (license: string): Promise<unknown> => {
const articleTitle = first(':--Article > :--title')
if (articleTitle === null) {
return Promise.reject(
new Error("Can't find element to bolt the download link on top of")
)
}
before(
articleTitle,
create(
'ul',
{ class: 'content-header__icons' },
create(
'li',
{},
create(
'a',
{
class: 'content-header__icon content-header__icon--oa',
href: 'https://en.wikipedia.org/wiki/Open_access',
},
create('span', { class: 'visuallyhidden' }, 'Open access')
)
),
create(
'li',
{},
create(
'a',
{
class: 'content-header__icon content-header__icon--cc',
href: deriveUrl(license),
},
create('span', { class: 'visuallyhidden' }, 'Copyright information')
)
)
)
)
return Promise.resolve()
}

export const build = (articleId: string): void => {
try {
getCopyrightLicense(articleId)
.then(buildMenu)
.catch((err: Error) => {
throw err
})
} catch (err) {
console.error(err)
}
}

0 comments on commit c6fcdcf

Please sign in to comment.