@manuscripts/transform 4.3.53 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/jats/__tests__/jats-roundtrip.test.js +3 -3
- package/dist/cjs/version.js +1 -1
- package/dist/es/jats/__tests__/jats-roundtrip.test.js +3 -3
- package/dist/es/version.js +1 -1
- package/dist/types/jats/__tests__/utils.d.ts +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +22 -21
- package/src/errors.ts +28 -0
- package/src/getVersion.ts +21 -0
- package/src/index.ts +28 -0
- package/src/jats/__tests__/__fixtures__/debug-example.xml +136 -0
- package/src/jats/__tests__/__fixtures__/jats-abstract-no-body.xml +163 -0
- package/src/jats/__tests__/__fixtures__/jats-citations.xml +472 -0
- package/src/jats/__tests__/__fixtures__/jats-comments.xml +46 -0
- package/src/jats/__tests__/__fixtures__/jats-document.xml +134 -0
- package/src/jats/__tests__/__fixtures__/jats-example-doc.xml +1235 -0
- package/src/jats/__tests__/__fixtures__/jats-example-front-only.xml +26 -0
- package/src/jats/__tests__/__fixtures__/jats-example-full.xml +209 -0
- package/src/jats/__tests__/__fixtures__/jats-example-no-back.xml +156 -0
- package/src/jats/__tests__/__fixtures__/jats-example-no-body.xml +91 -0
- package/src/jats/__tests__/__fixtures__/jats-example.xml +221 -0
- package/src/jats/__tests__/__fixtures__/jats-fn-group.xml +60 -0
- package/src/jats/__tests__/__fixtures__/jats-import-no-refs.xml +1077 -0
- package/src/jats/__tests__/__fixtures__/jats-import.xml +1561 -0
- package/src/jats/__tests__/__fixtures__/jats-roundtrip.xml +133 -0
- package/src/jats/__tests__/__fixtures__/jats-tables-example.xml +2043 -0
- package/src/jats/__tests__/__fixtures__/math-fragment.xml +12 -0
- package/src/jats/__tests__/__snapshots__/citeproc.test.ts.snap +57 -0
- package/src/jats/__tests__/__snapshots__/create-article-node.test.ts.snap +65 -0
- package/src/jats/__tests__/__snapshots__/jats-exporter.test.ts.snap +7 -0
- package/src/jats/__tests__/__snapshots__/jats-importer.test.ts.snap +59605 -0
- package/src/jats/__tests__/__snapshots__/jats-roundtrip.test.ts.snap +18 -0
- package/src/jats/__tests__/citations.ts +25 -0
- package/src/jats/__tests__/citeproc.test.ts +195 -0
- package/src/jats/__tests__/create-article-node.test.ts +29 -0
- package/src/jats/__tests__/data/section-categories.ts +212 -0
- package/src/jats/__tests__/files.ts +24 -0
- package/src/jats/__tests__/jats-exporter.test.ts +223 -0
- package/src/jats/__tests__/jats-importer.test.ts +567 -0
- package/src/jats/__tests__/jats-roundtrip.test.ts +66 -0
- package/src/jats/__tests__/utils.ts +90 -0
- package/src/jats/exporter/citeproc.ts +265 -0
- package/src/jats/exporter/jats-exporter.ts +2090 -0
- package/src/jats/exporter/jats-versions.ts +50 -0
- package/src/jats/exporter/labels.ts +101 -0
- package/src/jats/importer/create-article-node.ts +52 -0
- package/src/jats/importer/jats-comments.ts +111 -0
- package/src/jats/importer/jats-dom-parser.ts +1260 -0
- package/src/jats/importer/jats-parser-utils.ts +234 -0
- package/src/jats/importer/jats-transformations.ts +591 -0
- package/src/jats/importer/parse-jats-article.ts +106 -0
- package/src/jats/index.ts +21 -0
- package/src/jats/types.ts +16 -0
- package/src/lib/__tests__/footnotes.test.ts +36 -0
- package/src/lib/citeproc.ts +30 -0
- package/src/lib/credit-roles.ts +91 -0
- package/src/lib/deafults.ts +17 -0
- package/src/lib/footnotes.ts +85 -0
- package/src/lib/html.ts +56 -0
- package/src/lib/section-categories.ts +27 -0
- package/src/lib/utils.ts +145 -0
- package/src/lib/xml.ts +28 -0
- package/src/schema/__tests__/docs.ts +1911 -0
- package/src/schema/__tests__/groups.test.ts +61 -0
- package/src/schema/__tests__/migration.test.ts +23 -0
- package/src/schema/groups.ts +36 -0
- package/src/schema/index.ts +286 -0
- package/src/schema/marks.ts +254 -0
- package/src/schema/migration/migrate.ts +77 -0
- package/src/schema/migration/migration-script.ts +26 -0
- package/src/schema/migration/migration-scripts/1.2.5.ts +44 -0
- package/src/schema/migration/migration-scripts/2.3.22.ts +52 -0
- package/src/schema/migration/migration-scripts/3.0.12.ts +33 -0
- package/src/schema/migration/migration-scripts/3.0.21.ts +50 -0
- package/src/schema/migration/migration-scripts/3.0.30.ts +65 -0
- package/src/schema/migration/migration-scripts/3.0.31.ts +40 -0
- package/src/schema/migration/migration-scripts/3.0.41.ts +37 -0
- package/src/schema/migration/migration-scripts/3.0.55.ts +42 -0
- package/src/schema/migration/migration-scripts/3.0.56.ts +114 -0
- package/src/schema/migration/migration-scripts/4.2.13.ts +37 -0
- package/src/schema/migration/migration-scripts/4.2.15.ts +48 -0
- package/src/schema/migration/migration-scripts/4.3.23.ts +55 -0
- package/src/schema/migration/migration-scripts/4.3.34.ts +132 -0
- package/src/schema/migration/migration-scripts/4.3.35.ts +41 -0
- package/src/schema/migration/migration-scripts/index.ts +49 -0
- package/src/schema/nodes/abstracts.ts +29 -0
- package/src/schema/nodes/affiliation.ts +88 -0
- package/src/schema/nodes/affiliations.ts +30 -0
- package/src/schema/nodes/alt_text.ts +36 -0
- package/src/schema/nodes/alt_title.ts +40 -0
- package/src/schema/nodes/alt_titles_section.ts +58 -0
- package/src/schema/nodes/attachment.ts +46 -0
- package/src/schema/nodes/attachments.ts +45 -0
- package/src/schema/nodes/attribution.ts +38 -0
- package/src/schema/nodes/author_notes.ts +44 -0
- package/src/schema/nodes/award.ts +52 -0
- package/src/schema/nodes/awards.ts +45 -0
- package/src/schema/nodes/backmatter.ts +31 -0
- package/src/schema/nodes/bibliography_element.ts +58 -0
- package/src/schema/nodes/bibliography_item.ts +108 -0
- package/src/schema/nodes/bibliography_section.ts +60 -0
- package/src/schema/nodes/blockquote_element.ts +79 -0
- package/src/schema/nodes/body.ts +29 -0
- package/src/schema/nodes/box_element.ts +67 -0
- package/src/schema/nodes/caption.ts +67 -0
- package/src/schema/nodes/caption_title.ts +67 -0
- package/src/schema/nodes/citation.ts +70 -0
- package/src/schema/nodes/comment.ts +50 -0
- package/src/schema/nodes/comments.ts +28 -0
- package/src/schema/nodes/contributor.ts +87 -0
- package/src/schema/nodes/contributors.ts +40 -0
- package/src/schema/nodes/core_section.ts +27 -0
- package/src/schema/nodes/corresp.ts +49 -0
- package/src/schema/nodes/cross_reference.ts +64 -0
- package/src/schema/nodes/doc.ts +21 -0
- package/src/schema/nodes/embed.ts +55 -0
- package/src/schema/nodes/equation.ts +65 -0
- package/src/schema/nodes/equation_element.ts +61 -0
- package/src/schema/nodes/figure.ts +65 -0
- package/src/schema/nodes/figure_element.ts +73 -0
- package/src/schema/nodes/footnote.ts +95 -0
- package/src/schema/nodes/footnotes_element.ts +69 -0
- package/src/schema/nodes/footnotes_section.ts +61 -0
- package/src/schema/nodes/general_table_footnote.ts +42 -0
- package/src/schema/nodes/graphical_abstract_section.ts +62 -0
- package/src/schema/nodes/hard_break.ts +36 -0
- package/src/schema/nodes/hero_image.ts +46 -0
- package/src/schema/nodes/highlight_marker.ts +68 -0
- package/src/schema/nodes/image_element.ts +49 -0
- package/src/schema/nodes/inline_equation.ts +68 -0
- package/src/schema/nodes/inline_footnote.ts +66 -0
- package/src/schema/nodes/keyword.ts +61 -0
- package/src/schema/nodes/keyword_group.ts +63 -0
- package/src/schema/nodes/keywords.ts +59 -0
- package/src/schema/nodes/keywords_element.ts +64 -0
- package/src/schema/nodes/link.ts +79 -0
- package/src/schema/nodes/list.ts +134 -0
- package/src/schema/nodes/listing.ts +91 -0
- package/src/schema/nodes/listing_element.ts +61 -0
- package/src/schema/nodes/long_desc.ts +36 -0
- package/src/schema/nodes/manuscript.ts +86 -0
- package/src/schema/nodes/missing_figure.ts +59 -0
- package/src/schema/nodes/paragraph.ts +77 -0
- package/src/schema/nodes/placeholder.ts +62 -0
- package/src/schema/nodes/placeholder_element.ts +60 -0
- package/src/schema/nodes/pullquote_element.ts +81 -0
- package/src/schema/nodes/quote_image.ts +64 -0
- package/src/schema/nodes/section.ts +69 -0
- package/src/schema/nodes/section_label.ts +39 -0
- package/src/schema/nodes/section_title.ts +58 -0
- package/src/schema/nodes/subtitle.ts +40 -0
- package/src/schema/nodes/subtitles.ts +51 -0
- package/src/schema/nodes/supplement.ts +77 -0
- package/src/schema/nodes/supplements.ts +60 -0
- package/src/schema/nodes/table.ts +144 -0
- package/src/schema/nodes/table_col.ts +76 -0
- package/src/schema/nodes/table_element.ts +67 -0
- package/src/schema/nodes/table_element_footer.ts +42 -0
- package/src/schema/nodes/text.ts +31 -0
- package/src/schema/nodes/title.ts +43 -0
- package/src/schema/nodes/trans_abstract.ts +58 -0
- package/src/schema/nodes/trans_graphical_abstract.ts +67 -0
- package/src/schema/types.ts +240 -0
- package/src/tests.ts +33 -0
- package/src/transformer/id.ts +23 -0
- package/src/transformer/index.ts +21 -0
- package/src/transformer/node-names.ts +73 -0
- package/src/transformer/node-title.ts +111 -0
- package/src/transformer/node-types.ts +39 -0
- package/src/transformer/node-validator.ts +82 -0
- package/src/types.ts +25 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* © 2020 Atypon Systems LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { isContributorNode, ManuscriptNode, schema } from '../../schema'
|
|
18
|
+
import { generateNodeID } from '../../transformer'
|
|
19
|
+
|
|
20
|
+
export const updateDocumentIDs = (node: ManuscriptNode) => {
|
|
21
|
+
const replacements = new Map()
|
|
22
|
+
const warnings: string[] = []
|
|
23
|
+
|
|
24
|
+
recurseDoc(node, (n) => updateNodeID(n, replacements, warnings))
|
|
25
|
+
recurseDoc(node, (n) => updateNodeRID(n, replacements, warnings))
|
|
26
|
+
recurseDoc(node, (n) => updateNodeRIDS(n, replacements, warnings))
|
|
27
|
+
recurseDoc(node, (n) => updateContributorNodesIDS(n, replacements, warnings))
|
|
28
|
+
recurseDoc(node, (n) => updateCommentTarget(n, replacements, warnings))
|
|
29
|
+
|
|
30
|
+
return warnings
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Recurses the document starting from the top node (which is omitted with node.descendants function)
|
|
35
|
+
* @param node
|
|
36
|
+
* @param fn
|
|
37
|
+
*/
|
|
38
|
+
const recurseDoc = (node: ManuscriptNode, fn: (n: ManuscriptNode) => void) => {
|
|
39
|
+
fn(node)
|
|
40
|
+
node.descendants((n) => fn(n))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Provide IDs to nodes that have IDs but no value (null or '') by mutating them
|
|
45
|
+
*/
|
|
46
|
+
const updateNodeID = (
|
|
47
|
+
node: ManuscriptNode,
|
|
48
|
+
replacements: Map<string, string>,
|
|
49
|
+
warnings: string[]
|
|
50
|
+
) => {
|
|
51
|
+
if (
|
|
52
|
+
node.type === schema.nodes.comment ||
|
|
53
|
+
node.type === schema.nodes.highlight_marker
|
|
54
|
+
) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!('id' in node.attrs)) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
const previousID = node.attrs.id
|
|
62
|
+
const nextID = generateNodeID(node.type)
|
|
63
|
+
if (previousID) {
|
|
64
|
+
if (
|
|
65
|
+
replacements.has(previousID) ||
|
|
66
|
+
Array.from(replacements.values()).includes(previousID)
|
|
67
|
+
) {
|
|
68
|
+
warnings.push(`node.attrs.id ${previousID} exists twice!`)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
replacements.set(previousID, nextID)
|
|
72
|
+
}
|
|
73
|
+
// @ts-ignore - while attrs are readonly, it is acceptable to change them when document is inactive and there is no view
|
|
74
|
+
node.attrs = {
|
|
75
|
+
...node.attrs,
|
|
76
|
+
id: nextID,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Replaces cross-reference rids of nodes by mutating their attributes
|
|
82
|
+
*/
|
|
83
|
+
const updateNodeRID = (
|
|
84
|
+
node: ManuscriptNode,
|
|
85
|
+
replacements: Map<string, string>,
|
|
86
|
+
// eslint-disable-next-line
|
|
87
|
+
warnings: string[]
|
|
88
|
+
) => {
|
|
89
|
+
const previousRID = node.attrs.rid
|
|
90
|
+
if (!('rid' in node.attrs) || !previousRID) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
if (!replacements.has(previousRID)) {
|
|
94
|
+
// TODO produces a lot of missing replacements..
|
|
95
|
+
// warnings.push(`Missing replacement for node.attrs.rid ${previousRID}`)
|
|
96
|
+
} else {
|
|
97
|
+
// @ts-ignore - while attrs are readonly, it is acceptable to change them when document is inactive and there is no view
|
|
98
|
+
node.attrs = {
|
|
99
|
+
...node.attrs,
|
|
100
|
+
rid: replacements.get(previousRID),
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const updateNodeRIDS = (
|
|
106
|
+
node: ManuscriptNode,
|
|
107
|
+
replacements: Map<string, string>,
|
|
108
|
+
// eslint-disable-next-line
|
|
109
|
+
warnings: string[]
|
|
110
|
+
) => {
|
|
111
|
+
const previousRIDs: string[] = node.attrs.rids
|
|
112
|
+
if (!('rids' in node.attrs) || !previousRIDs.length) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
// @ts-ignore - while attrs are readonly, it is acceptable to change them when document is inactive and there is no view
|
|
116
|
+
node.attrs = {
|
|
117
|
+
...node.attrs,
|
|
118
|
+
rids: previousRIDs.map((r) => replacements.get(r) || r),
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Updates the IDS for corresps, affiliations and footnotes inside the contributor
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
const updateContributorNodesIDS = (
|
|
127
|
+
node: ManuscriptNode,
|
|
128
|
+
replacements: Map<string, string>,
|
|
129
|
+
// eslint-disable-next-line
|
|
130
|
+
warnings: string[]
|
|
131
|
+
) => {
|
|
132
|
+
if (isContributorNode(node)) {
|
|
133
|
+
const replaceAll = (ids: string[]) =>
|
|
134
|
+
ids?.map((i) => replacements.get(i)).filter((id): id is string => !!id)
|
|
135
|
+
// @ts-ignore - while attrs are readonly, it is acceptable to change them when document is inactive and there is no view
|
|
136
|
+
node.attrs = {
|
|
137
|
+
...node.attrs,
|
|
138
|
+
footnoteIDs: replaceAll(node.attrs.footnoteIDs),
|
|
139
|
+
correspIDs: replaceAll(node.attrs.correspIDs),
|
|
140
|
+
affiliationIDs: replaceAll(node.attrs.affiliationIDs),
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (node.type !== schema.nodes.contributors) {
|
|
145
|
+
return false
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const updateCommentTarget = (
|
|
150
|
+
node: ManuscriptNode,
|
|
151
|
+
replacements: Map<string, string>,
|
|
152
|
+
// eslint-disable-next-line
|
|
153
|
+
warnings: string[]
|
|
154
|
+
) => {
|
|
155
|
+
if (node.type !== schema.nodes.comment) {
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
const target = node.attrs.target
|
|
159
|
+
if (!target) {
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
if (!replacements.has(target)) {
|
|
163
|
+
// TODO produces a lot of missing replacements..
|
|
164
|
+
// warnings.push(`Missing replacement for node.attrs.rid ${previousRID}`)
|
|
165
|
+
} else {
|
|
166
|
+
// @ts-ignore - while attrs are readonly, it is acceptable to change them when document is inactive and there is no view
|
|
167
|
+
node.attrs = {
|
|
168
|
+
...node.attrs,
|
|
169
|
+
target: replacements.get(target),
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// JATS to HTML conversion
|
|
175
|
+
const JATS_TO_HTML_MAPPING = new Map<string, string>([
|
|
176
|
+
['bold', 'b'],
|
|
177
|
+
['italic', 'i'],
|
|
178
|
+
['sc', 'style'], // TODO: style
|
|
179
|
+
['sub', 'sub'],
|
|
180
|
+
['sup', 'sup'],
|
|
181
|
+
])
|
|
182
|
+
|
|
183
|
+
const renameJatsNodesToHTML = (
|
|
184
|
+
node: Node,
|
|
185
|
+
container: Node,
|
|
186
|
+
createElement: (tagName: string) => HTMLElement
|
|
187
|
+
) => {
|
|
188
|
+
node.childNodes.forEach((childNode) => {
|
|
189
|
+
switch (childNode.nodeType) {
|
|
190
|
+
case Node.ELEMENT_NODE: {
|
|
191
|
+
const newNodeName = JATS_TO_HTML_MAPPING.get(childNode.nodeName)
|
|
192
|
+
|
|
193
|
+
if (newNodeName) {
|
|
194
|
+
const newNode = createElement(newNodeName)
|
|
195
|
+
renameJatsNodesToHTML(childNode, newNode, createElement)
|
|
196
|
+
container.appendChild(newNode)
|
|
197
|
+
} else {
|
|
198
|
+
console.warn(`Unhandled node name: ${newNodeName}`)
|
|
199
|
+
container.appendChild(childNode.cloneNode())
|
|
200
|
+
}
|
|
201
|
+
break
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
case Node.TEXT_NODE:
|
|
205
|
+
default: {
|
|
206
|
+
container.appendChild(childNode.cloneNode())
|
|
207
|
+
break
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Renames JATS nodes to HTML. Doesn't mutate the original document.
|
|
215
|
+
* @param element
|
|
216
|
+
* @param createElement
|
|
217
|
+
* @returns
|
|
218
|
+
*/
|
|
219
|
+
export const htmlFromJatsNode = (
|
|
220
|
+
element: Element | undefined | null,
|
|
221
|
+
createElement?: (tagName: string) => HTMLElement
|
|
222
|
+
) => {
|
|
223
|
+
if (!element) {
|
|
224
|
+
return undefined
|
|
225
|
+
}
|
|
226
|
+
if (!createElement) {
|
|
227
|
+
createElement = (tagName) => element.ownerDocument.createElement(tagName)
|
|
228
|
+
}
|
|
229
|
+
const temp = createElement('template') as HTMLTemplateElement
|
|
230
|
+
// Interesting fact: template has special semantics that are not same as regular element's
|
|
231
|
+
// In this case unlike normal div, template's HTML has to be accessed via content
|
|
232
|
+
renameJatsNodesToHTML(element, temp, createElement)
|
|
233
|
+
return temp.innerHTML?.trim()
|
|
234
|
+
}
|