\d+)/sections',
array(
'methods' => 'GET',
'callback' => static function ( $request ) {
$post = get_post( (int) $request['id'] );
if ( ! $post || PostTypes::ARTICLE !== $post->post_type ) {
return new \WP_Error( 'kdocs_not_found', __( 'Article not found.', 'karma-docs' ), array( 'status' => 404 ) );
}
$list = array();
foreach ( array_slice( Index::sections( $post ), 1 ) as $section ) {
if ( $section['anchor'] ) {
$list[] = array(
'id' => $section['anchor'],
'text' => $section['heading'],
);
}
}
return rest_ensure_response( $list );
},
'permission_callback' => array( Routes::class, 'can_edit' ),
)
);
}
/**
* Adds the dark-mode variant next to the image.
*
* @param string $html Block HTML.
* @param array $block Block.
* @return string
*/
public function image( $html, $block ) {
$dark = esc_url( (string) ( $block['attrs']['darkUrl'] ?? '' ) );
if ( '' === $dark || ! preg_match( '#]*>#i', $html, $img ) ) {
return $html;
}
$alt = preg_match( '/\salt=(["\'])(.*?)\1/i', $img[0], $m ) ? $m[2] : '';
$variant = '
';
$html = str_replace( $img[0], $img[0] . $variant, $html );
return preg_replace( '/class="wp-block-image/', 'class="wp-block-image has-dark-variant', $html, 1 );
}
/**
* One section of an article: from its heading to the next heading of the
* same or a higher level.
*
* @param int $article_id Article.
* @param string $anchor Heading id ('' = the intro before the first heading).
* @return array{title:string,html:string}|null
*/
public static function section( $article_id, $anchor ) {
$post = get_post( (int) $article_id );
if ( ! $post || PostTypes::ARTICLE !== $post->post_type || 'publish' !== $post->post_status || ! apply_filters( 'kdocs_can_view_article', true, $post ) ) {
return null;
}
$html = Content::process( do_blocks( Blocks::strip_private( (string) $post->post_content ) ), 5 )['html'];
$html = preg_replace( '#]*>.*?#s', '', $html );
$parts = preg_split( '#(