@iebh/reflib 2.7.0 → 2.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/modules/bibtex.js +20 -14
- package/package.json +2 -2
package/modules/bibtex.js
CHANGED
|
@@ -23,9 +23,9 @@ const MODES = {
|
|
|
23
23
|
* @param {Boolean} [options.recNumberNumeric=true] Only process the BibTeX ID into a recNumber if its a finite numeric, otherwise disguard
|
|
24
24
|
* @param {Boolean} [options.recNumberRNPrefix=true] Accept `RN${NUMBER}` as recNumber if present
|
|
25
25
|
* @param {Boolean} [options.recNumberKey=true] If the reference key cannot be otherwise parsed store it in `key<String>` instead
|
|
26
|
-
* @param {Boolean} [options.omitUnkown=false] If true, only keep known reconised fields
|
|
27
26
|
* @param {String} [options.fallbackType='unkown'] Reflib fallback type if the incoming type is unrecognised or unsupported
|
|
28
27
|
* @param {Set<String>} [options.fieldsOverwrite] Set of field names where the value is clobbered rather than appended if discovered more than once
|
|
28
|
+
* @param {Boolean} [options.preserveUnknownKeys=true] Retain keys we do not have a direct lookup for in the output object
|
|
29
29
|
*
|
|
30
30
|
* @returns {Object} A readable stream analogue defined in `modules/interface.js`
|
|
31
31
|
*/
|
|
@@ -34,9 +34,9 @@ export function readStream(stream, options) {
|
|
|
34
34
|
recNumberNumeric: true,
|
|
35
35
|
recNumberRNPrefix: true,
|
|
36
36
|
recNumberKey: true,
|
|
37
|
-
omitUnknown: false,
|
|
38
37
|
fallbackType: 'unknown',
|
|
39
38
|
fieldsOverwrite: new Set(['type']),
|
|
39
|
+
preserveUnkownKeys: true,
|
|
40
40
|
...options,
|
|
41
41
|
};
|
|
42
42
|
|
|
@@ -100,7 +100,13 @@ export function readStream(stream, options) {
|
|
|
100
100
|
)
|
|
101
101
|
) {
|
|
102
102
|
mode = MODES.FIELDS;
|
|
103
|
-
if (
|
|
103
|
+
if (// Already have content - and we should overwrite
|
|
104
|
+
ref[state.field] !== undefined
|
|
105
|
+
&& (
|
|
106
|
+
settings.preserveUnkownKeys
|
|
107
|
+
|| settings.fieldsOverwrite.has(state.field)
|
|
108
|
+
)
|
|
109
|
+
) {
|
|
104
110
|
ref[state.field] = unescape(match.groups.value);
|
|
105
111
|
} else if (ref[state.field] !== undefined) { // Already have content - append
|
|
106
112
|
ref[state.field] += '\n' + unescape(match.groups.value);
|
|
@@ -142,13 +148,13 @@ export function tidyRef(ref, settings) {
|
|
|
142
148
|
return rlType
|
|
143
149
|
? [key, rlType.rl] // Can translate incoming type to Reflib type
|
|
144
150
|
: [key, settings.fallbackType] // Unknown Reflib type varient
|
|
145
|
-
} else if (settings.
|
|
151
|
+
} else if (!settings.preserveUnkownKeys && !rlField) { // Omit unknown fields
|
|
146
152
|
return;
|
|
147
153
|
} else if (rlField && rlField.array) { // Field needs array casting
|
|
148
154
|
return [rlField.rl, val.split(/\n*\s+and\s+/)];
|
|
149
155
|
} else if (rlField && rlField.rl) { // Known BT field but different RL field
|
|
150
156
|
return [rlField.rl, val];
|
|
151
|
-
} else if (
|
|
157
|
+
} else if (settings.preserveUnkownKeys) { // Everything else - add field
|
|
152
158
|
return [key, val];
|
|
153
159
|
}
|
|
154
160
|
})
|
|
@@ -194,10 +200,11 @@ export function escape(str) {
|
|
|
194
200
|
* @param {Object} [options] Additional options to use when parsing
|
|
195
201
|
* @param {string} [options.defaultType='Misc'] Default citation type to assume when no other type is specified
|
|
196
202
|
* @param {string} [options.delimeter='\r'] How to split multi-line items
|
|
197
|
-
* @param {Boolean} [options.omitUnkown=false] If true, only keep known reconised fields
|
|
198
203
|
* @param {Set} [options.omitFields] Set of special fields to always omit, either because we are ignoring or because we have special treatment for them
|
|
204
|
+
* @param {Boolean} [options.keyForce=true] Force a unique ID to exist if we don't already have one for each reference
|
|
199
205
|
* @param {Boolean} [options.recNumberRNPrefix=true] Rewrite recNumber fields as `RN${NUMBER}`
|
|
200
206
|
* @param {Boolean} [options.recNumberKey=true] If the reference `recNumber` is empty use `key<String>` instead
|
|
207
|
+
* @param {Boolean} [options.preserveUnknownKeys=true] Output keys we do not have a direct lookup for in the output object
|
|
201
208
|
*
|
|
202
209
|
* @returns {Object} A writable stream analogue defined in `modules/interface.js`
|
|
203
210
|
*/
|
|
@@ -205,10 +212,11 @@ export function writeStream(stream, options) {
|
|
|
205
212
|
let settings = {
|
|
206
213
|
defaultType: 'Misc',
|
|
207
214
|
delimeter: '\n',
|
|
208
|
-
|
|
209
|
-
|
|
215
|
+
omitFields: new Set(['key', 'recNumber', 'type']),
|
|
216
|
+
keyForce: true,
|
|
210
217
|
recNumberRNPrefix: true,
|
|
211
218
|
recNumberKey: true,
|
|
219
|
+
preserveUnkownKeys: true,
|
|
212
220
|
...options,
|
|
213
221
|
};
|
|
214
222
|
|
|
@@ -217,12 +225,9 @@ export function writeStream(stream, options) {
|
|
|
217
225
|
return Promise.resolve();
|
|
218
226
|
},
|
|
219
227
|
write: ref => {
|
|
220
|
-
if (!ref.key) {
|
|
221
|
-
ref.key = generateCitationKey(ref);
|
|
222
|
-
}
|
|
223
|
-
// console.log("Here is the id",ref.key)
|
|
224
228
|
// Fetch Reflib type definition
|
|
225
|
-
|
|
229
|
+
ref.type ||= settings.defaultType;
|
|
230
|
+
let rlType = translations.types.rlMap.get(ref.type.toLowerCase());
|
|
226
231
|
let btType = rlType?.bt || settings.defaultType;
|
|
227
232
|
|
|
228
233
|
stream.write(
|
|
@@ -231,6 +236,7 @@ export function writeStream(stream, options) {
|
|
|
231
236
|
ref.recNumber && settings.recNumberRNPrefix ? `RN${ref.recNumber},`
|
|
232
237
|
: ref.recNumber ? `${ref.recNumber},`
|
|
233
238
|
: ref.key ? `${ref.key},`
|
|
239
|
+
: settings.keyForce ? `${generateCitationKey(ref)},`
|
|
234
240
|
: ''
|
|
235
241
|
) + '\n'
|
|
236
242
|
+ Object.entries(ref)
|
|
@@ -241,7 +247,7 @@ export function writeStream(stream, options) {
|
|
|
241
247
|
.reduce((buf, [rawKey, rawVal], keyIndex, keys) => {
|
|
242
248
|
// Fetch Reflib field definition
|
|
243
249
|
let rlField = translations.fields.rlMap.get(rawKey)
|
|
244
|
-
if (!rlField && settings.
|
|
250
|
+
if (!rlField && !settings.preserveUnkownKeys) return buf; // Unknown field mapping - skip if were omitting unknown fields
|
|
245
251
|
|
|
246
252
|
let key = rlField ? rlField.bt : rawKey; // Use Reflib->BibTeX field mapping if we have one, otherwise use raw key
|
|
247
253
|
let val = escape( // Escape input value, either as an Array via join or as a flat string
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iebh/reflib",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.2",
|
|
4
4
|
"description": "Reference / Citation reference library utilities",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"homepage": "https://github.com/IEBH/Reflib",
|
|
32
32
|
"enginesStrict": true,
|
|
33
33
|
"engines": {
|
|
34
|
-
"node": "
|
|
34
|
+
"node": ">=16.6.0"
|
|
35
35
|
},
|
|
36
36
|
"type": "module",
|
|
37
37
|
"exports": {
|