site/node_modules/@citation-js/plugin-bibtex/lib/input/bibtxt.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-10-14 08:09:33 +02:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.textEntry = exports.text = exports.parse = void 0;
const bibTxtRegex = {
splitEntries: /\n\s*(?=\[)/g,
parseEntry: /^\[(.+?)\]\s*(?:\n([\s\S]+))?$/,
splitPairs: /((?=.)\s)*\n\s*/g,
splitPair: /:(.*)/
};
const parseBibTxtEntry = entry => {
const [, label, pairs] = entry.match(bibTxtRegex.parseEntry) || [];
if (!label || !pairs) {
return {};
} else {
const out = {
type: 'book',
label,
properties: {}
};
pairs.trim().split(bibTxtRegex.splitPairs).filter(v => v).forEach(pair => {
let [key, value] = pair.split(bibTxtRegex.splitPair);
if (value) {
key = key.trim();
value = value.trim();
if (key === 'type') {
out.type = value;
} else {
out.properties[key] = value;
}
}
});
return out;
}
};
exports.textEntry = parseBibTxtEntry;
const parseBibTxt = src => src.trim().split(bibTxtRegex.splitEntries).map(parseBibTxtEntry);
exports.text = exports.parse = parseBibTxt;