# 自动链接化单词

**URL:** https://meta.discourse.org/t/auto-linkify-words/82193
**Category:** Theme component
**Tags:** official, linkify-words
**Created:** [2018年三月5日 07:16 UTC](https://meta.discourse.org/t/auto-linkify-words/82193 "2018-03-05T07:16:06Z")
**Posts on this page:** 20
**Page:** 4

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年七月26日 15:33 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/67 "2018-07-26T15:33:20Z")

</div>

> [@brahn](#):
>
> Excuse my lack of context but why not just `RegExp("(\\b)" + escapedWords + "(\\b)", "ig")`

Looking at [Regular expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions), it seems that `\\b` is not so great in JS. For example it apparently wouldn’t work for non-latin characters like ě or š. @sam do you agree?

> [@sam](#):
>
> feel free to send in a PR for the component looks like the consensus is that it makes sense to add more stuff. I guess I support `([{}])?;!`

Sure. So if I understand correctly, I’ll just add those explicitly, right?

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年七月26日 16:38 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/68 "2018-07-26T16:38:01Z")

</div>

By the way, it seem that these autolink do not respect the “default other external links in new tab”. Even when this setting is activated, the autolinks open in the same tab. Can somebody replicate this behavior?

---

<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: [2018年七月26日 23:38 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/69 "2018-07-26T23:38:32Z")

</div>

> [@danekhollas](#):
>
> Sure. So if I understand correctly, I’ll just add those explicitly, right?

Yes, sure, just explicitly add those

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年七月27日 01:23 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/70 "2018-07-27T01:23:03Z")

</div>

I am playing with the regex, but the current behavior is a bit strange.

On one hand, we allow multiple special chars after the word, so `link_word,,,` will work, but we demand that after these characters, there is a space or endline so this does not work: `linked_word..other_word` This behavior seems both quite permissive and restrictive at the same time. 😕

You might think those are silly examples, but because we need demand space on the left as well, word lists like `link1, link2, link3` actually do not work either in the current code! 😱

Wouldn’t it be much simpler if we just checked one boundary character at both sides and did not care about the rest?

Question 2: What about `< >`?

Question 3: What about quotation marks? Probably not?

---

<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: [2018年七月27日 01:31 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/71 "2018-07-27T01:31:01Z")

</div>

> [@danekhollas](#):
>
> Wouldn’t it be much simpler if we just checked one boundary character at both sides and did not care about the rest?

I am fine to simplify this if it corrects these issues.

> [@danekhollas](#):
>
> Question 2: What about `< >` ?

Skip it cause it has special semantics in markdown (makes something an explicit link)

> [@danekhollas](#):
>
> Question 3: What about quotation marks? Probably not?

I guess they are OK

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年七月27日 02:10 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/72 "2018-07-27T02:10:57Z")

</div>

Wohoo, my first PR to Discourse got merged, thanks @sam! 🎉 😄  
[https://github.com/discourse/discourse-linkify-words/pull/2/files](https://github.com/discourse/discourse-linkify-words/pull/2/files)

Now for something more ambitious…what about supporting regexes? 👼

I can imagine many use cases:

- support for synonyms
- word cases, for example in Czech a noun can take 7 different forms depending on the context, with regex I can match all of them easily

In our actual use case, we need to generate the URL dynamically based on what was matched (linking to issues in JIRA). So ideally, something like this should work if I put it into the word list

```
/([A-Z]+-[0-9]+)/,https://ourjira.com/$1

```

I actually have a [hardcoded working version of this](https://github.com/KhanovaSkola/discourse-linkify-words-JIRA-hacked) for anyone interested.

Also, something like this could be very powerful e.g. for creating a glossary or in wiki categories.

Thoughts? 🙂

---

<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: [2018年八月14日 07:20 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/73 "2018-08-14T07:20:19Z")

</div>

@Johani now that @j.jaffeux added the new fancy control, can you have a look at using it in this component? It will make managing big lists so much easier.

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年八月14日 16:27 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/76 "2018-08-14T16:27:08Z")

</div>

I think it’s already there because it’s the new default 😉

---

<div class="post-metadata">

### Author: ![Nichalas\_Petranek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nichalas_petranek/32/92404_2.png) [@Nichalas\_Petranek](https://meta.discourse.org/u/Nichalas_Petranek)
#### Post date: [2018年九月13日 03:13 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/77 "2018-09-13T03:13:22Z")

</div>

There seems to be a bug with the links when a word or phrase gets linkified… I have `Discourse Meta` linkified and when I click on it, I get the `Oops! This page doesn't exist or is private!` message. Here is a photo of the apparent path of the link (which is the correct path btw)

 ![Screenshot_20180912-230815_Chrome](https://global.discourse-cdn.com/meta/original/3X/a/e/ae78c395c4f211b31c52431de28ff49262120eec.jpeg)

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年九月13日 06:40 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/78 "2018-09-13T06:40:50Z")

</div>

Could you psst the screenshot of the error? What is the URL with the error message?

---

<div class="post-metadata">

### Author: ![Nichalas\_Petranek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nichalas_petranek/32/92404_2.png) [@Nichalas\_Petranek](https://meta.discourse.org/u/Nichalas_Petranek)
#### Post date: [2018年九月13日 12:35 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/79 "2018-09-13T12:35:36Z")

</div>

URL: `http://oneworld-virtual.community/t/about-the-meta-category/210`

 ![Screenshot_20180913-083137_Chrome](https://global.discourse-cdn.com/meta/original/3X/c/f/cfbdc075bbc8979cd7ddfe060c1bdf5f625606a0.jpeg)

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年九月13日 18:14 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/80 "2018-09-13T18:14:41Z")

</div>

Hmm, I had a look and you may have a superfluous space at the beginning of the URL. Can you please check?

If this is the case, perhaps this theme could automatically trim spaces around URL to prevent this in the future.

---

<div class="post-metadata">

### Author: ![Nichalas\_Petranek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nichalas_petranek/32/92404_2.png) [@Nichalas\_Petranek](https://meta.discourse.org/u/Nichalas_Petranek)
#### Post date: [2018年九月13日 18:31 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/81 "2018-09-13T18:31:53Z")

</div>

The space did the trick. It works fine now

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年九月13日 18:52 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/82 "2018-09-13T18:52:14Z")

</div>

@sam I am surprised that this does not work e.g. that the code does not trim spaces (especially because OP contains examples with spaces!). Would you accept PR fix (although I don’t have time to fix it right away)

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [2018年九月13日 18:56 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/83 "2018-09-13T18:56:11Z")

</div>

> [@danekhollas](#):
>
> especially because OP contains examples with spaces!

That’s on me. I updated the screenshot / settings instructions in the OP not long ago and added those spaces. I just pushed a small fix and the component will now trim words / URLs so white-space shouldn’t matter anymore.

Thanks for the debug 👍

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年十月3日 19:45 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/84 "2018-10-03T19:45:40Z")

</div>

I’ve made a PR for supporting regular expressions. 🎉 Happy to take feedback…

> <https://github.com/discourse/discourse-linkify-words/pull/4>
>
> This PR adds support for regular expressions in the input instead of just using …a simple word.
> 
> We also support capturing variables i.e one can generate the URL dynamically based on what was matched.
> 
> Example input:
> /JIRA-(\[0-9\]+)/https://example.jira.com/$1
> 
> So "JIRA-654" will link to https://example.jira.com/654
> 
> Note that we do no use a comma as a separator
> cause we couldn't use things like /a{1,3}/
> 
> One open question: Should we allow arbitrary regexes or perhaps disallow greedy operators like + or \* which could be problematic if admin makes a mistake.

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年十月6日 23:02 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/85 "2018-10-06T23:02:15Z")

</div>

Okay, we’re getting closer with the PR. 🤞

After this gets merged, the OP will need to be updated. This is the notation that we landed upon:  
`/foo([0-9]+)/i, example.com/$1`

Note that we support capturing variables to dynamically generate the URLs, so foo234 will autolink to [example.com/234](http://example.com/234).

By default, the regex is case-sensitive (unlike the normal words), but you can add the “i” modifier. Other modifiers are not supported.

@sam also suggests to have some explanatory text below the site-settings, but thb I have no idea how that should be done…

 ![image](https://global.discourse-cdn.com/meta/original/3X/a/7/a788bbb9f79a26bbe30d284c80b7d2d718b8519e.png)

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年十月6日 23:11 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/86 "2018-10-06T23:11:50Z")

</div>

> [@fellowshipforums](#):
>
> Would it be possible to have a box like this on a ‘Tags’ page, where the user can type in specific tag words manually, and it would create a customized url based on these words? For example, the image below would produce a url something like this: [www.websitename.com/tags/word1/word2/word3](http://www.websitename.com/tags/word1/word2/word3). This would be a great feature for Tags Intersection, and would allow the user to specify the words in their intersection.

Just FYI: I don’t fully understand what you mean but it might be possible once the regular expressions are supported, see ⏫

> [@jerdog](#):
>
> One think that would be a nice addition, if possible, is to only linkify the first word in a post instead of each instance. For instance this post:

Hmm, I think it should be fairly easy todo, although there are some details to be discussed. If there is still interest in this feature, I might give it a try, although probably not right away.

---

<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: [2018年十月11日 20:13 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/87 "2018-10-11T20:13:35Z")

</div>

Feature suggestion: links in the form of prefix/suffix.

For example, “pr/1234” linking to `https://github.com/discourse/discourse/pulls/1234` and “pr/1234/files” sticking /files on the end

Configure as maybe “prefix/” and a percent sign or $0 as the replacement key

---

<div class="post-metadata">

### Author: ![danekhollas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/danekhollas/32/79637_2.png) [@danekhollas](https://meta.discourse.org/u/danekhollas)
#### Post date: [2018年十月11日 20:47 UTC](https://meta.discourse.org/t/auto-linkify-words/82193/88 "2018-10-11T20:47:01Z")

</div>

I forgot to write here, but the regex PR got merged! So I think your use case is now possible, perhaps like this:

`/pr\/([a-Z0-9]+)/, https://github.com/discourse/discourse/pull/$1`

@sam or @vinothkannans, would you mind updating the OP? Detail are in my previous post.

[上一頁](https://meta.discourse.org/t/auto-linkify-words/82193.md?page=3)

[下一頁](https://meta.discourse.org/t/auto-linkify-words/82193.md?page=5)
