-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add truncation for image URLs #21242
Add truncation for image URLs #21242
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @isabellachen, thanks for your first contribution!
To be honest, while I kind of understand what #2110 is trying to describe as problematic, it's still not entirely clear to me what it is that is special about media in particular that we would want to apply this treatment. Is there some prior art we can reference for this being a convention in other applications? Is it something where we don't need to target "media" files specifically, but rather any URL which has a file extension, if the idea is that it may be important to put focus on the filename + file extension of the URL?
@@ -19,5 +19,26 @@ export function filterURLForDisplay( url ) { | |||
return filteredURL.replace( '/', '' ); | |||
} | |||
|
|||
// Prettify image urls | |||
const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests pass if I change this to...
const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/g; | |
const mediaRegexp = /\.(?:jpg|jpeg|gif|png|svg)/g; |
I'd wonder then:
- Can we remove it?
- Or, are we missing a test case to protect against whatever its purpose for existing is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks safe to check only for extensions but that would include all kinds of media extensions (audio and video too)
@@ -19,5 +19,26 @@ export function filterURLForDisplay( url ) { | |||
return filteredURL.replace( '/', '' ); | |||
} | |||
|
|||
// Prettify image urls | |||
const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to be matching this globally? Are we expecting multiple matches?
const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/g; | |
const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/; |
const colon = ':'; | ||
const period = '.'; | ||
const query = '?'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these serving much purpose? A few points I'd make:
- In the context of their relevance to this function, they're only relevant within the
if
condition. While it's not much of a problem in this case, in other instances if these assignments were non-trivial function executions, it could cause some wasted resources if thatif
condition is not matched. This is quite similar to what we have enforced in the customno-unused-vars-before-return
ESLint rule. - But in any case, to me they appear to fall under the definition of a constant, and as such, would be best to rename and move to the top-most scope (reference documentation).
- Even still, I'm not clear on what their purpose is. And if it's related to matching ports and query strings, it would be good if we could avoid using string manipulation to work with these values, if we can instead rely on using a
URL
constructor and one of the properties of its instance (pathname, etc).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed using
let url = new URL('https://developer.mozilla.org/example.mov');
will return a nice object with all the elements decomposed like this:
hash: ""
host: "developer.mozilla.org"
hostname: "developer.mozilla.org"
href: "https://developer.mozilla.org/example.mov"
origin: "https://developer.mozilla.org"
password: ""
pathname: "/example.mov"
port: ""
protocol: "https:"
search: ""
searchParams: URLSearchParams { }
username: ""
so less need to split/join strings.
if ( tokens[ 0 ].includes( period ) ) { | ||
tokens = tokens[ 0 ].split( period ); | ||
} | ||
const prettifiedImageURL = tokens[ 0 ] + '...' + imageToken; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems an ellipsis character …
would be most appropriate here.
Related:
- https://en.wikipedia.org/wiki/Ellipsis
Lines 88 to 92 in 7e964a8
{ selector: 'CallExpression[callee.name=/^(__|_x|_n|_nx)$/] Literal[value=/\\.{3}/]', message: 'Use ellipsis character (…) in place of three dots', },
@aduth Thanks for the review. I take your point about targeting any URL with a file extension, maybe I should consider this? As for your question about this being a convention... I don't think I've seen the proposed solution elsewhere. Also, if the file name is even a bit long e.g. cape-verde-santo-antao-1234.jpg you end up with cape-verde... so you don't get to see the extension. Maybe @draganescu can provide an opinion on whether to target all URLs with file extensions and if its more important to preserve the start or the end. I will look at implementing your feedback over the coming weekend :) |
Howdy @isabellachen and thanks for this work!
@noisysocks who had the original comment for this issue might chime in with more detail, if any. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe, if the need looks justified that perhaps the MediaReplaceFlow
component should implement this as it seems something specific to that workflow. cc: @noisysocks
@@ -19,5 +19,26 @@ export function filterURLForDisplay( url ) { | |||
return filteredURL.replace( '/', '' ); | |||
} | |||
|
|||
// Prettify image urls | |||
const mediaRegexp = /([\w|:])*\.(?:jpg|jpeg|gif|png|svg)/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks safe to check only for extensions but that would include all kinds of media extensions (audio and video too)
const colon = ':'; | ||
const period = '.'; | ||
const query = '?'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed using
let url = new URL('https://developer.mozilla.org/example.mov');
will return a nice object with all the elements decomposed like this:
hash: ""
host: "developer.mozilla.org"
hostname: "developer.mozilla.org"
href: "https://developer.mozilla.org/example.mov"
origin: "https://developer.mozilla.org"
password: ""
pathname: "/example.mov"
port: ""
protocol: "https:"
search: ""
searchParams: URLSearchParams { }
username: ""
so less need to split/join strings.
@draganescu Thanks for your feedback! Very handy, this use URL(). I also did think the code might have to be moved out of this file (filter-url-for-display.js) to the MediaReplaceFlow component. The MediaReplaceFlow component is using another component to display links though, so I'll probably have to add a prop on it so it knows to display media links differently. Waiting on @noisysocks for further input, otherwise will take this approach :) |
The prior art is helpful, for sure 👍 I do wonder if this is more of a behavior of how to treat URLs generally when confined to a restricted width, even further away from the idea of this being specific to media. There's some relevant discussion on this topic:
I think it becomes trickier when you try to consider which URLs should be truncated. If we have the available space, it probably shouldn't be necessary to truncate? How would it be configured? One approach is that There's a separate problem of "How does the media replace dropdown menu know what to use for |
Hello, @isabellachen Thanks so much for working on this. It looks like others were interested in resolving this issue because there's PR that got merged and addressed the same area of work. I’m going to close this out as a result. |
Description
Fixes #21100
Truncate displayed media urls as outlined in Issue 21100
How has this been tested?
npm run test-unit
Does this change affect other areas of code?
If a link links directly to an image, the link displayed will be truncated
Display of non-image links are not affected
Types of changes
Checklist: