From 4de8b572c3a96d078b2758bc972bbdf1a50c8794 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 30 May 2019 12:32:44 -0600 Subject: [PATCH] Parse references on backend for view --- src/js/view/render.js | 15 +++------------ src/php/api/Dictionary.php | 11 ++++++----- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/js/view/render.js b/src/js/view/render.js index 7082d23..6c1b86e 100644 --- a/src/js/view/render.js +++ b/src/js/view/render.js @@ -4,6 +4,7 @@ import { getWordsStats, wordExists } from '../utilities'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from '../search'; import { showSection } from '../displayToggles'; import { setupSearchFilters, setupInfoModal } from './setupListeners'; +import { parseReferences } from '../wordManagement'; export function renderAll() { renderDictionaryDetails(); @@ -134,18 +135,8 @@ export function renderWords() { } words.forEach(originalWord => { - let detailsMarkdown = removeTags(originalWord.details); - const references = detailsMarkdown.match(/\{\{.+?\}\}/g); - if (references && Array.isArray(references)) { - new Set(references).forEach(reference => { - const wordToFind = reference.replace(/\{\{|\}\}/g, ''); - const existingWordId = wordExists(wordToFind, true); - if (existingWordId !== false) { - const wordMarkdownLink = `[${wordToFind}](#${existingWordId})`; - detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink); - } - }); - } + let detailsMarkdown = originalWord.details; + detailsMarkdown = parseReferences(detailsMarkdown); const word = highlightSearchTerm({ name: removeTags(originalWord.name), pronunciation: removeTags(originalWord.pronunciation), diff --git a/src/php/api/Dictionary.php b/src/php/api/Dictionary.php index 924a550..485d32d 100644 --- a/src/php/api/Dictionary.php +++ b/src/php/api/Dictionary.php @@ -143,13 +143,13 @@ VALUES ($new_id, ?, ?, ?, ?)"; $query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND is_public=1"; $results = $this->db->query($query, array($dictionary))->fetchAll(); if ($results) { - return array_map(function ($row) { + return array_map(function ($row) use ($dictionary) { return array( 'name' => $row['name'], 'pronunciation' => $row['pronunciation'], 'partOfSpeech' => $row['part_of_speech'], 'definition' => $row['definition'], - 'details' => $row['details'], + 'details' => $this->parseReferences($row['details'], $dictionary), 'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']), 'createdOn' => intval($row['created_on']), 'wordId' => intval($row['word_id']), @@ -165,13 +165,12 @@ VALUES ($new_id, ?, ?, ?, ?)"; $query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND word_id=? AND is_public=1"; $result = $this->db->query($query, array($dictionary, $word))->fetch(); if ($result) { - $details = $this->parseReferences($result['details'], $dictionary); return array( 'name' => $result['name'], 'pronunciation' => $result['pronunciation'], 'partOfSpeech' => $result['part_of_speech'], 'definition' => $result['definition'], - 'details' => $details, + 'details' => $this->parseReferences($result['details'], $dictionary), 'lastUpdated' => is_null($result['last_updated']) ? intval($result['created_on']) : intval($result['last_updated']), 'createdOn' => intval($result['created_on']), 'wordId' => intval($result['word_id']), @@ -182,6 +181,7 @@ VALUES ($new_id, ?, ?, ?, ?)"; } private function parseReferences($details, $dictionary_id) { + $details = strip_tags($details); if (preg_match_all('/\{\{.+?\}\}/', $details, $references) !== false) { $references = array_unique($references[0]); foreach($references as $reference) { @@ -217,7 +217,8 @@ VALUES ($new_id, ?, ?, ?, ?)"; } $homonymn_sub_html = $homonymn > 0 ? '' . $homonymn . '' : ''; $site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id)); - $markdown_link = '[' . $word_to_find . $homonymn_sub_html . '](' . $site_root . $dictionary_id . '/' . $target_id . ')'; + $markdown_link = '' + . $word_to_find . $homonymn_sub_html . ''; $details = str_replace($reference, $markdown_link, $details); } }