From 535e2c42f654d9657f8c1ce72ba489df758ae27b Mon Sep 17 00:00:00 2001 From: Nokome Bentley Date: Fri, 13 Nov 2020 17:46:09 +1300 Subject: [PATCH] fix(Crossref): Add DOI and URL as encode options --- .../article-elife-50356.xml | 2 +- .../__file_snapshots__/review-elife-50356.xml | 2 +- src/codecs/crossref/crossref.test.ts | 18 +++++++---- src/codecs/crossref/index.ts | 32 +++++++++++++++---- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/codecs/crossref/__file_snapshots__/article-elife-50356.xml b/src/codecs/crossref/__file_snapshots__/article-elife-50356.xml index 7557e0714..0be45b50d 100644 --- a/src/codecs/crossref/__file_snapshots__/article-elife-50356.xml +++ b/src/codecs/crossref/__file_snapshots__/article-elife-50356.xml @@ -47,7 +47,7 @@

Endometriosis is a chronic painful disease highly prevalent in women that is defined by growth of endometrial tissue outside the uterine cavity and lacks adequate treatment. Medical use of cannabis derivatives is a current hot topic and it is unknown whether phytocannabinoids may modify endometriosis symptoms and development. Here we evaluate the effects of repeated exposure to Δ9-tetrahydrocannabinol (THC) in a mouse model of surgically-induced endometriosis. In this model, female mice develop mechanical hypersensitivity in the caudal abdomen, mild anxiety-like behavior and substantial memory deficits associated with the presence of extrauterine endometrial cysts. Interestingly, daily treatments with THC (2 mg/kg) alleviate mechanical hypersensitivity and pain unpleasantness, modify uterine innervation and restore cognitive function without altering the anxiogenic phenotype. Strikingly, THC also inhibits the development of endometrial cysts. These data highlight the interest of scheduled clinical trials designed to investigate possible benefits of THC for women with endometriosis.

- 10.47704/1 + 10.5555/12345 https://example.org diff --git a/src/codecs/crossref/__file_snapshots__/review-elife-50356.xml b/src/codecs/crossref/__file_snapshots__/review-elife-50356.xml index 817700fb5..63cd4cd72 100644 --- a/src/codecs/crossref/__file_snapshots__/review-elife-50356.xml +++ b/src/codecs/crossref/__file_snapshots__/review-elife-50356.xml @@ -31,7 +31,7 @@ - 10.47704/1 + 10.5555/12345 https://example.org diff --git a/src/codecs/crossref/crossref.test.ts b/src/codecs/crossref/crossref.test.ts index 9582d6f39..f55c3c567 100644 --- a/src/codecs/crossref/crossref.test.ts +++ b/src/codecs/crossref/crossref.test.ts @@ -29,9 +29,12 @@ describe('decode', () => { describe('encode', () => { test('eLife article', async () => { - expect(await crossref.dump(elife50356)).toMatchFile( - snapshot('article-elife-50356.xml') - ) + expect( + await crossref.dump(elife50356, { + doi: '10.5555/12345', + url: 'https://example.org', + }) + ).toMatchFile(snapshot('article-elife-50356.xml')) }) test('review of eLife article', async () => { @@ -47,8 +50,11 @@ describe('encode', () => { datePublished: '2020-11-12', itemReviewed: elife50356, } - expect(await crossref.dump(review)).toMatchFile( - snapshot('review-elife-50356.xml') - ) + expect( + await crossref.dump(review, { + doi: '10.5555/12345', + url: 'https://example.org', + }) + ).toMatchFile(snapshot('review-elife-50356.xml')) }) }) diff --git a/src/codecs/crossref/index.ts b/src/codecs/crossref/index.ts index b51fb7bde..a97989d28 100644 --- a/src/codecs/crossref/index.ts +++ b/src/codecs/crossref/index.ts @@ -7,7 +7,7 @@ import * as http from '../../util/http' import * as vfile from '../../util/vfile' import * as xml from '../../util/xml' import { decodeCsl } from '../csl' -import { Codec } from '../types' +import { Codec, CommonEncodeOptions } from '../types' import { getLogger } from '@stencila/logga' import { TxtCodec } from '../txt' import { encodeAbstract } from '../jats' @@ -22,6 +22,18 @@ interface Review extends schema.CreativeWork { type ContributorRole = 'author' | 'reviewer' +interface EncodeOptions extends CommonEncodeOptions { + /** + * The DOI for the node + */ + doi?: string + + /** + * The URL for the node + */ + url?: string +} + const log = getLogger('encoda:crossref') export class CrossrefCodec extends Codec implements Codec { @@ -69,17 +81,25 @@ export class CrossrefCodec extends Codec implements Codec { * See https://www.crossref.org/education/content-registration/crossrefs-metadata-deposit-schema/crossref-xsd-schema-quick-reference/ * Generated XML can be validated via https://www.crossref.org/02publishers/parser.html */ - public readonly encode = (node: schema.Node): Promise => { + public readonly encode = ( + node: schema.Node, + options: EncodeOptions = this.commonEncodeDefaults + ): Promise => { + // The following "options" are actually required so throw an error + // if not supplied + const { doi, url } = options + if (doi === undefined) throw new Error(`The "doi" parameter is required.`) + if (url === undefined) throw new Error(`The "url" parameter is required.`) + + // These constant may be added to options later. const { - doi = '10.47704/1', - url = 'https://example.org', depositorName = 'Stencila', depositorEmail = 'doi@stenci.la', registrantName = 'Stencila', + version = '4.4.2', } = {} - const version = '4.4.2' - + // The root XMl document to create const doc = { declaration: { attributes: {