-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathfindIntroSectionClose.ts
29 lines (23 loc) · 976 Bytes
/
findIntroSectionClose.ts
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
29
import { existingBadgeMatcherCreators } from "./findExistingBadges.js";
export function findIntroSectionClose(contents: string) {
// Highest priority: after an existing create-typescript-app-style logo
const projectLogoMatch =
/<img align="right" alt="Project logo.+" src=".+">/.exec(contents);
if (projectLogoMatch) {
return contents.indexOf("\n", projectLogoMatch.index) + 2;
}
// Next: before a first code block or h2, presumably following badges
const indexOfH2OrCodeBlock = contents.search(/## |<\s*h2|```/);
if (indexOfH2OrCodeBlock !== -1) {
return indexOfH2OrCodeBlock - 2;
}
// Failing those, if any badges are found, go after the last of them
for (const createMatcher of existingBadgeMatcherCreators) {
const lastMatch = [...contents.matchAll(createMatcher())].at(-1);
if (lastMatch?.index) {
return lastMatch.index + lastMatch[0].length + 2;
}
}
// Lastly, go for the second line altogether
return contents.indexOf("\n", 1) + 1;
}