rehype-citation
Version:
rehype plugin to add citation and bibliography from bibtex files
57 lines (56 loc) • 2.58 kB
JavaScript
const formats = ['real', 'string'];
const types = ['json', 'html', 'string', 'rtf'];
const styles = ['csl', 'bibtex', 'bibtxt', 'citation-*', 'ris', 'ndjson'];
const wrapperTypes = ['string', 'function'];
export function validateOutputOptions(options) {
if (typeof options !== 'object') {
throw new TypeError('Options not an object!');
}
const { format, type, style, lang, append, prepend } = options;
if (format && !formats.includes(format)) {
throw new TypeError(`Option format ("${format}") should be one of: ${formats}`);
}
else if (type && !types.includes(type)) {
throw new TypeError(`Option type ("${type}") should be one of: ${types}`);
}
else if (style && !styles.includes(style) && !/^citation/.test(style)) {
throw new TypeError(`Option style ("${style}") should be one of: ${styles}`);
}
else if (lang && typeof lang !== 'string') {
throw new TypeError(`Option lang should be a string, but is a ${typeof lang}`);
}
else if (prepend && !wrapperTypes.includes(typeof prepend)) {
throw new TypeError(`Option prepend should be a string or a function, but is a ${typeof prepend}`);
}
else if (append && !wrapperTypes.includes(typeof append)) {
throw new TypeError(`Option append should be a string or a function, but is a ${typeof append}`);
}
if (/^citation/.test(style) && type === 'json') {
throw new Error(`Combination type/style of json/citation-* is not valid: ${type}/${style}`);
}
return true;
}
export function validateOptions(options) {
if (typeof options !== 'object') {
throw new TypeError('Options should be an object');
}
if (options.output) {
validateOutputOptions(options.output);
}
else if (options.maxChainLength && typeof options.maxChainLength !== 'number') {
throw new TypeError('Option maxChainLength should be a number');
}
else if (options.forceType && typeof options.forceType !== 'string') {
throw new TypeError('Option forceType should be a string');
}
else if (options.generateGraph != null && typeof options.generateGraph !== 'boolean') {
throw new TypeError('Option generateGraph should be a boolean');
}
else if (options.strict != null && typeof options.strict !== 'boolean') {
throw new TypeError('Option strict should be a boolean');
}
else if (options.target != null && typeof options.target !== 'string') {
throw new TypeError('Option target should be a boolean');
}
return true;
}