Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve post-badge paragraph in README.md migration #1609

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions script/__snapshots__/migrate-test-e2e.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ exports[`expected file changes > .prettierignore 1`] = `
exports[`expected file changes > README.md 1`] = `
"--- a/README.md
+++ b/README.md
@@ ... @@

<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">

-\`create-typescript-app\` is a one-stop-shop solution to set up a new or existing repository with the latest and greatest TypeScript tooling.
-It includes options not just for building and testing but also GitHub repository templates, contributor recognition, automated release management, and more.
-
## Getting Started

First make sure you have the following installed:
@@ ... @@ Thanks! 💖

<!-- ALL-CONTRIBUTORS-LIST:END -->
Expand Down
19 changes: 18 additions & 1 deletion src/steps/writeReadme/findIntroSectionClose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,24 @@ Next line.
`,
14,
],
])("%s", (contents, expected) => {
[
`<h1 align="center">Title</h1>

<p align="center">Description.</p>

<p align="center">
(existing badges)
</p>

<img align="right" alt="Project logo: ..." src="./logo.png">

First intro text.

## Getting Started
`,
173,
],
])("%o", (contents, expected) => {
expect(findIntroSectionClose(contents)).toEqual(expected);
});
});
11 changes: 9 additions & 2 deletions src/steps/writeReadme/findIntroSectionClose.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { existingBadgeMatcherCreators } from "./findExistingBadges.js";

export function findIntroSectionClose(contents: string) {
// Highest priority match: an h2, presumably following badges
// 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 that, if any badges are found, go after the last of them
// 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);

Expand Down
Loading