Parse references on backend for view

This commit is contained in:
Robbie Antenesse 2019-05-30 12:32:44 -06:00 committed by Robbie Antenesse
parent 601841c754
commit 4de8b572c3
2 changed files with 9 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import { getWordsStats, wordExists } from '../utilities';
import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from '../search'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from '../search';
import { showSection } from '../displayToggles'; import { showSection } from '../displayToggles';
import { setupSearchFilters, setupInfoModal } from './setupListeners'; import { setupSearchFilters, setupInfoModal } from './setupListeners';
import { parseReferences } from '../wordManagement';
export function renderAll() { export function renderAll() {
renderDictionaryDetails(); renderDictionaryDetails();
@ -134,18 +135,8 @@ export function renderWords() {
} }
words.forEach(originalWord => { words.forEach(originalWord => {
let detailsMarkdown = removeTags(originalWord.details); let detailsMarkdown = originalWord.details;
const references = detailsMarkdown.match(/\{\{.+?\}\}/g); detailsMarkdown = parseReferences(detailsMarkdown);
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);
}
});
}
const word = highlightSearchTerm({ const word = highlightSearchTerm({
name: removeTags(originalWord.name), name: removeTags(originalWord.name),
pronunciation: removeTags(originalWord.pronunciation), pronunciation: removeTags(originalWord.pronunciation),

View File

@ -143,13 +143,13 @@ VALUES ($new_id, ?, ?, ?, ?)";
$query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND is_public=1"; $query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND is_public=1";
$results = $this->db->query($query, array($dictionary))->fetchAll(); $results = $this->db->query($query, array($dictionary))->fetchAll();
if ($results) { if ($results) {
return array_map(function ($row) { return array_map(function ($row) use ($dictionary) {
return array( return array(
'name' => $row['name'], 'name' => $row['name'],
'pronunciation' => $row['pronunciation'], 'pronunciation' => $row['pronunciation'],
'partOfSpeech' => $row['part_of_speech'], 'partOfSpeech' => $row['part_of_speech'],
'definition' => $row['definition'], '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']), 'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']),
'createdOn' => intval($row['created_on']), 'createdOn' => intval($row['created_on']),
'wordId' => intval($row['word_id']), '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"; $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(); $result = $this->db->query($query, array($dictionary, $word))->fetch();
if ($result) { if ($result) {
$details = $this->parseReferences($result['details'], $dictionary);
return array( return array(
'name' => $result['name'], 'name' => $result['name'],
'pronunciation' => $result['pronunciation'], 'pronunciation' => $result['pronunciation'],
'partOfSpeech' => $result['part_of_speech'], 'partOfSpeech' => $result['part_of_speech'],
'definition' => $result['definition'], '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']), 'lastUpdated' => is_null($result['last_updated']) ? intval($result['created_on']) : intval($result['last_updated']),
'createdOn' => intval($result['created_on']), 'createdOn' => intval($result['created_on']),
'wordId' => intval($result['word_id']), 'wordId' => intval($result['word_id']),
@ -182,6 +181,7 @@ VALUES ($new_id, ?, ?, ?, ?)";
} }
private function parseReferences($details, $dictionary_id) { private function parseReferences($details, $dictionary_id) {
$details = strip_tags($details);
if (preg_match_all('/\{\{.+?\}\}/', $details, $references) !== false) { if (preg_match_all('/\{\{.+?\}\}/', $details, $references) !== false) {
$references = array_unique($references[0]); $references = array_unique($references[0]);
foreach($references as $reference) { foreach($references as $reference) {
@ -217,7 +217,8 @@ VALUES ($new_id, ?, ?, ?, ?)";
} }
$homonymn_sub_html = $homonymn > 0 ? '<sub>' . $homonymn . '</sub>' : ''; $homonymn_sub_html = $homonymn > 0 ? '<sub>' . $homonymn . '</sub>' : '';
$site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id)); $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 = '<a href="' . $site_root . $dictionary_id . '/' . $target_id .'" target="_blank" title="Link to Reference">'
. $word_to_find . $homonymn_sub_html . '</a>';
$details = str_replace($reference, $markdown_link, $details); $details = str_replace($reference, $markdown_link, $details);
} }
} }