Skip to content

feat: allow description to safely include HTML tags #1820

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

Merged
merged 11 commits into from
Dec 31, 2024
Merged
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"execa": "^9.5.2",
"git-remote-origin-url": "^4.0.0",
"git-url-parse": "^16.0.0",
"html-to-text": "^9.0.5",
"image-size": "^1.2.0",
"input-from-file": "0.1.0-alpha.4",
"input-from-file-json": "0.1.0-alpha.4",
Expand Down Expand Up @@ -79,6 +80,7 @@
"@release-it/conventional-changelog": "9.0.3",
"@types/eslint-plugin-markdown": "2.0.2",
"@types/git-url-parse": "9.0.3",
"@types/html-to-text": "^9.0.4",
"@types/js-yaml": "4.0.9",
"@types/node": "22.10.2",
"@types/parse-author": "2.0.3",
Expand Down
62 changes: 62 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/next/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { readEmails } from "../shared/options/createOptionDefaults/readEmails.js
import { readFunding } from "../shared/options/createOptionDefaults/readFunding.js";
import { readGuide } from "../shared/options/createOptionDefaults/readGuide.js";
import { readPackageData } from "../shared/packages.js";
import { readFileSafe } from "../shared/readFileSafe.js";
import { tryCatchLazyValueAsync } from "../shared/tryCatchLazyValueAsync.js";
import { AllContributorsData } from "../shared/types.js";
import { readDescription } from "./readDescription.js";
Expand Down Expand Up @@ -156,6 +157,8 @@ export const base = createBase({
)?.stdout?.toString();
});

const readme = lazyValue(async () => await readFileSafe("README.md", ""));

// TODO: Make these all use take

const gitDefaults = tryCatchLazyValueAsync(async () =>
Expand Down Expand Up @@ -196,7 +199,7 @@ export const base = createBase({
author,
bin: async () => (await packageData()).bin,
contributors: allContributors,
description: async () => await readDescription(packageData),
description: async () => await readDescription(packageData, readme),
documentation,
email: async () => readEmails(npmDefaults, packageAuthor),
funding: readFunding,
Expand All @@ -220,7 +223,7 @@ export const base = createBase({
options.repository ??
(await gitDefaults())?.name ??
(await packageData()).name,
...readDefaultsFromReadme(),
...readDefaultsFromReadme(readme),
version,
};
},
Expand Down
4 changes: 3 additions & 1 deletion src/next/blocks/blockPackageJson.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as htmlToText from "html-to-text";
import removeUndefinedObjects from "remove-undefined-objects";
import sortPackageJson from "sort-package-json";
import { z } from "zod";
Expand Down Expand Up @@ -44,6 +45,7 @@ export const blockPackageJson = base.createBlock({
...options.packageData?.devDependencies,
...addons.properties.devDependencies,
};
const description = htmlToText.convert(options.description);

return {
files: {
Expand All @@ -56,7 +58,7 @@ export const blockPackageJson = base.createBlock({
dependencies: Object.keys(dependencies).length
? dependencies
: undefined,
description: options.description,
description,
devDependencies: Object.keys(devDependencies).length
? devDependencies
: undefined,
Expand Down
6 changes: 5 additions & 1 deletion src/next/blocks/blockRepositorySettings.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import * as htmlToText from "html-to-text";

import { base } from "../base.js";

export const blockRepositorySettings = base.createBlock({
about: {
name: "Repository Settings",
},
produce({ options }) {
const description = htmlToText.convert(options.description);

return {
requests: [
{
Expand All @@ -16,7 +20,7 @@ export const blockRepositorySettings = base.createBlock({
allow_rebase_merge: false,
allow_squash_merge: true,
delete_branch_on_merge: true,
description: options.description,
description,
has_wiki: false,
owner: options.owner,
repo: options.repository,
Expand Down
56 changes: 40 additions & 16 deletions src/next/readDescription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,68 @@ import { describe, expect, it, vi } from "vitest";

import { readDescription } from "./readDescription.js";

const mockSourcePackageJsonDescription = vi.fn<() => string>();
const mockSourcePackageJson = vi.fn<() => string>();

vi.mock("./blocks/sourcePackageJson", () => ({
sourcePackageJson: {
get description() {
return mockSourcePackageJsonDescription();
return mockSourcePackageJson();
},
},
}));

describe("finalize", () => {
describe("readDescription", () => {
it("returns undefined when the description matches the current package.json description", async () => {
const existing = "Same description.";

mockSourcePackageJsonDescription.mockReturnValueOnce(existing);
mockSourcePackageJson.mockReturnValueOnce(existing);

const documentation = await readDescription(() =>
Promise.resolve({
description: existing,
}),
const description = await readDescription(
() => Promise.resolve({ description: existing }),
() => Promise.resolve(""),
);

expect(documentation).toBeUndefined();
expect(description).toBeUndefined();
});

it("returns the updated description when neither description nor name match the current package.json", async () => {
const updated = "Updated description.";

mockSourcePackageJsonDescription.mockReturnValueOnce(
"Existing description",
mockSourcePackageJson.mockReturnValueOnce("Existing description");

const description = await readDescription(
() => Promise.resolve({ description: updated }),
() => Promise.resolve(""),
);

const documentation = await readDescription(() =>
Promise.resolve({
description: updated,
}),
expect(description).toBe(updated);
});

it("uses the README.md HTML description when it matches what's inferred from package.json plus HTML tags", async () => {
const plaintext = "Updated description.";
const encoded = "Updated <code>description</code>.";

mockSourcePackageJson.mockReturnValueOnce("Existing description");

const description = await readDescription(
() => Promise.resolve({ description: plaintext }),
() => Promise.resolve(`<p align="center">${encoded}</p>`),
);

expect(description).toBe(encoded);
});

it("uses the package.json description when the README.md HTML description doesn't match what's inferred from package.json plus HTML tags", async () => {
const plaintext = "Updated description.";
const encoded = "Incorrect <code>description</code>.";

mockSourcePackageJson.mockReturnValueOnce("Existing description");

const description = await readDescription(
() => Promise.resolve({ description: plaintext }),
() => Promise.resolve(`<p align="center">${encoded}</p>`),
);

expect(documentation).toBe(updated);
expect(description).toBe(plaintext);
});
});
11 changes: 11 additions & 0 deletions src/next/readDescription.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { PartialPackageData } from "../shared/types.js";
import { sourcePackageJson } from "./blocks/sourcePackageJson.js";
import { readDescriptionFromReadme } from "./readDescriptionFromReadme.js";

export async function readDescription(
getPackageData: () => Promise<PartialPackageData>,
getReadme: () => Promise<string>,
) {
const { description: inferred } = await getPackageData();
if (!inferred) {
return undefined;
}

const { description: existing } = sourcePackageJson;
const fromReadme = await readDescriptionFromReadme(getReadme);

if (fromReadme?.replaceAll(/<\s*(?:\/\s*)?\w+\s*>/gu, "") === inferred) {
return fromReadme;
}

return existing === inferred ? undefined : inferred;
}
Loading
Loading