yorkxin / copy-as-markdown

A browser extension to copy tabs and links as Markdown
MIT License
546 stars 83 forks source link

Generated link title contains reserved characters (Brackets) in version 2.7.1 #121

Closed dank8 closed 7 months ago

dank8 commented 1 year ago

Summary

Tiles with brackets currently break the markdown coded link. Please sanitize the Title text to remove square brackets.

Reproduction Steps

  1. Open the example webpage
  2. click "copy as markdown" toolbar button.

Tested with the following options:

Expected Behavior

generated text is a valid markdown link

[Example title 2023 Asana](https://asana.com/resources/)

Optional request: can the hostname be added to the link title. example:

Actual Behavior

generated link text includes square brackets, breaking the markdown link format

[7 Types of ... [2023] • Asana](https://asana.com/resources/)

Reproducible Environment

dank8 commented 1 year ago

escaping reserved characters appears to be coded but not yet released. Cant find the issue number.

My other suggestion to add the hostname at the end of the link title. Here is modified version of function Markdown.linkTo() demonstrating an option (free for use):

function  linkTo(title, url) {
    let titleToUse;
    if (title === '') {
      titleToUse = this.constructor.DefaultTitle();
    } else {
      var hostName = '';
      try{ hostName = ' (' + url.split(/[\/\\]+/g)[1].replace(/^www\./g,'') + ')'; 
      } catch {}
      titleToUse = title + hostName;//this.escapeLinkText(title + hostName );
    }
    return `[${titleToUse}](${url})`;
  }

//some simple assertions
var result;

result = linkTo('hello world [1] all', 'https://helloworld.com/hellomonkey') === '[hello world [1] all (helloworld.com)](https://helloworld.com/hellomonkey)';
console.log( result );

result = linkTo('hello world [2] all', 'https://www.helloworld.com/hellomonkey') === '[hello world [2] all (helloworld.com)](https://www.helloworld.com/hellomonkey)';
console.log( result );

result = linkTo('hello world [3] all', 'file:///tmp/basic.htm') === '[hello world [3] all (tmp)](file:///tmp/basic.htm)';
console.log( result );
yorkxin commented 7 months ago

Sorry, just noticed this issue post. Most Markdown processors handle nested []s correctly. If your processor does not handle it properly, please turn on "always escape" in the Options page of Copy as Markdown (under Advanced section), so that it will always escape brackets in the link title. Let me know if you still run into this trouble when using this option. Thanks!