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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ to external servers.

[![npm version](https://img.shields.io/npm/v/strapi-plugin-magic-mark.svg)](https://www.npmjs.com/package/strapi-plugin-magic-mark)
[![npm downloads](https://img.shields.io/npm/dm/strapi-plugin-magic-mark.svg)](https://www.npmjs.com/package/strapi-plugin-magic-mark)
[![Test & Verify](https://github.com/Schero94/Magicmark/actions/workflows/test.yml/badge.svg)](https://github.com/Schero94/Magicmark/actions/workflows/test.yml)
[![Strapi 5.57+](https://img.shields.io/badge/Strapi-5.57%2B-4945ff.svg)](#compatibility)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Schero94/Magicmark/blob/main/LICENSE)
[![CI build status](https://img.shields.io/github/actions/workflow/status/Schero94/Magicmark/test.yml?branch=main&label=CI&logo=github)](https://github.com/Schero94/Magicmark/actions/workflows/test.yml?query=branch%3Amain)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Schero94/Magicmark/blob/main/LICENSE)
[![Strapi 5.57+](https://img.shields.io/badge/Strapi-5.57%2B-4945FF.svg)](https://github.com/Schero94/Magicmark#compatibility)
[![Documentation site](https://img.shields.io/badge/docs-project%20site-6d4aff.svg)](https://schero94.github.io/Magicmark/)

Website: [schero94.github.io/Magicmark](https://schero94.github.io/Magicmark/) ·
npm: [strapi-plugin-magic-mark](https://www.npmjs.com/package/strapi-plugin-magic-mark) ·
Expand Down
39 changes: 39 additions & 0 deletions docs/site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,45 @@ <h1 id="page-title">Save the view. Open it again with one click.</h1>
<a class="button" href="https://github.com/Schero94/Magicmark">Source on GitHub</a>
</div>
<code class="hero-install">npm install strapi-plugin-magic-mark</code>
<p class="shields" aria-label="Project status badges">
<a href="https://www.npmjs.com/package/strapi-plugin-magic-mark"
><img
src="https://img.shields.io/npm/v/strapi-plugin-magic-mark.svg"
alt="npm version"
height="20"
/></a>
<a href="https://www.npmjs.com/package/strapi-plugin-magic-mark"
><img
src="https://img.shields.io/npm/dm/strapi-plugin-magic-mark.svg"
alt="npm downloads"
height="20"
/></a>
<a
href="https://github.com/Schero94/Magicmark/actions/workflows/test.yml?query=branch%3Amain"
><img
src="https://img.shields.io/github/actions/workflow/status/Schero94/Magicmark/test.yml?branch=main&amp;label=CI&amp;logo=github"
alt="CI build status"
height="20"
/></a>
<a href="https://github.com/Schero94/Magicmark/blob/main/LICENSE"
><img
src="https://img.shields.io/badge/License-MIT-blue.svg"
alt="License: MIT"
height="20"
/></a>
<a href="https://github.com/Schero94/Magicmark#compatibility"
><img
src="https://img.shields.io/badge/Strapi-5.57%2B-4945FF.svg"
alt="Strapi 5.57+"
height="20"
/></a>
<a href="https://schero94.github.io/Magicmark/"
><img
src="https://img.shields.io/badge/docs-project%20site-6d4aff.svg"
alt="Documentation site"
height="20"
/></a>
</p>
<div class="badges" aria-label="Compatibility">
<span class="badge">Strapi 5.57+ (tested on 5.61)</span>
<span class="badge">Node 22 and 24</span>
Expand Down
16 changes: 16 additions & 0 deletions docs/site/site-extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,19 @@ details p {
width: 2rem;
height: 2rem;
}

/* Project status badges in the hero: same row as the README. */
.shields {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
margin: 1.25rem 0 0;
}
.shields a {
display: inline-flex;
}
.shields img {
display: block;
width: auto;
height: 20px;
}
153 changes: 153 additions & 0 deletions tests/readme-badges.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const { existsSync, readFileSync } = require('node:fs');
const path = require('node:path');

const root = path.resolve(__dirname, '..');

/**
* Badge row contract. Every plugin README starts with the same six shields.io badges in the
* same order (npm version, monthly downloads, CI, license, Strapi floor, docs site). The README
* ships in the npm package and is rendered on npmjs.com and the Strapi marketplace, which do not
* resolve relative URLs, so every badge image and link is absolute. The Strapi badge must state
* the floor of the @strapi/strapi peer dependency and the npm badges must name this package.
*/

const WORKFLOW = 'test.yml';
const COMPAT_ANCHOR = 'compatibility';

const read = (file) => readFileSync(path.join(root, file), 'utf8');
const pkg = JSON.parse(read('package.json'));
const readme = read('README.md');
const slug = pkg.repository.url.match(/github\.com\/([^/]+\/[^/.]+)/)[1];
const BADGE_LINE = /^\[!\[([^\]]+)\]\((\S+)\)\]\((\S+)\)$/;

/**
* Reads the first block of consecutive badge lines above the first "## " heading.
*
* @returns {{ alt: string, img: string, href: string }[]} The badges in README order.
*/
function badgeRow() {
const lines = readme.split('\n');
const end = lines.findIndex((line) => line.startsWith('## '));
const head = lines.slice(0, end === -1 ? lines.length : end);
const start = head.findIndex((line) => BADGE_LINE.test(line));
const row = [];
for (let i = start; i >= 0 && i < head.length && BADGE_LINE.test(head[i]); i += 1) {
const [, alt, img, href] = head[i].match(BADGE_LINE);
row.push({ alt, img, href });
}
return row;
}

/**
* Turns the @strapi/strapi peer range into the floor shown on the badge, e.g. "^5.24.0" -> "5.24".
*
* @returns {string} The supported Strapi floor.
*/
function strapiFloor() {
const range = pkg.peerDependencies['@strapi/strapi'];
const match = range.match(/^(?:\^|~|>=)?\s*(\d+)\.(\d+)\.(\d+)/);
assert.ok(match, `unexpected @strapi/strapi peer range: ${range}`);
return match[3] === '0' ? `${match[1]}.${match[2]}` : `${match[1]}.${match[2]}.${match[3]}`;
}

const shield = (text) => encodeURIComponent(text.replace(/-/g, '--').replace(/_/g, '__'));

test('README starts with the six-badge row in the shared order', () => {
const row = badgeRow();
assert.deepEqual(
row.map((badge) => badge.alt),
[
'npm version',
'npm downloads',
'CI build status',
`License: ${pkg.license}`,
`Strapi ${strapiFloor()}+`,
'Documentation site',
]
);
});

test('every badge image and link is an absolute https URL', () => {
for (const { alt, img, href } of badgeRow()) {
assert.match(img, /^https:\/\/img\.shields\.io\//, `${alt}: image must be a shields.io URL`);
assert.match(href, /^https:\/\//, `${alt}: link must be absolute`);
}
});

test('npm badges point at this package', () => {
const [version, downloads] = badgeRow();
const page = `https://www.npmjs.com/package/${pkg.name}`;
assert.equal(version.img, `https://img.shields.io/npm/v/${pkg.name}.svg`);
assert.equal(version.href, page);
assert.equal(downloads.img, `https://img.shields.io/npm/dm/${pkg.name}.svg`);
assert.equal(downloads.href, page);
});

test('CI badge shows a workflow that runs on main', () => {
const ci = badgeRow()[2];
assert.equal(
ci.img,
`https://img.shields.io/github/actions/workflow/status/${slug}/${WORKFLOW}?branch=main&label=CI&logo=github`
);
assert.equal(
ci.href,
`https://github.com/${slug}/actions/workflows/${WORKFLOW}?query=branch%3Amain`
);
const workflow = read(path.join('.github/workflows', WORKFLOW));
assert.match(workflow, /^\s+push:/m, `${WORKFLOW} must run on push`);
assert.doesNotMatch(
workflow,
/branches-ignore:\s*\n\s*-\s*main\b/,
`${WORKFLOW} must run on main`
);
});

test('license badge matches package.json and links to the LICENSE file', () => {
const license = badgeRow()[3];
assert.equal(license.img, `https://img.shields.io/badge/License-${shield(pkg.license)}-blue.svg`);
assert.equal(license.href, `https://github.com/${slug}/blob/main/LICENSE`);
assert.ok(existsSync(path.join(root, 'LICENSE')), 'LICENSE file is missing');
});

test('Strapi badge states the @strapi/strapi peer floor and links to the compatibility section', () => {
const strapi = badgeRow()[4];
assert.equal(
strapi.img,
`https://img.shields.io/badge/Strapi-${shield(`${strapiFloor()}+`)}-4945FF.svg`
);
assert.equal(strapi.href, `https://github.com/${slug}#${COMPAT_ANCHOR}`);
const headings = readme
.split('\n')
.filter((line) => /^#{2,3} /.test(line))
.map((line) =>
line
.replace(/^#+ /, '')
.trim()
.toLowerCase()
.replace(/[^\w\- ]/g, '')
.replace(/ /g, '-')
);
assert.ok(headings.includes(COMPAT_ANCHOR), `README has no "${COMPAT_ANCHOR}" heading`);
});

test('docs badge links to the project site', () => {
const docs = badgeRow()[5];
assert.equal(docs.img, 'https://img.shields.io/badge/docs-project%20site-6d4aff.svg');
assert.equal(docs.href, pkg.homepage);
assert.match(pkg.homepage, /^https:\/\/[\w-]+\.github\.io\/[\w-]+\/$/);
});

test('Pages hero shows the same badge row as the README', () => {
const html = read('docs/site/index.html');
const block = html.match(/<p class="shields"[^>]*>([\s\S]*?)<\/p>/);
assert.ok(block, 'docs/site/index.html has no shields row');
const unescape = (value) => value.replace(/&amp;/g, '&');
const site = [
...block[1].matchAll(/<a\s+href="([^"]+)"\s*>\s*<img\s+src="([^"]+)"\s+alt="([^"]+)"/g),
].map(([, href, img, alt]) => ({ alt, img: unescape(img), href: unescape(href) }));
assert.deepEqual(site, badgeRow());
});
Loading