diff --git a/src/components/Constitution.jsx b/src/components/Constitution.jsx index c57fa11..74c9e36 100644 --- a/src/components/Constitution.jsx +++ b/src/components/Constitution.jsx @@ -1,4 +1,5 @@ import accordionToggle from "../utils/accordion.js"; +import highlight from '../utils/highlight.js'; import "../styles/Normal.css"; export default function Constitution({constitution, search = null}) { @@ -23,7 +24,7 @@ export default function Constitution({constitution, search = null}) {
${search}`)}} + dangerouslySetInnerHTML={{__html: highlight(constitution, search) }} >
diff --git a/src/components/Sections.jsx b/src/components/Sections.jsx index 94d31ee..da22b6d 100644 --- a/src/components/Sections.jsx +++ b/src/components/Sections.jsx @@ -1,5 +1,6 @@ -import accordionToggle from "../utils/accordion.js"; import React, { useState, useEffect } from 'react'; +import accordionToggle from '../utils/accordion.js'; +import highlight from '../utils/highlight.js'; import "../styles/Section.css"; import pdf from "../images/PDF_file_icon.svg"; import "../styles/Normal.css"; @@ -100,7 +101,7 @@ export default function Sections({ sections, search = null }) {
${search}`) }} + dangerouslySetInnerHTML={{ __html: highlight(section.ร่างบทบัญญัติ, search) }} >
@@ -114,7 +115,7 @@ export default function Sections({ sections, search = null }) {
${search}`) }} + dangerouslySetInnerHTML={{ __html: highlight(section.ประเด็นการพิจารณา, search) }} />
{isShown && diff --git a/src/pages/Search.jsx b/src/pages/Search.jsx index 2e69399..022e076 100644 --- a/src/pages/Search.jsx +++ b/src/pages/Search.jsx @@ -101,7 +101,7 @@ export default function Search({ searchInputValue, setSearchInputValue }) { const [section, chapter] = item[1].split('|'); return ( saveHistory(searchInputValue)} key={index} state={{ backable: true }} diff --git a/src/utils/highlight.js b/src/utils/highlight.js new file mode 100644 index 0000000..af541d9 --- /dev/null +++ b/src/utils/highlight.js @@ -0,0 +1,12 @@ +export default function highlight(content, words='') { + return words + .trim() + .split('+') + .reduce( + (hc, word) => hc.replace( + new RegExp(word, 'gi'), + `${word}` + ) + , content) +} + \ No newline at end of file