PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 All 203 releases
betterdocs / react-src / public / shortcodes / search-modal / ExpandableContent.js

ExpandableContent.js in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.5, at react-src/public/shortcodes/search-modal/ExpandableContent.js

43 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useState } from 'react';
2
3 const ExpandableContent = ({ content = '', wordLimit }) => {
4 const [isExpanded, setIsExpanded] = useState(false);
5
6 const toggleExpand = () => {
7 setIsExpanded(!isExpanded);
8 };
9
10 const truncateContent = (content, wordLimit) => {
11 if (!content) return ''; // Handle case where content is not defined
12 const words = content.split(/\s+/);
13 return words.slice(0, wordLimit).join(' ') + (words.length > wordLimit ? '...' : '');
14 };
15
16 const stripPTags = (html) => {
17 if (!html) return ''; // Handle case where html is not defined
18 return html.replace(/<\/?p[^>]*>/g, '');
19 };
20
21 const wordCount = (text) => {
22 if (!text) return 0; // Handle case where text is not defined
23 return text.trim().split(/\s+/).length;
24 };
25
26 const displayedContent = isExpanded ? stripPTags(content) : stripPTags(truncateContent(content, wordLimit));
27 const wordCountExceeded = wordCount(content) > wordLimit;
28
29 return (
30 <div className="content-sub">
31 <p>
32 <span dangerouslySetInnerHTML={{ __html: displayedContent }} />
33 <span onClick={toggleExpand} className="show-more-btn" style={{ cursor: 'pointer' }}>
34 {(wordCountExceeded && !isExpanded) && 'Show More'}
35 {isExpanded && 'Show Less'}
36 </span>
37 </p>
38 </div>
39 );
40 };
41
42 export default ExpandableContent;
43