Skip to content

Commit d9905c6

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 c020975 commit d9905c6

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
@@ -102,6 +102,20 @@ By default is true."
102102
"List of hosts where the URL protocol should be http."
103103
:type '(repeat string))
104104

105+
(defcustom browse-at-remote-github-markup-extensions
106+
'(".markdown" ".mdown" ".mkdn" ".md"
107+
".textile"
108+
".rdoc"
109+
"org"
110+
".creole"
111+
".mediawiki" ".wiki"
112+
".rst"
113+
".asciidoc" ".adoc" ".asc"
114+
".pod")
115+
"List of file extensions where GitHub renders the content as HTML instead of raw.
116+
See https://github.com/github/markup#markups"
117+
:type '(repeat string))
118+
105119
(defun browse-at-remote--get-url-from-remote (remote-url)
106120
"Return a plist describing REMOTE-URL."
107121
;; If the protocol isn't specified, git treats it as an SSH URL.
@@ -277,11 +291,14 @@ related remote in `browse-at-remote-remote-type-regexps'."
277291

278292
(defun browse-at-remote--format-region-url-as-github (repo-url location filename &optional linestart lineend)
279293
"URL formatted for github."
280-
(cond
281-
((and linestart lineend)
282-
(format "%s/blob/%s/%s#L%d-L%d" repo-url location filename linestart lineend))
283-
(linestart (format "%s/blob/%s/%s#L%d" repo-url location filename linestart))
284-
(t (format "%s/tree/%s/%s" repo-url location filename))))
294+
(if linestart
295+
(let* ((markup-p (-some? (lambda (ext) (s-ends-with-p ext filename))
296+
browse-at-remote-github-markup-extensions))
297+
(fstr (if markup-p "%s/blob/%s/%s?plain=1#L%d" "%s/blob/%s/%s#L%d")))
298+
(if lineend
299+
(format (concat fstr "-L%d") repo-url location filename linestart lineend)
300+
(format fstr repo-url location filename linestart)))
301+
(format "%s/tree/%s/%s" repo-url location filename)))
285302

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

0 commit comments

Comments
 (0)