-
Notifications
You must be signed in to change notification settings - Fork 66
Normalize Sample Code URLs #979
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
Normalize Sample Code URLs #979
Conversation
| }, | ||
| methods: { | ||
| normalizeUrl(url) { | ||
| if (!this.linksToAsset || isAbsoluteUrl(url)) return url; |
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 even need to check for special flags or absolute URLs as a special case here? I think the logic should pretty much match up with the logic in components for image and video assets, which always "normalize" the URLs. I think the normalizePath function should already be a noop for absolute URLs, and I don't think there's any special logic for assets vs documents with regards to a custom base path.
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.
For the normalizePath function to work, the path needs to be relative. This means that we need to normalize the relative path first, to make sure that the normalizePath function reads it as a relative path.
An example is the simpleRelativePath const that I wrote in the tests, which value is foo/bar:
foo/bar needs to be normalize as a relative path to become /foo/bar instead.
Otherwise normalizePath will think that foo/bar is an absolute URL and it won't prefix the baseUrl.
The reason why I'm exiting early in the case of being a real absolute URL (by using the isAbsoluteUrl function) is that, if we pass an absolute URL to normalizeRelativePath, it will turn it into a relative path like /website.com/samplecode and then normalizePath will prefix that path as baseUrl/website.com/samplecode. We want to prevent this scenario.
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.
An alternative to this is to use the isAbsoluteUrl function within the normalizeRelativePath, so we will never accidentally turn an absolute URL into a relative path.
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.
The reason why I'm exiting early in the case of being a real absolute URL (by using the
isAbsoluteUrlfunction) is that, if we pass an absolute URL tonormalizeRelativePath, it will turn it into a relative path like/website.com/samplecodeand thennormalizePathwill prefix that path asbaseUrl/website.com/samplecode. We want to prevent this scenario.
I don't think that's an issue for the existing normalizePath function or we would have hit similar issues with external image/video assets where we already call this function on all URLs.
Here's an example unit test showing the expected behavior for this function when called with an absolute URL:
| it('does not change, if passed a url', () => { |
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.
The test is showing the expected behaviour for absolute URLs (like website.com/samplecode), but by normalising the relative path it adds a slash in the URL (like /website.com/samplecode) and we want to prevent this.
This works for images and videos, because we expect the relative path to always be correct. So we don't normalise the relative path.
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.
The test is showing the expected behaviour for absolute URLs (like
website.com/samplecode), but by normalising the relative path it adds a slash in the URL (like/website.com/samplecode) and we want to prevent this.
An absolute URL should have a scheme like https://website.com/samplecode.
This works for images and videos, because we expect the relative path to always be correct. So we don't normalise the relative path.
We do already normalize relative paths for images/videos in that function by prepending the base URL (example). Absolute URLs with a scheme would be unaffected by this normalizePath function as shown in the above linked unit test.
I think the confusion is that we also have a different normalizeRelativePath function (seen below in those tests) which normalizes relative paths to all be root relative (some/path => /some/path).
We can utilize both here as you've done, although it's slightly unfortunate if we're getting different styles of relative paths for sample code when compared to images/videos in the JSON.
It prefixes the URL with the baseUrl value if the path is relative and the baseUrl is provided. If the path is absolute or the baseUrl is not provided, it does not prefixes the URL.
We only want to normalize the URL in the case that it links to an asset, that's why in this commit we introduce a new boolean prop for the CallToActionButton called `linksToAsset`.
Now that we have the prop linksToAsset available for CallToActionButton components, we want to pass it as true for the download button used for Sample Codes, since the URL links to an asset.
We want to use this function to check if urls are absolute or not.
For the normalizePath function to work, the path needs to be relative. This means that we need to normalize the relative path first, to make sure that the normalizePath function reads it as a relative path. For this, we should use the function isAbsoluteUrl to see if the url is absolute or not.
We want to make sure that simple-relative paths like 'foo/bar' also work when prefixing the baseUrl.
e3ae6c5 to
f910276
Compare
|
@swift-ci test |
Bug/issue #165347857, if applicable:
Summary
We want to normalize Sample Code URLs, by adding the
baseUrlto them if the path is relative and the baseUrl is provided. If the path is absolute or the baseUrl is not provided, it does not prefixes the URL.Because the component
CallToActionButton.vueis not only being used for Sample Code links, we created a new prop calledlinksToAssetthat will normalize the relative URLs, in the cases that they are assets, like Sample Code.Dependencies
NA
Testing
Steps:
baseUrlChecklist
Make sure you check off the following items. If they cannot be completed, provide a reason.
npm test, and it succeeded