From e6f3280e5fff8fbdd46a5bd75c11673648750760 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Sat, 24 Sep 2022 19:22:35 +0200 Subject: [PATCH] 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/ --- browse-at-remote.el | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/browse-at-remote.el b/browse-at-remote.el index 22b1040..73a7742 100644 --- a/browse-at-remote.el +++ b/browse-at-remote.el @@ -111,6 +111,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. @@ -298,11 +312,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"