# Onebox: rewrite Github links to specific commit?

**URL:** https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090
**Category:** Feature
**Created:** [March 7, 2015, 9:07am UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090 "2015-03-07T09:07:12Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![elberet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elberet/32/122404_2.png) [@elberet](https://meta.discourse.org/u/elberet)
#### Post date: [March 7, 2015, 9:07am UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/1 "2015-03-07T09:07:12Z")

</div>

In this old topic from two years ago ([Only 21 “likes” allowed in 24 hours?](https://meta.discourse.org/t/only-21-likes-allowed-in-24-hours/798/30)), a post includes a link to Github, citing a specific line in one of Discourse’s source files.

> [@craftsman](#):
>
> Here is the source:  
> [discourse/app/models/site\_setting.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/master/app/models/site_setting.rb#L73)

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?

---

<div class="post-metadata">

### Author: ![chapel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chapel/32/122414_2.png) [@chapel](https://meta.discourse.org/u/chapel)
#### Post date: [March 7, 2015, 9:26am UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/2 "2015-03-07T09:26:27Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![elberet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elberet/32/122404_2.png) [@elberet](https://meta.discourse.org/u/elberet)
#### Post date: [March 7, 2015, 9:41am UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/3 "2015-03-07T09:41:27Z")

</div>

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:

> <https://github.com/discourse/discourse/blob/27e1a81b345d6d87a305860c8a0b63144dab2db6/app/models/site_setting.rb#L73>

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [March 7, 2015, 5:04pm UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/4 "2015-03-07T17:04:16Z")

</div>

> [@elberet](#):
>
> 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?

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.

---

<div class="post-metadata">

### Author: ![cfstras](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cfstras/32/103104_2.png) [@cfstras](https://meta.discourse.org/u/cfstras)
#### Post date: [March 8, 2015, 1:44pm UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/5 "2015-03-08T13:44:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![elberet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elberet/32/122404_2.png) [@elberet](https://meta.discourse.org/u/elberet)
#### Post date: [March 8, 2015, 1:47pm UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/6 "2015-03-08T13:47:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cfstras](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cfstras/32/103104_2.png) [@cfstras](https://meta.discourse.org/u/cfstras)
#### Post date: [March 8, 2015, 1:50pm UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/7 "2015-03-08T13:50:27Z")

</div>

(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.

---

<div class="post-metadata">

### Author: ![elberet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elberet/32/122404_2.png) [@elberet](https://meta.discourse.org/u/elberet)
#### Post date: [March 8, 2015, 2:04pm UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/8 "2015-03-08T14:04:39Z")

</div>

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. 😉

---

<div class="post-metadata">

### Author: ![cfstras](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cfstras/32/103104_2.png) [@cfstras](https://meta.discourse.org/u/cfstras)
#### Post date: [March 8, 2015, 2:18pm UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/9 "2015-03-08T14:18:46Z")

</div>

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 😀

---

<div class="post-metadata">

### Author: ![chapoi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chapoi/32/537252_2.png) [@chapoi](https://meta.discourse.org/u/chapoi)
#### Post date: [December 4, 2025, 11:35am UTC](https://meta.discourse.org/t/onebox-rewrite-github-links-to-specific-commit/26090/10 "2025-12-04T11:35:45Z")

</div>


