Pasting a link whose text/html label is a shortened form of its URL plants a `[` inside the URL

Offen Anfängerfreundlich
#118 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
75/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Ruhig
Tech-Stack
javascript
Bereich
frontend

Rechercherichtung

Beginne in src/paste-markdown-html.ts ungefähr bei den Zeilen 83–90, wo convertToMarkdown die HTML-Bezeichnung im Klartext findet. Reproduziere den Clipboard-Fall aus dem Issue, füge dann die vier im vorgeschlagenen Fix beschriebenen Regressionstests hinzu und führe die bestehende Testsuite aus. Die Aufgabe ist erledigt, wenn verkürzte URL-Bezeichnungen korrekt ersetzt werden, ohne URLs zu beschädigen, während andere Fälle ihr bisheriges Verhalten beibehalten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

What happens

When a clipboard carries a URL as text/plain and an anchor labelled with a shortened rendering of that URL as text/html, pasting into a GitHub comment box inserts the Markdown link syntax inside the URL:

text/plain: https://github.com/owner/repo/blob/main/a.js#L7
text/html:  <a href="https://github.com/owner/repo/blob/main/a.js#L7">repo/blob/main/a.js#L7</a>

pasted:     https://github.com/owner/[repo/blob/main/a.js#L7](https://github.com/owner/repo/blob/main/a.js#L7)
expected:   [repo/blob/main/a.js#L7](https://github.com/owner/repo/blob/main/a.js#L7)

It renders as two links, one of them junk (https://github.com/owner/ on its own), and the comment has to be repaired by hand.

Reproduction

Run this in the console of any page with a GitHub comment box, press ⌘C/Ctrl+C, then paste into the box:

document.addEventListener('copy', event => {
  const url = 'https://github.com/owner/repo/blob/main/a.js#L7'
  event.clipboardData.setData('text/plain', url)
  event.clipboardData.setData('text/html', `<a href="${url}">repo/blob/main/a.js#L7</a>`)
  event.preventDefault()
}, {once: true})
Why it happens

convertToMarkdown starts from the plaintext flavour and splices [label](href) over the label at the offset where indexOf finds it:

https://github.com/github/paste-markdown/blob/main/src/paste-markdown-html.ts#L83-L90

That is correct for prose containing a link, where the plaintext is a flattening of the HTML. Here the plaintext is the URL and the label is a substring of it, so the label is found at offset 25 rather than 0 and the splice covers only the tail.

The same corruption happens when prose wraps the URL in punctuation — plaintext (https://github.com/owner/repo/blob/main/a.js#L7) yields (https://github.com/owner/[repo/…#L7](…)).

Who hits it

Any tool that writes a link to the clipboard as "URL as plaintext, shortened label as HTML" — macOS automation scripts (Hammerspoon/Alfred/Keyboard Maestro), clipboard managers, and "copy link" affordances that shorten the visible text. Labels containing a character the URL does not (repo#1234) are unaffected, because indexOf misses and the paste is left alone; every label that is a substring of its own URL is affected.

Proposed fix

#117 — replaces the whole URL when the label is a shortened rendering of it, declines rather than corrupting when the label sits inside a URL that is not its own href, and leaves every other case on the existing path. Four regression tests, all failing before the change.

Vorherrschende Sprache
JavaScript
Sterne
234
Forks
47
Ø Merge
1 T. 12 Std.
Gemergte PRs (30 T.)
5

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus github/paste-markdown

Alle Issues in github/paste-markdown

Ähnliche Issues

Weitere Issues zu JavaScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.