Linkify words in post

theme-component

(Daniel Hollas) #67

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?


(Daniel Hollas) #68

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?


(Sam Saffron) #69

Yes, sure, just explicitly add those


(Daniel Hollas) #70

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?


(Sam Saffron) #71

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


(Daniel Hollas) #72

Wohoo, my first PR to Discourse got merged, thanks @sam! :tada: :smile:

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:


(Sam Saffron) #73

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


(Daniel Hollas) #76

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


(Nichalas Petranek) #77

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)


(Daniel Hollas) #78

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


(Nichalas Petranek) #79

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


(Daniel Hollas) #80

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.


(Nichalas Petranek) #81

The space did the trick. It works fine now


(Daniel Hollas) #82

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


(Joe) #83

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:


(Daniel Hollas) #84

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


(Daniel Hollas) #85

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…


(Daniel Hollas) #86

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.


(Kane York) #87

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


(Daniel Hollas) #88

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.