Onebox: rewrite Github links to specific commit?


(Jens Maier) #1

In this old topic from two years ago (Only 21 “likes” allowed in 24 hours?), a post includes a link to Github, citing a specific line in one of Discourse’s source files.

However, time has moved on and what was once helpful is now quite confusing as the lines shown are not related to the topic at all.

Should onebox’d Github links be rewritten (on submitting a post or asynchronously, similar to when linked images are downloaded and lightboxed), so that the link points to a specific commit instead of a movable target?


(Jacob Chapel) #2

This is a hard problem and is more of an issue for Github.

What if in the time the user links the lines, a new commit is made invalidating the original link. How would Onebox know that?


(Jens Maier) #3

The onebox gem wouldn’t. A background task in Discourse needs to do the heavy lifting. Using the commit that is closest but not after the post revision’s created_at timestamp where the unresolved Github link was first introduced should be close enough.

For instance, regarding the link in question, this commit would apply:


(Régis Hanol) #4

I don’t know if that’s easy or not but that sure would be a good idea. We hate links that break over time.


(Claus Strasburger) #5

While that kind of magic would be quite helpful in most cases, I could imagine posts where either the author did not intend to link to a specific line (but got the link from somewhere else, or clicked on the line), but actually the file (or that line in the file), in the latest version.

Maybe a hint in a popover:

Would you like to use a permalink instead? [learn more]

Where the learn more button expands the popup and explains a bit about permalinks.


(Jens Maier) #6

True, so the rewriting should probably only catch links that include any line anchors /#L\d+(-L\d+)?$/


(Claus Strasburger) #7

(I clarified my post above)
It could still happen that an author got a link from somewhere (the code search, for example) and just copied it verbatim. That would create another instance where the author might not actually want to link to the line, but point to the class or similar.


(Jens Maier) #8

Well, first of all, I don’t think this even applies since we’re only discussing oneboxed links. Oneboxes get embedded into the post, the user can see this happen in the post preview pane, embedded content should not change over time.

If the link is not oneboxed, it is not rewritten at all, so you can always link to a given file at latest commit by surrounding that link with text or giving it a label different from the link target.

By the way… the Github code search emits links to specific commits. :wink:


(Claus Strasburger) #9

Right, I forgot the user will instantly see the oneboxed response, and the fact that oneboxes shouldn’t change (I guess noone expects them to) is a good point.

I withdraw my concerns :grinning: