-
Notifications
You must be signed in to change notification settings - Fork 1
/
citation-transformer.js
86 lines (78 loc) · 4.17 KB
/
citation-transformer.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
fetch('./cache.json')
.then(response => {
//console.log('found cache.json');
response.json().then(json => {
const head = document.head;
const metaElements = head.getElementsByTagName('meta');
let pageId = undefined;
for (const el of metaElements) {
if (el.getAttribute('property') === 'neuron:zettel-id') {
pageId = el.getAttribute('content');
}
}
if (pageId === undefined) {
console.error('could not find neuron:zettel-id metadata in <head>');
return;
}
// const url = window.location.href;
// const lastSegment = url.substring(url.lastIndexOf('/') + 1);
// const pageNode = lastSegment.substring(0, lastSegment.lastIndexOf('.'));
const Cite = window.require('citation-js');
let references = {};
for (const node in json.Graph.vertices) {
const data = json.Graph.vertices[node];
const reference = data.Meta.reference;
if (reference !== undefined) {
const results = Cite.parse.input.chain(reference);
for (const result of results) {
// TODO: this is a horrible way to build references, we need a way of mapping from each variation of the same ID (e.g. DOI, DOI URL, ...) to the same object
if (result.id !== undefined) {
references[result.id] = result;
}
for (const equivalent of result._graph) {
if (equivalent.data !== undefined) {
references[equivalent.data] = result;
//console.log(`${equivalent.data} -> ${result}`);
}
}
}
}
}
//console.log(references);
// transform parts of the page using <cite> elements
const citations = document.body.querySelectorAll('cite');
for (const citation of citations) {
//console.log(citation);
const zettelLinks = citation.querySelectorAll('.zettel-link');
for (const zettelLink of zettelLinks) {
const cite = new Cite();
// two options, either the <cite> contains a ref attribute, or it doesn't, and we use the reference of each zettel link in the citation
const label = citation.getAttribute('label');
if (label !== undefined && label !== null) {
const style = citation.getAttribute('style');
if (style === undefined || style === null) {
// TODO: output a code block containing bibtex here
// TODO: add a "copy text" widget to the box
}
// get first anchor
// TODO: figure out support for multiple elements inside the <cite>
const anchor = zettelLink.querySelector('a');
const type = citation.getAttribute('type');
if (type === 'title') {
anchor.innerHTML = references[label].title;
} else if (type === 'titleyear') {
anchor.innerHTML = `${references[label].title} (${references[label].issued['date-parts'][0][0]})`;
} else {
cite.add(references[label]);
const content = cite.get({format: 'string', type: 'string', style: 'citation-apa', lang: 'en'});
anchor.innerHTML = content;
}
} else {
// TODO
}
}
}
})
.catch(json => console.log(`encountered error when working with the cache data: ${json}`));
})
.catch(json => console.log(`encountered error when fetching cache.json: ${json}`));