diff --git a/src/js/render.js b/src/js/render.js index 86563f5..ab8ed87 100644 --- a/src/js/render.js +++ b/src/js/render.js @@ -1,6 +1,6 @@ import md from 'snarkdown'; import { removeTags } from '../helpers'; -import { getWordsStats } from './utilities'; +import { getWordsStats, wordExists } from './utilities'; import { showSection } from './displayToggles'; export function renderAll() { @@ -95,6 +95,21 @@ export function renderWords() { const { words } = window.currentDictionary; let wordsHTML = ''; words.forEach(word => { + let detailsMarkdown = removeTags(word.longDefinition); + const references = detailsMarkdown.match(/\{\{.+?\}\}/g); + if (references && Array.isArray(references)) { + new Set(references).forEach(reference => { + console.log(reference); + const wordToFind = reference.replace(/\{\{|\}\}/g, ''); + const existingWordId = wordExists(wordToFind, true); + if (existingWordId !== false) { + const wordMarkdownLink = `[${wordToFind}](#${existingWordId})`; + console.log(wordMarkdownLink); + detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink); + } + }); + } + console.log(detailsMarkdown); wordsHTML += `

${removeTags(word.name)}

@@ -104,7 +119,7 @@ export function renderWords() {
${removeTags(word.simpleDefinition)}
- ${md(removeTags(word.longDefinition))} + ${md(detailsMarkdown)}
`; diff --git a/src/js/utilities.js b/src/js/utilities.js index 6e17171..f3ecba0 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -89,3 +89,12 @@ export function getWordsStats() { return wordStats; } + +export function wordExists(word, returnId = false) { + const { currentDictionary } = window; + const { caseSensitive } = currentDictionary.settings; + const foundWord = currentDictionary.words.find(existingWord => { + return caseSensitive ? existingWord.name === word : existingWord.name.toLowerCase() === word.toLowerCase(); + }); + return foundWord ? (returnId ? foundWord.wordId : true) : false; +} \ No newline at end of file