GithubHelp home page GithubHelp logo

visualjerk / quill-magic-url Goto Github PK

View Code? Open in Web Editor NEW
120.0 3.0 41.0 2.43 MB

Automatically convert URLs to links in Quill

License: MIT License

JavaScript 68.98% TypeScript 31.02%
quill quilljs quill-editor url link links automatic

quill-magic-url's Introduction

quill-magic-url

tests publish

Checks for URLs and mail addresses during typing and pasting and automatically converts them to links and normalizes the links URL.

Thanks to @LFDM for the groundwork with quill-auto-links.

You can find a demo page here.

quill-magic-url in action

Install

From CDN

<!-- After quill script includes -->
<script src="https://unpkg.com/[email protected]/dist/index.js"></script>

With NPM

npm install quill-magic-url --save
import Quill from 'quill'
import MagicUrl from 'quill-magic-url'

Quill.register('modules/magicUrl', MagicUrl)

Usage

Basic usage with default options:

const quill = new Quill(editor, {
  modules: {
    magicUrl: true,
  },
})

Usage with custom options:

const quill = new Quill(editor, {
  modules: {
    magicUrl: {
      // Regex used to check URLs during typing
      urlRegularExpression: /(https?:\/\/[\S]+)|(www.[\S]+)|(tel:[\S]+)/g,
      // Regex used to check URLs on paste
      globalRegularExpression: /(https?:\/\/|www\.|tel:)[\S]+/g,
    },
  },
})

Options

urlRegularExpression

Regex used to check for URLs during typing.

Default: /(https?:\/\/|www\.)[\w-\.]+\.[\w-\.]+(\/([\S]+)?)?/gi

Example with custom Regex

magicUrl: {
  urlRegularExpression: /(https?:\/\/[\S]+)|(www.[\S]+)|(tel:[\S]+)/g
}

globalRegularExpression

Regex used to check for URLs on paste.

Default: /(https?:\/\/|www\.)[\w-\.]+\.[\w-\.]+(\/([\S]+)?)?/gi

Example with custom Regex

magicUrl: {
  globalRegularExpression: /(https?:\/\/|www\.|tel:)[\S]+/g
}

mailRegularExpression

Regex used to check for mail addresses during typing. Set to null to disable conversion of mail addresses.

Default: /([\w-\.]+@[\w-\.]+\.[\w-\.]+)/gi

Example with custom Regex

magicUrl: {
  mailRegularExpression: /([\w-\.]+@[\w-\.]+\.[\w-\.]+)/gi
}

globalMailRegularExpression

Regex used to check for mail addresses on paste. Set to null to disable conversion of mail addresses.

Default: /([\w-\.]+@[\w-\.]+\.[\w-\.]+)/gi

Example with custom Regex

magicUrl: {
  globalMailRegularExpression: /([\w-\.]+@[\w-\.]+\.[\w-\.]+)/gi
}

normalizeRegularExpression

Regex used to check for URLs to be normalized.

Default: /(https?:\/\/|www\.)[\S]+/i

You will most likely want to keep this options default value.

normalizeUrlOptions

Options for normalizing the URL

Default:

{
  stripWWW: false
}

Example with custom options

magicUrl: {
  normalizeUrlOptions: {
    stripHash: true,
    stripWWW: false,
    normalizeProtocol: false
  }
}

Available options

We use normalize-url for normalizing URLs. You can find a detailed description of the possible options here.

More infos on URL Regex

For some advanced URL Regex check this out.

quill-magic-url's People

Contributors

beaudry avatar cuddlecake avatar dependabot[bot] avatar fschindler avatar hacklone avatar heicode avatar julianlam avatar marciodsousa avatar n1ghtmare avatar pmellingimenes avatar vfonic avatar viktor-ku avatar visualjerk avatar walt1992 avatar xale-a avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

quill-magic-url's Issues

Why are UTM params being stripped away?

I've been very puzzled by a recent discovery that UTM params from links are being stripped away, and after debugging I've narrowed it down to this extension.

Eg using the demo: https://visualjerk.github.io/quill-magic-url/. If I paste the following: https://github.com/visualjerk/quill-magic-url/issues?ut_source=1&ut_campaign=2&utm_medium=3

The actual link becomes: https://github.com/visualjerk/quill-magic-url/issues?ut_campaign=2&ut_source=1

image

Any param with "utm_" is stripped away. Another example. Pasting this: https://github.com/visualjerk/quill-magic-url/issues?utm_source=1&utm_campaign=2&utm_medium=3

turns into: https://github.com/visualjerk/quill-magic-url/issues

image

Include license file

The package.json file mentions that this project has an MIT license, but there's no license file.
Could a license file be added to the project?

Version 1.0.0 breaking UglifyJs

Latest version (1.0.0) is breaking uglify -

ERROR in bundle.3ea945f01cedf37be812.js from UglifyJs
Unexpected token: operator (>) [./node_modules/quill-magic-url/dist/index.js:1

Thank you.

Feature request: case insensitive & magic email

Hi there, noticing that when capitals are used, the url is not picked up. I've added the below to this working on my side to get this working:

    magicUrl: {
        globalRegularExpression: /(https?:\/\/\.|mailto:|www.)[\S]+/gi,
        urlRegularExpression: /(https?:\/\/[\S]+)|(mailto:[\S]+)|(www.[\S]+)/i
      }

It would also be neat for automagic conversion of email addresses to mailto: links. I gave it a stab with the below, which picks up email addresses... But sadly does not magically add the mailto. I'd have to do PR on it and add it to your normalize(). Are you open to contributions?

    magicUrl: {
        globalRegularExpression: /((https?:\/\/\.|mailto:|www.)[\S]+)|(^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$)/gi,
        urlRegularExpression: /(https?:\/\/[\S]+)|(mailto:[\S]+)|(www.[\S]+)|(^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$)/i
      }

Bug: additional delta operation for newline character

Hi,

version 3.0.0 introduced the following bug:
For any text node containing a newline character an additional delta operation is added.

I've created a unit test for this:
https://github.com/fschindler/quill-magic-url/blob/adb2d775be482330fbf0fc878ab8f176cbba83ea/src/index.test.js#L37

For 9bffda9 this test fails:

    - Expected  - 1
    + Received  + 6

      Object {
    -   "ops": Array [],
    +   "ops": Array [
    +     Object {
    +       "insert": "
    + ",
    +     },
    +   ],
      }

For e8f7919 the test passes.

Problems saving the link

I'm experiencing a strange issue that may be quill-magic-url or quill itself.

I run quill in a WKWebView and use a message in the text-changed event in quill to pickup on the changes.
However if I type www.google.com the link gets link formatting, right clicking works to follow etc.
But when I look at what editor says the deltas ops are, the link attributes are gone.
The really strange thing is it appears to work if there is other text after the links insertion point.
I'm still investigating.
Any ideas would be appreciated.

Empty anchor tags causes a crash

Hello! Great project!

I found that magic url crashes when trying to parse empty anchor tags. This happened when we provided a quill field with some initial value. The smallest input I found to recreate this was <a href="http://www.google.com/"></a>.

Feature Request: CDN JS link and demo page

Can you please upload a static JS file to a CDN like unpkg.com instead of having to use Node?

Also, it would be a lot better to host a page on GitHub Pages for demo.

Not working with React-Quill

Trying to use this with React-quill but does not seem to work.
I've imported and registered it to the quill instance and set to true in modules.

I get this warning message sometimes in my console related to it
Screen Shot 2022-07-26 at 16 17 17 PM

Any way to get this working with React-quill?

Why is "babel-loader" not a dev dependency?

First of all, thanks for this great Quill module.

Is there a reason to add babel-loader as a dependency instead of a dev dependency? It causes some issues when adding this module to a project that depends on another version of babel-loader.

handling expection for ssr that window is undefined

This register itself if Quill is defined in window object.

But, on the SSR condition that has no window object, this throw error that window is undefined.

So I think this needs to handle exception on that condition. For example,

/// end of index.js

if (typeof window !== 'undfined' && window.Quill) {
  window.Quill.register('modules/magicUrl', MagicUrl);
}

Unexpected triggering of URLs on blur

There is some behavior that seems unexpected that triggers on blur.

Unexpected cases

There are two cases that seem off from my expectations:

Removing a link and changing focus

  • Enter the Quill editor
  • Enter text https://www.google.com test
  • Click to move the cursor to be between the o and the m in .com
  • Use the pop-up to remove the link
  • Click out of the editor
  • Expected: All text will be plain text
  • Actual: There is a URL for https://www.google.co with the m from .com left as plain text

Clicking into URL-like text and changing focus

  • Enter the Quill editor
  • Enter text https://www.google.com test
  • Click to move the cursor to be between the o and the m in .com
  • Use the pop-up to remove the link
  • Click to the end of the text
  • Click out of the editor
  • (Note: At this time https://www.google.com test will all be present as plain text)
  • Click to move the cursor to be between the o and the m in .com
  • Use the pop-up to remove the link
  • Click out of the editor
  • Expected: All text will be plain text
  • Actual: There is a URL for https://www.google.co with the m from .com left as plain text

Add option to checkTextForURL on each character on each text-change

Currently, quill-magic-url checks the text for URL patterns :

  • after whitespace is added
  • after the quill editor is blurred

Some editors such (eg. Mac Notes, Evernote) only do transforms after whitespace is entered. Others, such as Slack perform URL transformations as soon as a valid URL is typed (without whitespace).

It would be helpful to add an optional parameter to quill-magic-url to perform the checkTextForURL operation on each text-change, so the consumer can decide which approach is best for their particular use-case.

URLs break if you edit them

Once magic-url recognized the URLs, you cannot edit them or they break.
For example, adding a space in the middle of www.github.com turns it into one URL looking www.git linking to http://www.git and one URL looking like hub.com linking to http://www.github.com.
Additionally, if you start typing in a recognized URL, the text content changes, but the href stays the same. If you change www.github.com to www.foobar.com the href is still www.github.com.

Expected result would be, that the href mirrors the visible URL.

Link tag is not created correctly.

so using the following:

magicUrl: {
          globalRegularExpression: /(https?:\/\/|www\.|mailto:)[\S]+/g,
          urlRegularExpression: /(https?:\/\/[\S]+)|(www.[\S]+)|(mailto:[\S]+)/,
        },

if I type www.google.com -> when I display the content, when click it appends current url + www.google.com
if I type https://www.google.com -> it display the link correctly.

Do i miss something?

BTW Awesome module.

Typing in a URL but not hitting enter, tab or spacebar does not detect link properly

I'm not sure that this is going to be easy to fix, but my Quill Editor autosaves on blur, and I noticed that if I type in something that ends in a URL like the following (note there are no characters at the end of the link and this was typed in and not pasted):

Please follow this link: https://www.example.com

and either click save or just blur away from the editor, it will not detect that as a link unless I hit enter, tab or spacebar after the link before. Seems the link detection requires whitespace after the link to run the auto link detection.

This is a pretty minor issue, but just thought I would raise it anyway since there are workflows that may not end links with whitespace.

Thanks for putting this tool together. Much appreciated.

no types

There are no types for this library. Please consider refactoring to use Typescript so that it can be used with types by Typescript users

Query parameters reordered when linkified

Hi there -- this is low priority and causes issues with My Specific Implementation:tm: but most likely won't affect anyone else whatsoever.

Regardless...

When a URL is pasted, it seems any query string parameters are parsed and re-ordered into alphabetical order.

The following example string:

https://example.org?foobar=true&barbaz=quux

https://example.org?barbaz=quux&foobar=true

Will result in both URLs with the anchor https://example.org?barbaz=quux&foobar=true even though the text remains as entered.

This only caused issues for me because I have a separate piece of logic that checks for anchors and only does its thing if anchor href and anchor text are an exact match. I'll adjust it so it is less strict, but I thought maybe you might want to know about this behaviour too ๐Ÿ˜„

Magic URL doesn't break if the text is no longer a valid url while typing

I'll show an example here of the Quill magic URL demo:

Screen.Recording.2021-03-18.at.1.07.24.PM.mov

As you can see in the example above as the typed URL is no longer a valid URL, it's still linked. Also when you remove a link but the text is still valid it will auto link again. Any idea on how to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.