@manuscripts/transform 1.0.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.
Files changed (315) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +193 -0
  3. package/dist/cjs/errors.js +17 -0
  4. package/dist/cjs/index.js +27 -0
  5. package/dist/cjs/jats/importer/index.js +24 -0
  6. package/dist/cjs/jats/importer/jats-body-dom-parser.js +650 -0
  7. package/dist/cjs/jats/importer/jats-body-transformations.js +269 -0
  8. package/dist/cjs/jats/importer/jats-comments.js +131 -0
  9. package/dist/cjs/jats/importer/jats-front-parser.js +341 -0
  10. package/dist/cjs/jats/importer/jats-journal-meta-parser.js +77 -0
  11. package/dist/cjs/jats/importer/jats-parser-utils.js +145 -0
  12. package/dist/cjs/jats/importer/jats-reference-parser.js +180 -0
  13. package/dist/cjs/jats/importer/parse-jats-article.js +237 -0
  14. package/dist/cjs/jats/index.js +40 -0
  15. package/dist/cjs/jats/jats-exporter.js +1569 -0
  16. package/dist/cjs/jats/jats-versions.js +41 -0
  17. package/dist/cjs/lib/attributes.js +26 -0
  18. package/dist/cjs/lib/html.js +48 -0
  19. package/dist/cjs/lib/styled-content.js +28 -0
  20. package/dist/cjs/lib/table-cell-styles.js +52 -0
  21. package/dist/cjs/lib/utils.js +66 -0
  22. package/dist/cjs/mathjax/index.js +41 -0
  23. package/dist/cjs/mathjax/mathjax-packages.js +20 -0
  24. package/dist/cjs/mathjax/mathml-to-svg.js +56 -0
  25. package/dist/cjs/mathjax/tex-to-mathml.js +49 -0
  26. package/dist/cjs/mathjax/tex-to-svg.js +59 -0
  27. package/dist/cjs/schema/groups.js +31 -0
  28. package/dist/cjs/schema/index.js +195 -0
  29. package/dist/cjs/schema/marks.js +159 -0
  30. package/dist/cjs/schema/nodes/attribution.js +32 -0
  31. package/dist/cjs/schema/nodes/bibliography_element.js +44 -0
  32. package/dist/cjs/schema/nodes/bibliography_item.js +101 -0
  33. package/dist/cjs/schema/nodes/bibliography_section.js +46 -0
  34. package/dist/cjs/schema/nodes/blockquote_element.js +62 -0
  35. package/dist/cjs/schema/nodes/caption.js +52 -0
  36. package/dist/cjs/schema/nodes/caption_title.js +51 -0
  37. package/dist/cjs/schema/nodes/citation.js +52 -0
  38. package/dist/cjs/schema/nodes/comment.js +26 -0
  39. package/dist/cjs/schema/nodes/cross_reference.js +58 -0
  40. package/dist/cjs/schema/nodes/doc.js +21 -0
  41. package/dist/cjs/schema/nodes/equation.js +55 -0
  42. package/dist/cjs/schema/nodes/equation_element.js +52 -0
  43. package/dist/cjs/schema/nodes/figcaption.js +31 -0
  44. package/dist/cjs/schema/nodes/figure.js +52 -0
  45. package/dist/cjs/schema/nodes/figure_element.js +78 -0
  46. package/dist/cjs/schema/nodes/footnote.js +67 -0
  47. package/dist/cjs/schema/nodes/footnotes_element.js +54 -0
  48. package/dist/cjs/schema/nodes/footnotes_section.js +45 -0
  49. package/dist/cjs/schema/nodes/graphical_abstract_section.js +46 -0
  50. package/dist/cjs/schema/nodes/hard_break.js +30 -0
  51. package/dist/cjs/schema/nodes/highlight_marker.js +53 -0
  52. package/dist/cjs/schema/nodes/inline_equation.js +58 -0
  53. package/dist/cjs/schema/nodes/inline_footnote.js +51 -0
  54. package/dist/cjs/schema/nodes/keywords_element.js +52 -0
  55. package/dist/cjs/schema/nodes/keywords_section.js +46 -0
  56. package/dist/cjs/schema/nodes/link.js +61 -0
  57. package/dist/cjs/schema/nodes/list.js +121 -0
  58. package/dist/cjs/schema/nodes/listing.js +73 -0
  59. package/dist/cjs/schema/nodes/listing_element.js +52 -0
  60. package/dist/cjs/schema/nodes/manuscript.js +46 -0
  61. package/dist/cjs/schema/nodes/missing_figure.js +50 -0
  62. package/dist/cjs/schema/nodes/paragraph.js +63 -0
  63. package/dist/cjs/schema/nodes/placeholder.js +49 -0
  64. package/dist/cjs/schema/nodes/placeholder_element.js +48 -0
  65. package/dist/cjs/schema/nodes/pullquote_element.js +65 -0
  66. package/dist/cjs/schema/nodes/section.js +94 -0
  67. package/dist/cjs/schema/nodes/section_label.js +30 -0
  68. package/dist/cjs/schema/nodes/section_title.js +48 -0
  69. package/dist/cjs/schema/nodes/table.js +70 -0
  70. package/dist/cjs/schema/nodes/table_col.js +57 -0
  71. package/dist/cjs/schema/nodes/table_element.js +62 -0
  72. package/dist/cjs/schema/nodes/table_row.js +123 -0
  73. package/dist/cjs/schema/nodes/text.js +21 -0
  74. package/dist/cjs/schema/nodes/toc_element.js +52 -0
  75. package/dist/cjs/schema/nodes/toc_section.js +46 -0
  76. package/dist/cjs/schema/types.js +17 -0
  77. package/dist/cjs/tests.js +30 -0
  78. package/dist/cjs/transformer/builders.js +305 -0
  79. package/dist/cjs/transformer/bundles-data.js +36 -0
  80. package/dist/cjs/transformer/bundles.js +32 -0
  81. package/dist/cjs/transformer/decode.js +619 -0
  82. package/dist/cjs/transformer/document-object-types.js +31 -0
  83. package/dist/cjs/transformer/encode.js +545 -0
  84. package/dist/cjs/transformer/filename.js +27 -0
  85. package/dist/cjs/transformer/footnote-category.js +20 -0
  86. package/dist/cjs/transformer/footnotes-order.js +59 -0
  87. package/dist/cjs/transformer/highlight-markers.js +126 -0
  88. package/dist/cjs/transformer/html.js +414 -0
  89. package/dist/cjs/transformer/id.js +28 -0
  90. package/dist/cjs/transformer/index.js +54 -0
  91. package/dist/cjs/transformer/labels.js +85 -0
  92. package/dist/cjs/transformer/manuscript-dependencies.js +74 -0
  93. package/dist/cjs/transformer/model-map.js +26 -0
  94. package/dist/cjs/transformer/models.js +17 -0
  95. package/dist/cjs/transformer/node-names.js +42 -0
  96. package/dist/cjs/transformer/node-title.js +87 -0
  97. package/dist/cjs/transformer/node-types.js +62 -0
  98. package/dist/cjs/transformer/object-types.js +57 -0
  99. package/dist/cjs/transformer/project-bundle.js +86 -0
  100. package/dist/cjs/transformer/section-category.js +222 -0
  101. package/dist/cjs/transformer/serializer.js +23 -0
  102. package/dist/cjs/transformer/shared-data.js +31 -0
  103. package/dist/cjs/transformer/tei-grobid-importer.js +121 -0
  104. package/dist/cjs/transformer/timestamp.js +20 -0
  105. package/dist/cjs/transformer/update-identifiers.js +93 -0
  106. package/dist/cjs/types.js +2 -0
  107. package/dist/es/errors.js +12 -0
  108. package/dist/es/index.js +9 -0
  109. package/dist/es/jats/importer/index.js +16 -0
  110. package/dist/es/jats/importer/jats-body-dom-parser.js +644 -0
  111. package/dist/es/jats/importer/jats-body-transformations.js +266 -0
  112. package/dist/es/jats/importer/jats-comments.js +124 -0
  113. package/dist/es/jats/importer/jats-front-parser.js +335 -0
  114. package/dist/es/jats/importer/jats-journal-meta-parser.js +70 -0
  115. package/dist/es/jats/importer/jats-parser-utils.js +139 -0
  116. package/dist/es/jats/importer/jats-reference-parser.js +177 -0
  117. package/dist/es/jats/importer/parse-jats-article.js +229 -0
  118. package/dist/es/jats/index.js +18 -0
  119. package/dist/es/jats/jats-exporter.js +1561 -0
  120. package/dist/es/jats/jats-versions.js +36 -0
  121. package/dist/es/lib/attributes.js +22 -0
  122. package/dist/es/lib/html.js +43 -0
  123. package/dist/es/lib/styled-content.js +23 -0
  124. package/dist/es/lib/table-cell-styles.js +47 -0
  125. package/dist/es/lib/utils.js +59 -0
  126. package/dist/es/mathjax/index.js +12 -0
  127. package/dist/es/mathjax/mathjax-packages.js +17 -0
  128. package/dist/es/mathjax/mathml-to-svg.js +52 -0
  129. package/dist/es/mathjax/tex-to-mathml.js +45 -0
  130. package/dist/es/mathjax/tex-to-svg.js +55 -0
  131. package/dist/es/schema/groups.js +27 -0
  132. package/dist/es/schema/index.js +178 -0
  133. package/dist/es/schema/marks.js +156 -0
  134. package/dist/es/schema/nodes/attribution.js +29 -0
  135. package/dist/es/schema/nodes/bibliography_element.js +41 -0
  136. package/dist/es/schema/nodes/bibliography_item.js +98 -0
  137. package/dist/es/schema/nodes/bibliography_section.js +42 -0
  138. package/dist/es/schema/nodes/blockquote_element.js +58 -0
  139. package/dist/es/schema/nodes/caption.js +49 -0
  140. package/dist/es/schema/nodes/caption_title.js +48 -0
  141. package/dist/es/schema/nodes/citation.js +48 -0
  142. package/dist/es/schema/nodes/comment.js +23 -0
  143. package/dist/es/schema/nodes/cross_reference.js +55 -0
  144. package/dist/es/schema/nodes/doc.js +18 -0
  145. package/dist/es/schema/nodes/equation.js +52 -0
  146. package/dist/es/schema/nodes/equation_element.js +49 -0
  147. package/dist/es/schema/nodes/figcaption.js +28 -0
  148. package/dist/es/schema/nodes/figure.js +49 -0
  149. package/dist/es/schema/nodes/figure_element.js +74 -0
  150. package/dist/es/schema/nodes/footnote.js +63 -0
  151. package/dist/es/schema/nodes/footnotes_element.js +50 -0
  152. package/dist/es/schema/nodes/footnotes_section.js +41 -0
  153. package/dist/es/schema/nodes/graphical_abstract_section.js +42 -0
  154. package/dist/es/schema/nodes/hard_break.js +27 -0
  155. package/dist/es/schema/nodes/highlight_marker.js +49 -0
  156. package/dist/es/schema/nodes/inline_equation.js +55 -0
  157. package/dist/es/schema/nodes/inline_footnote.js +47 -0
  158. package/dist/es/schema/nodes/keywords_element.js +49 -0
  159. package/dist/es/schema/nodes/keywords_section.js +42 -0
  160. package/dist/es/schema/nodes/link.js +58 -0
  161. package/dist/es/schema/nodes/list.js +117 -0
  162. package/dist/es/schema/nodes/listing.js +69 -0
  163. package/dist/es/schema/nodes/listing_element.js +49 -0
  164. package/dist/es/schema/nodes/manuscript.js +43 -0
  165. package/dist/es/schema/nodes/missing_figure.js +47 -0
  166. package/dist/es/schema/nodes/paragraph.js +59 -0
  167. package/dist/es/schema/nodes/placeholder.js +46 -0
  168. package/dist/es/schema/nodes/placeholder_element.js +45 -0
  169. package/dist/es/schema/nodes/pullquote_element.js +61 -0
  170. package/dist/es/schema/nodes/section.js +90 -0
  171. package/dist/es/schema/nodes/section_label.js +26 -0
  172. package/dist/es/schema/nodes/section_title.js +44 -0
  173. package/dist/es/schema/nodes/table.js +67 -0
  174. package/dist/es/schema/nodes/table_col.js +54 -0
  175. package/dist/es/schema/nodes/table_element.js +59 -0
  176. package/dist/es/schema/nodes/table_row.js +120 -0
  177. package/dist/es/schema/nodes/text.js +18 -0
  178. package/dist/es/schema/nodes/toc_element.js +49 -0
  179. package/dist/es/schema/nodes/toc_section.js +42 -0
  180. package/dist/es/schema/types.js +16 -0
  181. package/dist/es/tests.js +28 -0
  182. package/dist/es/transformer/builders.js +261 -0
  183. package/dist/es/transformer/bundles-data.js +8 -0
  184. package/dist/es/transformer/bundles.js +27 -0
  185. package/dist/es/transformer/decode.js +608 -0
  186. package/dist/es/transformer/document-object-types.js +28 -0
  187. package/dist/es/transformer/encode.js +535 -0
  188. package/dist/es/transformer/filename.js +23 -0
  189. package/dist/es/transformer/footnote-category.js +16 -0
  190. package/dist/es/transformer/footnotes-order.js +53 -0
  191. package/dist/es/transformer/highlight-markers.js +120 -0
  192. package/dist/es/transformer/html.js +407 -0
  193. package/dist/es/transformer/id.js +23 -0
  194. package/dist/es/transformer/index.js +38 -0
  195. package/dist/es/transformer/labels.js +81 -0
  196. package/dist/es/transformer/manuscript-dependencies.js +64 -0
  197. package/dist/es/transformer/model-map.js +22 -0
  198. package/dist/es/transformer/models.js +16 -0
  199. package/dist/es/transformer/node-names.js +39 -0
  200. package/dist/es/transformer/node-title.js +82 -0
  201. package/dist/es/transformer/node-types.js +55 -0
  202. package/dist/es/transformer/object-types.js +52 -0
  203. package/dist/es/transformer/project-bundle.js +77 -0
  204. package/dist/es/transformer/section-category.js +210 -0
  205. package/dist/es/transformer/serializer.js +17 -0
  206. package/dist/es/transformer/shared-data.js +1 -0
  207. package/dist/es/transformer/tei-grobid-importer.js +115 -0
  208. package/dist/es/transformer/timestamp.js +16 -0
  209. package/dist/es/transformer/update-identifiers.js +87 -0
  210. package/dist/es/types.js +1 -0
  211. package/dist/types/errors.d.ts +6 -0
  212. package/dist/types/index.d.ts +9 -0
  213. package/dist/types/jats/importer/index.d.ts +16 -0
  214. package/dist/types/jats/importer/jats-body-dom-parser.d.ts +24 -0
  215. package/dist/types/jats/importer/jats-body-transformations.d.ts +31 -0
  216. package/dist/types/jats/importer/jats-comments.d.ts +26 -0
  217. package/dist/types/jats/importer/jats-front-parser.d.ts +62 -0
  218. package/dist/types/jats/importer/jats-journal-meta-parser.d.ts +37 -0
  219. package/dist/types/jats/importer/jats-parser-utils.d.ts +23 -0
  220. package/dist/types/jats/importer/jats-reference-parser.d.ts +23 -0
  221. package/dist/types/jats/importer/parse-jats-article.d.ts +30 -0
  222. package/dist/types/jats/index.d.ts +18 -0
  223. package/dist/types/jats/jats-exporter.d.ts +72 -0
  224. package/dist/types/jats/jats-versions.d.ts +23 -0
  225. package/dist/types/lib/attributes.d.ts +20 -0
  226. package/dist/types/lib/html.d.ts +17 -0
  227. package/dist/types/lib/styled-content.d.ts +22 -0
  228. package/dist/types/lib/table-cell-styles.d.ts +27 -0
  229. package/dist/types/lib/utils.d.ts +26 -0
  230. package/dist/types/mathjax/index.d.ts +3 -0
  231. package/dist/types/mathjax/mathjax-packages.d.ts +16 -0
  232. package/dist/types/mathjax/mathml-to-svg.d.ts +17 -0
  233. package/dist/types/mathjax/tex-to-mathml.d.ts +17 -0
  234. package/dist/types/mathjax/tex-to-svg.d.ts +17 -0
  235. package/dist/types/schema/groups.d.ts +22 -0
  236. package/dist/types/schema/index.d.ts +65 -0
  237. package/dist/types/schema/marks.d.ts +27 -0
  238. package/dist/types/schema/nodes/attribution.d.ts +21 -0
  239. package/dist/types/schema/nodes/bibliography_element.d.ts +26 -0
  240. package/dist/types/schema/nodes/bibliography_item.d.ts +36 -0
  241. package/dist/types/schema/nodes/bibliography_section.d.ts +26 -0
  242. package/dist/types/schema/nodes/blockquote_element.d.ts +28 -0
  243. package/dist/types/schema/nodes/caption.d.ts +25 -0
  244. package/dist/types/schema/nodes/caption_title.d.ts +25 -0
  245. package/dist/types/schema/nodes/citation.d.ts +27 -0
  246. package/dist/types/schema/nodes/comment.d.ts +31 -0
  247. package/dist/types/schema/nodes/cross_reference.d.ts +26 -0
  248. package/dist/types/schema/nodes/doc.d.ts +17 -0
  249. package/dist/types/schema/nodes/equation.d.ts +28 -0
  250. package/dist/types/schema/nodes/equation_element.d.ts +29 -0
  251. package/dist/types/schema/nodes/figcaption.d.ts +21 -0
  252. package/dist/types/schema/nodes/figure.d.ts +25 -0
  253. package/dist/types/schema/nodes/figure_element.d.ts +44 -0
  254. package/dist/types/schema/nodes/footnote.d.ts +32 -0
  255. package/dist/types/schema/nodes/footnotes_element.d.ts +28 -0
  256. package/dist/types/schema/nodes/footnotes_section.d.ts +26 -0
  257. package/dist/types/schema/nodes/graphical_abstract_section.d.ts +26 -0
  258. package/dist/types/schema/nodes/hard_break.d.ts +21 -0
  259. package/dist/types/schema/nodes/highlight_marker.d.ts +29 -0
  260. package/dist/types/schema/nodes/inline_equation.d.ts +28 -0
  261. package/dist/types/schema/nodes/inline_footnote.d.ts +27 -0
  262. package/dist/types/schema/nodes/keywords_element.d.ts +27 -0
  263. package/dist/types/schema/nodes/keywords_section.d.ts +26 -0
  264. package/dist/types/schema/nodes/link.d.ts +26 -0
  265. package/dist/types/schema/nodes/list.d.ts +39 -0
  266. package/dist/types/schema/nodes/listing.d.ts +33 -0
  267. package/dist/types/schema/nodes/listing_element.d.ts +29 -0
  268. package/dist/types/schema/nodes/manuscript.d.ts +23 -0
  269. package/dist/types/schema/nodes/missing_figure.d.ts +23 -0
  270. package/dist/types/schema/nodes/paragraph.d.ts +30 -0
  271. package/dist/types/schema/nodes/placeholder.d.ts +26 -0
  272. package/dist/types/schema/nodes/placeholder_element.d.ts +25 -0
  273. package/dist/types/schema/nodes/pullquote_element.d.ts +28 -0
  274. package/dist/types/schema/nodes/section.d.ts +36 -0
  275. package/dist/types/schema/nodes/section_label.d.ts +22 -0
  276. package/dist/types/schema/nodes/section_title.d.ts +22 -0
  277. package/dist/types/schema/nodes/table.d.ts +27 -0
  278. package/dist/types/schema/nodes/table_col.d.ts +24 -0
  279. package/dist/types/schema/nodes/table_element.d.ts +35 -0
  280. package/dist/types/schema/nodes/table_row.d.ts +41 -0
  281. package/dist/types/schema/nodes/text.d.ts +17 -0
  282. package/dist/types/schema/nodes/toc_element.d.ts +27 -0
  283. package/dist/types/schema/nodes/toc_section.d.ts +26 -0
  284. package/dist/types/schema/types.d.ts +45 -0
  285. package/dist/types/tests.d.ts +16 -0
  286. package/dist/types/transformer/builders.d.ts +74 -0
  287. package/dist/types/transformer/bundles-data.d.ts +3 -0
  288. package/dist/types/transformer/bundles.d.ts +3 -0
  289. package/dist/types/transformer/decode.d.ts +38 -0
  290. package/dist/types/transformer/document-object-types.d.ts +17 -0
  291. package/dist/types/transformer/encode.d.ts +29 -0
  292. package/dist/types/transformer/filename.d.ts +16 -0
  293. package/dist/types/transformer/footnote-category.d.ts +17 -0
  294. package/dist/types/transformer/footnotes-order.d.ts +23 -0
  295. package/dist/types/transformer/highlight-markers.d.ts +26 -0
  296. package/dist/types/transformer/html.d.ts +36 -0
  297. package/dist/types/transformer/id.d.ts +20 -0
  298. package/dist/types/transformer/index.d.ts +38 -0
  299. package/dist/types/transformer/labels.d.ts +24 -0
  300. package/dist/types/transformer/manuscript-dependencies.d.ts +12 -0
  301. package/dist/types/transformer/model-map.d.ts +19 -0
  302. package/dist/types/transformer/models.d.ts +48 -0
  303. package/dist/types/transformer/node-names.d.ts +17 -0
  304. package/dist/types/transformer/node-title.d.ts +19 -0
  305. package/dist/types/transformer/node-types.d.ts +22 -0
  306. package/dist/types/transformer/object-types.d.ts +29 -0
  307. package/dist/types/transformer/project-bundle.d.ts +30 -0
  308. package/dist/types/transformer/section-category.d.ts +28 -0
  309. package/dist/types/transformer/serializer.d.ts +19 -0
  310. package/dist/types/transformer/shared-data.d.ts +2 -0
  311. package/dist/types/transformer/tei-grobid-importer.d.ts +20 -0
  312. package/dist/types/transformer/timestamp.d.ts +16 -0
  313. package/dist/types/transformer/update-identifiers.d.ts +23 -0
  314. package/dist/types/types.d.ts +2 -0
  315. package/package.json +89 -0
@@ -0,0 +1,1569 @@
1
+ "use strict";
2
+ /*!
3
+ * © 2019 Atypon Systems LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.JATSExporter = exports.createCounter = void 0;
22
+ const manuscripts_json_schema_1 = require("@manuscripts/manuscripts-json-schema");
23
+ const debug_1 = __importDefault(require("debug"));
24
+ const prosemirror_model_1 = require("prosemirror-model");
25
+ const w3c_xmlserializer_1 = __importDefault(require("w3c-xmlserializer"));
26
+ const html_1 = require("../lib/html");
27
+ const styled_content_1 = require("../lib/styled-content");
28
+ const utils_1 = require("../lib/utils");
29
+ const schema_1 = require("../schema");
30
+ const filename_1 = require("../transformer/filename");
31
+ const labels_1 = require("../transformer/labels");
32
+ const node_types_1 = require("../transformer/node-types");
33
+ const object_types_1 = require("../transformer/object-types");
34
+ const project_bundle_1 = require("../transformer/project-bundle");
35
+ const section_category_1 = require("../transformer/section-category");
36
+ const jats_versions_1 = require("./jats-versions");
37
+ const warn = (0, debug_1.default)('manuscripts-transform');
38
+ const XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink';
39
+ const normalizeID = (id) => id.replace(/:/g, '_');
40
+ const parser = prosemirror_model_1.DOMParser.fromSchema(schema_1.schema);
41
+ const findChildNodeOfType = (node, nodeType) => {
42
+ for (const child of (0, utils_1.iterateChildren)(node)) {
43
+ if (child.type === nodeType) {
44
+ return child;
45
+ }
46
+ }
47
+ };
48
+ const isContributor = (0, object_types_1.hasObjectType)(manuscripts_json_schema_1.ObjectTypes.Contributor);
49
+ const CREDIT_VOCAB_IDENTIFIER = 'https://dictionary.casrai.org/Contributor_Roles';
50
+ const chooseRoleVocabAttributes = (role) => {
51
+ if (role.uri && role.uri.startsWith(CREDIT_VOCAB_IDENTIFIER)) {
52
+ return {
53
+ vocab: 'credit',
54
+ 'vocab-identifier': CREDIT_VOCAB_IDENTIFIER,
55
+ 'vocab-term': role.name,
56
+ 'vocab-term-identifier': role.uri,
57
+ };
58
+ }
59
+ return {
60
+ vocab: 'uncontrolled',
61
+ };
62
+ };
63
+ const insertAbstractNode = (articleMeta, abstractNode) => {
64
+ const siblings = [
65
+ 'kwd-group',
66
+ 'funding-group',
67
+ 'support-group',
68
+ 'conference',
69
+ 'counts',
70
+ 'custom-meta-group',
71
+ ];
72
+ for (const sibling of siblings) {
73
+ const siblingNode = articleMeta.querySelector(`:scope > ${sibling}`);
74
+ if (siblingNode) {
75
+ articleMeta.insertBefore(abstractNode, siblingNode);
76
+ return;
77
+ }
78
+ }
79
+ articleMeta.appendChild(abstractNode);
80
+ };
81
+ const createCounter = () => {
82
+ const counts = new Map();
83
+ return {
84
+ increment: (field) => {
85
+ const value = counts.get(field);
86
+ const newValue = value === undefined ? 1 : value + 1;
87
+ counts.set(field, newValue);
88
+ return newValue;
89
+ },
90
+ };
91
+ };
92
+ exports.createCounter = createCounter;
93
+ const createDefaultIdGenerator = () => {
94
+ const counter = (0, exports.createCounter)();
95
+ return async (element) => {
96
+ const value = String(counter.increment(element.nodeName));
97
+ return `${element.nodeName}-${value}`;
98
+ };
99
+ };
100
+ const chooseRefType = (objectType) => {
101
+ switch (objectType) {
102
+ case manuscripts_json_schema_1.ObjectTypes.Figure:
103
+ case manuscripts_json_schema_1.ObjectTypes.FigureElement:
104
+ return 'fig';
105
+ case manuscripts_json_schema_1.ObjectTypes.Footnote:
106
+ return 'fn';
107
+ case manuscripts_json_schema_1.ObjectTypes.Table:
108
+ case manuscripts_json_schema_1.ObjectTypes.TableElement:
109
+ return 'table';
110
+ case manuscripts_json_schema_1.ObjectTypes.Section:
111
+ return 'sec';
112
+ case manuscripts_json_schema_1.ObjectTypes.Equation:
113
+ case manuscripts_json_schema_1.ObjectTypes.EquationElement:
114
+ return 'disp-formula';
115
+ }
116
+ };
117
+ const sortContributors = (a, b) => Number(a.priority) - Number(b.priority);
118
+ class JATSExporter {
119
+ constructor() {
120
+ this.serializeToJATS = async (fragment, modelMap, manuscriptID, options = {}) => {
121
+ const { version = '1.2', doi, id, frontMatterOnly = false, links, idGenerator, mediaPathGenerator, } = options;
122
+ this.modelMap = modelMap;
123
+ this.models = Array.from(this.modelMap.values());
124
+ this.createSerializer();
125
+ const versionIds = (0, jats_versions_1.selectVersionIds)(version);
126
+ this.document = document.implementation.createDocument(null, 'article', document.implementation.createDocumentType('article', versionIds.publicId, versionIds.systemId));
127
+ const article = this.document.documentElement;
128
+ article.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', XLINK_NAMESPACE);
129
+ const front = this.buildFront(doi, id, links);
130
+ article.appendChild(front);
131
+ const manuscript = (0, project_bundle_1.findManuscriptById)(this.modelMap, manuscriptID);
132
+ article.setAttribute('article-type', manuscript.articleType || 'other');
133
+ if (!frontMatterOnly) {
134
+ this.labelTargets = (0, labels_1.buildTargets)(fragment, manuscript);
135
+ const body = this.buildBody(fragment);
136
+ article.appendChild(body);
137
+ const back = this.buildBack(body);
138
+ article.appendChild(back);
139
+ this.moveAbstracts(front, body);
140
+ this.moveFloatsGroup(body, article);
141
+ }
142
+ await this.rewriteIDs(idGenerator);
143
+ if (mediaPathGenerator) {
144
+ await this.rewriteMediaPaths(mediaPathGenerator);
145
+ }
146
+ this.rewriteCrossReferenceTypes();
147
+ return (0, w3c_xmlserializer_1.default)(this.document);
148
+ };
149
+ this.nodeFromJATS = (JATSFragment) => {
150
+ JATSFragment = JATSFragment.trim();
151
+ if (!JATSFragment.length) {
152
+ return null;
153
+ }
154
+ const template = this.document.createElement('template');
155
+ template.innerHTML = JATSFragment;
156
+ return template.firstChild;
157
+ };
158
+ this.rewriteCrossReferenceTypes = () => {
159
+ var _a;
160
+ const figRefs = this.document.querySelectorAll('xref[ref-type=fig][rid]');
161
+ if (!figRefs.length) {
162
+ return;
163
+ }
164
+ for (const xref of figRefs) {
165
+ const rid = xref.getAttribute('rid');
166
+ if (rid) {
167
+ const nodeName = (_a = this.document.getElementById(rid)) === null || _a === void 0 ? void 0 : _a.nodeName;
168
+ if (nodeName) {
169
+ switch (nodeName) {
170
+ case 'table-wrap-group':
171
+ case 'table-wrap':
172
+ case 'table':
173
+ xref.setAttribute('ref-type', 'table');
174
+ break;
175
+ }
176
+ }
177
+ }
178
+ }
179
+ };
180
+ this.rewriteMediaPaths = async (mediaPathGenerator) => {
181
+ for (const fig of this.document.querySelectorAll('fig')) {
182
+ const parentID = fig.getAttribute('id');
183
+ for (const graphic of fig.querySelectorAll('graphic')) {
184
+ const newHref = await mediaPathGenerator(graphic, parentID);
185
+ graphic.setAttributeNS(XLINK_NAMESPACE, 'href', newHref);
186
+ }
187
+ }
188
+ for (const suppMaterial of this.document.querySelectorAll('supplementary-material')) {
189
+ const newHref = await mediaPathGenerator(suppMaterial, suppMaterial.id);
190
+ suppMaterial.setAttributeNS(XLINK_NAMESPACE, 'href', newHref);
191
+ }
192
+ };
193
+ this.rewriteIDs = async (idGenerator = createDefaultIdGenerator()) => {
194
+ const idMap = new Map();
195
+ for (const element of this.document.querySelectorAll('[id]')) {
196
+ const previousID = element.getAttribute('id');
197
+ const newID = await idGenerator(element);
198
+ if (newID) {
199
+ element.setAttribute('id', newID);
200
+ }
201
+ else {
202
+ element.removeAttribute('id');
203
+ }
204
+ if (previousID) {
205
+ idMap.set(previousID, newID);
206
+ }
207
+ }
208
+ for (const node of this.document.querySelectorAll('[rid]')) {
209
+ const rids = node.getAttribute('rid');
210
+ if (rids) {
211
+ const newRIDs = rids
212
+ .split(/\s+/)
213
+ .filter(Boolean)
214
+ .map((previousRID) => idMap.get(previousRID))
215
+ .filter(Boolean);
216
+ if (newRIDs.length) {
217
+ node.setAttribute('rid', newRIDs.join(' '));
218
+ }
219
+ }
220
+ }
221
+ };
222
+ this.setTitleContent = (element, title) => {
223
+ const htmlTitleNode = (0, html_1.nodeFromHTML)(`<h1>${title}</h1>`);
224
+ if (htmlTitleNode) {
225
+ const titleNode = parser.parse(htmlTitleNode, {
226
+ topNode: schema_1.schema.nodes.section_title.create(),
227
+ });
228
+ const jatsTitleNode = this.serializeNode(titleNode);
229
+ while (jatsTitleNode.firstChild) {
230
+ element.appendChild(jatsTitleNode.firstChild);
231
+ }
232
+ }
233
+ };
234
+ this.buildFront = (doi, id, links) => {
235
+ var _a, _b, _c, _d;
236
+ const manuscript = (0, project_bundle_1.findManuscript)(this.modelMap);
237
+ const submission = (0, project_bundle_1.findLatestManuscriptSubmission)(this.modelMap, manuscript);
238
+ const front = this.document.createElement('front');
239
+ const journalMeta = this.document.createElement('journal-meta');
240
+ front.appendChild(journalMeta);
241
+ const articleMeta = this.document.createElement('article-meta');
242
+ front.appendChild(articleMeta);
243
+ const journal = [...this.modelMap.values()].find((model) => model.objectType === manuscripts_json_schema_1.ObjectTypes.Journal);
244
+ if (journal) {
245
+ if (journal.journalIdentifiers) {
246
+ for (const item of journal.journalIdentifiers) {
247
+ const element = this.document.createElement('journal-id');
248
+ if (item.journalIDType) {
249
+ element.setAttribute('journal-id-type', item.journalIDType);
250
+ }
251
+ element.textContent = item.journalID;
252
+ journalMeta.appendChild(element);
253
+ }
254
+ }
255
+ if (journal.title || journal.abbreviatedTitles) {
256
+ const parentElement = this.document.createElement('journal-title-group');
257
+ journalMeta.appendChild(parentElement);
258
+ if (journal.title) {
259
+ const element = this.document.createElement('journal-title');
260
+ element.textContent = journal.title;
261
+ parentElement.appendChild(element);
262
+ }
263
+ if (journal.abbreviatedTitles) {
264
+ for (const item of journal.abbreviatedTitles) {
265
+ const element = this.document.createElement('abbrev-journal-title');
266
+ if (item.abbrevType) {
267
+ element.setAttribute('abbrev-type', item.abbrevType);
268
+ }
269
+ element.textContent = item.abbreviatedTitle;
270
+ parentElement.appendChild(element);
271
+ }
272
+ }
273
+ }
274
+ if (journal.ISSNs) {
275
+ for (const item of journal.ISSNs) {
276
+ const element = this.document.createElement('issn');
277
+ if (item.publicationType) {
278
+ element.setAttribute('pub-type', item.publicationType);
279
+ }
280
+ element.textContent = item.ISSN;
281
+ journalMeta.appendChild(element);
282
+ }
283
+ }
284
+ if (journal.publisherName) {
285
+ const publisher = this.document.createElement('publisher');
286
+ const publisherName = this.document.createElement('publisher-name');
287
+ publisherName.textContent = journal.publisherName;
288
+ publisher.appendChild(publisherName);
289
+ journalMeta.appendChild(publisher);
290
+ }
291
+ }
292
+ else {
293
+ if (submission) {
294
+ if (submission.journalCode) {
295
+ const journalID = this.document.createElement('journal-id');
296
+ journalID.setAttribute('journal-id-type', 'publisher-id');
297
+ journalID.textContent = submission.journalCode;
298
+ journalMeta.appendChild(journalID);
299
+ }
300
+ if (submission.journalTitle) {
301
+ const journalTitleGroup = this.document.createElement('journal-title-group');
302
+ journalMeta.appendChild(journalTitleGroup);
303
+ const journalTitle = this.document.createElement('journal-title');
304
+ journalTitle.textContent = submission.journalTitle;
305
+ journalTitleGroup.appendChild(journalTitle);
306
+ }
307
+ if (submission.issn) {
308
+ const issn = this.document.createElement('issn');
309
+ issn.setAttribute('pub-type', 'epub');
310
+ issn.textContent = submission.issn;
311
+ journalMeta.appendChild(issn);
312
+ }
313
+ }
314
+ }
315
+ if (id) {
316
+ const articleID = this.document.createElement('article-id');
317
+ articleID.setAttribute('pub-id-type', 'publisher-id');
318
+ articleID.textContent = id;
319
+ articleMeta.appendChild(articleID);
320
+ }
321
+ if (doi) {
322
+ const articleID = this.document.createElement('article-id');
323
+ articleID.setAttribute('pub-id-type', 'doi');
324
+ articleID.textContent = doi;
325
+ articleMeta.appendChild(articleID);
326
+ }
327
+ const titleGroup = this.document.createElement('title-group');
328
+ articleMeta.appendChild(titleGroup);
329
+ this.buildContributors(articleMeta);
330
+ if (links && links.self) {
331
+ for (const [key, value] of Object.entries(links.self)) {
332
+ const link = this.document.createElement('self-uri');
333
+ link.setAttribute('content-type', key);
334
+ link.setAttributeNS(XLINK_NAMESPACE, 'href', value);
335
+ articleMeta.appendChild(link);
336
+ }
337
+ }
338
+ if (manuscript.title) {
339
+ const element = this.document.createElement('article-title');
340
+ this.setTitleContent(element, manuscript.title);
341
+ titleGroup.appendChild(element);
342
+ }
343
+ if (manuscript.subtitle) {
344
+ const element = this.document.createElement('subtitle');
345
+ this.setTitleContent(element, manuscript.subtitle);
346
+ titleGroup.appendChild(element);
347
+ }
348
+ if (manuscript.runningTitle) {
349
+ const element = this.document.createElement('alt-title');
350
+ element.setAttribute('alt-title-type', 'right-running');
351
+ this.setTitleContent(element, manuscript.runningTitle);
352
+ titleGroup.appendChild(element);
353
+ }
354
+ const supplements = [...this.modelMap.values()].filter((model) => model.objectType === manuscripts_json_schema_1.ObjectTypes.Supplement);
355
+ if (supplements && supplements.length > 0) {
356
+ for (const supplement of supplements) {
357
+ const supplementaryMaterial = this.document.createElement('supplementary-material');
358
+ supplementaryMaterial.setAttribute('id', normalizeID(supplement._id));
359
+ supplementaryMaterial.setAttributeNS(XLINK_NAMESPACE, 'href', (_a = supplement.href) !== null && _a !== void 0 ? _a : '');
360
+ const mimeType = (_b = supplement.MIME) === null || _b === void 0 ? void 0 : _b.split('/')[0];
361
+ const mimeSubType = (_c = supplement.MIME) === null || _c === void 0 ? void 0 : _c.split('/')[1];
362
+ supplementaryMaterial.setAttribute('mimetype', mimeType !== null && mimeType !== void 0 ? mimeType : '');
363
+ supplementaryMaterial.setAttribute('mime-subtype', mimeSubType !== null && mimeSubType !== void 0 ? mimeSubType : '');
364
+ const caption = this.document.createElement('caption');
365
+ const title = this.document.createElement('title');
366
+ title.textContent = (_d = supplement.title) !== null && _d !== void 0 ? _d : '';
367
+ caption.append(title);
368
+ supplementaryMaterial.append(caption);
369
+ articleMeta.append(supplementaryMaterial);
370
+ }
371
+ }
372
+ const history = articleMeta.querySelector('history') ||
373
+ this.document.createElement('history');
374
+ if (manuscript.acceptanceDate) {
375
+ const date = this.buildDateElement(manuscript.acceptanceDate, 'accepted');
376
+ history.appendChild(date);
377
+ }
378
+ if (manuscript.correctionDate) {
379
+ const date = this.buildDateElement(manuscript.correctionDate, 'corrected');
380
+ history.appendChild(date);
381
+ }
382
+ if (manuscript.retractionDate) {
383
+ const date = this.buildDateElement(manuscript.retractionDate, 'retracted');
384
+ history.appendChild(date);
385
+ }
386
+ if (manuscript.receiveDate) {
387
+ const date = this.buildDateElement(manuscript.receiveDate, 'received');
388
+ history.appendChild(date);
389
+ }
390
+ if (manuscript.revisionReceiveDate) {
391
+ const date = this.buildDateElement(manuscript.revisionReceiveDate, 'rev-recd');
392
+ history.appendChild(date);
393
+ }
394
+ if (manuscript.revisionRequestDate) {
395
+ const date = this.buildDateElement(manuscript.revisionRequestDate, 'rev-request');
396
+ history.appendChild(date);
397
+ }
398
+ if (history.childElementCount) {
399
+ articleMeta.appendChild(history);
400
+ }
401
+ if (manuscript.keywordIDs) {
402
+ this.buildKeywords(articleMeta, manuscript.keywordIDs);
403
+ }
404
+ let countingElements = [];
405
+ if (manuscript.genericCounts) {
406
+ const elements = manuscript.genericCounts.map((el) => {
407
+ const countingElement = this.buildCountingElement('count', el.count);
408
+ if (countingElement) {
409
+ countingElement.setAttribute('count-type', el.countType);
410
+ }
411
+ return countingElement;
412
+ });
413
+ countingElements.push(...elements);
414
+ }
415
+ countingElements.push(this.buildCountingElement('fig-count', manuscript.figureCount));
416
+ countingElements.push(this.buildCountingElement('table-count', manuscript.tableCount));
417
+ countingElements.push(this.buildCountingElement('equation-count', manuscript.equationCount));
418
+ countingElements.push(this.buildCountingElement('ref-count', manuscript.referencesCount));
419
+ countingElements.push(this.buildCountingElement('word-count', manuscript.wordCount));
420
+ countingElements = countingElements.filter((el) => el);
421
+ if (countingElements.length > 0) {
422
+ const counts = this.document.createElement('counts');
423
+ counts.append(...countingElements);
424
+ articleMeta.append(counts);
425
+ }
426
+ if (!journalMeta.hasChildNodes()) {
427
+ journalMeta.remove();
428
+ }
429
+ return front;
430
+ };
431
+ this.buildDateElement = (timestamp, type) => {
432
+ const dateElement = this.document.createElement('date');
433
+ dateElement.setAttribute('date-type', type);
434
+ const date = new Date(timestamp * 1000);
435
+ const lookup = {
436
+ year: date.getUTCFullYear().toString(),
437
+ month: date.getUTCMonth().toString(),
438
+ day: date.getUTCDate().toString(),
439
+ };
440
+ for (const [key, value] of Object.entries(lookup).reverse()) {
441
+ const el = this.document.createElement(key);
442
+ el.textContent = value;
443
+ dateElement.appendChild(el);
444
+ }
445
+ return dateElement;
446
+ };
447
+ this.buildCountingElement = (tagName, count) => {
448
+ if (count) {
449
+ const wordCount = this.document.createElement(tagName);
450
+ wordCount.setAttribute('count', String(count));
451
+ return wordCount;
452
+ }
453
+ };
454
+ this.buildBody = (fragment) => {
455
+ const content = this.serializeFragment(fragment);
456
+ const body = this.document.createElement('body');
457
+ body.appendChild(content);
458
+ this.fixBody(body, fragment);
459
+ return body;
460
+ };
461
+ this.buildBack = (body) => {
462
+ const back = this.document.createElement('back');
463
+ this.moveSectionsToBack(back, body);
464
+ const footnotesElements = this.document.querySelectorAll('sec > fn-group');
465
+ for (const footnotesElement of footnotesElements) {
466
+ const previousParent = footnotesElement.parentElement;
467
+ back.appendChild(footnotesElement);
468
+ if (previousParent) {
469
+ const title = previousParent.querySelector('title');
470
+ if (title) {
471
+ footnotesElement.insertBefore(title, footnotesElement.firstElementChild);
472
+ }
473
+ if (!previousParent.childElementCount) {
474
+ previousParent.remove();
475
+ }
476
+ }
477
+ }
478
+ let refList = this.document.querySelector('ref-list');
479
+ if (!refList) {
480
+ warn('No bibliography element, creating a ref-list anyway');
481
+ refList = this.document.createElement('ref-list');
482
+ }
483
+ back.appendChild(refList);
484
+ const bibliographyItems = this.models.filter((0, object_types_1.hasObjectType)(manuscripts_json_schema_1.ObjectTypes.BibliographyItem));
485
+ for (const bibliographyItem of bibliographyItems) {
486
+ const ref = this.document.createElement('ref');
487
+ ref.setAttribute('id', normalizeID(bibliographyItem._id));
488
+ const updateCitationPubType = (citationEl, pubType) => {
489
+ if (pubType) {
490
+ switch (pubType) {
491
+ case 'article':
492
+ case 'article-journal':
493
+ citationEl.setAttribute('publication-type', 'journal');
494
+ break;
495
+ default:
496
+ citationEl.setAttribute('publication-type', pubType);
497
+ break;
498
+ }
499
+ }
500
+ else {
501
+ citationEl.setAttribute('publication-type', 'journal');
502
+ }
503
+ };
504
+ if (bibliographyItem.literal) {
505
+ const mixedCitation = this.document.createElement('mixed-citation');
506
+ updateCitationPubType(mixedCitation, bibliographyItem.type);
507
+ mixedCitation.textContent = bibliographyItem.literal;
508
+ ref.appendChild(mixedCitation);
509
+ refList.appendChild(ref);
510
+ }
511
+ else {
512
+ const citation = this.document.createElement('element-citation');
513
+ updateCitationPubType(citation, bibliographyItem.type);
514
+ if (bibliographyItem.author) {
515
+ const personGroupNode = this.document.createElement('person-group');
516
+ personGroupNode.setAttribute('person-group-type', 'author');
517
+ citation.appendChild(personGroupNode);
518
+ bibliographyItem.author.forEach((author) => {
519
+ const name = this.document.createElement('string-name');
520
+ if (author.family) {
521
+ const node = this.document.createElement('surname');
522
+ node.textContent = author.family;
523
+ name.appendChild(node);
524
+ }
525
+ if (author.given) {
526
+ const node = this.document.createElement('given-names');
527
+ node.textContent = author.given;
528
+ name.appendChild(node);
529
+ }
530
+ if (name.hasChildNodes()) {
531
+ personGroupNode.appendChild(name);
532
+ }
533
+ if (author.literal) {
534
+ const collab = this.document.createElement('collab');
535
+ collab.textContent = author.literal;
536
+ personGroupNode.appendChild(collab);
537
+ }
538
+ });
539
+ }
540
+ if (bibliographyItem.issued) {
541
+ const dateParts = bibliographyItem.issued['date-parts'];
542
+ if (dateParts && dateParts.length) {
543
+ const [[year, month, day]] = dateParts;
544
+ if (year) {
545
+ const node = this.document.createElement('year');
546
+ node.textContent = String(year);
547
+ citation.appendChild(node);
548
+ }
549
+ if (month) {
550
+ const node = this.document.createElement('month');
551
+ node.textContent = String(month);
552
+ citation.appendChild(node);
553
+ }
554
+ if (day) {
555
+ const node = this.document.createElement('day');
556
+ node.textContent = String(day);
557
+ citation.appendChild(node);
558
+ }
559
+ }
560
+ }
561
+ if (bibliographyItem.title) {
562
+ const node = this.document.createElement('article-title');
563
+ this.setTitleContent(node, bibliographyItem.title);
564
+ citation.appendChild(node);
565
+ }
566
+ if (bibliographyItem['container-title']) {
567
+ const node = this.document.createElement('source');
568
+ node.textContent = bibliographyItem['container-title'];
569
+ citation.appendChild(node);
570
+ }
571
+ if (bibliographyItem.volume) {
572
+ const node = this.document.createElement('volume');
573
+ node.textContent = String(bibliographyItem.volume);
574
+ citation.appendChild(node);
575
+ }
576
+ if (bibliographyItem.issue) {
577
+ const node = this.document.createElement('issue');
578
+ node.textContent = String(bibliographyItem.issue);
579
+ citation.appendChild(node);
580
+ }
581
+ if (bibliographyItem.supplement) {
582
+ const node = this.document.createElement('supplement');
583
+ node.textContent = bibliographyItem.supplement;
584
+ citation.appendChild(node);
585
+ }
586
+ if (bibliographyItem.page) {
587
+ const pageString = String(bibliographyItem.page);
588
+ if (/^\d+$/.test(pageString)) {
589
+ const node = this.document.createElement('fpage');
590
+ node.textContent = pageString;
591
+ citation.appendChild(node);
592
+ }
593
+ else if (/^\d+-\d+$/.test(pageString)) {
594
+ const [fpage, lpage] = pageString.split('-');
595
+ const fpageNode = this.document.createElement('fpage');
596
+ fpageNode.textContent = fpage;
597
+ citation.appendChild(fpageNode);
598
+ const lpageNode = this.document.createElement('lpage');
599
+ lpageNode.textContent = lpage;
600
+ citation.appendChild(lpageNode);
601
+ }
602
+ else {
603
+ const node = this.document.createElement('page-range');
604
+ node.textContent = pageString;
605
+ citation.appendChild(node);
606
+ }
607
+ }
608
+ if (bibliographyItem.DOI) {
609
+ const node = this.document.createElement('pub-id');
610
+ node.setAttribute('pub-id-type', 'doi');
611
+ node.textContent = String(bibliographyItem.DOI);
612
+ citation.appendChild(node);
613
+ }
614
+ ref.appendChild(citation);
615
+ refList.appendChild(ref);
616
+ }
617
+ }
618
+ return back;
619
+ };
620
+ this.createSerializer = () => {
621
+ const getModel = (id) => id ? this.modelMap.get(id) : undefined;
622
+ const nodes = {
623
+ attribution: () => ['attrib', 0],
624
+ bibliography_element: () => '',
625
+ bibliography_item: () => '',
626
+ bibliography_section: (node) => [
627
+ 'ref-list',
628
+ { id: normalizeID(node.attrs.id) },
629
+ 0,
630
+ ],
631
+ blockquote_element: () => ['disp-quote', { 'content-type': 'quote' }, 0],
632
+ bullet_list: () => ['list', { 'list-type': 'bullet' }, 0],
633
+ caption: () => ['p', 0],
634
+ caption_title: (node) => {
635
+ if (!node.textContent) {
636
+ return '';
637
+ }
638
+ return ['title', 0];
639
+ },
640
+ citation: (node) => {
641
+ if (!node.attrs.rid) {
642
+ warn(`${node.attrs.id} has no rid`);
643
+ return node.attrs.label;
644
+ }
645
+ const citation = getModel(node.attrs.rid);
646
+ if (!citation) {
647
+ warn(`Missing citation ${node.attrs.rid}`);
648
+ return '';
649
+ }
650
+ const rids = citation.embeddedCitationItems.filter((item) => {
651
+ if (!this.modelMap.has(item.bibliographyItem)) {
652
+ warn(`Missing ${item.bibliographyItem} referenced by ${citation._id}`);
653
+ return false;
654
+ }
655
+ return true;
656
+ });
657
+ if (!rids.length) {
658
+ warn(`${citation._id} has no confirmed rids`);
659
+ return '';
660
+ }
661
+ const xref = this.document.createElement('xref');
662
+ xref.setAttribute('ref-type', 'bibr');
663
+ xref.setAttribute('rid', rids.map((item) => normalizeID(item.bibliographyItem)).join(' '));
664
+ if (node.attrs.contents) {
665
+ const text = (0, html_1.textFromHTML)(node.attrs.contents);
666
+ if (text !== null && text.length) {
667
+ xref.textContent = text;
668
+ }
669
+ }
670
+ return xref;
671
+ },
672
+ cross_reference: (node) => {
673
+ var _a;
674
+ if (!node.attrs.rid) {
675
+ warn(`${node.attrs.id} has no rid`);
676
+ return node.attrs.label;
677
+ }
678
+ const auxiliaryObjectReference = getModel(node.attrs.rid);
679
+ if (!auxiliaryObjectReference) {
680
+ warn(`Missing model ${node.attrs.rid}`);
681
+ return node.attrs.label;
682
+ }
683
+ const xref = this.document.createElement('xref');
684
+ const referencedObject = getModel(auxiliaryObjectReference.referencedObject ||
685
+ ((auxiliaryObjectReference === null || auxiliaryObjectReference === void 0 ? void 0 : auxiliaryObjectReference.referencedObjects) &&
686
+ auxiliaryObjectReference.referencedObjects[0]));
687
+ if (referencedObject) {
688
+ const refType = chooseRefType(referencedObject.objectType);
689
+ if (refType) {
690
+ xref.setAttribute('ref-type', refType);
691
+ }
692
+ else {
693
+ warn(`Unset ref-type for objectType ${referencedObject.objectType}`);
694
+ }
695
+ }
696
+ const getReferencedObjectId = (referencedObject) => {
697
+ return normalizeID(referencedObject);
698
+ };
699
+ if (auxiliaryObjectReference.referencedObjects) {
700
+ const rid = (_a = auxiliaryObjectReference.referencedObjects) === null || _a === void 0 ? void 0 : _a.map((referencedObject) => getReferencedObjectId(referencedObject)).join(' ');
701
+ xref.setAttribute('rid', rid);
702
+ }
703
+ if (auxiliaryObjectReference.referencedObject) {
704
+ const rid = getReferencedObjectId(auxiliaryObjectReference.referencedObject);
705
+ xref.setAttribute('rid', rid);
706
+ }
707
+ xref.textContent = node.attrs.label;
708
+ return xref;
709
+ },
710
+ doc: () => '',
711
+ equation: (node) => {
712
+ const formula = this.document.createElement('disp-formula');
713
+ formula.setAttribute('id', normalizeID(node.attrs.id));
714
+ if (node.attrs.TeXRepresentation) {
715
+ const math = this.document.createElement('tex-math');
716
+ math.textContent = node.attrs.TeXRepresentation;
717
+ formula.appendChild(math);
718
+ }
719
+ else if (node.attrs.MathMLStringRepresentation) {
720
+ const math = this.nodeFromJATS(node.attrs.MathMLStringRepresentation);
721
+ if (math) {
722
+ formula.appendChild(math);
723
+ }
724
+ }
725
+ return formula;
726
+ },
727
+ equation_element: (node) => createFigureElement(node, 'fig', node.type.schema.nodes.equation, 'equation'),
728
+ figcaption: (node) => {
729
+ if (!node.textContent) {
730
+ return '';
731
+ }
732
+ return ['caption', 0];
733
+ },
734
+ figure: (node) => {
735
+ const graphic = this.document.createElement('graphic');
736
+ const filename = (0, filename_1.generateAttachmentFilename)(node.attrs.id, node.attrs.contentType);
737
+ graphic.setAttributeNS(XLINK_NAMESPACE, 'xlink:href', `graphic/${filename}`);
738
+ if (node.attrs.contentType) {
739
+ const [mimeType, mimeSubType] = node.attrs.contentType.split('/');
740
+ if (mimeType) {
741
+ graphic.setAttribute('mimetype', mimeType);
742
+ if (mimeSubType) {
743
+ graphic.setAttribute('mime-subtype', mimeSubType);
744
+ }
745
+ }
746
+ }
747
+ return graphic;
748
+ },
749
+ figure_element: (node) => createFigureElement(node, 'fig', node.type.schema.nodes.figure, 'figure'),
750
+ footnote: (node) => {
751
+ const attrs = {};
752
+ if (node.attrs.id) {
753
+ attrs.id = normalizeID(node.attrs.id);
754
+ }
755
+ if (node.attrs.category) {
756
+ attrs['fn-type'] = node.attrs.category;
757
+ }
758
+ return ['fn', attrs, 0];
759
+ },
760
+ footnotes_element: (node) => {
761
+ const kind = node.attrs.kind;
762
+ let tag = 'fn-group';
763
+ if (kind && kind.includes('table')) {
764
+ tag = 'table-wrap-foot';
765
+ }
766
+ return [tag, { id: normalizeID(node.attrs.id) }, 0];
767
+ },
768
+ footnotes_section: (node) => {
769
+ const attrs = {
770
+ id: normalizeID(node.attrs.id),
771
+ 'sec-type': 'endnotes',
772
+ };
773
+ return ['sec', attrs, 0];
774
+ },
775
+ hard_break: () => ['break'],
776
+ highlight_marker: () => '',
777
+ inline_equation: (node) => {
778
+ const formula = this.document.createElement('inline-formula');
779
+ formula.setAttribute('id', normalizeID(node.attrs.id));
780
+ if (node.attrs.TeXRepresentation) {
781
+ const math = this.document.createElement('tex-math');
782
+ math.textContent = node.attrs.TeXRepresentation;
783
+ formula.appendChild(math);
784
+ }
785
+ else if (node.attrs.MathMLRepresentation) {
786
+ const math = this.nodeFromJATS(node.attrs.MathMLRepresentation);
787
+ if (math) {
788
+ formula.appendChild(math);
789
+ }
790
+ }
791
+ else if (node.attrs.SVGRepresentation) {
792
+ const math = this.nodeFromJATS(node.attrs.SVGRepresentation);
793
+ if (math) {
794
+ formula.appendChild(math);
795
+ }
796
+ }
797
+ return formula;
798
+ },
799
+ inline_footnote: (node) => {
800
+ const xref = this.document.createElement('xref');
801
+ xref.setAttribute('ref-type', 'fn');
802
+ xref.setAttribute('rid', normalizeID(node.attrs.rid));
803
+ xref.textContent = node.attrs.contents;
804
+ return xref;
805
+ },
806
+ keywords_element: () => '',
807
+ keywords_section: () => '',
808
+ link: (node) => {
809
+ const text = node.textContent;
810
+ if (!text) {
811
+ return '';
812
+ }
813
+ if (!node.attrs.href) {
814
+ return text;
815
+ }
816
+ const linkNode = this.document.createElement('ext-link');
817
+ linkNode.setAttribute('ext-link-type', 'uri');
818
+ linkNode.setAttributeNS(XLINK_NAMESPACE, 'href', node.attrs.href);
819
+ linkNode.textContent = text;
820
+ if (node.attrs.title) {
821
+ linkNode.setAttributeNS(XLINK_NAMESPACE, 'xlink:title', node.attrs.title);
822
+ }
823
+ return linkNode;
824
+ },
825
+ list_item: () => ['list-item', 0],
826
+ listing: (node) => {
827
+ const code = this.document.createElement('code');
828
+ code.setAttribute('id', normalizeID(node.attrs.id));
829
+ code.setAttribute('language', node.attrs.languageKey);
830
+ code.textContent = node.attrs.contents;
831
+ return code;
832
+ },
833
+ listing_element: (node) => createFigureElement(node, 'fig', node.type.schema.nodes.listing, 'listing'),
834
+ manuscript: (node) => ['article', { id: normalizeID(node.attrs.id) }, 0],
835
+ missing_figure: () => {
836
+ const graphic = this.document.createElement('graphic');
837
+ graphic.setAttribute('specific-use', 'MISSING');
838
+ graphic.setAttributeNS(XLINK_NAMESPACE, 'xlink:href', '');
839
+ return graphic;
840
+ },
841
+ ordered_list: () => ['list', { 'list-type': 'order' }, 0],
842
+ paragraph: (node) => {
843
+ if (!node.childCount) {
844
+ return '';
845
+ }
846
+ const attrs = {};
847
+ if (node.attrs.id) {
848
+ attrs.id = normalizeID(node.attrs.id);
849
+ }
850
+ if (node.attrs.contentType) {
851
+ attrs['content-type'] = node.attrs.contentType;
852
+ }
853
+ return ['p', attrs, 0];
854
+ },
855
+ placeholder: () => {
856
+ return this.document.createElement('boxed-text');
857
+ },
858
+ placeholder_element: () => {
859
+ return this.document.createElement('boxed-text');
860
+ },
861
+ pullquote_element: () => [
862
+ 'disp-quote',
863
+ { 'content-type': 'pullquote' },
864
+ 0,
865
+ ],
866
+ graphical_abstract_section: (node) => {
867
+ const attrs = {
868
+ id: normalizeID(node.attrs.id),
869
+ };
870
+ attrs['sec-type'] = 'abstract-graphical';
871
+ return ['sec', attrs, 0];
872
+ },
873
+ section: (node) => {
874
+ const attrs = {
875
+ id: normalizeID(node.attrs.id),
876
+ };
877
+ if (node.attrs.category) {
878
+ attrs['sec-type'] = (0, section_category_1.chooseSecType)(node.attrs.category);
879
+ }
880
+ return ['sec', attrs, 0];
881
+ },
882
+ section_label: () => ['label', 0],
883
+ section_title: () => ['title', 0],
884
+ table: (node) => ['table', { id: normalizeID(node.attrs.id) }, 0],
885
+ table_element: (node) => {
886
+ const element = createFigureElement(node, 'table-wrap', node.type.schema.nodes.table);
887
+ element.setAttribute('position', 'anchor');
888
+ return element;
889
+ },
890
+ table_body: () => ['tbody', 0],
891
+ table_cell: (node) => [
892
+ node.attrs.celltype,
893
+ Object.assign(Object.assign({ valign: node.attrs.valign, align: node.attrs.align, scope: node.attrs.scope, style: node.attrs.style }, (node.attrs.rowspan > 1 && { rowspan: node.attrs.rowspan })), (node.attrs.colspan > 1 && { colspan: node.attrs.colspan })),
894
+ 0,
895
+ ],
896
+ table_row: () => ['tr', 0],
897
+ table_col: (node) => ['col', { width: node.attrs.width }],
898
+ table_colgroup: () => ['colgroup', 0],
899
+ text: (node) => node.text,
900
+ toc_element: () => '',
901
+ toc_section: () => '',
902
+ comment: () => '',
903
+ };
904
+ const marks = {
905
+ bold: () => ['bold'],
906
+ code: () => ['code', { position: 'anchor' }],
907
+ italic: () => ['italic'],
908
+ smallcaps: () => ['sc'],
909
+ strikethrough: () => ['strike'],
910
+ styled: (mark) => {
911
+ const inlineStyle = getModel(mark.attrs.rid);
912
+ const attrs = {};
913
+ if (inlineStyle && inlineStyle.title) {
914
+ attrs.style = (0, styled_content_1.normalizeStyleName)(inlineStyle.title);
915
+ }
916
+ return ['styled-content', attrs];
917
+ },
918
+ superscript: () => ['sup'],
919
+ subscript: () => ['sub'],
920
+ underline: () => ['underline'],
921
+ tracked_insert: () => ['ins'],
922
+ tracked_delete: () => ['del'],
923
+ };
924
+ this.serializer = new prosemirror_model_1.DOMSerializer(nodes, marks);
925
+ const createFigureElement = (node, nodeName, contentNodeType, figType) => {
926
+ const element = this.document.createElement(nodeName);
927
+ element.setAttribute('id', normalizeID(node.attrs.id));
928
+ if (figType) {
929
+ element.setAttribute('fig-type', figType);
930
+ }
931
+ if (node.attrs.label) {
932
+ const label = this.document.createElement('label');
933
+ label.textContent = node.attrs.label;
934
+ element.appendChild(label);
935
+ }
936
+ else if (this.labelTargets) {
937
+ const target = this.labelTargets.get(node.attrs.id);
938
+ if (target) {
939
+ const label = this.document.createElement('label');
940
+ label.textContent = target.label;
941
+ element.appendChild(label);
942
+ }
943
+ }
944
+ const figcaptionNode = findChildNodeOfType(node, node.type.schema.nodes.figcaption);
945
+ if (figcaptionNode) {
946
+ element.appendChild(this.serializeNode(figcaptionNode));
947
+ }
948
+ const footnotesNode = findChildNodeOfType(node, node.type.schema.nodes.footnotes_element);
949
+ if (footnotesNode) {
950
+ element.appendChild(this.serializeNode(footnotesNode));
951
+ }
952
+ node.forEach((childNode) => {
953
+ if (childNode.type === contentNodeType) {
954
+ if (childNode.attrs.id) {
955
+ element.appendChild(this.serializeNode(childNode));
956
+ }
957
+ }
958
+ else if (childNode.type === node.type.schema.nodes.paragraph) {
959
+ element.appendChild(this.serializeNode(childNode));
960
+ }
961
+ else if (childNode.type === node.type.schema.nodes.missing_figure) {
962
+ element.appendChild(this.serializeNode(childNode));
963
+ }
964
+ });
965
+ if (node.attrs.attribution) {
966
+ const attribution = this.document.createElement('attrib');
967
+ attribution.textContent = node.attrs.attribution.literal;
968
+ element.appendChild(attribution);
969
+ }
970
+ if ((0, node_types_1.isExecutableNodeType)(node.type)) {
971
+ processExecutableNode(node, element);
972
+ }
973
+ return element;
974
+ };
975
+ const processExecutableNode = (node, element) => {
976
+ const listingNode = findChildNodeOfType(node, node.type.schema.nodes.listing);
977
+ if (listingNode) {
978
+ const { contents, languageKey } = listingNode.attrs;
979
+ if (contents && languageKey) {
980
+ const listing = this.document.createElement('fig');
981
+ listing.setAttribute('specific-use', 'source');
982
+ element.appendChild(listing);
983
+ const code = this.document.createElement('code');
984
+ code.setAttribute('executable', 'true');
985
+ code.setAttribute('language', languageKey);
986
+ code.textContent = contents;
987
+ listing.appendChild(code);
988
+ const caption = this.document.createElement('caption');
989
+ listing.appendChild(caption);
990
+ const attachments = [];
991
+ for (const attachment of attachments) {
992
+ const p = this.document.createElement('p');
993
+ caption.appendChild(p);
994
+ const filename = (0, filename_1.generateAttachmentFilename)(`${listingNode.attrs.id}:${attachment.id}`, attachment.type);
995
+ const supp = this.document.createElement('supplementary-material');
996
+ supp.setAttributeNS(XLINK_NAMESPACE, 'xlink:href', `suppl/${filename}`);
997
+ const [mimeType, mimeSubType] = attachment.type.split('/');
998
+ if (mimeType) {
999
+ supp.setAttribute('mimetype', mimeType);
1000
+ if (mimeSubType) {
1001
+ supp.setAttribute('mime-subtype', mimeSubType);
1002
+ }
1003
+ }
1004
+ p.appendChild(supp);
1005
+ }
1006
+ }
1007
+ }
1008
+ };
1009
+ };
1010
+ this.serializeFragment = (fragment) => this.serializer.serializeFragment(fragment, {
1011
+ document: this.document,
1012
+ });
1013
+ this.serializeNode = (node) => this.serializer.serializeNode(node, {
1014
+ document: this.document,
1015
+ });
1016
+ this.validateContributor = (contributor) => {
1017
+ if (!contributor.bibliographicName) {
1018
+ throw new Error(`${contributor._id} has no bibliographicName`);
1019
+ }
1020
+ const { family, given } = contributor.bibliographicName;
1021
+ if (!family && !given) {
1022
+ throw new Error(`${contributor._id} has neither family nor given name`);
1023
+ }
1024
+ };
1025
+ this.buildContributors = (articleMeta) => {
1026
+ const contributors = this.models.filter(isContributor);
1027
+ const authorContributors = contributors
1028
+ .filter((contributor) => contributor.role === 'author')
1029
+ .sort(sortContributors);
1030
+ const affiliationLabels = new Map();
1031
+ const creatAffiliationLabel = (rid) => {
1032
+ let label = affiliationLabels.get(rid);
1033
+ if (!label) {
1034
+ label = affiliationLabels.size + 1;
1035
+ affiliationLabels.set(rid, label);
1036
+ }
1037
+ const sup = this.document.createElement('sup');
1038
+ sup.textContent = String(label);
1039
+ return sup;
1040
+ };
1041
+ const createFootNotesLabels = (content) => {
1042
+ const sup = this.document.createElement('sup');
1043
+ sup.textContent = String(content);
1044
+ return sup;
1045
+ };
1046
+ if (authorContributors.length) {
1047
+ const contribGroup = this.document.createElement('contrib-group');
1048
+ contribGroup.setAttribute('content-type', 'authors');
1049
+ articleMeta.appendChild(contribGroup);
1050
+ authorContributors.forEach((contributor) => {
1051
+ try {
1052
+ this.validateContributor(contributor);
1053
+ }
1054
+ catch (error) {
1055
+ warn(error.message);
1056
+ return;
1057
+ }
1058
+ const contrib = this.document.createElement('contrib');
1059
+ contrib.setAttribute('contrib-type', 'author');
1060
+ contrib.setAttribute('id', normalizeID(contributor._id));
1061
+ if (contributor.isCorresponding) {
1062
+ contrib.setAttribute('corresp', 'yes');
1063
+ }
1064
+ if (contributor.ORCIDIdentifier) {
1065
+ const identifier = this.document.createElement('contrib-id');
1066
+ identifier.setAttribute('contrib-id-type', 'orcid');
1067
+ identifier.textContent = contributor.ORCIDIdentifier;
1068
+ contrib.appendChild(identifier);
1069
+ }
1070
+ const name = this.buildContributorName(contributor);
1071
+ contrib.appendChild(name);
1072
+ if (contributor.email) {
1073
+ const email = this.document.createElement('email');
1074
+ email.textContent = contributor.email;
1075
+ contrib.appendChild(email);
1076
+ }
1077
+ if (contributor.roles) {
1078
+ contributor.roles.forEach((rid) => {
1079
+ const contributorRole = this.modelMap.get(rid);
1080
+ if (contributorRole) {
1081
+ const role = this.document.createElement('role');
1082
+ const attributes = chooseRoleVocabAttributes(contributorRole);
1083
+ for (const [key, value] of Object.entries(attributes)) {
1084
+ role.setAttribute(key, value);
1085
+ }
1086
+ role.textContent = contributorRole.name;
1087
+ contrib.appendChild(role);
1088
+ }
1089
+ });
1090
+ }
1091
+ if (contributor.affiliations) {
1092
+ contributor.affiliations.forEach((rid) => {
1093
+ const xref = this.document.createElement('xref');
1094
+ xref.setAttribute('ref-type', 'aff');
1095
+ xref.setAttribute('rid', normalizeID(rid));
1096
+ xref.appendChild(creatAffiliationLabel(rid));
1097
+ contrib.appendChild(xref);
1098
+ });
1099
+ }
1100
+ if (contributor.footnote) {
1101
+ contributor.footnote.map((note) => {
1102
+ const xref = this.document.createElement('xref');
1103
+ xref.setAttribute('ref-type', 'fn');
1104
+ xref.setAttribute('rid', normalizeID(note.noteID));
1105
+ xref.appendChild(createFootNotesLabels(note.noteLabel));
1106
+ contrib.appendChild(xref);
1107
+ });
1108
+ }
1109
+ if (contributor.corresp) {
1110
+ contributor.corresp.map((corresp) => {
1111
+ const xref = this.document.createElement('xref');
1112
+ xref.setAttribute('ref-type', 'corresp');
1113
+ xref.setAttribute('rid', normalizeID(corresp.correspID));
1114
+ xref.appendChild(createFootNotesLabels(corresp.correspLabel));
1115
+ contrib.appendChild(xref);
1116
+ });
1117
+ }
1118
+ contribGroup.appendChild(contrib);
1119
+ });
1120
+ const otherContributors = contributors
1121
+ .filter((contributor) => contributor.role !== 'author')
1122
+ .sort(sortContributors);
1123
+ if (otherContributors.length) {
1124
+ const contribGroup = this.document.createElement('contrib-group');
1125
+ articleMeta.appendChild(contribGroup);
1126
+ otherContributors.forEach((contributor) => {
1127
+ try {
1128
+ this.validateContributor(contributor);
1129
+ }
1130
+ catch (error) {
1131
+ warn(error.message);
1132
+ return;
1133
+ }
1134
+ const contrib = this.document.createElement('contrib');
1135
+ contrib.setAttribute('id', normalizeID(contributor._id));
1136
+ const name = this.buildContributorName(contributor);
1137
+ contrib.appendChild(name);
1138
+ if (contributor.email) {
1139
+ const email = this.document.createElement('email');
1140
+ email.textContent = contributor.email;
1141
+ contrib.appendChild(email);
1142
+ }
1143
+ if (contributor.roles) {
1144
+ contributor.roles.forEach((rid) => {
1145
+ const contributorRole = this.modelMap.get(rid);
1146
+ if (contributorRole) {
1147
+ const role = this.document.createElement('role');
1148
+ const attributes = chooseRoleVocabAttributes(contributorRole);
1149
+ for (const [key, value] of Object.entries(attributes)) {
1150
+ role.setAttribute(key, value);
1151
+ }
1152
+ role.textContent = contributorRole.name;
1153
+ contrib.appendChild(role);
1154
+ }
1155
+ });
1156
+ }
1157
+ if (contributor.affiliations) {
1158
+ contributor.affiliations.forEach((rid) => {
1159
+ const xref = this.document.createElement('xref');
1160
+ xref.setAttribute('ref-type', 'aff');
1161
+ xref.setAttribute('rid', normalizeID(rid));
1162
+ xref.appendChild(creatAffiliationLabel(rid));
1163
+ contrib.appendChild(xref);
1164
+ });
1165
+ }
1166
+ if (contributor.footnote) {
1167
+ contributor.footnote.map((note) => {
1168
+ const xref = this.document.createElement('xref');
1169
+ xref.setAttribute('ref-type', 'fn');
1170
+ xref.setAttribute('rid', normalizeID(note.noteID));
1171
+ xref.appendChild(createFootNotesLabels(note.noteLabel));
1172
+ contrib.appendChild(xref);
1173
+ });
1174
+ }
1175
+ contribGroup.appendChild(contrib);
1176
+ });
1177
+ }
1178
+ const affiliationRIDs = [];
1179
+ const sortedContributors = [...authorContributors, ...otherContributors];
1180
+ for (const contributor of sortedContributors) {
1181
+ if (contributor.affiliations) {
1182
+ affiliationRIDs.push(...contributor.affiliations);
1183
+ }
1184
+ }
1185
+ const affiliations = this.models.filter((0, object_types_1.hasObjectType)(manuscripts_json_schema_1.ObjectTypes.Affiliation));
1186
+ if (affiliations) {
1187
+ const usedAffiliations = affiliations.filter((affiliation) => affiliationRIDs.includes(affiliation._id));
1188
+ usedAffiliations.sort((a, b) => affiliationRIDs.indexOf(a._id) - affiliationRIDs.indexOf(b._id));
1189
+ usedAffiliations.forEach((affiliation) => {
1190
+ var _a, _b;
1191
+ const aff = this.document.createElement('aff');
1192
+ aff.setAttribute('id', normalizeID(affiliation._id));
1193
+ contribGroup.appendChild(aff);
1194
+ if (affiliation.department) {
1195
+ const department = this.document.createElement('institution');
1196
+ department.setAttribute('content-type', 'dept');
1197
+ department.textContent = affiliation.department;
1198
+ aff.appendChild(department);
1199
+ }
1200
+ if (affiliation.institution) {
1201
+ const institution = this.document.createElement('institution');
1202
+ institution.textContent = affiliation.institution;
1203
+ aff.appendChild(institution);
1204
+ }
1205
+ if (affiliation.addressLine1) {
1206
+ const addressLine = this.document.createElement('addr-line');
1207
+ addressLine.textContent = affiliation.addressLine1;
1208
+ aff.appendChild(addressLine);
1209
+ }
1210
+ if (affiliation.addressLine2) {
1211
+ const addressLine = this.document.createElement('addr-line');
1212
+ addressLine.textContent = affiliation.addressLine2;
1213
+ aff.appendChild(addressLine);
1214
+ }
1215
+ if (affiliation.addressLine3) {
1216
+ const addressLine = this.document.createElement('addr-line');
1217
+ addressLine.textContent = affiliation.addressLine3;
1218
+ aff.appendChild(addressLine);
1219
+ }
1220
+ if (affiliation.city) {
1221
+ const city = this.document.createElement('city');
1222
+ city.textContent = affiliation.city;
1223
+ aff.appendChild(city);
1224
+ }
1225
+ if (affiliation.country) {
1226
+ const country = this.document.createElement('country');
1227
+ country.textContent = affiliation.country;
1228
+ aff.appendChild(country);
1229
+ }
1230
+ if (affiliation.email) {
1231
+ const email = this.document.createElement('email');
1232
+ email.setAttributeNS(XLINK_NAMESPACE, 'href', (_a = affiliation.email.href) !== null && _a !== void 0 ? _a : '');
1233
+ email.textContent = (_b = affiliation.email.text) !== null && _b !== void 0 ? _b : '';
1234
+ aff.appendChild(email);
1235
+ }
1236
+ const labelNumber = affiliationLabels.get(affiliation._id);
1237
+ if (labelNumber) {
1238
+ const label = this.document.createElement('label');
1239
+ label.textContent = String(labelNumber);
1240
+ aff.appendChild(label);
1241
+ }
1242
+ });
1243
+ }
1244
+ const noteIDs = [];
1245
+ for (const contributor of [...authorContributors, ...otherContributors]) {
1246
+ if (contributor.footnote) {
1247
+ const ids = contributor.footnote.map((note) => {
1248
+ return note.noteID;
1249
+ });
1250
+ noteIDs.push(...ids);
1251
+ }
1252
+ if (contributor.corresp) {
1253
+ const ids = contributor.corresp.map((corresp) => {
1254
+ return corresp.correspID;
1255
+ });
1256
+ noteIDs.push(...ids);
1257
+ }
1258
+ }
1259
+ const footnotes = [];
1260
+ footnotes.push(...this.models.filter((0, object_types_1.hasObjectType)(manuscripts_json_schema_1.ObjectTypes.Footnote)));
1261
+ const correspodings = [];
1262
+ correspodings.push(...this.models.filter((0, object_types_1.hasObjectType)(manuscripts_json_schema_1.ObjectTypes.Corresponding)));
1263
+ if (footnotes || correspodings) {
1264
+ const authorNotesEl = this.document.createElement('author-notes');
1265
+ const usedFootnotes = footnotes.filter((footnote) => {
1266
+ return noteIDs.includes(footnote._id);
1267
+ });
1268
+ const usedCorrespodings = correspodings.filter((corresp) => {
1269
+ return noteIDs.includes(corresp._id);
1270
+ });
1271
+ usedFootnotes.forEach((footnote) => {
1272
+ const authorFootNote = this.document.createElement('fn');
1273
+ authorFootNote.setAttribute('id', normalizeID(footnote._id));
1274
+ authorFootNote.innerHTML = footnote.contents;
1275
+ authorNotesEl.appendChild(authorFootNote);
1276
+ });
1277
+ usedCorrespodings.forEach((corresponding) => {
1278
+ const correspondingEl = this.document.createElement('corresp');
1279
+ correspondingEl.setAttribute('id', normalizeID(corresponding._id));
1280
+ if (corresponding.label) {
1281
+ const labelEl = this.document.createElement('label');
1282
+ labelEl.textContent = corresponding.label;
1283
+ correspondingEl.appendChild(labelEl);
1284
+ }
1285
+ correspondingEl.append(corresponding.contents);
1286
+ authorNotesEl.appendChild(correspondingEl);
1287
+ });
1288
+ if (authorNotesEl.childNodes.length > 0) {
1289
+ articleMeta.appendChild(authorNotesEl);
1290
+ }
1291
+ }
1292
+ }
1293
+ };
1294
+ this.fixBody = (body, fragment) => {
1295
+ fragment.descendants((node) => {
1296
+ if (node.attrs.id) {
1297
+ if (node.attrs.titleSuppressed) {
1298
+ const title = body.querySelector(`#${normalizeID(node.attrs.id)} > title`);
1299
+ if (title && title.parentNode) {
1300
+ title.parentNode.removeChild(title);
1301
+ }
1302
+ }
1303
+ if (node.attrs.suppressCaption) {
1304
+ const caption = body.querySelector(`#${normalizeID(node.attrs.id)} > caption`);
1305
+ if (caption) {
1306
+ caption.remove();
1307
+ }
1308
+ const label = body.querySelector(`#${normalizeID(node.attrs.id)} > label`);
1309
+ if (label) {
1310
+ label.remove();
1311
+ }
1312
+ }
1313
+ if ((0, node_types_1.isNodeType)(node, 'table_element')) {
1314
+ const tableElement = body.querySelector(`#${normalizeID(node.attrs.id)}`);
1315
+ if (tableElement) {
1316
+ for (const childNode of tableElement.childNodes) {
1317
+ switch (childNode.nodeName) {
1318
+ case 'caption': {
1319
+ if (node.attrs.suppressCaption) {
1320
+ tableElement.removeChild(childNode);
1321
+ }
1322
+ else {
1323
+ const label = tableElement.querySelector('label');
1324
+ tableElement.insertBefore(childNode, label ? label.nextSibling : tableElement.firstChild);
1325
+ }
1326
+ break;
1327
+ }
1328
+ case 'table': {
1329
+ this.fixTable(childNode, node);
1330
+ break;
1331
+ }
1332
+ }
1333
+ }
1334
+ }
1335
+ }
1336
+ }
1337
+ });
1338
+ };
1339
+ this.changeTag = (node, tag) => {
1340
+ const clone = this.document.createElement(tag);
1341
+ for (const attr of node.attributes) {
1342
+ clone.setAttributeNS(null, attr.name, attr.value);
1343
+ }
1344
+ while (node.firstChild) {
1345
+ clone.appendChild(node.firstChild);
1346
+ }
1347
+ node.replaceWith(clone);
1348
+ return clone;
1349
+ };
1350
+ this.fixTable = (table, node) => {
1351
+ let tbody;
1352
+ Array.from(table.childNodes).forEach((child) => {
1353
+ if (child instanceof Element && child.tagName.toLowerCase() === 'tbody') {
1354
+ tbody = child;
1355
+ }
1356
+ });
1357
+ if (!tbody) {
1358
+ return;
1359
+ }
1360
+ const tbodyRows = Array.from(tbody.childNodes);
1361
+ const thead = this.document.createElement('thead');
1362
+ const tfoot = this.document.createElement('tfoot');
1363
+ tbodyRows.forEach((row, i) => {
1364
+ const isRow = row instanceof Element && row.tagName.toLowerCase() === 'tr';
1365
+ if (isRow) {
1366
+ const headerCell = row.querySelector('th[scope="col"], th[scope="colgroup"]');
1367
+ if (i === 0 || headerCell) {
1368
+ tbody === null || tbody === void 0 ? void 0 : tbody.removeChild(row);
1369
+ if (!node.attrs.suppressHeader) {
1370
+ const tableCells = row.querySelectorAll('td');
1371
+ for (const td of tableCells) {
1372
+ this.changeTag(td, 'th');
1373
+ }
1374
+ thead.appendChild(row);
1375
+ }
1376
+ }
1377
+ else if (i === tbodyRows.length - 1) {
1378
+ tbody === null || tbody === void 0 ? void 0 : tbody.removeChild(row);
1379
+ if (!node.attrs.suppressFooter) {
1380
+ tfoot.appendChild(row);
1381
+ }
1382
+ }
1383
+ }
1384
+ });
1385
+ if (thead.hasChildNodes()) {
1386
+ table.insertBefore(thead, tbody);
1387
+ }
1388
+ if (tfoot.hasChildNodes()) {
1389
+ table.insertBefore(tfoot, tbody);
1390
+ }
1391
+ };
1392
+ this.moveAbstracts = (front, body) => {
1393
+ const sections = body.querySelectorAll(':scope > sec');
1394
+ const abstractSections = Array.from(sections).filter((section) => {
1395
+ const sectionType = section.getAttribute('sec-type');
1396
+ if (sectionType === 'abstract' ||
1397
+ sectionType === 'abstract-teaser' ||
1398
+ sectionType === 'abstract-graphical') {
1399
+ return true;
1400
+ }
1401
+ const sectionTitle = section.querySelector(':scope > title');
1402
+ if (!sectionTitle) {
1403
+ return false;
1404
+ }
1405
+ return sectionTitle.textContent === 'Abstract';
1406
+ });
1407
+ if (abstractSections.length) {
1408
+ for (const abstractSection of abstractSections) {
1409
+ const abstractNode = this.document.createElement('abstract');
1410
+ for (const node of abstractSection.childNodes) {
1411
+ if (node.nodeName !== 'title') {
1412
+ abstractNode.appendChild(node.cloneNode(true));
1413
+ }
1414
+ }
1415
+ const sectionType = abstractSection.getAttribute('sec-type');
1416
+ if (sectionType && sectionType !== 'abstract') {
1417
+ const [, abstractType] = sectionType.split('-', 2);
1418
+ abstractNode.setAttribute('abstract-type', abstractType);
1419
+ }
1420
+ abstractSection.remove();
1421
+ const articleMeta = front.querySelector(':scope > article-meta');
1422
+ if (articleMeta) {
1423
+ insertAbstractNode(articleMeta, abstractNode);
1424
+ }
1425
+ }
1426
+ }
1427
+ };
1428
+ this.moveSectionsToBack = (back, body) => {
1429
+ const availabilitySection = body.querySelector('sec[sec-type="availability"]');
1430
+ if (availabilitySection) {
1431
+ back.insertBefore(availabilitySection, back.firstChild);
1432
+ }
1433
+ const section = body.querySelector('sec[sec-type="acknowledgments"]');
1434
+ if (section) {
1435
+ const ack = this.document.createElement('ack');
1436
+ while (section.firstChild) {
1437
+ ack.appendChild(section.firstChild);
1438
+ }
1439
+ if (section.parentNode) {
1440
+ section.parentNode.removeChild(section);
1441
+ }
1442
+ back.insertBefore(ack, back.firstChild);
1443
+ }
1444
+ const appendicesSections = body.querySelectorAll('sec[sec-type="appendices"]');
1445
+ if (appendicesSections) {
1446
+ const appGroup = this.document.createElement('app-group');
1447
+ appendicesSections.forEach((section) => {
1448
+ if (section.parentNode) {
1449
+ section.parentNode.removeChild(section);
1450
+ }
1451
+ const app = this.document.createElement('app');
1452
+ app.appendChild(section);
1453
+ appGroup.appendChild(app);
1454
+ });
1455
+ back.insertBefore(appGroup, back.firstChild);
1456
+ }
1457
+ const footNotes = [];
1458
+ const footnoteCategories = [
1459
+ 'con',
1460
+ 'conflict',
1461
+ 'deceased',
1462
+ 'equal',
1463
+ 'present-address',
1464
+ 'presented-at',
1465
+ 'previously-at',
1466
+ 'supplementary-material',
1467
+ 'supported-by',
1468
+ 'financial-disclosure',
1469
+ 'ethics-statement',
1470
+ 'competing-interests',
1471
+ ];
1472
+ const sections = body.querySelectorAll('sec');
1473
+ for (const currentSection of sections) {
1474
+ const currentSectionType = currentSection.getAttribute('sec-type');
1475
+ if (currentSectionType &&
1476
+ footnoteCategories.indexOf(currentSectionType) >= 0) {
1477
+ footNotes.push(this.sectionToFootnote(currentSection, currentSectionType));
1478
+ }
1479
+ }
1480
+ if (footNotes.length > 0) {
1481
+ const fnGroup = this.document.createElement('fn-group');
1482
+ fnGroup.append(...footNotes);
1483
+ back.append(fnGroup);
1484
+ }
1485
+ };
1486
+ this.sectionToFootnote = (section, fnType) => {
1487
+ const footNote = this.document.createElement('fn');
1488
+ footNote.setAttribute('fn-type', fnType);
1489
+ const title = section.querySelector('title');
1490
+ if (title) {
1491
+ const footNoteTitle = this.document.createElement('p');
1492
+ footNoteTitle.setAttribute('content-type', 'fn-title');
1493
+ footNoteTitle.textContent = title.textContent;
1494
+ section.removeChild(title);
1495
+ footNote.append(footNoteTitle);
1496
+ }
1497
+ footNote.append(...section.children);
1498
+ if (section.parentNode) {
1499
+ section.parentNode.removeChild(section);
1500
+ }
1501
+ return footNote;
1502
+ };
1503
+ this.moveFloatsGroup = (body, article) => {
1504
+ const floatsGroup = this.document.createElement('floats-group');
1505
+ const section = body.querySelector('sec[sec-type="floating-element"]');
1506
+ if (section) {
1507
+ floatsGroup.append(...section.children);
1508
+ if (section === null || section === void 0 ? void 0 : section.parentNode) {
1509
+ section.parentNode.removeChild(section);
1510
+ }
1511
+ article.appendChild(floatsGroup);
1512
+ }
1513
+ };
1514
+ this.buildContributorName = (contributor) => {
1515
+ const name = this.document.createElement('name');
1516
+ if (contributor.bibliographicName.family) {
1517
+ const surname = this.document.createElement('surname');
1518
+ surname.textContent = contributor.bibliographicName.family;
1519
+ name.appendChild(surname);
1520
+ }
1521
+ if (contributor.bibliographicName.given) {
1522
+ const givenNames = this.document.createElement('given-names');
1523
+ givenNames.textContent = contributor.bibliographicName.given;
1524
+ name.appendChild(givenNames);
1525
+ }
1526
+ return name;
1527
+ };
1528
+ }
1529
+ buildKeywords(articleMeta, keywordIDs) {
1530
+ const keywords = keywordIDs
1531
+ .map((id) => this.modelMap.get(id))
1532
+ .filter((model) => model && model.name);
1533
+ const keywordGroups = new Map();
1534
+ keywords.forEach((keyword) => {
1535
+ const containedGroup = keyword.containedGroup || '';
1536
+ const group = keywordGroups.get(containedGroup);
1537
+ if (group) {
1538
+ group.push(keyword);
1539
+ }
1540
+ else {
1541
+ keywordGroups.set(containedGroup, [keyword]);
1542
+ }
1543
+ });
1544
+ for (const [groupID, keywords] of keywordGroups) {
1545
+ const keywordGroup = (this.modelMap.get(groupID) || {});
1546
+ const kwdGroup = this.document.createElement('kwd-group');
1547
+ if (keywordGroup.type) {
1548
+ kwdGroup.setAttribute('kwd-group-type', keywordGroup.type);
1549
+ }
1550
+ if (keywordGroup.label) {
1551
+ const label = this.document.createElement('label');
1552
+ label.textContent = keywordGroup.label;
1553
+ kwdGroup.appendChild(label);
1554
+ }
1555
+ if (keywordGroup.title) {
1556
+ const title = this.document.createElement('title');
1557
+ title.textContent = keywordGroup.title;
1558
+ kwdGroup.appendChild(title);
1559
+ }
1560
+ articleMeta.appendChild(kwdGroup);
1561
+ for (const keyword of keywords) {
1562
+ const kwd = this.document.createElement('kwd');
1563
+ kwd.textContent = keyword.name;
1564
+ kwdGroup.appendChild(kwd);
1565
+ }
1566
+ }
1567
+ }
1568
+ }
1569
+ exports.JATSExporter = JATSExporter;