dvFindMirrorBlocks function

List<DVFindBlock> dvFindMirrorBlocks(
  1. Iterable<DVFindBlock> rendered, {
  2. int maxChars = dvFindMirrorMaxChars,
})

The paragraphs a mirror is written from, cleaned.

Whitespace collapsed as the reader sees it, empty paragraphs dropped, and the total held to maxChars. Repeats are kept: the same words twice on a page are two places find can land.

Implementation

List<DVFindBlock> dvFindMirrorBlocks(
  Iterable<DVFindBlock> rendered, {
  int maxChars = dvFindMirrorMaxChars,
}) {
  final List<DVFindBlock> blocks = <DVFindBlock>[];
  int total = 0;
  for (final DVFindBlock block in rendered) {
    final String text = block.text.replaceAll(RegExp(r'\s+'), ' ').trim();
    if (text.isEmpty) continue;
    if (total + text.length > maxChars) break;
    total += text.length;
    blocks.add(DVFindBlock(text, headingLevel: block.headingLevel));
  }
  return blocks;
}