Linkify Words

Looking at Regular Expressions - JavaScript | MDN, 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?

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

3 Likes

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?

Yes, sure, just explicitly add those

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. :confused:

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! :scream:

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?

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

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

I guess they are OK

3 Likes

Wohoo, my first PR to Discourse got merged, thanks @sam! :tada: :smile:
https://github.com/discourse/discourse-linkify-words/pull/2/files

Now for something more ambitious…what about supporting regexes? :angel:

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 for anyone interested.

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

Thoughts? :slight_smile:

11 Likes

@Johani now that @joffreyjaffeux 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.

5 Likes

I think it’s already there because it’s the new default :wink:

3 Likes

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)

1 Like

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

2 Likes

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

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.

6 Likes

The space did the trick. It works fine now

4 Likes

@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)

3 Likes

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 :+1:

9 Likes

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

https://github.com/discourse/discourse-linkify-words/pull/4

7 Likes

Okay, we’re getting closer with the PR. :crossed_fingers:

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.

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…

3 Likes

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

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.

1 Like

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

1 Like

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.

6 Likes