# Request for help with javascript for the citation search feature

I would like to switch over the citation helper from the current kludged together back-end based on MathSciNet data, to an officially supported back-end (very kindly!) provided by Zentralblatt.

We'll need to rewrite a small amount of javascript for this to happen. If someone (ideally who knows javascript, and ideally who knows how to hack the existing javascript served on a page, so the new code can be tested before deployment) would like to help, please let me know.

The problem is simple to describe:

We need to modify the code currently served via http://cdn.sstatic.net/js/third-party/citation-helper.js?v=2, so that instead of retrieving results from, https://ec2.mathoverflow.org/cite?q=, it retrieves results from https://zbmath.org/citationmatching/mathoverflow?q=, and appropriately parses the JSON.

It's mostly just a matter of working out how the JSON structure has changed, and emitting appropriate HTML based on that. The HTML should be basically the same as what we already produce, but we should also include the Zbl identifier, linked directly to the corresponding Zbl page. The title of the article itself should be a hyperlink to the first "link" result.

• So it's really just modifying getCitationHtmlto match the JSON data from ZBL? – Asaf Karagila Dec 13 '16 at 6:31
• Yes, I think so. It should be quite straightforward. – Scott Morrison Dec 13 '16 at 21:54
• I gave it my mediocre shot. – Asaf Karagila Dec 13 '16 at 22:26
• I've marked this as "status-planned", as the modified code is now off to Stack Exchange for review, and hopefully deployment not far down the track. – Scott Morrison Jan 13 '17 at 0:03
• I think now it's as good a time as any to put status-completed on this post. – Asaf Karagila Mar 10 '17 at 17:47

As an example, the first result here contains the JSON

{
"results": [
{
"pagination": "266-278",
"http://dx.doi.org/10.1017/jsl.2013.12"
],
"title": "On $\\omega_1$-strongly compact cardinals",
"year": "2014",
"source": "J. Symb. Log. 79, No. 1, 266-278 (2014).",
"score": 3.316698,
"zbl_id": "1337.03076",
"authors": "Bagaria, Joan; Magidor, Menachem"
},
...
}


which should render as something like

Bagaria, Joan; Magidor, Menachem, On $\omega_1$-strongly compact cardinals, J. Symb. Log. 79, No. 1, 266-278 (2014). Zbl 1337.03076

• Is there any chance to get the "source" field break down to journal/volume/number/pages/year? You know, like a proper .bib citation? – Asaf Karagila Dec 15 '16 at 7:29
• I'll ask, but my contact at Zentralblatt is now on holidays. – Scott Morrison Dec 15 '16 at 22:09
• Scott, being unfamiliar with the Christian world's holidays. When do these holidays end usually? Do we have to wait until next year? That's insane, it's an entire year! :-) – Asaf Karagila Dec 26 '16 at 10:05
• He said he'll back be mid-January. – Scott Morrison Dec 26 '16 at 21:13
• Is next week "mid-January"? – Asaf Karagila Jan 7 '17 at 8:35
• Ok, "mid-January" is now. :-) Could you explain why it would be better to break the source field down into components? Fabian at Zentralblatt says it might be possible, but I'm hesitant to ask him to do this work without good motivation. – Scott Morrison Jan 11 '17 at 22:16
• Well, it's nicer to have a better control over the formatting of the output. I figured it must be a few lines of code (instead of collecting journal/volume/number/pages/year into one field, just return them separately). If it's a lot of work, I guess we can work with what we have. – Asaf Karagila Jan 12 '17 at 6:07
• We've decided it's too much work. – Scott Morrison Jan 12 '17 at 22:31
• Oh well. Thanks anyway. – Asaf Karagila Jan 13 '17 at 4:45
function getCitationHtml(json) {
var cite = $('<cite>').attr('authors', json.authors) .append('_' + json.authors + '_, ') .append('[**' + json.title + '**](' + encodeURI(json.links.length > 0 ? json.links[0] : '') + '), ') .append(json.source + ' [Zbl' + json.zbl_id + '](https://zbmath.org/?q=an:' + json.zbl_id + ')') .append('.'); var citeContainer =$('<span></span>').append(cite).html();

return citeContainer;
}

• I have never worked with Jason, so I have no idea if this is even valid. Do let me know, though. – Asaf Karagila Dec 13 '16 at 22:25
• First --- we're going to have to work out how to test this. Can someone who knows how to do this show us how to modify in place the javascript coming from SE? – Scott Morrison Dec 15 '16 at 1:10
• Second, I see some problems. 1) json.links is an array, possibly empty. If it is non-empty, we want to use the first entry as the URL. If it is empty we don't want to hyperlink the title. 2) There is no longer a json.citation_markdown field. – Scott Morrison Dec 15 '16 at 1:12
• (Thanks, of course, for making a start!) – Scott Morrison Dec 15 '16 at 1:13
• I guess we can change this using Developer Tools to change an instance of the running code and check. I'll give it a shot in a little bit. – Asaf Karagila Dec 15 '16 at 7:06
• Also, I think that in answers if you use an empty link, it just produces a text. – Asaf Karagila Dec 15 '16 at 7:07
• I added a test for json.links being empty; I think the previous code would have linked to "undefined". – Scott Morrison Dec 15 '16 at 22:35
• We are now waiting on Zentralblatt adding an Allow-Access-Control-Origin header, so that we can test this code. No update expected until mid-January. – Scott Morrison Dec 15 '16 at 23:09
• Any news on the header issue? – Asaf Karagila Jan 13 '17 at 4:57
• The headers are all fixed. For now it allows all cross domain requests. – Scott Morrison Jan 13 '17 at 21:41

We will also need to modify the fetchCallback function from http://cdn.sstatic.net/js/third-party/citation-helper.js?v=2.

This is the current content of that function, which we can "edit in place" to update.

// Callback to run when search completes.
function fetchCallback(response) {
// Here's an example of what the 'response' argument might contain.
// response = {
//  "results": [
//    {
//      "pagination": "2586-2620",
//        "http://dx.doi.org/10.1016/j.jfa.2014.12.018",
//        "http://www.arxiv.org/abs/1406.4766"
//      ],
//      "title": "Free transport for finite depth subfactor planar algebras",
//      "year": "2015",
//      "source": "",
//      "score": 2.15747,
//      "zbl_id": "06435710",
//      "authors": "Nelson, Brent"
//    },
//    {
//      "pagination": "183-214",
//        "http://dx.doi.org/10.1023/A:1021296230310"
//      ],
//      "title": "Exchange relation planar algebras",
//      "year": "2002",
//      "source": "Geom. Dedicata 95, 183-214 (2002).",
//      "score": 2.15747,
//      "zbl_id": "1022.46039",
//      "authors": "Landau, Zeph A."
//    }
//  ]
// }
var html = $('<div class="list">'); for (var i = 0; i < response.results.length; i++) { var result = response.results[i]; var zbl = 'https://zbmath.org/?q=an:' + result.zbl_id; var link = result.links.length > 0 ? result.links[0] : ''; var arxiv = ''; for (var j = 0; j < result.links.length; j++) { arxiv = result.links[j].includes("arxiv") ? result.links[j] : arxiv; } var authors = sanitizeForDisplay(result.authors); var title = sanitizeForDisplay(result.title); var citationHtml = sanitizeForDisplay(result.source); var result =$('<div class="item" style="float:none;padding:5px">')
.html($('<div class = "summary post-link" style="float:none;width:auto;font-weight:bold;">') .text(title)) .append('<br/>') .append($('<span class="body-summary" style="float:none"></span>')
.append(authors + '<br/>' + citationHtml + '<br/> Preview (opens in new tab): ')
)
.hover(function () { $(this).css('background-color', '#e6e6e6') }, function () {$(this).css('background-color', '#fff') });

html.append(result);
result.find('a').on('click', function(e) { e.stopPropagation(); });
}
$('#results').html('').append(html); MathJax.Hub.Queue(['Typeset', MathJax.Hub, 'results']);$('#popup-cite .search-spinner').removeSpinner();
}

• I've tried to do some editing. Of course we don't know how badly it will fail before we can test it properly. But I do think that having separate fields for the citation parts is crucial for getting properly formatted output. I'm not entirely sure about the free and pdf fields, though. – Asaf Karagila Dec 17 '16 at 9:23
• I probably messed it up, but I tried to add a code that checks if there is a link with arxiv substring, and in that case it adds it to the optional links. – Asaf Karagila Dec 18 '16 at 7:54
• Ah, good idea. Fixed two problems. – Scott Morrison Dec 18 '16 at 21:08
• Wow, only one minor typo and one syntactic problem? Impressive. – Asaf Karagila Dec 19 '16 at 5:17
• You might want to test it before you push it, I have created a Stack Snippet here. I'm not sure what is in the pending edit but you need to remove .citation from this line var result = response.results[i].citation; otherwise it won't work. If you find Stack Snippets to cumbersome, consider a jsfiddle. – rene Dec 23 '16 at 20:48
• @rene: Of course there will be testing before we push the new code. But we need to wait for ZBL to fix some stuff, and maybe return "better json" (as you can find in the comments to the other answers on this page). – Asaf Karagila Dec 26 '16 at 9:34