fix: close three README and version gaps found in review
A README could borrow the site's own classes (card__link::after is a full-page overlay), so classes are now limited to what shiki writes. <source srcset> was not rebased, which broke the common dark-mode logo. Yanked JSR versions were listed as current; the registry hides them and so does the listing now. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+52
-2
No files matched your search
@@ -37,10 +37,35 @@ export function cleanReadme(html, base) {
|
|||||||
allowedStyles: {
|
allowedStyles: {
|
||||||
'*': { color: [HEX], 'background-color': [HEX], 'overflow-x': [/^auto$/] },
|
'*': { color: [HEX], 'background-color': [HEX], 'overflow-x': [/^auto$/] },
|
||||||
},
|
},
|
||||||
|
// Only the classes shiki writes, spelled out. A README that could use any class could borrow the
|
||||||
|
// site's own (`card__link::after` is a full-page overlay) and draw over the page that way.
|
||||||
|
// `github-dark` is Astro's default theme; change it here if astro.config.mjs picks another.
|
||||||
|
allowedClasses: {
|
||||||
|
pre: ['astro-code', 'github-dark'],
|
||||||
|
code: [/^language-[a-z0-9+#-]+$/],
|
||||||
|
span: ['line'],
|
||||||
|
},
|
||||||
allowedSchemes: ['http', 'https', 'mailto'],
|
allowedSchemes: ['http', 'https', 'mailto'],
|
||||||
transformTags: {
|
transformTags: {
|
||||||
a: (tagName, attribs) => ({ tagName, attribs: rebase(attribs, 'href', base) }),
|
a: (tagName, attribs) => ({ tagName, attribs: rebase(attribs, 'href', base) }),
|
||||||
img: (tagName, attribs) => ({ tagName, attribs: rebase(attribs, 'src', base) }),
|
img: (tagName, attribs) => ({ tagName, attribs: rebase(attribs, 'src', base) }),
|
||||||
|
// `srcset` is a comma-separated list of "url descriptor" pairs; only the url moves.
|
||||||
|
source: (tagName, attribs) =>
|
||||||
|
'srcset' in attribs
|
||||||
|
? {
|
||||||
|
tagName,
|
||||||
|
attribs: {
|
||||||
|
...attribs,
|
||||||
|
srcset: attribs.srcset
|
||||||
|
.split(',')
|
||||||
|
.map((candidate) => {
|
||||||
|
const [url, ...descriptor] = candidate.trim().split(/\s+/);
|
||||||
|
return [absolute(url, base), ...descriptor].join(' ');
|
||||||
|
})
|
||||||
|
.join(', '),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: { tagName, attribs },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -73,6 +73,11 @@ export function isEsmOnly({ type, main, exports }) {
|
|||||||
return !cjs(main) && !cjs(exports);
|
return !cjs(main) && !cjs(exports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** JSR's version list minus yanked ones — the registry hides those, so the listing must too. */
|
||||||
|
export function publishedVersions(items) {
|
||||||
|
return items.filter((item) => !item.yanked).map((item) => item.version);
|
||||||
|
}
|
||||||
|
|
||||||
/** npm stores this literal string when a package ships no README. */
|
/** npm stores this literal string when a package ships no README. */
|
||||||
export function npmReadme(packument) {
|
export function npmReadme(packument) {
|
||||||
const readme = packument.readme ?? '';
|
const readme = packument.readme ?? '';
|
||||||
@@ -126,7 +131,7 @@ export async function readJsr(name) {
|
|||||||
const info = await getJson(base, `jsr ${name}`);
|
const info = await getJson(base, `jsr ${name}`);
|
||||||
const listing = await getJson(`${base}/versions`, `jsr ${name} versions`);
|
const listing = await getJson(`${base}/versions`, `jsr ${name} versions`);
|
||||||
// `items`, not `latestVersion`. See newestVersion.
|
// `items`, not `latestVersion`. See newestVersion.
|
||||||
const versions = (listing.items ?? []).map((item) => item.version);
|
const versions = publishedVersions(listing.items ?? []);
|
||||||
const newest = newestVersion(versions);
|
const newest = newestVersion(versions);
|
||||||
const dependencies = newest ? await getJson(`${base}/versions/${newest}/dependencies`, `jsr ${name} deps`) : [];
|
const dependencies = newest ? await getJson(`${base}/versions/${newest}/dependencies`, `jsr ${name} deps`) : [];
|
||||||
const meta = newest ? await getJson(`${base}/versions/${newest}`, `jsr ${name} ${newest}`) : {};
|
const meta = newest ? await getJson(`${base}/versions/${newest}`, `jsr ${name} ${newest}`) : {};
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { test } from 'node:test';
|
import { test } from 'node:test';
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
|
|
||||||
import { isEsmOnly, byNewest, npmReadme } from './registry.mjs';
|
import { isEsmOnly, byNewest, npmReadme, publishedVersions } from './registry.mjs';
|
||||||
import { cleanReadme } from './readme.mjs';
|
import { cleanReadme } from './readme.mjs';
|
||||||
|
|
||||||
test('isEsmOnly: type module with ESM exports passes', () => {
|
test('isEsmOnly: type module with ESM exports passes', () => {
|
||||||
@@ -77,3 +77,23 @@ test('cleanReadme: an <a> without href does not become a link', () => {
|
|||||||
const html = cleanReadme('<a name="x">t</a><img alt="n">', BASE);
|
const html = cleanReadme('<a name="x">t</a><img alt="n">', BASE);
|
||||||
assert.doesNotMatch(html, /href|src/);
|
assert.doesNotMatch(html, /href|src/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('cleanReadme: our own class names cannot be borrowed, shiki ones stay', () => {
|
||||||
|
const html = cleanReadme(
|
||||||
|
'<span class="card__link">x</span><span class="version">y</span><pre class="astro-code github-dark"><code class="language-ts"><span class="line">z</span></code></pre>',
|
||||||
|
BASE,
|
||||||
|
);
|
||||||
|
assert.doesNotMatch(html, /card__link|class="version"/);
|
||||||
|
assert.match(html, /class="astro-code github-dark"/);
|
||||||
|
assert.match(html, /class="line"/);
|
||||||
|
assert.match(html, /class="language-ts"/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cleanReadme: <source srcset> points at the published version, descriptors kept', () => {
|
||||||
|
const html = cleanReadme('<picture><source media="(prefers-color-scheme: dark)" srcset="docs/dark.svg, docs/dark@2x.svg 2x"><img src="docs/l.svg"></picture>', BASE);
|
||||||
|
assert.match(html, /srcset="https:\/\/jsr\.io\/@green-tea\/jwt\/26\.9\.0-beta\.0\/docs\/dark\.svg, https:\/\/jsr\.io\/@green-tea\/jwt\/26\.9\.0-beta\.0\/docs\/dark@2x\.svg 2x"/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('publishedVersions: yanked JSR versions are not listed', () => {
|
||||||
|
assert.deepEqual(publishedVersions([{ version: '1.0.0', yanked: false }, { version: '1.0.1', yanked: true }]), ['1.0.0']);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user