Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Relative image URLs in package READMEs are now resolved correctly #996

Merged
merged 5 commits into from
Oct 26, 2017
Merged

Relative image URLs in package READMEs are now resolved correctly #996

merged 5 commits into from
Oct 26, 2017

Conversation

ftm
Copy link
Contributor

@ftm ftm commented Sep 14, 2017

Description of the Change

When rendering package READMEs, whilst sanitizing the source image, URLs are now checked to see if they are absolute or relative and if they are relative then they are resolved appropriately. For example for non-installed plugins the base URL would be the GitHub repo however if they are installed it uses the path of the installed plugin.

Before:
relative url before

After:
relative url after

Alternate Designs

I considered attempting to match up each image in the Markdown with its counterpart on its Atom.io page or its README on GitHub and using those URLs. I chose this solution in the end as it was much simpler and in the cases I've tested still seems to work.

Benefits

Packages using relative URLs for images in their README files as recommended by GitHub will now be rendered correctly

Possible Drawbacks

Any URL scheme not recognized by the code I have written will be assumed to be a relative path so the image URL may be altered incorrectly.

Applicable Issues

The applicable issue would be here

@@ -45,13 +45,42 @@ function sanitize (html) {
checkbox.setAttribute('disabled', true)
}

let path = require('path')
let urlProtocolRegex = new RegExp("^(?:[a-z]+:)?\/\/", "i")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be a simple constructed without the RegExp constructor, as in: /^(?:[a-z]+:)?\/\//i? Then it can be inlined on L57.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest commit

for (const image of temporaryContainer.querySelectorAll('img')) {
let imageSrc = image.getAttribute('src')

let changeSrc = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For extra clarity I'd rename this to changeImageSrc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest commit

// If repoUrl is a local path
image.setAttribute('src', path.join(readmeSrcBase, imageSrc))
} else {
// If repoUrl is a URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe expound on when this can happen in the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

// If repoUrl is a local path (i.e. package is installed)
// If repoUrl is a URL (i.e. package isn't installed)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest commit

@@ -372,7 +372,16 @@ export default class PackageDetailView {
readme = fs.readFileSync(this.readmePath, {encoding: 'utf8'})
}

const readmeView = new PackageReadmeView(readme)
let readmeSrcBase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about renaming this to readmeSrc instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think that's better than I'm happy to change it. Would you want it changed in package-readme-view.js as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest commit

readmeSrcBase = this.pack.path
} else {
let repoUrl = this.packageManager.getRepositoryUrl(this.pack)
readmeSrcBase = repoUrl + '/blob/master/'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be changed to point to the last published tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if there's a simple way to do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked around a bit but it doesn't look like atom.io's API has a way of getting the latest tag. There's also the problem that master isn't guaranteed to exist (and be the default branch). Let me see if anyone else has any ideas.

if (this.pack.path) {
readmeSrcBase = this.pack.path
} else {
let repoUrl = this.packageManager.getRepositoryUrl(this.pack)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge case, but this might return undefined if the repository no longer exists or the package has been unpublished. You might want to make sure that this works in those cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, is it getRepositoryUrl() which could return undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In latest commit I've added some checks to make try and catch this

@iolsen
Copy link

iolsen commented Sep 18, 2017

We'd love to see this change go in if you can address @50Wliu's feedback.

@ftm
Copy link
Contributor Author

ftm commented Sep 18, 2017

I've addressed most of it, I'm just waiting for some clarification on a couple of points

@50Wliu
Copy link
Contributor

50Wliu commented Sep 18, 2017

Sorry, I'll try to get back to you soon!

@ftm
Copy link
Contributor Author

ftm commented Sep 18, 2017

Cool, no rush

@damieng
Copy link
Contributor

damieng commented Oct 10, 2017

I'd love to get this merged in - there seems to be some conflicting files now though - possibly because of my change from our own sanitization to dompurify.

@nathansobo
Copy link
Contributor

I'm going to attempt to resolve these conflicts. 🤞

@50Wliu
Copy link
Contributor

50Wliu commented Oct 26, 2017

@nathansobo Just so that you're aware of my last review comment that we're not sure how to resolve:
the readme location for packages that aren't installed (i.e. you're browsing in the Install view) is currently hardcoded to readmeSrc = repoUrl + '/blob/master/' instead of the last-published release tag.

@nathansobo nathansobo merged commit e962a54 into atom:master Oct 26, 2017
@nathansobo
Copy link
Contributor

Thanks for contributing this @frasertmay!

@nathansobo
Copy link
Contributor

nathansobo commented Oct 26, 2017

@50Wliu Hey, sorry, I missed your comment somehow before merging this 😬 . Guess the PR didn't auto-update? Should I roll this back? Still seems like an overall improvement to have some sort of README images though it won't be 100% due to the fact they could be deleted, master might not be there, etc.

@50Wliu
Copy link
Contributor

50Wliu commented Oct 26, 2017

Yeah, it's definitely an improvement. I don't think it needs to be reverted; I'll create a follow-up issue for it.

@ftm
Copy link
Contributor Author

ftm commented Oct 26, 2017

Thank's for sorting out the merge conflicts @nathansobo!

@50Wliu, if you're happy to sort out the URL issue in a separate issue/PR I'll delete the branch.

@Ben3eeE
Copy link
Contributor

Ben3eeE commented Oct 26, 2017

🎉

@50Wliu
Copy link
Contributor

50Wliu commented Oct 26, 2017

@frasertmay Feel free to delete the branch.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants