Censor specific text patterns

AI-generated summary

In the Support category, user optimus asked if Discourse could censor specific text patterns, such as Social Security Numbers, beyond the standard word censoring found in site settings. codinghorror initially responded that the core software does not support wildcards or regular expressions for this purpose, noting that a plugin would be required. He explained that such a feature would necessitate the earlier implementation of hoisting code and pre blocks out of the processing pipeline to prevent regex patterns from matching inside code snippets [8]. downey added that this functionality would also help prevent accidental posting of full email addresses. optimus offered to help test the underlying work by zogstrip. Once the hoisting feature was completed, zogstrip clarified that optimus would need to write a custom plugin using Discourse.Dialect.addPreProcessor to implement the regex logic. Finally, codinghorror confirmed that support for wildcards in word censoring is now available, linking to a meta discussion for details.

Hi, I saw in Discourse that there is a way to censor words in admin/site_settings/category/posting - does anyone know of a way to censor specific patterns? For example, I foresee HR people eventually using the Discourse forum and to protect them from themselves posting sensitive info I want to automatically remove SSNs (i.e. XXX-XX-XXXX). Has anyone had this need before and found a way to do it?

Please share any advice :smile:

Cheers,
Chris

We don’t support censoring wildcards or regular expressions, would probably need to be a plugin.

Something like this would also be useful for the inclusion of full email addresses being discussed in other topics right now and caused by HTML email parsing bugs.

This would absolutely require the work that @zogstrip is doing around hoisting out code and pre blocks in the pipeline earlier.

Thanks Jeff. If @zogstrip needs help testing at any point I would be happy to help :smile:.

How is it related to the hoisting of code blocks?

Honestly, not sure…just asked about censoring text patterns. Jeff mentioned you were working on something related, maybe he can clarify :smiling_face:?

The regex to block, say, a phone number pattern, would match inside pre and code blocks unless they were hoisted out very early in the pipeline.

Ok, so now that it’s done, @optimus, you’ll need to write a plugin :wink:

Something like (not tested)

Discourse.Dialect.addPreProcessor(function(text) {
   return text.replace(/your-regexp/g, "your replacement");
}); 

This now exists, see