Skip to content

Commit

Permalink
Merge pull request #53 from newfold-labs/enhance/render-markdown-in-c…
Browse files Browse the repository at this point in the history
…ontent

Render markdown in search result content
  • Loading branch information
abhijitb authored Aug 21, 2024
2 parents 83a1382 + 4681e62 commit f5c8c59
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '35e0e269e4ed473deb03');
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '85bd21a71ebc930f4591');
34 changes: 17 additions & 17 deletions build/index.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"algoliasearch": "4.20.0",
"lodash": "4.17.21",
"lodash.debounce": "4.0.8",
"marked": "14.0.0",
"react-instantsearch-hooks-web": "6.43.0",
"react-loader-spinner": "6.1.6",
"react-router-dom": "6.6.2",
Expand Down
16 changes: 15 additions & 1 deletion src/components/ResultContent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//
import Feedback from './Feedback';
import NoResults from './NoResults';
import { useEffect, useState } from 'react';
import { marked } from 'marked';

export const ResultContent = ( {
content,
Expand All @@ -13,10 +15,22 @@ export const ResultContent = ( {
return <NoResults />;
}

const MarkdownRenderer = ( { markdownText } ) => {
const [ htmlContent, setHtmlContent ] = useState( '' );

useEffect( () => {
// Convert Markdown to HTML whenever markdownText changes
const convertedHTML = marked( markdownText );
setHtmlContent( convertedHTML );
}, [ markdownText ] ); // Dependency array ensures this runs on markdownText change

return <p dangerouslySetInnerHTML={ { __html: htmlContent } } />;
};

if ( content && content.length > 0 ) {
return (
<>
<p dangerouslySetInnerHTML={ { __html: content } } />
<MarkdownRenderer markdownText={ content } />
{ showFeedbackSection && content && content.length > 0 && (
<Feedback postId={ postId } source={ source } />
) }
Expand Down
4 changes: 2 additions & 2 deletions src/components/SearchResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const SearchResults = ( props ) => {
setPostId( currentResultPostId );
}
const savedInput = LocalStorageUtils.getSearchInput();
const input = savedInput || ' ';
const input = savedInput || '';
setSearchInput( input );
const brand = await CapabilityAPI.getBrand();
const multiSearchResults = await fetchMultiSearchResults(
Expand Down Expand Up @@ -164,7 +164,7 @@ const SearchResults = ( props ) => {
} finally {
setLoading( false );
}
}, 300 );
}, 500 );
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );

Expand Down

0 comments on commit f5c8c59

Please sign in to comment.