You can use the
closest()[
^] method which traverses up the DOM to find the first matching parent with a provided selector. You're already matching whether an element text contains the search term you could simply change:
$(this).html($(this).text().replace(r, "<mark>$1</mark>"));
}
$(".timeline-compendium__content").collapse("show");
With:
$(this).html($(this).text().replace(r, "<mark>$1</mark>"));
$(this).closest('.timeline-compendium__content').collapse('show');
}
For any elements which match the search term, this should navigate up the DOM to the containing section and expand it.