Display word etymology if is present

This commit is contained in:
Robbie Antenesse 2020-05-03 22:28:31 -06:00
parent 65cf421cbe
commit 40feae7194
2 changed files with 48 additions and 33 deletions

View File

@ -9,7 +9,7 @@ import {
setupWordEditFormButtons, setupWordEditFormButtons,
} from '../setupListeners/words'; } from '../setupListeners/words';
import { getPaginationData } from '../pagination'; import { getPaginationData } from '../pagination';
import { getOpenEditForms, translateOrthography, parseReferences } from '../wordManagement'; import { getOpenEditForms, translateOrthography, parseReferences, getWordReferenceMarkdown } from '../wordManagement';
import { renderAd } from '../ads'; import { renderAd } from '../ads';
import { getPublicLink } from '../account/utilities'; import { getPublicLink } from '../account/utilities';
import { renderPartsOfSpeech } from './details'; import { renderPartsOfSpeech } from './details';
@ -60,6 +60,8 @@ export function renderWords() {
partOfSpeech: removeTags(originalWord.partOfSpeech), partOfSpeech: removeTags(originalWord.partOfSpeech),
definition: removeTags(originalWord.definition), definition: removeTags(originalWord.definition),
details: parseReferences(removeTags(originalWord.details)), details: parseReferences(removeTags(originalWord.details)),
etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null
: originalWord.etymology.map(root => getWordReferenceMarkdown(removeTags(root))).join(', '),
wordId: originalWord.wordId, wordId: originalWord.wordId,
}); });
const homonymnNumber = getHomonymnNumber(originalWord); const homonymnNumber = getHomonymnNumber(originalWord);
@ -86,6 +88,11 @@ export function renderWords() {
<dd class="details"> <dd class="details">
${md(word.details)} ${md(word.details)}
</dd> </dd>
${word.etymology === null ? '' : `<hr>
<dt>Etymology <small>(Root Word${originalWord.etymology.length !== 1 ? 's' : ''})</small></dt>
<dd class="etymology">
${md(word.etymology).replace(/<\/?p>/g, '')}
</dd>`}
</dl> </dl>
</article>`; </article>`;
}); });

View File

@ -116,38 +116,8 @@ export function parseReferences(detailsMarkdown) {
const references = detailsMarkdown.match(/\{\{.+?\}\}/g); const references = detailsMarkdown.match(/\{\{.+?\}\}/g);
if (references && Array.isArray(references)) { if (references && Array.isArray(references)) {
new Set(references).forEach(reference => { new Set(references).forEach(reference => {
let wordToFind = reference.replace(/\{\{|\}\}/g, ''); const wordMarkdownLink = getWordReferenceMarkdown(reference);
let homonymn = 0; if (wordMarkdownLink !== reference) {
if (wordToFind.includes(':')) {
const separator = wordToFind.indexOf(':');
homonymn = wordToFind.substr(separator + 1);
wordToFind = wordToFind.substring(0, separator);
if (homonymn && homonymn.trim()
&& !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) {
homonymn = parseInt(homonymn.trim());
} else {
homonymn = false;
}
}
let existingWordId = false;
const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 });
if (homonymn !== false && homonymn > 0) {
if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') {
existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId;
}
} else if (homonymn !== false) {
existingWordId = wordExists(wordToFind, true);
}
if (existingWordId !== false) {
if (homonymn < 1 && homonymnIndexes.length > 0) {
homonymn = 1;
}
const homonymnSubHTML = homonymnIndexes.length > 1 && homonymn - 1 >= 0 ? '<sub>' + homonymn.toString() + '</sub>' : '';
const wordMarkdownLink = `<span class="word-reference">[<span class="orthographic-translation">${translateOrthography(wordToFind)}</span>${homonymnSubHTML}](#${existingWordId})</span>`;
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink); detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
} }
}); });
@ -155,6 +125,44 @@ export function parseReferences(detailsMarkdown) {
return detailsMarkdown; return detailsMarkdown;
} }
export function getWordReferenceMarkdown(reference) {
const wordToFind = reference.replace(/\{\{|\}\}/g, '');
let homonymn = 0;
if (wordToFind.includes(':')) {
const separator = wordToFind.indexOf(':');
homonymn = wordToFind.substr(separator + 1);
wordToFind = wordToFind.substring(0, separator);
if (homonymn && homonymn.trim()
&& !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) {
homonymn = parseInt(homonymn.trim());
} else {
homonymn = false;
}
}
let existingWordId = false;
const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 });
if (homonymn !== false && homonymn > 0) {
if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') {
existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId;
}
} else if (homonymn !== false) {
existingWordId = wordExists(wordToFind, true);
}
if (existingWordId !== false) {
if (homonymn < 1 && homonymnIndexes.length > 0) {
homonymn = 1;
}
const homonymnSubHTML = homonymnIndexes.length > 1 && homonymn - 1 >= 0 ? '<sub>' + homonymn.toString() + '</sub>' : '';
return `<span class="word-reference">[<span class="orthographic-translation">${translateOrthography(wordToFind)}</span>${homonymnSubHTML}](#${existingWordId})</span>`;
}
return reference;
}
export function expandAdvancedForm(id = false) { export function expandAdvancedForm(id = false) {
const wordId = typeof id.target !== 'undefined' ? this.id.replace('expandAdvancedForm', '') : id; const wordId = typeof id.target !== 'undefined' ? this.id.replace('expandAdvancedForm', '') : id;
const button = typeof id.target !== 'undefined' ? this : document.getElementById('expandAdvancedForm' + (!wordId ? '' : wordId)), const button = typeof id.target !== 'undefined' ? this : document.getElementById('expandAdvancedForm' + (!wordId ? '' : wordId)),