# "http" gets parsed incorrectly in posts

**URL:** https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866
**Category:** Bug
**Created:** [2015年二月5日 13:55 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866 "2015-02-05T13:55:17Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![anon42064797](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@anon42064797](https://meta.discourse.org/u/anon42064797)
#### Post date: [2015年二月5日 13:55 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/1 "2015-02-05T13:55:17Z")

</div>

In a post that contains the string “http”, the text after it might not get rendered if a full URL is posted “near it”.

Example:

```
The string http with some more words after it.
http://www.discourse.org
More text

```

The string http with some more words after it.

> **[Where Tech Companies Build Communities](https://www.discourse.org)**
>
> The customizable, scalable community platform powering over 22,000 communities. Create knowledge through conversation.

More text

The same thing also happens to “https”.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [2015年二月5日 14:31 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/2 "2015-02-05T14:31:41Z")

</div>

Because the cleverness of this isn’t very clear, the above post history reveals the issue (or the raw input).

 ![](https://global.discourse-cdn.com/meta/original/3X/e/3/e37613ed66a30515cad530dab99caa0462aeb854.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015年二月5日 21:34 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/3 "2015-02-05T21:34:02Z")

</div>

wow … great catch.

---

<div class="post-metadata">

### Author: ![rhulse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@rhulse](https://meta.discourse.org/u/rhulse)
#### Post date: [2015年二月6日 06:09 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/4 "2015-02-06T06:09:44Z")

</div>

Does this bug come from ruby code or external library? If it’s ruby and there is a bug open in the tracker can I have link? I could have a look into it.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015年二月6日 06:12 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/5 "2015-02-06T06:12:00Z")

</div>

This is from our markdown parser, most likely from this file

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/dialects/autolink\_dialect.js](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/dialects/autolink_dialect.js)

---

<div class="post-metadata">

### Author: ![rhulse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@rhulse](https://meta.discourse.org/u/rhulse)
#### Post date: [2015年二月6日 06:24 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/6 "2015-02-06T06:24:17Z")

</div>

OK. I can cope with JS. You want me to have a crack at it?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015年二月6日 06:25 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/7 "2015-02-06T06:25:30Z")

</div>

Sure would be more than happy for you to!

---

<div class="post-metadata">

### Author: ![rhulse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@rhulse](https://meta.discourse.org/u/rhulse)
#### Post date: [2015年二月6日 06:42 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/8 "2015-02-06T06:42:31Z")

</div>

OK. Have looked at the code.

Initial thoughts:

I had a think about what the user posts and expects from their input, and what is likely to be valid. Anyone posting a valid link is always going to include http(s): with the colon. Always. Anyone leaving that off the link is going to put a dot after the www. Always. Well they are if they want to work if it is cut and pasted.

So based on that I would first suggest (before diving into the really hard regexp) a simple change to the tokens that you look for (lines 24-25). Very roughly:

```
Discourse.Dialect.inlineRegexp(_.merge({start: 'http:'}, urlReplacerArgs));
Discourse.Dialect.inlineRegexp(_.merge({start: 'https:'}, urlReplacerArgs));
Discourse.Dialect.inlineRegexp(_.merge({start: 'www.'}, urlReplacerArgs));

```

I am not sure if the start token can be a regex, or need to be escaped, but that is the suggestion.

(EDIT: Nope. Problem is deeper. )

---

<div class="post-metadata">

### Author: ![rhulse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@rhulse](https://meta.discourse.org/u/rhulse)
#### Post date: [2015年二月6日 06:43 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/9 "2015-02-06T06:43:32Z")

</div>

To fix the regex itself the start token would have to be non-greedy. Possibly also a simple fix.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [2015年二月6日 06:49 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/10 "2015-02-06T06:49:05Z")

</div>

It might be a good idea to also look at

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/dialects/dialect.js](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/dialects/dialect.js)

to make sure the `spaceOrTagBoundary: true,` doesn’t have something to do with the problem.

Curious that both  
` with some more word`  
and  
`://www.discourse.com`  
have 20 characters

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2015年二月6日 06:52 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/11 "2015-02-06T06:52:42Z")

</div>

Preliminary testing indicates that the character count does indeed factor into it somehow.

---

<div class="post-metadata">

### Author: ![rhulse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@rhulse](https://meta.discourse.org/u/rhulse)
#### Post date: [2015年二月6日 06:58 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/12 "2015-02-06T06:58:55Z")

</div>

Thank you for the file ref, and yes that is a useful clue.

---

<div class="post-metadata">

### Author: ![chapel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chapel/32/122414_2.png) [@chapel](https://meta.discourse.org/u/chapel)
#### Post date: [2015年二月6日 08:30 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/14 "2015-02-06T08:30:41Z")

</div>

Okay, through some trial and error and some looking around I found a regex that passes this edge case and seems to be simple enough but strong enough as well.

```regex
/^((?:https?):\/\/(?:-\.)?(?:[^\s\/?\.#-]+\.?)+(?:\/[^\s]*)?)/

```

I tested it live on here by replacing the existing regex with the above one and testing how it handled the example text from the OP.

Looking at what I can change with the existing regex to have the same properties.

Found the source regex for the above here listed under `@imme_emosol`: [In search of the perfect URL validation regex](https://mathiasbynens.be/demo/url-regex)

Edit: I’ve identified the `g` and `m` flags as causing the issue with the current regex.

Should be:

```plaintext
/^((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))/

```

Edit 2: Just submitted a pull request to fix this issue.

[https://github.com/discourse/discourse/pull/3175](https://github.com/discourse/discourse/pull/3175)

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2015年二月6日 09:13 UTC](https://meta.discourse.org/t/http-gets-parsed-incorrectly-in-posts/24866/15 "2015-02-06T09:13:43Z")

</div>


