Show a picture whose address was typed without a scheme
CI / Typecheck, test, build (pull_request) Successful in 26s
CI / Typecheck, test, build (pull_request) Successful in 26s
`preview.redd.it/lz4drsqh0clh1.jpeg?width=1290&...` was rendering as plain text -- not a picture, and not even a link. The bare-address rule has always insisted on `https://`, so anything copied out of an address bar, where the browser hides the scheme, fell through to nothing at all. That predates the inline images from the last commit; it just did not matter until pictures started being worth placing. The rule is deliberately narrow: a host with a dot, a path, and an image extension. Widening it to every schemeless address would be the obvious move and is wrong, because a comment thread is full of dotted, slashed prose -- `src/render/post.ts` would become a link to a website in Tonga, and `node_modules/foo/bar.js` a website in Jersey. Requiring the extension costs nothing here, since the only thing worth guessing a scheme for is a picture. https is assumed. Every host that serves these redirects to it anyway. Checked both ways: the four address shapes that should become pictures do, and nine pieces of ordinary prose that must not -- file paths, a Windows drive letter, a relative path, an email address followed by a filename, a version number -- still do not. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_017nMQ2eDKnqALYhAibpTKTu
This commit is contained in:
@@ -122,3 +122,34 @@ test("an image's alt text is escaped like anything else a stranger wrote", () =>
|
||||
test('images inside a quote are still placed', () => {
|
||||
assert.match(mdi('> '), /<blockquote><p><img/);
|
||||
});
|
||||
|
||||
test('an image address typed without a scheme is still the picture', () => {
|
||||
// Which is how people type them: no https, straight from the address bar.
|
||||
const out = mdi('preview.redd.it/lz4drsqh0clh1.jpeg?width=1290&s=b27e');
|
||||
assert.ok(out.includes('<img src="https://preview.redd.it/lz4drsqh0clh1.jpeg?width=1290&s=b27e"'), out);
|
||||
});
|
||||
|
||||
test('a schemeless image address mid-sentence keeps the sentence', () => {
|
||||
const out = mdi('look at i.redd.it/x.png nice one');
|
||||
assert.ok(out.startsWith('<p>look at <img'), out);
|
||||
assert.ok(out.endsWith(' nice one</p>'), out);
|
||||
});
|
||||
|
||||
test('prose full of dots and slashes is not mistaken for an address', () => {
|
||||
// The reason this rule insists on a host, a path and an image extension.
|
||||
for (const text of [
|
||||
'the file is at src/render/post.ts',
|
||||
'see node_modules/foo/bar.js',
|
||||
'a path like ./images/cat.jpg',
|
||||
'C:/Users/x/cat.png',
|
||||
'email [email protected]/nope.jpg',
|
||||
'version 1.2.3/4.png',
|
||||
]) {
|
||||
assert.ok(!mdi(text).includes('<img'), `treated as an image: ${text}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('a schemeless address that is not an image is left alone', () => {
|
||||
// Guessing a scheme is worth it for a picture and not for prose.
|
||||
assert.equal(mdi('example.com/article'), '<p>example.com/article</p>');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user