Skip to content

Commit c2352ff

Browse files
committed
Make region URLs in GitHub use ?plain=1 for HTML rendered files
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/
1 parent 010639f commit c2352ff

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

browse-at-remote.el

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ By default is true."
9898
"List of domains where the web URL should be http."
9999
:type '(repeat string))
100100

101+
(defcustom browse-at-remote-github-markup-extensions
102+
'(".markdown" ".mdown" ".mkdn" ".md"
103+
".textile"
104+
".rdoc"
105+
"org"
106+
".creole"
107+
".mediawiki" ".wiki"
108+
".rst"
109+
".asciidoc" ".adoc" ".asc"
110+
".pod")
111+
"List of file extensions where GitHub renders the content as HTML instead of raw.
112+
See https://github.com/github/markup#markups"
113+
:type '(repeat string))
114+
101115
(defun browse-at-remote--get-url-from-remote (remote-url)
102116
"Return (DOMAIN . URL) from REMOTE-URL."
103117
;; If the protocol isn't specified, git treats it as an SSH URL.
@@ -254,11 +268,14 @@ If HEAD is detached, return nil."
254268

255269
(defun browse-at-remote--format-region-url-as-github (repo-url location filename &optional linestart lineend)
256270
"URL formatted for github."
257-
(cond
258-
((and linestart lineend)
259-
(format "%s/blob/%s/%s#L%d-L%d" repo-url location filename linestart lineend))
260-
(linestart (format "%s/blob/%s/%s#L%d" repo-url location filename linestart))
261-
(t (format "%s/tree/%s/%s" repo-url location filename))))
271+
(if linestart
272+
(let* ((markup-p (-some? (lambda (ext) (s-ends-with-p ext filename))
273+
browse-at-remote-github-markup-extensions))
274+
(fstr (if markup-p "%s/blob/%s/%s?plain=1#L%d" "%s/blob/%s/%s#L%d")))
275+
(if lineend
276+
(format (concat fstr "-L%d") repo-url location filename linestart lineend)
277+
(format fstr repo-url location filename linestart)))
278+
(format "%s/tree/%s/%s" repo-url location filename)))
262279

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

0 commit comments

Comments
 (0)