-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csl-to-jast.ts
168 lines (155 loc) · 4.36 KB
/
csl-to-jast.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Data as CSL } from 'csl-json'
import {
Ref,
RefList,
Front,
PubId,
PersonGroup,
Source,
Issn,
Isbn,
ExtLink,
Issue,
Volume,
Edition,
PublisherLoc,
PublisherName,
} from 'jast-types'
import { x } from 'xastscript'
export function cslToJats(data: CSL | CSL[]) {
if (Array.isArray(data)) {
return cslToRefList(data)
}
return cslToFront(data)
}
//
export function cslToFront(data: CSL) {
//TODO: [csl-to-jast] write a function that converts CSL to JAST frontmatter
}
export function cslToRefList(
data: CSL[] | { [key: string | number]: CSL }
): RefList {
if (Array.isArray(data)) {
const reflist = data.map((csl, index) => cslToRef(csl, index))
return x('refList', reflist) as RefList
}
const reflist = Object.entries(data).map(([index, csl]) =>
cslToRef(csl, index)
)
return x('refList', reflist) as RefList
}
export function cslToRef(data: CSL, index: number | string): Ref {
const date = data.issued?.['date-parts']?.[0]
const [year, month, day] = date || data.issued?.literal?.split('-') || []
const pubIds = ['DOI', 'PMID', 'PMCID'].flatMap(
//@ts-expect-error no idea why not work
(id: 'DOI' | 'PMID' | 'PMCID') =>
data[id]
? //@ts-expect-error I can assign it to this dw bby
(x('pubId', { pubIdType: id.toLowerCase() }, [
{ type: 'text', value: data[id] },
]) as PubId)
: []
)
const names = data.author?.map((person) => {
return x(
'name',
Object.entries(person).flatMap(
([name, val]: [name: string, val: string]) => {
switch (name) {
case 'family':
return nameMap('surname', val)
case 'given':
return nameMap('givenNames', val)
case 'suffix':
return nameMap('suffix', val)
case 'dropping-particle':
return nameMap('prefix', val)
case 'non-dropping-particle':
return nameMap('prefix', val)
default:
return []
}
}
)
)
})
const authors = names
? (x('personGroup', { personGroupType: 'author' }, names) as PersonGroup)
: []
const pages = getPages(data)
const source = getTitle(data)
const elementCitationChildren = [
nameMap('publisherLoc', data['publisher-place']) as PublisherLoc,
nameMap('publisherName', data['publisher']) as PublisherName,
//x(
// 'date',
// [
nameMap('year', `${year || ''}`),
nameMap('month', `${month || ''}`),
nameMap('day', `${day || ''}`),
// ].flat()
// ) as Date,
pubIds as PubId[],
authors,
pages,
source as Source,
nameMap('source', data['container-title']) as Source,
nameMap('issn', data['ISSN']) as Issn,
nameMap('isbn', data['ISBN']) as Isbn,
nameMap('extLink', data['URL']) as ExtLink,
nameMap('issue', `${data['issue'] || data['number'] || ''}`) as Issue,
nameMap('volume', `${data['volume'] || ''}`) as Volume,
nameMap('edition', `${data['edition'] || ''}`) as Edition,
].flat()
return x('ref', { id: typeof index === 'string' ? index : `bib${index}` }, [
x(
'elementCitation',
{ publicationType: getPublicationType(data) },
elementCitationChildren
),
]) as Ref
}
function nameMap(name: string, value: string | undefined) {
return value ? x(name, [{ type: 'text', value: value }]) : []
}
function getPages(data: CSL) {
if (data.page) {
return nameMap('pageRange', data.page)
} else if (data['page-first']) {
return nameMap('pageFirst', data['page-first'])
}
return []
}
function getTitle(data: CSL) {
if (!data.title) return []
if (data.type === 'book') {
return nameMap('source', data.title)
}
return nameMap('articleTitle', data.title)
}
function getPublicationType(data: CSL) {
switch (data.type) {
case 'article':
case 'article-journal':
case 'article-magazine':
case 'article-newspaper':
return 'journal'
case 'book':
return 'book'
case 'chapter':
return 'bookchapter'
case 'dataset':
return 'dataset'
case 'patent':
return 'patent'
case 'review':
return 'review'
default:
return 'standard'
}
}
// TODO: [csl-to-jts] add reviewer support
// TODO: [csl-to-jts] do something with abstract maybe
// TODO: [csl-to-jts] add editor support
// TODO: [csl-to-jts] use citation key if available