react-cheminfo 0.140.0 → 0.141.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 (196) hide show
  1. package/README.md +14 -8
  2. package/lib/citation/core/citationMarkup.d.ts +14 -0
  3. package/lib/citation/core/citationMarkup.d.ts.map +1 -0
  4. package/lib/citation/core/citationMarkup.js +27 -0
  5. package/lib/citation/core/citationMarkup.js.map +1 -0
  6. package/lib/citation/core/clipboard.d.ts +7 -6
  7. package/lib/citation/core/clipboard.d.ts.map +1 -1
  8. package/lib/citation/core/clipboard.js +19 -14
  9. package/lib/citation/core/clipboard.js.map +1 -1
  10. package/lib/citation/core/download.d.ts +4 -2
  11. package/lib/citation/core/download.d.ts.map +1 -1
  12. package/lib/citation/core/download.js +5 -3
  13. package/lib/citation/core/download.js.map +1 -1
  14. package/lib/citation/core/formats.d.ts +14 -3
  15. package/lib/citation/core/formats.d.ts.map +1 -1
  16. package/lib/citation/core/formats.js +31 -13
  17. package/lib/citation/core/formats.js.map +1 -1
  18. package/lib/citation/core/index.d.ts +3 -3
  19. package/lib/citation/core/index.d.ts.map +1 -1
  20. package/lib/citation/core/index.js +2 -2
  21. package/lib/citation/core/index.js.map +1 -1
  22. package/lib/citation/core/reference.d.ts +15 -0
  23. package/lib/citation/core/reference.d.ts.map +1 -1
  24. package/lib/citation/core/reference.js +24 -0
  25. package/lib/citation/core/reference.js.map +1 -1
  26. package/lib/citation/core/styles.d.ts +35 -0
  27. package/lib/citation/core/styles.d.ts.map +1 -0
  28. package/lib/citation/core/styles.js +21 -0
  29. package/lib/citation/core/styles.js.map +1 -0
  30. package/lib/citation/core/works.d.ts +2 -3
  31. package/lib/citation/core/works.d.ts.map +1 -1
  32. package/lib/citation/core/works.js +5 -6
  33. package/lib/citation/core/works.js.map +1 -1
  34. package/lib/citation/ui/CitationHtml.d.ts +13 -0
  35. package/lib/citation/ui/CitationHtml.d.ts.map +1 -0
  36. package/lib/citation/ui/CitationHtml.js +42 -0
  37. package/lib/citation/ui/CitationHtml.js.map +1 -0
  38. package/lib/citation/ui/CitationPreview.d.ts +7 -4
  39. package/lib/citation/ui/CitationPreview.d.ts.map +1 -1
  40. package/lib/citation/ui/CitationPreview.js +17 -21
  41. package/lib/citation/ui/CitationPreview.js.map +1 -1
  42. package/lib/citation/ui/CopyFormatEntries.js +1 -1
  43. package/lib/citation/ui/CopyFormatEntries.js.map +1 -1
  44. package/lib/citation/ui/entries.d.ts +1 -1
  45. package/lib/citation/ui/entries.d.ts.map +1 -1
  46. package/lib/citation/ui/entries.js +1 -1
  47. package/lib/citation/ui/entries.js.map +1 -1
  48. package/lib/citation/ui/useCopyFeedback.d.ts +1 -2
  49. package/lib/citation/ui/useCopyFeedback.d.ts.map +1 -1
  50. package/lib/citation/ui/useCopyFeedback.js.map +1 -1
  51. package/lib/credits/core/credits.d.ts +13 -0
  52. package/lib/credits/core/credits.d.ts.map +1 -1
  53. package/lib/credits/core/credits.js +17 -0
  54. package/lib/credits/core/credits.js.map +1 -1
  55. package/lib/csl/core/cslItems.d.ts +97 -0
  56. package/lib/csl/core/cslItems.d.ts.map +1 -0
  57. package/lib/csl/core/cslItems.js +166 -0
  58. package/lib/csl/core/cslItems.js.map +1 -0
  59. package/lib/csl/core/cslStyles.d.ts +30 -0
  60. package/lib/csl/core/cslStyles.d.ts.map +1 -0
  61. package/lib/csl/core/cslStyles.js +48 -0
  62. package/lib/csl/core/cslStyles.js.map +1 -0
  63. package/lib/csl/core/formatCsl.d.ts +35 -0
  64. package/lib/csl/core/formatCsl.d.ts.map +1 -0
  65. package/lib/csl/core/formatCsl.js +100 -0
  66. package/lib/csl/core/formatCsl.js.map +1 -0
  67. package/lib/csl/core/referenceItem.d.ts +14 -0
  68. package/lib/csl/core/referenceItem.d.ts.map +1 -0
  69. package/lib/csl/core/referenceItem.js +60 -0
  70. package/lib/csl/core/referenceItem.js.map +1 -0
  71. package/lib/csl/core/styles/americanChemicalSociety.d.ts +8 -0
  72. package/lib/csl/core/styles/americanChemicalSociety.d.ts.map +1 -0
  73. package/lib/csl/core/styles/americanChemicalSociety.js +311 -0
  74. package/lib/csl/core/styles/americanChemicalSociety.js.map +1 -0
  75. package/lib/csl/core/styles/americanInstituteOfPhysics.d.ts +8 -0
  76. package/lib/csl/core/styles/americanInstituteOfPhysics.d.ts.map +1 -0
  77. package/lib/csl/core/styles/americanInstituteOfPhysics.js +172 -0
  78. package/lib/csl/core/styles/americanInstituteOfPhysics.js.map +1 -0
  79. package/lib/csl/core/styles/americanMathematicalSocietyNumeric.d.ts +8 -0
  80. package/lib/csl/core/styles/americanMathematicalSocietyNumeric.d.ts.map +1 -0
  81. package/lib/csl/core/styles/americanMathematicalSocietyNumeric.js +174 -0
  82. package/lib/csl/core/styles/americanMathematicalSocietyNumeric.js.map +1 -0
  83. package/lib/csl/core/styles/americanPhysicsSociety.d.ts +8 -0
  84. package/lib/csl/core/styles/americanPhysicsSociety.d.ts.map +1 -0
  85. package/lib/csl/core/styles/americanPhysicsSociety.js +227 -0
  86. package/lib/csl/core/styles/americanPhysicsSociety.js.map +1 -0
  87. package/lib/csl/core/styles/angewandteChemie.d.ts +8 -0
  88. package/lib/csl/core/styles/angewandteChemie.d.ts.map +1 -0
  89. package/lib/csl/core/styles/angewandteChemie.js +187 -0
  90. package/lib/csl/core/styles/angewandteChemie.js.map +1 -0
  91. package/lib/csl/core/styles/beilsteinJournalOfOrganicChemistry.d.ts +8 -0
  92. package/lib/csl/core/styles/beilsteinJournalOfOrganicChemistry.d.ts.map +1 -0
  93. package/lib/csl/core/styles/beilsteinJournalOfOrganicChemistry.js +280 -0
  94. package/lib/csl/core/styles/beilsteinJournalOfOrganicChemistry.js.map +1 -0
  95. package/lib/csl/core/styles/elsevierWithTitles.d.ts +8 -0
  96. package/lib/csl/core/styles/elsevierWithTitles.d.ts.map +1 -0
  97. package/lib/csl/core/styles/elsevierWithTitles.js +156 -0
  98. package/lib/csl/core/styles/elsevierWithTitles.js.map +1 -0
  99. package/lib/csl/core/styles/helveticaChimicaActa.d.ts +8 -0
  100. package/lib/csl/core/styles/helveticaChimicaActa.d.ts.map +1 -0
  101. package/lib/csl/core/styles/helveticaChimicaActa.js +189 -0
  102. package/lib/csl/core/styles/helveticaChimicaActa.js.map +1 -0
  103. package/lib/csl/core/styles/ieee.d.ts +8 -0
  104. package/lib/csl/core/styles/ieee.d.ts.map +1 -0
  105. package/lib/csl/core/styles/ieee.js +527 -0
  106. package/lib/csl/core/styles/ieee.js.map +1 -0
  107. package/lib/csl/core/styles/instituteOfPhysicsNumeric.d.ts +8 -0
  108. package/lib/csl/core/styles/instituteOfPhysicsNumeric.d.ts.map +1 -0
  109. package/lib/csl/core/styles/instituteOfPhysicsNumeric.js +135 -0
  110. package/lib/csl/core/styles/instituteOfPhysicsNumeric.js.map +1 -0
  111. package/lib/csl/core/styles/localeEnUs.d.ts +8 -0
  112. package/lib/csl/core/styles/localeEnUs.d.ts.map +1 -0
  113. package/lib/csl/core/styles/localeEnUs.js +872 -0
  114. package/lib/csl/core/styles/localeEnUs.js.map +1 -0
  115. package/lib/csl/core/styles/nature.d.ts +8 -0
  116. package/lib/csl/core/styles/nature.d.ts.map +1 -0
  117. package/lib/csl/core/styles/nature.js +197 -0
  118. package/lib/csl/core/styles/nature.js.map +1 -0
  119. package/lib/csl/core/styles/royalSocietyOfChemistry.d.ts +8 -0
  120. package/lib/csl/core/styles/royalSocietyOfChemistry.d.ts.map +1 -0
  121. package/lib/csl/core/styles/royalSocietyOfChemistry.js +267 -0
  122. package/lib/csl/core/styles/royalSocietyOfChemistry.js.map +1 -0
  123. package/lib/csl/core/styles/springerMathphysBrackets.d.ts +8 -0
  124. package/lib/csl/core/styles/springerMathphysBrackets.d.ts.map +1 -0
  125. package/lib/csl/core/styles/springerMathphysBrackets.js +159 -0
  126. package/lib/csl/core/styles/springerMathphysBrackets.js.map +1 -0
  127. package/lib/csl/core/styles/springerPhysicsBrackets.d.ts +8 -0
  128. package/lib/csl/core/styles/springerPhysicsBrackets.d.ts.map +1 -0
  129. package/lib/csl/core/styles/springerPhysicsBrackets.js +166 -0
  130. package/lib/csl/core/styles/springerPhysicsBrackets.js.map +1 -0
  131. package/lib/csl/core/styles/styleLoaders.d.ts +6 -0
  132. package/lib/csl/core/styles/styleLoaders.d.ts.map +1 -0
  133. package/lib/csl/core/styles/styleLoaders.js +23 -0
  134. package/lib/csl/core/styles/styleLoaders.js.map +1 -0
  135. package/lib/csl/core/styles/synthesis.d.ts +8 -0
  136. package/lib/csl/core/styles/synthesis.d.ts.map +1 -0
  137. package/lib/csl/core/styles/synthesis.js +196 -0
  138. package/lib/csl/core/styles/synthesis.js.map +1 -0
  139. package/lib/csl/core/styles/tetrahedron.d.ts +8 -0
  140. package/lib/csl/core/styles/tetrahedron.d.ts.map +1 -0
  141. package/lib/csl/core/styles/tetrahedron.js +22 -0
  142. package/lib/csl/core/styles/tetrahedron.js.map +1 -0
  143. package/lib/csl.d.ts +17 -0
  144. package/lib/csl.d.ts.map +1 -0
  145. package/lib/csl.js +15 -0
  146. package/lib/csl.js.map +1 -0
  147. package/package.json +3 -1
  148. package/src/citation/core/citationMarkup.ts +28 -0
  149. package/src/citation/core/clipboard.ts +26 -17
  150. package/src/citation/core/download.ts +7 -5
  151. package/src/citation/core/formats.ts +41 -16
  152. package/src/citation/core/index.ts +3 -6
  153. package/src/citation/core/reference.ts +25 -0
  154. package/src/citation/core/styles.ts +34 -0
  155. package/src/citation/core/works.ts +7 -9
  156. package/src/citation/ui/CitationHtml.tsx +42 -0
  157. package/src/citation/ui/CitationPreview.tsx +36 -35
  158. package/src/citation/ui/CopyFormatEntries.tsx +1 -1
  159. package/src/citation/ui/entries.tsx +2 -2
  160. package/src/citation/ui/useCopyFeedback.ts +1 -2
  161. package/src/credits/core/credits.ts +17 -0
  162. package/src/csl/core/citeproc.d.ts +43 -0
  163. package/src/csl/core/cslItems.ts +208 -0
  164. package/src/csl/core/cslStyles.ts +52 -0
  165. package/src/csl/core/formatCsl.ts +135 -0
  166. package/src/csl/core/referenceItem.ts +75 -0
  167. package/src/csl/core/styles/americanChemicalSociety.ts +310 -0
  168. package/src/csl/core/styles/americanInstituteOfPhysics.ts +171 -0
  169. package/src/csl/core/styles/americanMathematicalSocietyNumeric.ts +173 -0
  170. package/src/csl/core/styles/americanPhysicsSociety.ts +226 -0
  171. package/src/csl/core/styles/angewandteChemie.ts +186 -0
  172. package/src/csl/core/styles/beilsteinJournalOfOrganicChemistry.ts +279 -0
  173. package/src/csl/core/styles/elsevierWithTitles.ts +155 -0
  174. package/src/csl/core/styles/helveticaChimicaActa.ts +188 -0
  175. package/src/csl/core/styles/ieee.ts +526 -0
  176. package/src/csl/core/styles/instituteOfPhysicsNumeric.ts +134 -0
  177. package/src/csl/core/styles/localeEnUs.ts +871 -0
  178. package/src/csl/core/styles/nature.ts +196 -0
  179. package/src/csl/core/styles/royalSocietyOfChemistry.ts +266 -0
  180. package/src/csl/core/styles/springerMathphysBrackets.ts +158 -0
  181. package/src/csl/core/styles/springerPhysicsBrackets.ts +165 -0
  182. package/src/csl/core/styles/styleLoaders.ts +38 -0
  183. package/src/csl/core/styles/synthesis.ts +195 -0
  184. package/src/csl/core/styles/tetrahedron.ts +21 -0
  185. package/src/csl.ts +31 -0
  186. package/styles/chrome.css +30 -6
  187. package/lib/citation/core/render.d.ts +0 -21
  188. package/lib/citation/core/render.d.ts.map +0 -1
  189. package/lib/citation/core/render.js +0 -74
  190. package/lib/citation/core/render.js.map +0 -1
  191. package/lib/citation/core/segments.d.ts +0 -62
  192. package/lib/citation/core/segments.d.ts.map +0 -1
  193. package/lib/citation/core/segments.js +0 -210
  194. package/lib/citation/core/segments.js.map +0 -1
  195. package/src/citation/core/render.ts +0 -78
  196. package/src/citation/core/segments.ts +0 -254
@@ -6,7 +6,6 @@ import { useChromeT } from '../../i18n/ui/useT.ts';
6
6
  import { copyCitations } from '../core/clipboard.ts';
7
7
  import type { CitationFormatId } from '../core/formats.ts';
8
8
  import type { Reference } from '../core/reference.ts';
9
- import type { CitationStyleId } from '../core/segments.ts';
10
9
 
11
10
  import type { CopyState } from './entries.tsx';
12
11
 
@@ -24,7 +23,7 @@ export interface CopyFeedback {
24
23
  key: string,
25
24
  references: readonly Reference[],
26
25
  format: CitationFormatId,
27
- style?: CitationStyleId,
26
+ style?: string,
28
27
  ) => void;
29
28
  }
30
29
 
@@ -504,6 +504,23 @@ export const CREDITS = [
504
504
  description: 'shows a Word document in the page, laid out as it prints.',
505
505
  license: 'Apache-2.0',
506
506
  },
507
+ {
508
+ // By Frank Bennett; the processor Zotero runs.
509
+ id: 'citeproc',
510
+ name: 'citeproc-js',
511
+ href: 'https://citeproc-js.readthedocs.io/',
512
+ packageName: 'citeproc',
513
+ description: 'formats citations and reference lists in a journal’s style.',
514
+ license: 'CPAL-1.0 OR AGPL-1.0',
515
+ },
516
+ {
517
+ // The styles are written by their contributors, each named in its file.
518
+ id: 'csl-styles',
519
+ name: 'Citation Style Language styles',
520
+ href: 'https://citationstyles.org/',
521
+ description: 'how each journal writes its references, as Zotero uses it.',
522
+ license: 'CC-BY-SA-3.0',
523
+ },
507
524
  {
508
525
  // By catamphetamine.
509
526
  id: 'read-excel-file',
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The part of citeproc-js this package calls, which ships no types of its own: a
3
+ * processor built from a CSL style, formatting the citations of a document in
4
+ * order and the reference list they make.
5
+ */
6
+ declare module 'citeproc' {
7
+ /** How the processor reads the works and the terms of a language. */
8
+ export interface Sys {
9
+ retrieveLocale: (language: string) => string;
10
+ retrieveItem: (id: string) => object | undefined;
11
+ }
12
+
13
+ /** One citation of the text: the works it cites. */
14
+ export interface Citation {
15
+ citationID: string;
16
+ citationItems: Array<{ id: string }>;
17
+ properties: { noteIndex: number };
18
+ }
19
+
20
+ /** A processor for one style. */
21
+ export interface Engine {
22
+ rebuildProcessorState: (
23
+ citations: Citation[],
24
+ mode?: 'html' | 'text' | 'rtf',
25
+ uncited?: string[],
26
+ ) => Array<[string, number, string]>;
27
+ /**
28
+ * The reference list: how it is laid out — `entry_ids` holding the work
29
+ * each entry lists — then its entries.
30
+ */
31
+ makeBibliography: () => false | [Record<string, unknown>, string[]];
32
+ }
33
+
34
+ const CSL: {
35
+ Engine: new (
36
+ sys: Sys,
37
+ style: string,
38
+ language?: string,
39
+ force?: boolean,
40
+ ) => Engine;
41
+ };
42
+ export default CSL;
43
+ }
@@ -0,0 +1,208 @@
1
+ /**
2
+ * The works a document cites, as CSL JSON: the format Zotero copies and
3
+ * exports (Quick Copy set to CSL JSON, or Better BibTeX's Better CSL JSON),
4
+ * Mendeley exports and doi.org answers. A work is identified by its DOI when
5
+ * it has one, so the same paper copied twice from two libraries is cited as
6
+ * one reference.
7
+ */
8
+
9
+ /** A name as CSL JSON writes it. */
10
+ export interface CslName {
11
+ family?: string;
12
+ given?: string;
13
+ /** A name not split in two: an organisation. */
14
+ literal?: string;
15
+ }
16
+
17
+ /**
18
+ * A date as CSL JSON writes it: its `date-parts`, or a text, `literal` or
19
+ * `raw`.
20
+ */
21
+ export interface CslDate {
22
+ literal?: string;
23
+ raw?: string;
24
+ [part: string]: unknown;
25
+ }
26
+
27
+ /**
28
+ * One work, as CSL JSON describes it. Every other variable is kept as is,
29
+ * the ones named with a hyphen (`container-title`) read with
30
+ * {@link textVariable}.
31
+ */
32
+ export interface CslItem {
33
+ /** The key the work is cited by, the same for every copy of it. */
34
+ id: string;
35
+ type: string;
36
+ title?: string;
37
+ author?: CslName[];
38
+ editor?: CslName[];
39
+ issued?: CslDate;
40
+ DOI?: string;
41
+ volume?: string;
42
+ page?: string;
43
+ [variable: string]: unknown;
44
+ }
45
+
46
+ /** What weighs on a saved document without changing a reference. */
47
+ const DROPPED = new Set(['abstract']);
48
+
49
+ /**
50
+ * The works a text holds, when it is CSL JSON: one work, or a list of them.
51
+ * @param text - What was pasted, dropped or read from a file.
52
+ * @returns The works, each under its key; undefined for a text that is not
53
+ * CSL JSON or names no work.
54
+ */
55
+ export function readCslText(text: string): CslItem[] | undefined {
56
+ const trimmed = text.trim();
57
+ if (!trimmed.startsWith('[') && !trimmed.startsWith('{')) return undefined;
58
+ let value: unknown;
59
+ try {
60
+ value = JSON.parse(trimmed);
61
+ } catch {
62
+ return undefined;
63
+ }
64
+ const items = readCslItems(value);
65
+ return items.length > 0 ? items : undefined;
66
+ }
67
+
68
+ /**
69
+ * The works a value holds, trusted for nothing: an entry without a type is
70
+ * left out.
71
+ * @param value - Parsed CSL JSON, one work or a list of them.
72
+ * @returns The works, each under its key, the first of two copies kept.
73
+ */
74
+ export function readCslItems(value: unknown): CslItem[] {
75
+ const entries = Array.isArray(value) ? value : [value];
76
+ const items: CslItem[] = [];
77
+ const seen = new Set<string>();
78
+ for (const entry of entries) {
79
+ const item = readCslItem(entry);
80
+ if (item === undefined || seen.has(item.id)) continue;
81
+ seen.add(item.id);
82
+ items.push(item);
83
+ }
84
+ return items;
85
+ }
86
+
87
+ /**
88
+ * One work, under the key it is cited by.
89
+ * @param value - A parsed CSL JSON object.
90
+ * @returns The work, or undefined when it is not one.
91
+ */
92
+ export function readCslItem(value: unknown): CslItem | undefined {
93
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
94
+ return undefined;
95
+ }
96
+ const record = value as Record<string, unknown>;
97
+ if (typeof record.type !== 'string' || record.type === '') return undefined;
98
+ const key = keyOf(record);
99
+ if (key === undefined) return undefined;
100
+ const item: Record<string, unknown> = {};
101
+ for (const [variable, content] of Object.entries(record)) {
102
+ if (!DROPPED.has(variable)) item[variable] = content;
103
+ }
104
+ item.id = key;
105
+ return item as CslItem;
106
+ }
107
+
108
+ /**
109
+ * How a work is named in a menu: its first author and the year,
110
+ * `Patiny & Borel 2013`, `Patiny et al. 2024`.
111
+ * @param item - The work.
112
+ * @returns The name.
113
+ */
114
+ export function itemLabel(item: CslItem): string {
115
+ const names = (item.author ?? item.editor ?? []).map(nameOf);
116
+ const year = itemYear(item);
117
+ let who = names[0] ?? '';
118
+ if (names.length === 2) who = `${who} & ${names[1] ?? ''}`;
119
+ else if (names.length > 2) who = `${who} et al.`;
120
+ if (who === '') who = item.title ?? item.id;
121
+ return year === undefined ? who : `${who} ${year}`;
122
+ }
123
+
124
+ /**
125
+ * Where a work was published, as a menu writes it beside its title: the
126
+ * journal's abbreviation, else its name, with the volume.
127
+ * @param item - The work.
128
+ * @returns `J. Chem. Inf. Model. 53`, or an empty string.
129
+ */
130
+ export function itemVenue(item: CslItem): string {
131
+ // Zotero copies the abbreviation under a name of its own.
132
+ const venue =
133
+ textVariable(item, 'container-title-short') ||
134
+ stringOf(item.journalAbbreviation) ||
135
+ textVariable(item, 'container-title');
136
+ return [venue, item.volume ?? ''].filter(Boolean).join(' ');
137
+ }
138
+
139
+ /**
140
+ * The year a work was published.
141
+ * @param item - The work.
142
+ * @returns The year, or undefined when the work gives none.
143
+ */
144
+ export function itemYear(item: CslItem): number | undefined {
145
+ const year = Number(firstOf(firstOf(item.issued?.['date-parts'])));
146
+ if (Number.isInteger(year) && year > 0) return year;
147
+ const written = /\b(?<year>\d{4})\b/.exec(
148
+ item.issued?.literal ?? item.issued?.raw ?? '',
149
+ );
150
+ return written?.groups ? Number(written.groups.year) : undefined;
151
+ }
152
+
153
+ /**
154
+ * Everything a search reads a work by: its authors, year, title, journal,
155
+ * DOI and the key a library gave it.
156
+ * @param item - The work.
157
+ * @returns The words, in lower case.
158
+ */
159
+ export function itemSearchText(item: CslItem): string {
160
+ const names = (item.author ?? []).map(
161
+ (name) => `${name.family ?? ''} ${name.given ?? ''} ${name.literal ?? ''}`,
162
+ );
163
+ const words = [
164
+ ...names,
165
+ String(itemYear(item) ?? ''),
166
+ item.title ?? '',
167
+ textVariable(item, 'container-title'),
168
+ textVariable(item, 'container-title-short'),
169
+ stringOf(item.journalAbbreviation),
170
+ item.DOI ?? '',
171
+ stringOf(item['citation-key']),
172
+ item.id,
173
+ ];
174
+ return words.join(' ').toLowerCase();
175
+ }
176
+
177
+ /**
178
+ * A variable of a work that is text, `container-title` and the others named
179
+ * with a hyphen.
180
+ * @param item - The work.
181
+ * @param variable - The variable's name.
182
+ * @returns Its text; empty when the work has none.
183
+ */
184
+ export function textVariable(item: CslItem, variable: string): string {
185
+ return stringOf(item[variable]);
186
+ }
187
+
188
+ function firstOf(value: unknown): unknown {
189
+ return Array.isArray(value) ? (value as unknown[])[0] : undefined;
190
+ }
191
+
192
+ function nameOf(name: CslName): string {
193
+ return name.family ?? name.literal ?? name.given ?? '';
194
+ }
195
+
196
+ // A DOI is the same in every library; any other key only in the one that made
197
+ // it.
198
+ function keyOf(record: Record<string, unknown>): string | undefined {
199
+ const doi = stringOf(record.DOI).trim().toLowerCase();
200
+ if (doi !== '') return `doi:${doi}`;
201
+ const key = stringOf(record['citation-key']) || stringOf(record.id);
202
+ return key === '' ? undefined : key;
203
+ }
204
+
205
+ function stringOf(value: unknown): string {
206
+ if (typeof value === 'string') return value;
207
+ return typeof value === 'number' ? String(value) : '';
208
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The Citation Style Language styles the journals of the family format their
3
+ * references in, kept as the CSL project publishes them (each says who wrote
4
+ * it and under which licence) and loaded one at a time, only when something is
5
+ * formatted. A dependent style — Tetrahedron's — is formatted by the style it
6
+ * names as its parent. `scripts/csl-styles.mjs` writes them.
7
+ */
8
+
9
+ import { STYLE_LOADERS } from './styles/styleLoaders.ts';
10
+
11
+ /** The ids of the styles there are, as the CSL project names them. */
12
+ export const CSL_STYLE_IDS: readonly string[] = Object.keys(STYLE_LOADERS);
13
+
14
+ /**
15
+ * Whether a style is one there is.
16
+ * @param id - The style's id, `american-chemical-society`.
17
+ * @returns True when it is.
18
+ */
19
+ export function hasCslStyle(id: string): boolean {
20
+ return Object.hasOwn(STYLE_LOADERS, id);
21
+ }
22
+
23
+ /**
24
+ * The style that formats a style's references: itself, or the parent a
25
+ * dependent style names.
26
+ * @param id - The style's id, one there is.
27
+ * @returns Its XML.
28
+ * @throws {Error} For a style there is not.
29
+ */
30
+ export async function loadCslStyle(id: string): Promise<string> {
31
+ const load = hasCslStyle(id) ? STYLE_LOADERS[id] : undefined;
32
+ if (load === undefined) throw new Error(`unknown citation style: ${id}`);
33
+ const xml = await load();
34
+ const parent = parentOf(xml);
35
+ return parent === undefined || parent === id ? xml : loadCslStyle(parent);
36
+ }
37
+
38
+ /**
39
+ * The terms a style writes its references with — `et al.`, `p.`, the months —
40
+ * in English, the language every journal of the family publishes in.
41
+ * @returns The locale's XML.
42
+ */
43
+ export function loadCslLocale(): Promise<string> {
44
+ return import('./styles/localeEnUs.ts').then((module) => module.LOCALE);
45
+ }
46
+
47
+ function parentOf(xml: string): string | undefined {
48
+ const link = /<link\s[^>]*rel="independent-parent"[^>]*>/.exec(xml)?.[0];
49
+ return link === undefined
50
+ ? undefined
51
+ : /href="[^"]*\/styles\/(?<parent>[^"/]+)"/.exec(link)?.groups?.parent;
52
+ }
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Citations and reference lists formatted by citeproc-js — the processor
3
+ * Zotero runs — in a journal's CSL style: numbered in the order the works are
4
+ * first cited for a numeric style, by author and year for another, each
5
+ * citation as the style sets it, `<sup>1,2</sup>`, `[3]`, `(Patiny 2013)`. The
6
+ * processor and the style are loaded the first time anything is formatted.
7
+ */
8
+
9
+ import type { Citation, Engine } from 'citeproc';
10
+
11
+ import { unescapeXml } from '../../drawing/render/markup.ts';
12
+
13
+ import type { CslItem } from './cslItems.ts';
14
+ import { loadCslLocale, loadCslStyle } from './cslStyles.ts';
15
+
16
+ /** A document's citations and reference list, as the style writes them. */
17
+ export interface FormattedCslCitations {
18
+ /** Each citation, in document order, as HTML; empty for one citing none. */
19
+ citations: string[];
20
+ /** Each entry of the reference list, in its order, as HTML. */
21
+ bibliography: string[];
22
+ }
23
+
24
+ /** What an entry is written as: HTML, or the text it reads. */
25
+ export type CslOutput = 'html' | 'text';
26
+
27
+ /**
28
+ * Format the citations of a document, and its reference list, in a style.
29
+ * @param clusters - The works each citation cites, citations in document
30
+ * order; a work cited twice is the same id both times.
31
+ * @param style - The id of the style, one there is.
32
+ * @returns The citations and the reference list, as HTML.
33
+ */
34
+ export async function formatCslCitations(
35
+ clusters: ReadonlyArray<readonly CslItem[]>,
36
+ style: string,
37
+ ): Promise<FormattedCslCitations> {
38
+ const cited = clusters.filter((cluster) => cluster.length > 0);
39
+ if (cited.length === 0) {
40
+ return { citations: clusters.map(() => ''), bibliography: [] };
41
+ }
42
+ const engine = await engineFor(cited.flat(), style);
43
+ const written = engine.rebuildProcessorState(citationsOf(cited), 'html');
44
+ const texts = written.map(([, , html]) => html);
45
+ let next = 0;
46
+ const citations = clusters.map((cluster) =>
47
+ cluster.length === 0 ? '' : (texts[next++] ?? ''),
48
+ );
49
+ const list = engine.makeBibliography();
50
+ return {
51
+ citations,
52
+ bibliography: list === false ? [] : list[1].map(compactEntry),
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Each work as the entry a reference list of the style gives it, without the
58
+ * number a numeric style puts before it: what a reader copies to cite one work.
59
+ * @param items - The works, each cited once, in this order.
60
+ * @param style - The id of the style, one there is.
61
+ * @param output - HTML, or plain text.
62
+ * @returns Each work's entry, in the order given.
63
+ */
64
+ export async function formatCslEntries(
65
+ items: readonly CslItem[],
66
+ style: string,
67
+ output: CslOutput,
68
+ ): Promise<string[]> {
69
+ if (items.length === 0) return [];
70
+ const engine = await engineFor(items, style);
71
+ engine.rebuildProcessorState(citationsOf(items.map((item) => [item])));
72
+ const list = engine.makeBibliography();
73
+ if (list === false) return items.map(() => '');
74
+ const [parameters, entries] = list;
75
+ const ids = parameters.entry_ids;
76
+ const byId = new Map<string, string>();
77
+ for (const [index, entry] of entries.entries()) {
78
+ const id: unknown = Array.isArray(ids) ? ids[index]?.[0] : undefined;
79
+ if (typeof id === 'string' || typeof id === 'number') {
80
+ byId.set(String(id), entryOf(entry, output));
81
+ }
82
+ }
83
+ return items.map((item) => byId.get(item.id) ?? '');
84
+ }
85
+
86
+ async function engineFor(
87
+ items: readonly CslItem[],
88
+ style: string,
89
+ ): Promise<Engine> {
90
+ const [{ default: CSL }, styleXml, locale] = await Promise.all([
91
+ import('citeproc'),
92
+ loadCslStyle(style),
93
+ loadCslLocale(),
94
+ ]);
95
+ const works = new Map<string, CslItem>();
96
+ for (const item of items) {
97
+ // The processor writes into what it reads; the caller's copy stays.
98
+ if (!works.has(item.id)) works.set(item.id, structuredClone(item));
99
+ }
100
+ return new CSL.Engine(
101
+ {
102
+ retrieveLocale: () => locale,
103
+ retrieveItem: (id) => works.get(id),
104
+ },
105
+ styleXml,
106
+ 'en-US',
107
+ true,
108
+ );
109
+ }
110
+
111
+ function citationsOf(clusters: ReadonlyArray<readonly CslItem[]>): Citation[] {
112
+ return clusters.map((cluster, index) => ({
113
+ citationID: `citation-${index}`,
114
+ citationItems: cluster.map(({ id }) => ({ id })),
115
+ properties: { noteIndex: 0 },
116
+ }));
117
+ }
118
+
119
+ // The processor lays an entry's blocks out on lines of their own; a space
120
+ // between two words of the entry is never on a line break.
121
+ function compactEntry(entry: string): string {
122
+ return entry.trim().replaceAll(/>\s*\n\s*</g, '><');
123
+ }
124
+
125
+ // An entry copied on its own has no place in a list to be numbered by, and
126
+ // its text is what its HTML reads.
127
+ function entryOf(entry: string, output: CslOutput): string {
128
+ const html = compactEntry(entry)
129
+ .replace(/<div class="csl-left-margin">[^<]*<\/div>/, '')
130
+ .replace(/^<div class="csl-entry">(?<body>.*)<\/div>$/s, '$<body>')
131
+ .replace(/^<div class="csl-right-inline">(?<body>.*)<\/div>$/s, '$<body>');
132
+ return output === 'html'
133
+ ? html
134
+ : unescapeXml(html.replaceAll(/<[^>]*>/g, ''));
135
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * A work the family cites — its own papers, the datasets behind them — as the
3
+ * CSL JSON a style formats, under the key its DOI gives it, so it is the same
4
+ * work as the copy Zotero or doi.org would hand over.
5
+ */
6
+
7
+ import type {
8
+ ArticleReference,
9
+ DatasetReference,
10
+ Reference,
11
+ ReferenceAuthor,
12
+ } from '../../citation/core/reference.ts';
13
+
14
+ import type { CslItem, CslName } from './cslItems.ts';
15
+
16
+ /**
17
+ * A reference as CSL JSON.
18
+ * @param reference - An article or a dataset.
19
+ * @returns The work, keyed `doi:<its DOI>`.
20
+ */
21
+ export function referenceToCsl(reference: Reference): CslItem {
22
+ return reference.kind === 'dataset'
23
+ ? datasetItem(reference)
24
+ : articleItem(reference);
25
+ }
26
+
27
+ function articleItem(reference: ArticleReference): CslItem {
28
+ const { authors, title, journal, journalAbbreviation, year, volume } =
29
+ reference;
30
+ const { issue, firstPage, lastPage, doi, publisher } = reference;
31
+ // An article number is one page, never a range from it to itself.
32
+ const page =
33
+ lastPage === '' || lastPage === firstPage
34
+ ? firstPage
35
+ : `${firstPage}-${lastPage}`;
36
+ return {
37
+ id: keyOf(doi),
38
+ type: 'article-journal',
39
+ title,
40
+ 'container-title': journal,
41
+ 'container-title-short': journalAbbreviation,
42
+ author: authors.map(nameOf),
43
+ issued: { 'date-parts': [[year]] },
44
+ volume,
45
+ ...(issue === undefined ? {} : { issue }),
46
+ page,
47
+ publisher,
48
+ DOI: doi,
49
+ };
50
+ }
51
+
52
+ function datasetItem(reference: DatasetReference): CslItem {
53
+ const { authors, title, repository, year, version, doi } = reference;
54
+ return {
55
+ id: keyOf(doi),
56
+ type: 'dataset',
57
+ title,
58
+ publisher: repository,
59
+ author: authors.map(nameOf),
60
+ issued: { 'date-parts': [[year]] },
61
+ ...(version === undefined ? {} : { version }),
62
+ DOI: doi,
63
+ };
64
+ }
65
+
66
+ // An organisation has no given name, and is written whole.
67
+ function nameOf(author: ReferenceAuthor): CslName {
68
+ return author.given === ''
69
+ ? { literal: author.family }
70
+ : { family: author.family, given: author.given };
71
+ }
72
+
73
+ function keyOf(doi: string): string {
74
+ return `doi:${doi.toLowerCase()}`;
75
+ }