@@ -41,15 +41,32 @@ input.addEventListener("input", () => {
41
41
resultsContainer . innerHTML =
42
42
results . length === 0
43
43
? "<div>no results</div>"
44
- : `<div>${ results . length . toLocaleString ( "en-US" ) } result${ results . length === 1 ? "" : "s" } </div><ol>${ results
45
- . map ( renderResult )
46
- . join ( "" ) } </ol>`;
44
+ : `<div>${ results . length . toLocaleString ( "en-US" ) } result${
45
+ results . length === 1 ? "" : "s"
46
+ } </div><ol>${ renderResults ( results ) } </ol>`;
47
+ resultsContainer . querySelector ( `.${ activeClass } ` ) . scrollIntoView ( { block : "nearest" } ) ;
47
48
} ) ;
48
49
49
- function renderResult ( { id, score, title} , i ) {
50
- return `<li data-score="${ Math . min ( 5 , Math . round ( 0.6 * score ) ) } " class="observablehq-link${
51
- i === 0 ? ` ${ activeClass } ` : ""
52
- } "><a href="${ escapeDoubleQuote ( import . meta. resolve ( `../${ id } ` ) ) } ">${ escapeText ( String ( title ?? "—" ) ) } </a></li>`;
50
+ function renderResults ( results ) {
51
+ const me = document . location . href . replace ( / [ ? ] .* / , "" ) ;
52
+ let found ;
53
+ results = results . map ( ( { id, score, title} ) => {
54
+ const href = import . meta. resolve ( `../${ id } ` ) ;
55
+ return {
56
+ title : String ( title ?? "—" ) ,
57
+ href,
58
+ score : Math . min ( 5 , Math . round ( 0.6 * score ) ) ,
59
+ active : me === href && ( found = true )
60
+ } ;
61
+ } ) ;
62
+ if ( ! found ) results [ 0 ] . active = true ;
63
+ return results . map ( renderResult ) . join ( "" ) ;
64
+ }
65
+
66
+ function renderResult ( { href, score, title, active} ) {
67
+ return `<li data-score="${ score } " class="observablehq-link${
68
+ active ? ` ${ activeClass } ` : ""
69
+ } "><a href="${ escapeDoubleQuote ( href ) } ">${ escapeText ( title ) } </a></li>`;
53
70
}
54
71
55
72
function escapeDoubleQuote ( text ) {
0 commit comments