mirror of
				https://github.com/Alamantus/Lexiconga.git
				synced 2025-10-31 00:16:44 +01:00 
			
		
		
		
	Add text replacer to auto-reference tagged words
This commit is contained in:
		
							parent
							
								
									e4369f0ffd
								
							
						
					
					
						commit
						060a78b3e4
					
				
					 2 changed files with 26 additions and 2 deletions
				
			
		|  | @ -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 += `<article class="entry" id="${word.wordId}">
 | ||||
|       <header> | ||||
|         <h4 class="word">${removeTags(word.name)}</h4> | ||||
|  | @ -104,7 +119,7 @@ export function renderWords() { | |||
|       <dl> | ||||
|         <dt class="definition">${removeTags(word.simpleDefinition)}</dt> | ||||
|         <dd class="details"> | ||||
|           ${md(removeTags(word.longDefinition))} | ||||
|           ${md(detailsMarkdown)} | ||||
|         </dd> | ||||
|       </dl> | ||||
|     </article>`; | ||||
|  |  | |||
|  | @ -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; | ||||
| } | ||||
		Loading…
	
	Add table
		
		Reference in a new issue