@manuscripts/transform 4.5.7 → 4.6.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-exporter.test.js +50 -20
- package/dist/cjs/jats/__tests__/jats-importer.test.js +26 -0
- package/dist/cjs/jats/__tests__/jats-roundtrip.test.js +6 -1
- package/dist/cjs/jats/exporter/JATSExporterBase.js +118 -0
- package/dist/cjs/jats/exporter/jats-exporter.js +227 -349
- package/dist/cjs/jats/exporter/lib.js +62 -0
- package/dist/cjs/jats/importer/jats-dom-parser.js +11 -3
- package/dist/cjs/schema/index.js +4 -1
- package/dist/cjs/schema/migration/migration-scripts/4.5.9.js +30 -0
- package/dist/cjs/schema/migration/migration-scripts/index.js +3 -1
- package/dist/cjs/schema/nodes/bibliography_element.js +0 -1
- package/dist/cjs/schema/nodes/bio.js +24 -0
- package/dist/cjs/schema/nodes/contributor.js +1 -2
- package/dist/cjs/schema/nodes/keywords_element.js +0 -1
- package/dist/cjs/types.js +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/es/jats/__tests__/jats-exporter.test.js +47 -20
- package/dist/es/jats/__tests__/jats-importer.test.js +26 -0
- package/dist/es/jats/__tests__/jats-roundtrip.test.js +3 -1
- package/dist/es/jats/exporter/JATSExporterBase.js +114 -0
- package/dist/es/jats/exporter/jats-exporter.js +201 -319
- package/dist/es/jats/exporter/lib.js +55 -0
- package/dist/es/jats/importer/jats-dom-parser.js +11 -3
- package/dist/es/schema/index.js +4 -1
- package/dist/es/schema/migration/migration-scripts/4.5.9.js +28 -0
- package/dist/es/schema/migration/migration-scripts/index.js +3 -1
- package/dist/es/schema/nodes/bibliography_element.js +0 -1
- package/dist/es/schema/nodes/bio.js +20 -0
- package/dist/es/schema/nodes/contributor.js +1 -2
- package/dist/es/schema/nodes/keywords_element.js +0 -1
- package/dist/es/types.js +1 -1
- package/dist/es/version.js +1 -1
- package/dist/types/jats/exporter/JATSExporterBase.d.ts +35 -0
- package/dist/types/jats/exporter/jats-exporter.d.ts +23 -19
- package/dist/types/jats/exporter/lib.d.ts +23 -0
- package/dist/types/schema/index.d.ts +2 -1
- package/dist/types/schema/migration/migration-scripts/4.5.9.d.ts +8 -0
- package/dist/types/schema/migration/migration-scripts/index.d.ts +3 -2
- package/dist/types/schema/nodes/bio.d.ts +10 -0
- package/dist/types/schema/types.d.ts +1 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +4 -4
- package/src/jats/__tests__/__fixtures__/jats-import.xml +7 -0
- package/src/jats/__tests__/__snapshots__/jats-importer.test.ts.snap +272 -151
- package/src/jats/__tests__/__snapshots__/jats-roundtrip.test.ts.snap +2 -2
- package/src/jats/__tests__/jats-exporter.test.ts +60 -20
- package/src/jats/__tests__/jats-importer.test.ts +37 -0
- package/src/jats/__tests__/jats-roundtrip.test.ts +3 -1
- package/src/jats/exporter/JATSExporterBase.ts +164 -0
- package/src/jats/exporter/jats-exporter.ts +217 -386
- package/src/jats/exporter/lib.ts +69 -0
- package/src/jats/importer/jats-dom-parser.ts +15 -5
- package/src/schema/index.ts +4 -1
- package/src/schema/migration/migration-scripts/4.5.9.ts +62 -0
- package/src/schema/migration/migration-scripts/index.ts +3 -1
- package/src/schema/nodes/bibliography_element.ts +0 -1
- package/src/schema/nodes/bio.ts +48 -0
- package/src/schema/nodes/contributor.ts +1 -2
- package/src/schema/nodes/keywords_element.ts +0 -1
- package/src/schema/types.ts +1 -0
- package/src/types.ts +1 -1
- package/src/version.ts +1 -1
|
@@ -68,6 +68,43 @@ describe('JATS importer', () => {
|
|
|
68
68
|
expect(contributorsNode).toHaveLength(0)
|
|
69
69
|
})
|
|
70
70
|
})
|
|
71
|
+
describe('bio', () => {
|
|
72
|
+
it('should parse bio with graphic and paragraph into the contributor node', async () => {
|
|
73
|
+
const jats = await readAndParseFixture('jats-import.xml')
|
|
74
|
+
const node = parseJATSArticle(jats, sectionCategories)
|
|
75
|
+
changeIDs(node)
|
|
76
|
+
const [contributor] = findNodesByType(node, schema.nodes.contributor)
|
|
77
|
+
|
|
78
|
+
expect(contributor.childCount).toBe(1)
|
|
79
|
+
const bio = contributor.firstChild!
|
|
80
|
+
expect(bio.type).toBe(schema.nodes.bio)
|
|
81
|
+
expect(bio.childCount).toBe(2)
|
|
82
|
+
|
|
83
|
+
const image = findNodeByType(bio, schema.nodes.image_element)
|
|
84
|
+
expect(findNodeByType(image, schema.nodes.figure).attrs.src).toBe(
|
|
85
|
+
'agnete-hornnes.png'
|
|
86
|
+
)
|
|
87
|
+
expect(findNodeByType(image, schema.nodes.alt_text).textContent).toBe(
|
|
88
|
+
'Headshot of Agnete Hviid Hornnes'
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
// exact paragraph text proves name/email/xref metadata did not leak into the bio
|
|
92
|
+
const paragraph = bio.lastChild!
|
|
93
|
+
expect(paragraph.type).toBe(schema.nodes.paragraph)
|
|
94
|
+
expect(paragraph.textContent).toBe(
|
|
95
|
+
'Agnete is a Professor of Neurology at Herlev og Gentofte Hospital, specializing in stroke research.'
|
|
96
|
+
)
|
|
97
|
+
expect(bio).toMatchSnapshot()
|
|
98
|
+
})
|
|
99
|
+
it('should not add bio content to contributors without a bio element', async () => {
|
|
100
|
+
const jats = await readAndParseFixture('jats-import.xml')
|
|
101
|
+
const node = parseJATSArticle(jats, sectionCategories)
|
|
102
|
+
const [, contributor] = findNodesByType(node, schema.nodes.contributor)
|
|
103
|
+
|
|
104
|
+
expect(contributor.childCount).toBe(0)
|
|
105
|
+
expect(findNodesByType(node, schema.nodes.bio)).toHaveLength(1)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
71
108
|
describe('affiliations', () => {
|
|
72
109
|
it('should correctly parse affiliation nodes', async () => {
|
|
73
110
|
const jats = await readAndParseFixture('jats-import.xml')
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { parseXml } from 'libxmljs2'
|
|
18
|
+
import serializeToXML from 'w3c-xmlserializer'
|
|
18
19
|
|
|
19
20
|
import { JATSExporter } from '../exporter/jats-exporter'
|
|
20
21
|
import { parseJATSArticle } from '../importer/parse-jats-article'
|
|
@@ -36,9 +37,10 @@ const roundtrip = async (filename: string) => {
|
|
|
36
37
|
const node = parseJATSArticle(doc, sectionCategories)
|
|
37
38
|
|
|
38
39
|
const exporter = new JATSExporter()
|
|
39
|
-
|
|
40
|
+
const document = await exporter.export(node, {
|
|
40
41
|
csl: DEFAULT_CSL_OPTIONS,
|
|
41
42
|
})
|
|
43
|
+
return serializeToXML(document)
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
describe('JATS roundtrip', () => {
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* © 2026 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 { DOMSerializer, type NodeType } from 'prosemirror-model'
|
|
18
|
+
import { findChildrenByAttr, findChildrenByType } from 'prosemirror-utils'
|
|
19
|
+
|
|
20
|
+
import { isNodeOfType, ManuscriptNode, ManuscriptNodeType } from '../../schema'
|
|
21
|
+
import { normalizeID } from './lib'
|
|
22
|
+
|
|
23
|
+
export class JATSExporterBase {
|
|
24
|
+
private nodesMap: Map<NodeType, ManuscriptNode[]> = new Map()
|
|
25
|
+
protected manuscriptNode: ManuscriptNode
|
|
26
|
+
protected document: Document
|
|
27
|
+
protected serializer: DOMSerializer
|
|
28
|
+
|
|
29
|
+
public isChildOfNodeType(targetID: string, type: NodeType, descend = false) {
|
|
30
|
+
const nodes = this.getChildrenOfType(type)
|
|
31
|
+
return nodes.some((node) => {
|
|
32
|
+
const result = findChildrenByAttr(
|
|
33
|
+
node,
|
|
34
|
+
(attrs) => attrs.id === targetID,
|
|
35
|
+
descend
|
|
36
|
+
)[0]
|
|
37
|
+
return !!result
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected populateNodesMap = () => {
|
|
42
|
+
this.manuscriptNode.descendants((node) => {
|
|
43
|
+
const type = node.type
|
|
44
|
+
const nodes = this.nodesMap.get(type) ?? []
|
|
45
|
+
nodes.push(node)
|
|
46
|
+
this.nodesMap.set(type, nodes)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getChildrenOfType<T extends ManuscriptNode>(
|
|
51
|
+
type: NodeType,
|
|
52
|
+
node?: ManuscriptNode
|
|
53
|
+
): T[] {
|
|
54
|
+
const nodes = node
|
|
55
|
+
? findChildrenByType(node, type).map(({ node }) => node)
|
|
56
|
+
: this.nodesMap.get(type)
|
|
57
|
+
return (nodes ?? []).filter((n): n is T => isNodeOfType<T>(n, type))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public getFirstChildOfType<T extends ManuscriptNode>(
|
|
61
|
+
type: NodeType,
|
|
62
|
+
node?: ManuscriptNode
|
|
63
|
+
): T | undefined {
|
|
64
|
+
return this.getChildrenOfType<T>(type, node)[0]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected fillEmptyElements(
|
|
68
|
+
$article: Element,
|
|
69
|
+
selector: string,
|
|
70
|
+
tagName = 'p'
|
|
71
|
+
) {
|
|
72
|
+
const $empty = Array.from($article.querySelectorAll(selector)).filter(
|
|
73
|
+
($el) => !$el.innerHTML
|
|
74
|
+
)
|
|
75
|
+
$empty.forEach(($element) =>
|
|
76
|
+
$element.appendChild(this.createElement(tagName))
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected createElement = (
|
|
81
|
+
tag: string,
|
|
82
|
+
content?: string,
|
|
83
|
+
attrs?: Record<string, string | undefined>
|
|
84
|
+
) => {
|
|
85
|
+
const $element = this.document.createElement(tag)
|
|
86
|
+
if (content) {
|
|
87
|
+
$element.textContent = content
|
|
88
|
+
}
|
|
89
|
+
if (attrs) {
|
|
90
|
+
Object.entries(attrs).forEach(([k, v]) => {
|
|
91
|
+
if (v) {
|
|
92
|
+
$element.setAttribute(k, v)
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
return $element
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected appendElement = (
|
|
100
|
+
$parent: Element,
|
|
101
|
+
tag: string,
|
|
102
|
+
content?: string,
|
|
103
|
+
attrs?: Record<string, string | undefined>
|
|
104
|
+
) => {
|
|
105
|
+
const $element = this.createElement(tag, content, attrs)
|
|
106
|
+
$parent.appendChild($element)
|
|
107
|
+
return $element
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
protected changeTag = ($node: Element, tag: string) => {
|
|
111
|
+
const $clone = this.createElement(tag)
|
|
112
|
+
for (const attr of $node.attributes) {
|
|
113
|
+
$clone.setAttributeNS(null, attr.name, attr.value)
|
|
114
|
+
}
|
|
115
|
+
while ($node.firstChild) {
|
|
116
|
+
$clone.appendChild($node.firstChild)
|
|
117
|
+
}
|
|
118
|
+
$node.replaceWith($clone)
|
|
119
|
+
return $clone
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
protected createElementWithID(node: ManuscriptNode, nodeName: string) {
|
|
123
|
+
const $element = this.createElement(nodeName)
|
|
124
|
+
$element.setAttribute('id', normalizeID(node.attrs.id))
|
|
125
|
+
return $element
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
protected serializeNode(node: ManuscriptNode) {
|
|
129
|
+
return this.serializer.serializeNode(node, {
|
|
130
|
+
document: this.document,
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
protected appendChildIfPresent(
|
|
135
|
+
$parent: Element,
|
|
136
|
+
tagName: string,
|
|
137
|
+
textContent: string
|
|
138
|
+
) {
|
|
139
|
+
if (!textContent) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
const $element = this.createElement(tagName)
|
|
143
|
+
$element.textContent = textContent
|
|
144
|
+
$parent.appendChild($element)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
protected processChildNodes = (
|
|
148
|
+
$element: Element,
|
|
149
|
+
node: ManuscriptNode,
|
|
150
|
+
contentNodeType: ManuscriptNodeType
|
|
151
|
+
) => {
|
|
152
|
+
node.forEach((childNode) => {
|
|
153
|
+
if (childNode.type === contentNodeType) {
|
|
154
|
+
if (childNode.attrs.id) {
|
|
155
|
+
$element.appendChild(this.serializeNode(childNode))
|
|
156
|
+
}
|
|
157
|
+
} else if (childNode.type === node.type.schema.nodes.paragraph) {
|
|
158
|
+
$element.appendChild(this.serializeNode(childNode))
|
|
159
|
+
} else if (childNode.type === node.type.schema.nodes.missing_figure) {
|
|
160
|
+
$element.appendChild(this.serializeNode(childNode))
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
}
|