Skip to content

Commit f11bb76

Browse files
authored
Merge pull request #95 from easyops-cn/steve/fix-marking-in-code-blocks
fix: fix an issue of highlighting in code blocks
2 parents 841a82a + 3378b9c commit f11bb76

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/client/theme/SearchBar/SearchBar.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff 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(() => {

0 commit comments

Comments
 (0)