Skip to content

Commit

Permalink
Make region URLs in GitHub use ?plain=1 for HTML rendered files
Browse files Browse the repository at this point in the history
When you select a region in a file that's supported by the GitHub
markup library, the content is rendered as HTML and doesn't have
line numbers to jump to.

If you want to mark line numbers you have to use `?plain=1` in your
URL, see https://github.blog/changelog/2021-06-30-parameter-to-disable-markdown-rendering/
  • Loading branch information
dakra committed Jan 10, 2023
1 parent d81643c commit ed04688
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions browse-at-remote.el
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ By default is true."
"List of hosts where the URL protocol should be http."
:type '(repeat string))

(defcustom browse-at-remote-github-markup-extensions
'(".markdown" ".mdown" ".mkdn" ".md"
".textile"
".rdoc"
"org"
".creole"
".mediawiki" ".wiki"
".rst"
".asciidoc" ".adoc" ".asc"
".pod")
"List of file extensions where GitHub renders the content as HTML instead of raw.
See https://github.com/github/markup#markups"
:type '(repeat string))

(defun browse-at-remote--get-url-from-remote (remote-url)
"Return a plist describing REMOTE-URL."
;; If the protocol isn't specified, git treats it as an SSH URL.
Expand Down Expand Up @@ -277,11 +291,14 @@ related remote in `browse-at-remote-remote-type-regexps'."

(defun browse-at-remote--format-region-url-as-github (repo-url location filename &optional linestart lineend)
"URL formatted for github."
(cond
((and linestart lineend)
(format "%s/blob/%s/%s#L%d-L%d" repo-url location filename linestart lineend))
(linestart (format "%s/blob/%s/%s#L%d" repo-url location filename linestart))
(t (format "%s/tree/%s/%s" repo-url location filename))))
(if linestart
(let* ((markup-p (-some? (lambda (ext) (s-ends-with-p ext filename))
browse-at-remote-github-markup-extensions))
(fstr (if markup-p "%s/blob/%s/%s?plain=1#L%d" "%s/blob/%s/%s#L%d")))
(if lineend
(format (concat fstr "-L%d") repo-url location filename linestart lineend)
(format fstr repo-url location filename linestart)))
(format "%s/tree/%s/%s" repo-url location filename)))

(defun browse-at-remote--format-commit-url-as-github (repo-url commithash)
"Commit URL formatted for github"
Expand Down

0 comments on commit ed04688

Please sign in to comment.