-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprint.ts
More file actions
126 lines (122 loc) · 3.79 KB
/
preprint.ts
File metadata and controls
126 lines (122 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { Element } from 'xast';
import { e } from './utils.js';
import type { Preprint } from './types.js';
import type { ProjectFrontmatter } from 'myst-frontmatter';
import { normalize } from 'doi-utils';
import { contributorsXmlFromMystAuthors } from './contributors.js';
import { dateXml } from './dates.js';
import { createFundingXml, fundrefFromMyst } from './funding.js';
import type { ISession } from 'myst-cli';
/**
* Create posted content xml
*
* Required fields: titles, posted_date, doi_data
*
* Optional fields: group_title, contributors, acceptance_date, institution,
* fr:program, ai:program, rel:program, citation_list, jats:abstract, scn_policy
*/
export function preprintXml({
contributors,
title,
subtitle,
abstract,
funding,
doi_data,
citations,
license,
date,
}: Preprint) {
if (!title) throw new Error('Missing required frontmatter field: title');
const posted_date = dateXml('posted_date', date);
if (!posted_date) throw new Error('Missing required frontmatter field: date');
if (!doi_data?.doi) throw new Error('Missing required frontmatter field: doi');
const children: Element[] = [];
if (contributors) children.push(contributors);
const titles = [e('title', title)];
if (subtitle) titles.push(e('subtitle', subtitle));
children.push(e('titles', titles));
children.push(posted_date);
if (abstract) children.push(abstract);
const fundingXml = createFundingXml(funding);
if (fundingXml) children.push(fundingXml);
if (license) {
children.push(
e('ai:program', { name: 'AccessIndicators' }, [
e('ai:free_to_read'),
e('ai:license_ref', { applies_to: 'vor' }, license),
]),
);
}
const doiChildren = [e('doi', doi_data.doi)];
if (doi_data.resource) {
doiChildren.push(e('resource', { content_version: 'vor' }, doi_data.resource));
}
if (doi_data.xml || doi_data.pdf || doi_data.zip) {
const collectionChildren = [];
if (doi_data.xml) {
collectionChildren.push(
e('item', [e('resource', { mime_type: 'text/xml', content_version: 'vor' }, doi_data.xml)]),
);
}
if (doi_data.pdf) {
collectionChildren.push(
e('item', [
e('resource', { mime_type: 'application/pdf', content_version: 'vor' }, doi_data.pdf),
]),
);
}
if (doi_data.zip) {
collectionChildren.push(
e('item', [
e('resource', { mime_type: 'application/zip', content_version: 'vor' }, doi_data.zip),
]),
);
}
doiChildren.push(e('collection', { property: 'text-mining' }, collectionChildren));
}
children.push(e('doi_data', doiChildren));
if (citations) {
children.push(
e(
'citation_list',
Object.entries(citations).map(([key, value]) => {
return e('citation', { key }, [e('doi', value)]);
}),
),
);
}
return e('posted_content', children);
}
export function preprintFromMyst(
session: ISession,
myst: ProjectFrontmatter,
citations?: Record<string, string>,
abstract?: Element,
) {
const { title, subtitle, license, doi, date } = myst;
const contributors = contributorsXmlFromMystAuthors(myst);
const paperOpts: Preprint = {
contributors,
title,
subtitle,
date: typeof date === 'string' ? new Date(date) : undefined,
license: license?.content?.url,
abstract,
funding: fundrefFromMyst(session, myst),
};
if (license && license.content?.CC) {
// Only put in CC licenses at this time
paperOpts.license = license.content.url;
}
const normalizedDoi = normalize(doi);
if (normalizedDoi) {
paperOpts.doi_data = {
doi: normalizedDoi,
resource: `https://doi.curvenote.com/${normalizedDoi}`,
};
}
if (citations && Object.keys(citations).length) {
paperOpts.citations = citations;
}
return preprintXml(paperOpts);
}