Skip to content
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
2 changes: 2 additions & 0 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ New modules get this from the [Plaster template](https://github.com/AsBuiltRepor
- section icons in place of the emoji on known section headings, sized to the heading level (headings inside code blocks are left alone)
- badges in the brand palette: link blue `#1F5FD0` for versions, console blue `#012456` for counts (downloads, contributors), highlighter `#FFD02B` for compatibility (platform)
- central highlight swatches in place of placeholder images, in the colour the repo's report style uses (by the image's alt text: `Critical`, `Warning`, `Info` or `OK`), so a README never shows a colour the report does not use
- the title heading below the badges removed (for example `# VMware vSphere As Built Report`), because the banner already names the repo. This is the first level 1 heading before any known section; a README without one is left alone
- the X (Twitter) follow @AsBuiltReport badge removed (follow badges for other accounts are kept)

Where the pull request is already open, it commits to that branch instead. It skips READMEs that already have every change. It is a dry run unless you pass `--apply`:

Expand Down
44 changes: 43 additions & 1 deletion generator/readme-brand.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// addSectionIcons section icons in place of the emoji on known section headings
// recolourBadges shields.io badges in the brand palette
// replaceHighlightSwatches central health check highlight swatches in place of placeholder services
// removeTitleHeading the title heading, which the banner repeats
// removeFollowBadge the X (Twitter) follow @AsBuiltReport badge
// readme-banners.mjs uses these to open pull requests. readme-brand.test.mjs tests them.

import { readFileSync } from "node:fs";
Expand Down Expand Up @@ -135,6 +137,46 @@ export function unmappedPlaceholders(readme) {
return [...readme.matchAll(placeholderImage)].map((m) => m[0]);
}

// ---------- Title heading and follow badge ----------

// The banner already names the repo, so the title heading below the badges
// ("# VMware vSphere As Built Report", "# AsBuiltReport.Core") repeats it. The title is the
// first level 1 heading, as long as no known section heading comes before it. Lower level
// headings before it, such as the "community maintained" notice in some READMEs, are skipped.
// It is removed with the blank line after it.
export function removeTitleHeading(readme) {
const eol = eolOf(readme);
const lines = readme.split(/\r?\n/);
let fence = null;
for (let i = 0; i < lines.length; i++) {
const fenceMatch = lines[i].match(/^\s*(`{3,}|~{3,})/);
if (fenceMatch) {
if (!fence) fence = fenceMatch[1][0];
else if (fenceMatch[1][0] === fence) fence = null;
continue;
}
if (fence) continue;
const heading = lines[i].match(/^(#{1,6})\s+(.*?)\s*$/);
if (!heading) continue;
// Once the known sections start, there is no title to remove.
const text = heading[2].replace(leadingEmoji, "").replace(trailingEmoji, "").trim();
if (heading[2].includes("/assets/section-icons/") || iconByHeading.has(normalise(text))) return readme;
if (heading[1].length !== 1) continue;
lines.splice(i, lines[i + 1] !== undefined && lines[i + 1].trim() === "" ? 2 : 1);
return lines.join(eol);
}
return readme;
}

// The X (Twitter) follow badge for @AsBuiltReport, in its own centred paragraph.
// Follow badges for other accounts are left alone.
const followBadge = /<p align="center">\s*<a href="https?:\/\/(?:www\.)?(?:twitter|x)\.com\/AsBuiltReport\/?"[^>]*>\s*<img src="https:\/\/img\.shields\.io\/(?:twitter|x)\/follow\/AsBuiltReport[^"]*"\s*\/?>\s*<\/a>\s*<\/p>[ \t]*\r?\n?/gi;

export function removeFollowBadge(readme) {
return readme.replace(followBadge, "");
}

export function applyBrand(readme, repo) {
return replaceHighlightSwatches(recolourBadges(addSectionIcons(addBanner(readme, repo))), repo);
const branded = replaceHighlightSwatches(recolourBadges(addSectionIcons(addBanner(readme, repo))), repo);
return removeTitleHeading(removeFollowBadge(branded));
}
38 changes: 37 additions & 1 deletion generator/readme-brand.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { addBanner, addSectionIcons, recolourBadge, recolourBadges, replaceHighlightSwatches, unmappedPlaceholders, swatchUrl, applyBrand } from "./readme-brand.mjs";
import { addBanner, addSectionIcons, recolourBadge, recolourBadges, replaceHighlightSwatches, unmappedPlaceholders, swatchUrl, removeTitleHeading, removeFollowBadge, applyBrand } from "./readme-brand.mjs";

const icon = (name, size) => `<img src="https://raw.githubusercontent.com/AsBuiltReport/.github/main/assets/section-icons/${name}.svg" alt="" width="${size}" height="${size}" align="top">`;

Expand Down Expand Up @@ -88,6 +88,40 @@ test("leaves a placeholder alone when neither the style name nor the colour is k
assert.deepEqual(unmappedPlaceholders(unknown), [unknown]);
});

test("removes the title heading and the blank line after it", () => {
const readme = "</p>\n<!-- ********** DO NOT EDIT THESE LINKS ********** -->\n\n# VMware vSphere As Built Report\n\nVMware vSphere As Built Report is a PowerShell module";
assert.equal(removeTitleHeading(readme), "</p>\n<!-- ********** DO NOT EDIT THESE LINKS ********** -->\n\nVMware vSphere As Built Report is a PowerShell module");
assert.equal(removeTitleHeading("# AsBuiltReport.Core\r\n\r\nText\r\n"), "Text\r\n");
});

// Microsoft.Azure has no title: its first heading is Getting Started, which must stay.
test("keeps the first heading when it is a known section or not level 1", () => {
const azure = "Intro\n\n# :beginner: Getting Started\n\nText";
assert.equal(removeTitleHeading(azure), azure);
const iconed = `# ${icon("getting-started", 28)} Getting Started`;
assert.equal(removeTitleHeading(iconed), iconed);
const late = "## :wrench: System Requirements\n# Later Heading";
assert.equal(removeTitleHeading(late), late);
});

// Microsoft.AD, Microsoft.SCVMM, Veeam.VB365 and Veeam.VBR put a "community maintained" notice above the title.
test("skips lower level headings above the title", () => {
const readme = "#### This project is community maintained\n\n# Microsoft AD As Built Report\n\nText\n\n# :books: Sample Reports";
assert.equal(removeTitleHeading(readme), "#### This project is community maintained\n\nText\n\n# :books: Sample Reports");
});

test("ignores # lines in code blocks when finding the title", () => {
const readme = "```powershell\n# Install\n```\n# Rubrik CDM As Built Report\n\nText";
assert.equal(removeTitleHeading(readme), "```powershell\n# Install\n```\nText");
});

test("removes the follow @AsBuiltReport badge paragraph only", () => {
const badge = '<p align="center">\n <a href="https://twitter.com/AsBuiltReport" alt="Twitter">\n <img src="https://img.shields.io/twitter/follow/AsBuiltReport.svg?style=social"/></a>\n</p>\n';
assert.equal(removeFollowBadge(`</p>\n${badge}<!-- comment -->`), "</p>\n<!-- comment -->");
const other = badge.replace(/AsBuiltReport/g, "rebelinux");
assert.equal(removeFollowBadge(other), other);
});

test("adds the banner once", () => {
const readme = "<p align=\"center\">\n <a href=\"https://www.asbuiltreport.com/\" alt=\"AsBuiltReport\"></a>\n <img src='https://github.com/AsBuiltReport.png' width=\"8%\" height=\"8%\" /></a>\n</p>\nText";
const once = addBanner(readme, "AsBuiltReport.Core");
Expand All @@ -106,6 +140,8 @@ if (readmeDir) {
const after = applyBrand(before, repo);
assert.equal(applyBrand(after, repo), after, "second run changes nothing");
assert.deepEqual(unmappedPlaceholders(after), [], "no placeholder images left");
assert.ok(!/(?:twitter|x)\.com\/AsBuiltReport\b/i.test(after), "no follow @AsBuiltReport badge left");
assert.equal(removeTitleHeading(after), after, "no title heading left");
const lines = (t) => t.split(/\r?\n/);
const changed = lines(after).filter((l, i, all) => !lines(before).includes(l));
for (const line of changed) {
Expand Down
4 changes: 4 additions & 0 deletions generator/readme-prs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const commitMessage = [
"- Section icons in place of emoji on the section headings",
"- Badges in the brand palette: link blue for versions, console blue for counts, highlighter for compatibility",
"- Central health check highlight swatches in place of placeholder image services, in the report style's colours",
"- Title heading removed, as the banner names the repo",
"- X (Twitter) follow @AsBuiltReport badge removed",
"",
"The banner, icons and swatches are rendered centrally by the generator in the .github repo.",
"",
Expand All @@ -44,6 +46,8 @@ const prBody = (repo) => [
`- **Section icons:** the emoji on the section headings are replaced with the [section icon pack](https://github.com/${owner}/.github/tree/main/assets/section-icons), in the style of the feature icons on the website.`,
"- **Badges:** recoloured to the brand palette: link blue `#1F5FD0` for versions, console blue `#012456` for counts, highlighter `#FFD02B` for compatibility.",
`- **Highlight swatches** (where the README has a HealthCheck table): the colour images now come from the [central swatches](https://github.com/${owner}/.github/tree/main/assets/highlight-swatches) instead of placeholder services such as placehold.co, which break. Each swatch shows the colour this module's report style uses, so it can differ from the old image where that showed another module's colour.`,
"- **Title heading:** removed (for example `# VMware vSphere As Built Report`), because the banner already names the repo.",
"- **X follow badge:** the follow @AsBuiltReport badge is removed.",
"",
`The banner, icons and swatches are rendered by the [generator in the .github repo](https://github.com/${owner}/.github/tree/main/generator), which keeps them up to date, so this repo does not store the images. Only \`README.md\` changes.`,
"",
Expand Down
Loading