File tree Expand file tree Collapse file tree 1 file changed +13
-7
lines changed
src/client/theme/SearchBar Expand file tree Collapse file tree 1 file changed +13
-7
lines changed Original file line number Diff line number Diff line change @@ -163,13 +163,19 @@ export default function SearchBar({
163163 if ( keywords . length === 0 ) {
164164 return ;
165165 }
166- const root = document . querySelector ( "article" ) ;
167- if ( ! root ) {
168- return ;
169- }
170- const mark = new Mark ( root ) ;
171- mark . unmark ( ) ;
172- mark . mark ( keywords ) ;
166+ // A workaround to fix an issue of highlighting in code blocks.
167+ // See https://github.com/easyops-cn/docusaurus-search-local/issues/92
168+ // Code blocks will be re-rendered after this `useEffect` ran.
169+ // So we make the marking run after a macro task.
170+ setTimeout ( ( ) => {
171+ const root = document . querySelector ( "article" ) ;
172+ if ( ! root ) {
173+ return ;
174+ }
175+ const mark = new Mark ( root ) ;
176+ mark . unmark ( ) ;
177+ mark . mark ( keywords ) ;
178+ } ) ;
173179 } , [ location . search ] ) ;
174180
175181 const onInputFocus = useCallback ( ( ) => {
You can’t perform that action at this time.
0 commit comments