plainText function
Reduces markdown to searchable prose.
Implementation
String plainText(String markdown) {
var text = markdown;
// Component tags (<Info>, <CardGrid …>) and raw HTML carry no query terms.
text = text.replaceAll(RegExp('<!--.*?-->', dotAll: true), ' ');
text = text.replaceAll(RegExp(r'</?[A-Za-z][\w-]*(\s[^<>]*)?/?>'), ' ');
text = text.replaceAllMapped(
RegExp(r'!\[([^\]]*)\]\([^)]*\)'),
(m) => m.group(1)!,
);
text = text.replaceAllMapped(
RegExp(r'\[([^\]]+)\]\([^)]*\)'),
(m) => m.group(1)!,
);
// Reference-style links: keep the label, drop the key.
text = text.replaceAllMapped(
RegExp(r'\[([^\]]+)\]\[[^\]]*\]'),
(m) => m.group(1)!,
);
text = text.replaceAll(
RegExp(r'^\[[^\]]+\]:\s*\S+\s*$', multiLine: true),
'',
);
text = text.replaceAll(RegExp(r'^\s{0,3}>\s?', multiLine: true), '');
text = text.replaceAll(RegExp(r'^\s*[-*+]\s+', multiLine: true), '');
text = text.replaceAll(
RegExp(
r'^\s*\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)*\|?\s*$',
multiLine: true,
),
'',
);
text = text.replaceAll('|', ' ');
// Underscores survive: `snake_case` identifiers and file names are exactly
// what people search for, and `_emphasis_` is not a style docs prose uses.
text = text.replaceAll(RegExp('[`*#]'), '');
return text.replaceAll(RegExp(r'\s+'), ' ').trim();
}