-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstExample.js
97 lines (83 loc) · 2.61 KB
/
firstExample.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { plugins } from '@citation-js/core'
import '@citation-js/plugin-csl'
import { data1, noAuthorData as data2 } from './dummyData'
const cslConfig = plugins.config.get('@csl')
let citeproc
let engineData = []
/**
* Runs the first example with either "bug" or "normal" case.
*
* @param {"bug" | "normal"} whichCase - The type of case to run.
*/
export function runFirstExample(whichCase) {
if (whichCase === 'normal')
normalCase()
else if (whichCase === 'bug')
bugCase()
else
throw new Error('no such case', whichCase)
}
/**
* This one produce expected result. Two data in two citaions out.
*/
function normalCase() {
const firstResult = cite(data1(), 0, [], [])
const citationPre = [[firstResult[0].citationId, 0]]
const secondResult = cite(data2(), 1, citationPre, [])
console.log(secondResult)
// secondResult
// [
// {
// "citationId": "a18i6afo7go",
// "index": 0,
// "citation": "(Gerald et al., 2004)"
// },
// {
// "citationId": "a1vf8ihn1v6",
// "index": 1,
// "citation": "(<i>Brief Introduction to Kudos</i>, 2015)"
// }
// ]
}
/**
* This one has bug. The only difference between the two is the
* order of data being passed in. In this case, a "webpage" from `data2()`
* is passed first, and it got swalloed somehow. it seems that an empty
* author field of `data2()` is casuing the bug, but it is fine in
* `normalCase()`
*/
function bugCase() {
const firstResult = cite(data2(), 0, [], [])
const citationPre = [[firstResult[0].citationId, 0]]
const secondResult = cite(data1(), 1, citationPre, [])
console.log(secondResult)
// (<i>Brief Introduction to Kudos</i>, 2015) of firstResult
// is missing in the secondResult
// [
// {
// "citationId": "a201uc87grd",
// "index": 1,
// "citation": "(Gerald et al., 2004)"
// }
// ]
}
function cite(data, insertIndex, citationPre, citationPost) {
engineData = [...engineData, ...data]
citeproc = cslConfig.engine(engineData, 'apa', 'en-US', 'html')
const processResult = citeproc.processCitationCluster(
{
citationItems: data,
properties: {
noteIndex: insertIndex,
},
},
citationPre,
citationPost,
)
const citations = processResult[1].map((e) => {
const [index, citation, citationId] = e
return { citationId, index, citation }
})
console.log({ engineData, insertIndex, citationPre, citationPost, processResult, citations })
return citations
}