/
bibliography.js
44 lines (40 loc) · 1.22 KB
/
bibliography.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { spawnSync } from 'child_process';
import Cite from 'citation-js';
export const extractRefBlocks = (pandocAst) => {
const refsBlock = pandocAst.blocks.find(
(block) => block.attrs && block.attrs.identifier === 'refs',
);
if (refsBlock) {
return {
pandocAst: {
...pandocAst,
blocks: pandocAst.blocks.filter((block) => block !== refsBlock),
},
refBlocks: refsBlock.content,
};
}
return { pandocAst: pandocAst, refBlocks: null };
};
const createIdToCiteGetter = (array, processEntry) => {
const map = new Map();
array.forEach((entry) => {
const [key, value] = processEntry(entry);
map.set(key, value);
});
return (id) => map.get(id);
};
const extractUsingPandocCiteproc = (bibliographyTmpPath) => {
const proc = spawnSync('pandoc-citeproc', ['-j', bibliographyTmpPath]);
const output = proc.stdout.toString();
const cslJson = JSON.parse(output);
return createIdToCiteGetter(cslJson, (entry) => {
const structuredValue = Cite.get.bibtex.text([entry]);
return [entry.id, { structuredValue: structuredValue }];
});
};
export const extractBibliographyItems = (bibliographyTmpPath) => {
if (bibliographyTmpPath) {
return extractUsingPandocCiteproc(bibliographyTmpPath);
}
return () => null;
};