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 md from 'snarkdown';
|
||||||
import { removeTags } from '../helpers';
|
import { removeTags } from '../helpers';
|
||||||
import { getWordsStats } from './utilities';
|
import { getWordsStats, wordExists } from './utilities';
|
||||||
import { showSection } from './displayToggles';
|
import { showSection } from './displayToggles';
|
||||||
|
|
||||||
export function renderAll() {
|
export function renderAll() {
|
||||||
|
@ -95,6 +95,21 @@ export function renderWords() {
|
||||||
const { words } = window.currentDictionary;
|
const { words } = window.currentDictionary;
|
||||||
let wordsHTML = '';
|
let wordsHTML = '';
|
||||||
words.forEach(word => {
|
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}">
|
wordsHTML += `<article class="entry" id="${word.wordId}">
|
||||||
<header>
|
<header>
|
||||||
<h4 class="word">${removeTags(word.name)}</h4>
|
<h4 class="word">${removeTags(word.name)}</h4>
|
||||||
|
@ -104,7 +119,7 @@ export function renderWords() {
|
||||||
<dl>
|
<dl>
|
||||||
<dt class="definition">${removeTags(word.simpleDefinition)}</dt>
|
<dt class="definition">${removeTags(word.simpleDefinition)}</dt>
|
||||||
<dd class="details">
|
<dd class="details">
|
||||||
${md(removeTags(word.longDefinition))}
|
${md(detailsMarkdown)}
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</article>`;
|
</article>`;
|
||||||
|
|
|
@ -89,3 +89,12 @@ export function getWordsStats() {
|
||||||
|
|
||||||
return wordStats;
|
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