Auto-Linkify Words

@md-misko thanks for the PR!

Are you sure? I am a little surprised by this. I would think that if your regex is greedy enough you should be able to match it. Note removing the the dot from the boundary characters breaks linkifying words at the end of the sentence.

I did a little test with your regex below on regex101.com and it seems to work with the current boundary characters, see regex101: build, test, and debug regex
Note that if I understand your purpose correctly, you may need to turn some of your capturing groups into non-capturing groups with (?:)

The inability of using | is very annoying, agreed. Note that for ID|id part you can just use the i modifier to make the regex case insensitive. For the numbers if you really need the exact range between 1-10 and 1-100 then it’s tricky, relaxing to 1-19 and 1-199 would make it easier. :slight_smile:

Here’s your regex with non-capturing groups that I think should work

/id\s?((?:[1-9]|10)\.(?:[1-9]|[1-9][0-9]|100))/i, https://example.com/id$2

@danekhollas thanks for the feedback and the regex! I’ve changed the code based on your comments, PR is ready for review.

1 Like

Nice! Somebody from the Discourse team will have to review this though, CC @sam

Note that you can simply install the extension from your forked repository (you can even specify a branch).

1 Like

I would like to clarify first, why are users using the component vs built in watched words?

4 Likes

I found two main issues with the built-in watched words

  • cannot add complex regexes: An error occurred: Word is too long (maximum is 100 characters)
  • cannot use arbitrary character as word boundary: namely underscore
  • also inability to edit rules or change order of execution is less then ideal

PR for the component exposes word boundaries to the user, and there are no issues with long regexes (apart from inability to use |, which is also addressed in PR).

Otherwise watched words work perfectly, and If these can be addressed in core I’m all for it.

Made this a separate post following Linkify words in post - #216 by md-misko, not sure if those qualify as bugs:

Watched words doesn’t respect unicode, it treats all unicode characters as word boundaries (when using \b, but this is to be expected I guess).

And more (running some test cases through watched words and found these two):

  • The watched word "\bid\(d+)\b" is an invalid regular expression. (true, but it still adds the rule)
  • \bid\s?(\d+)\b → https://example.com/id$1 linkifies to https://example.com/id%241 (adds urlencoded $1 instead of doing the substitution)

Is substitution not supported or is this a bug?

Has anyone found a workaround to use vertical lines | at this point? I have some regex’s where they are crucial.

Hi, thanks for developing this amazing theme, I loved it so much! Could you release this for all users to use? And not only admin

GitHub - renato/discourse-imgify-words: theme to auto imgify urls in discourse, almost the same as discourse-linkify-works

Hi,

This theme component you linked is an adaptation of Sam’s that I did as a workaround for a need you described in another topic.

They are simple theme components which only change how these words are rendered in a post (Sam’s converts specific words to links, mine converts specific words to images) based on the theme component settings, which are only managed by admins.

The feature you describe can’t be done in a theme component, it would need a plugin to store a per-user set of (word, image url) in the database and the word to image conversion should be done server-side, when building the cooked post (as HTML) content. These can’t be done in a theme component, which is only frontend/client-side code (if you hire someone to do this work it’s critical that they understand these details).

This is out of scope of this theme component and would need a way more involved work. The suggestion is to post the details of how you want this feature to behave on marketplace and you can hire someone here with prior experience with the Discourse internals to help you.

Thanks for letting me know, I’ll go in that category and talk about it

It’s been brought up many times in this topic, but I’d like to check again; is there any way to restrict this to a certain category or exclude categories? I would like to use this in our beginner’s category to have links to all the basic terms newbs need to learn, but in other areas, these would just be annoying to the experienced users. Thanks!

3 Likes

I found an annoying limitation: given a regular expression, $1 is only replaced exactly once.

Example

/foo-([0-9a-f]+)/,https://target.example/foo/$1/foo-$1

Expected

keyword link
foo-abcd https://target.example/foo/abcd/foo-abcd

Actual

keyword link
foo-abcd https://target.example/foo/abcd/foo-$1

The trouble with watched words is, that a watched word is linked as many times as the watched word appears in a post. It makes a mess.

P.S. Say that three times fast. :grin:

To be precise, the word is replaced once per line, not once per whole post. :slight_smile:

I wish that were true Coin, but it’s not on my install. :frowning:

Here’s how it works on my test instance:

:thinking:

Check this out…

My rule:

The result:

@Canapin you overlooked my issue: it’s not about repeated words, but about repeated pattern replacement of a single word: the URL contains the pattern to be replaced twice.

Since the /g pattern substitution option is default in the plugin, it should apply to all occurrences of $1 in the replacement, not just for the first one.

It’s a different case than having the keyword appear multiple times in a sentence, line, or paragraph. The replacement should account for multiple occurrences of the selection.

1 Like

I was talking about the linkify word component, not the watched words feature :slight_smile:

@hellekin, thanks for the clarification :+1:

I was responding to Sam about watched words. :grin:

Good to know linkify only does one, but would love it one per post. I may have to try this out today, watched words is unbearable at times when it finds a match.

1 Like