|
@@ -132,6 +132,69 @@ function dw_guestbook_render_block( $block_content, $block ) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
add_action( 'wp_enqueue_scripts', 'dw_scripts' );
|
|
add_action( 'wp_enqueue_scripts', 'dw_scripts' );
|
|
|
|
|
+
|
|
|
|
|
+// On the search results page, rewrite each post-excerpt block to a snippet
|
|
|
|
|
+// centered on the match with the term(s) highlighted — mirroring the TIL column.
|
|
|
|
|
+// Hooked at render_block (not get_the_excerpt) because the core post-excerpt block
|
|
|
|
|
+// runs wp_trim_words() → wp_strip_all_tags() after the excerpt filters, which would
|
|
|
|
|
+// otherwise eat the <mark> tags. Titles are left alone (never marks <title>/nav).
|
|
|
|
|
+// When the term isn't in the body (title-only match) the block is left untouched.
|
|
|
|
|
+// Styled by `body.search mark`.
|
|
|
|
|
+function dw_search_excerpt_highlight( $block_content, $block, $instance ) {
|
|
|
|
|
+ if ( is_admin() || ! is_search() ) {
|
|
|
|
|
+ return $block_content;
|
|
|
|
|
+ }
|
|
|
|
|
+ $query = trim( get_search_query() );
|
|
|
|
|
+ if ( '' === $query ) {
|
|
|
|
|
+ return $block_content;
|
|
|
|
|
+ }
|
|
|
|
|
+ $post_id = isset( $instance->context['postId'] ) ? $instance->context['postId'] : 0;
|
|
|
|
|
+ $post = $post_id ? get_post( $post_id ) : null;
|
|
|
|
|
+ if ( ! $post instanceof WP_Post ) {
|
|
|
|
|
+ return $block_content;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $terms = array_values( array_unique( array_filter( preg_split( '/\s+/', $query ) ) ) );
|
|
|
|
|
+ $has_mb = function_exists( 'mb_stripos' );
|
|
|
|
|
+
|
|
|
|
|
+ // Plain-text body (strip shortcodes, block markup, tags).
|
|
|
|
|
+ $text = html_entity_decode(
|
|
|
|
|
+ wp_strip_all_tags( strip_shortcodes( $post->post_content ) ),
|
|
|
|
|
+ ENT_QUOTES,
|
|
|
|
|
+ 'UTF-8'
|
|
|
|
|
+ );
|
|
|
|
|
+ $text = trim( preg_replace( '/\s+/', ' ', $text ) );
|
|
|
|
|
+
|
|
|
|
|
+ // Locate the first term; bail (leave the intro excerpt) if it's not in the body.
|
|
|
|
|
+ $pos = false;
|
|
|
|
|
+ foreach ( $terms as $t ) {
|
|
|
|
|
+ $p = $has_mb ? mb_stripos( $text, $t ) : stripos( $text, $t );
|
|
|
|
|
+ if ( false !== $p ) { $pos = $p; break; }
|
|
|
|
|
+ }
|
|
|
|
|
+ if ( false === $pos || '' === $text ) {
|
|
|
|
|
+ return $block_content;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Window a snippet around the match, snapped to word boundaries.
|
|
|
|
|
+ $len = $has_mb ? mb_strlen( $text ) : strlen( $text );
|
|
|
|
|
+ $start = max( 0, $pos - 60 );
|
|
|
|
|
+ $slice = $has_mb ? mb_substr( $text, $start, 220 ) : substr( $text, $start, 220 );
|
|
|
|
|
+ if ( $start > 0 ) { $slice = preg_replace( '/^\S*\s/', '', $slice ); }
|
|
|
|
|
+ if ( $start + 220 < $len ) { $slice = preg_replace( '/\s\S*$/', '', $slice ); }
|
|
|
|
|
+ $snippet = esc_html( ( $start > 0 ? '… ' : '' ) . $slice . ( $start + 220 < $len ? ' …' : '' ) );
|
|
|
|
|
+ foreach ( $terms as $t ) {
|
|
|
|
|
+ $snippet = preg_replace( '/(' . preg_quote( $t, '/' ) . ')/i', '<mark>$1</mark>', $snippet );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Swap the excerpt paragraph's inner content, preserving the block wrapper.
|
|
|
|
|
+ return preg_replace_callback(
|
|
|
|
|
+ '#(<p class="wp-block-post-excerpt__excerpt">).*?(</p>)#s',
|
|
|
|
|
+ function ( $m ) use ( $snippet ) { return $m[1] . $snippet . $m[2]; },
|
|
|
|
|
+ $block_content,
|
|
|
|
|
+ 1
|
|
|
|
|
+ );
|
|
|
|
|
+}
|
|
|
|
|
+add_filter( 'render_block_core/post-excerpt', 'dw_search_excerpt_highlight', 10, 3 );
|
|
|
function dw_scripts() {
|
|
function dw_scripts() {
|
|
|
global $post;
|
|
global $post;
|
|
|
wp_enqueue_style( 'style-min', get_template_directory_uri() . '/v4-style.min.css', array(), filemtime( get_template_directory() . '/v4-style.min.css' ) );
|
|
wp_enqueue_style( 'style-min', get_template_directory_uri() . '/v4-style.min.css', array(), filemtime( get_template_directory() . '/v4-style.min.css' ) );
|
|
@@ -211,6 +274,19 @@ function dw_scripts() {
|
|
|
wp_enqueue_style( 'lychee-embed', 'https://davidwindham.com/photo/embed/lychee-embed.css', array(), '1.0.0' );
|
|
wp_enqueue_style( 'lychee-embed', 'https://davidwindham.com/photo/embed/lychee-embed.css', array(), '1.0.0' );
|
|
|
wp_enqueue_script( 'lychee-embed', 'https://davidwindham.com/photo/embed/lychee-embed.js', array(), '1.0.0', true );
|
|
wp_enqueue_script( 'lychee-embed', 'https://davidwindham.com/photo/embed/lychee-embed.js', array(), '1.0.0', true );
|
|
|
}
|
|
}
|
|
|
|
|
+ // --- TIL search (removable): merge the Docusaurus /til lunr results into the
|
|
|
|
|
+ // WP search-results page. See js/til-search.js for the full removal checklist. ---
|
|
|
|
|
+ if ( is_search() ) {
|
|
|
|
|
+ wp_enqueue_script( 'lunr', get_template_directory_uri() . '/js/vendor/lunr.min.js', array(), '2.3.9', true );
|
|
|
|
|
+ wp_enqueue_script( 'dw-til-search', get_template_directory_uri() . '/js/til-search.js', array( 'lunr' ), filemtime( get_template_directory() . '/js/til-search.js' ), true );
|
|
|
|
|
+ wp_localize_script( 'dw-til-search', 'dwTilSearch', array(
|
|
|
|
|
+ 'query' => get_search_query(),
|
|
|
|
|
+ 'index' => home_url( '/til/lunr-index.json' ),
|
|
|
|
|
+ 'docs' => home_url( '/til/search-doc.json' ),
|
|
|
|
|
+ 'maxHits' => 10,
|
|
|
|
|
+ ) );
|
|
|
|
|
+ }
|
|
|
|
|
+ // --- end TIL search ---
|
|
|
wp_enqueue_script( '_s_backbone-loop', get_template_directory_uri() . '/js/loop.js', array( 'jquery', 'backbone', 'underscore', 'wp-api' ), '1.0', true );
|
|
wp_enqueue_script( '_s_backbone-loop', get_template_directory_uri() . '/js/loop.js', array( 'jquery', 'backbone', 'underscore', 'wp-api' ), '1.0', true );
|
|
|
$queried_object = get_queried_object();
|
|
$queried_object = get_queried_object();
|
|
|
$local = array(
|
|
$local = array(
|