Skip to content

Commit c967173

Browse files
authoredAug 11, 2024··
fix: preserve post-badge paragraph in README.md migration (#1609)
## PR Checklist - [x] Addresses an existing open issue: fixes #1159 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Adds a special case for a README.md already having a CTA-style project logo image. 💖
1 parent 2d59a38 commit c967173

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed
 

‎script/__snapshots__/migrate-test-e2e.ts.snap

-10
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,6 @@ exports[`expected file changes > .prettierignore 1`] = `
3939
exports[`expected file changes > README.md 1`] = `
4040
"--- a/README.md
4141
+++ b/README.md
42-
@@ ... @@
43-
44-
<img align="right" alt="Project logo: the TypeScript blue square with rounded corners, but a plus sign instead of 'TS'" src="./docs/create-typescript-app.png">
45-
46-
-\`create-typescript-app\` is a one-stop-shop solution to set up a new or existing repository with the latest and greatest TypeScript tooling.
47-
-It includes options not just for building and testing but also GitHub repository templates, contributor recognition, automated release management, and more.
48-
-
49-
## Getting Started
50-
51-
First make sure you have the following installed:
5242
@@ ... @@ Thanks! 💖
5343
5444
<!-- ALL-CONTRIBUTORS-LIST:END -->

‎src/steps/writeReadme/findIntroSectionClose.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,24 @@ Next line.
4242
`,
4343
14,
4444
],
45-
])("%s", (contents, expected) => {
45+
[
46+
`<h1 align="center">Title</h1>
47+
48+
<p align="center">Description.</p>
49+
50+
<p align="center">
51+
(existing badges)
52+
</p>
53+
54+
<img align="right" alt="Project logo: ..." src="./logo.png">
55+
56+
First intro text.
57+
58+
## Getting Started
59+
`,
60+
173,
61+
],
62+
])("%o", (contents, expected) => {
4663
expect(findIntroSectionClose(contents)).toEqual(expected);
4764
});
4865
});

‎src/steps/writeReadme/findIntroSectionClose.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { existingBadgeMatcherCreators } from "./findExistingBadges.js";
22

33
export function findIntroSectionClose(contents: string) {
4-
// Highest priority match: an h2, presumably following badges
4+
// Highest priority: after an existing create-typescript-app-style logo
5+
const projectLogoMatch =
6+
/<img align="right" alt="Project logo.+" src=".+">/.exec(contents);
7+
if (projectLogoMatch) {
8+
return contents.indexOf("\n", projectLogoMatch.index) + 2;
9+
}
10+
11+
// Next: before a first code block or h2, presumably following badges
512
const indexOfH2OrCodeBlock = contents.search(/## |<\s*h2|```/);
613

714
if (indexOfH2OrCodeBlock !== -1) {
815
return indexOfH2OrCodeBlock - 2;
916
}
1017

11-
// Failing that, if any badges are found, go after the last of them
18+
// Failing those, if any badges are found, go after the last of them
1219
for (const createMatcher of existingBadgeMatcherCreators) {
1320
const lastMatch = [...contents.matchAll(createMatcher())].at(-1);
1421

0 commit comments

Comments
 (0)
Please sign in to comment.