diff --git a/static/annotate-wikidata-entity-ids-in-shex.js b/static/annotate-wikidata-entity-ids-in-shex.js
index f43977d..824f54d 100644
--- a/static/annotate-wikidata-entity-ids-in-shex.js
+++ b/static/annotate-wikidata-entity-ids-in-shex.js
@@ -1,72 +1,72 @@
'use strict';
async function annotateWikidataEntityIdsInShex(shexElement, languageCodes = ['en']) {
if (shexElement.childElementCount) {
console.warn(`ShEx element already has ${shexElement.childElementCount} child elements, adding Wikidata annotations might break them!`);
}
const shexHTML = shexElement.innerHTML,
entityIdsRegexp = /\b[PQ][1-9][0-9]*\b/g;
let entityIds = shexHTML.match(entityIdsRegexp);
entityIds = [...new Set(entityIds)];
const labels = new Map();
let someEntityIds;
while ((someEntityIds = entityIds.splice(0, 50)).length) {
const url = 'https://www.wikidata.org/w/api.php?' +
'action=wbgetentities&' +
'ids=' + someEntityIds.join('|') + '&' +
'props=labels&' +
'origin=*&' +
'format=json&formatversion=2';
const { entities } = await fetch(url, { mode: 'cors', credentials: 'omit' }).then(response => response.json());
for (const [entityId, entity] of Object.entries(entities)) {
for (const languageCode of languageCodes) {
if (languageCode in entity.labels) {
labels.set(entityId, entity.labels[languageCode].value);
break;
}
}
if (!labels.has(entityId)) {
for (const [languageCode, label] of Object.entries(entity.labels)) {
- console.warn(`No ${languageCodes.join('/')} label for ${entityId}, using ${languageCode} label instead!`);
+ console.warn(`No ${languageCodes.join('/')} label for ${entityId}, using ${languageCode} label "${label.value}" instead!`);
labels.set(entityId, label.value);
break;
}
}
}
}
shexElement.innerHTML = shexHTML
.replace(
entityIdsRegexp,
function(match) {
if (labels.has(match)) {
return `${match}`;
} else {
console.warn(`No label for ${match}!`);
}
}
).replace(
/([^]?)(wd<\/span>:<\/span>|wd:)()?(Q[1-9][0-9]*)<\/abbr>(<\/span>)?/g,
function(_, lineend, wd, ntOpen = '', title, qid, ntClose = '') {
let href, attr = '';
if (lineend === '' || lineend === '\n') {
href = `http://www.wikidata.org/entity/${qid}`;
attr = ` id="${qid}"`;
} else {
href = `#${qid}`;
}
return `${lineend}${wd}${ntOpen}${qid}${ntClose}`;
}
);
}
async function annotateWikidataEntityIdsInShex_default() {
const shexElement = document.getElementById('shex');
if (shexElement) {
await annotateWikidataEntityIdsInShex(shexElement, navigator.languages.concat('en'));
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', annotateWikidataEntityIdsInShex_default);
} else {
annotateWikidataEntityIdsInShex_default();
}