Make parseReferences() parse its own references

This commit is contained in:
Robbie Antenesse 2019-05-30 11:18:16 -06:00 committed by Robbie Antenesse
parent dafecd9582
commit 19e41958a4
1 changed files with 34 additions and 31 deletions

View File

@ -46,43 +46,46 @@ export function sortWords(render) {
} }
} }
export function parseReferences(detailsMarkdown, references) { export function parseReferences(detailsMarkdown) {
new Set(references).forEach(reference => { const references = detailsMarkdown.match(/\{\{.+?\}\}/g);
let wordToFind = reference.replace(/\{\{|\}\}/g, ''); if (references && Array.isArray(references)) {
let homonymn = 0; new Set(references).forEach(reference => {
let wordToFind = reference.replace(/\{\{|\}\}/g, '');
if (wordToFind.includes(':')) { let homonymn = 0;
const separator = wordToFind.indexOf(':');
homonymn = wordToFind.substr(separator + 1); if (wordToFind.includes(':')) {
wordToFind = wordToFind.substring(0, separator); const separator = wordToFind.indexOf(':');
if (homonymn && homonymn.trim() homonymn = wordToFind.substr(separator + 1);
&& !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) { wordToFind = wordToFind.substring(0, separator);
homonymn = parseInt(homonymn.trim()); if (homonymn && homonymn.trim()
} else { && !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) {
homonymn = false; homonymn = parseInt(homonymn.trim());
} else {
homonymn = false;
}
} }
}
let existingWordId = false; let existingWordId = false;
const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 }); const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 });
if (homonymn !== false && homonymn > 0) { if (homonymn !== false && homonymn > 0) {
if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') { if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') {
existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId; existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId;
}
} else if (homonymn !== false) {
existingWordId = wordExists(wordToFind, true);
} }
} else if (homonymn !== false) {
existingWordId = wordExists(wordToFind, true);
}
if (existingWordId !== false) { if (existingWordId !== false) {
if (homonymn < 1 && homonymnIndexes.length > 0) { if (homonymn < 1 && homonymnIndexes.length > 0) {
homonymn = 1; homonymn = 1;
}
const homonymnSubHTML = homonymn > 0 ? '<sub>' + homonymn.toString() + '</sub>' : '';
const wordMarkdownLink = `[${wordToFind}${homonymnSubHTML}](#${existingWordId})`;
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
} }
const homonymnSubHTML = homonymn > 0 ? '<sub>' + homonymn.toString() + '</sub>' : ''; });
const wordMarkdownLink = `[${wordToFind}${homonymnSubHTML}](#${existingWordId})`; }
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
}
});
return detailsMarkdown; return detailsMarkdown;
} }