File size: 707 Bytes
0ad74ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
const RE_HEADING = /^(#\s*)(.+)$/m;
export function inject(text: string): [string | false, string | false] {
const trimmed_text = text.trim();
const heading_match = trimmed_text.match(RE_HEADING);
if (!heading_match) {
return [false, trimmed_text || false];
}
const [full_match, , heading_content] = heading_match;
const _heading = heading_content.trim();
if (trimmed_text === full_match) {
return [_heading, false];
}
const heading_end_index =
heading_match.index !== undefined
? heading_match.index + full_match.length
: 0;
const remaining_text = trimmed_text.substring(heading_end_index).trim();
const _paragraph = remaining_text || false;
return [_heading, _paragraph];
}
|