From 34cb3509bb7ce38d7d0da2ae847e0e70d5c0680e Mon Sep 17 00:00:00 2001 From: Andrei Rybak Date: Tue, 29 Aug 2023 00:16:09 +0200 Subject: [PATCH 1/2] WIP support for copying Markdown https://github.com/orgs/community/discussions/65235 --- CHANGELOG.md | 6 ++++++ copy-commit-reference.user.js | 23 ++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 044e0e4..736f564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +- Added formatting of copied reference in GitHub Flavored Markdown format. + This is useful, for example, if you want to paste a reference to a commit on + GitHub to a comment on GitLab. GitLab's Markdown editor can pick out the GFM + content out of the clipboard when it is included alongside plain text and + HTML. + ## 1.1 - CGit: clicking on userscript's link caused some content to "jump around" on the webpage, which has been corrected. diff --git a/copy-commit-reference.user.js b/copy-commit-reference.user.js index 9088838..13a7dc0 100644 --- a/copy-commit-reference.user.js +++ b/copy-commit-reference.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Git: copy commit reference // @namespace https://github.com/rybak -// @version 1.2 +// @version 2.0-alpha // @description "Copy commit reference" for GitWeb, Cgit, GitHub, GitLab, Bitbucket, and other Git hosting sites. // @author Andrei Rybak // @license AGPL-3.0-only @@ -363,13 +363,29 @@ return html; } - #addLinkToClipboard(event, plainText, html) { + /** + * Renders given commit that has the provided subject line and date + * in reference format as GitHub Flavored Markdown content. + * + * @param {string} commitHash {@link getFullHash hash} of the commit + * @param {string} subject subject line of the commit message + * @param {string} dateIso author date of commit in ISO 8601 format + * @returns {string} a commit reference + */ + #gfmSyntaxCommitReference(commitHash, subject, dateIso) { + const url = document.location.href; + const abbrev = this.#abbreviateCommitHash(commitHash); + return `[${abbrev}](${url}) (${subject}, ${dateIso})`; + } + + #addLinkToClipboard(event, plainText, html, gfm) { event.stopPropagation(); event.preventDefault(); let clipboardData = event.clipboardData || window.clipboardData; clipboardData.setData('text/plain', plainText); clipboardData.setData('text/html', html); + clipboardData.setData('text/x-gfm', gfm); this.#showCheckmark(); setTimeout(() => this.#hideCheckmark(), 2000); } @@ -415,12 +431,13 @@ const plainText = this.#plainTextCommitReference(commitHash, subject, dateIso); const htmlSubject = await this.convertPlainSubjectToHtml(subject, commitHash); const html = this.#htmlSyntaxCommitReference(commitHash, htmlSubject, dateIso); + const gfm = this.#gfmSyntaxCommitReference(commitHash, subject, dateIso); info("plain text:", plainText); info("HTML:", html); const handleCopyEvent = e => { - this.#addLinkToClipboard(e, plainText, html); + this.#addLinkToClipboard(e, plainText, html, gfm); }; document.addEventListener('copy', handleCopyEvent); document.execCommand('copy'); From cd63e271ba496e57d40dd27d8d55d130c9cb0b0f Mon Sep 17 00:00:00 2001 From: Andrei Rybak Date: Sat, 16 Sep 2023 14:40:39 +0200 Subject: [PATCH 2/2] squash! WIP support for copying Markdown Closes https://github.com/rybak/copy-commit-reference-userscript/issues/13