Cannot add new profane words.
adornerz opened this issue · 3 comments
I was checking out this and was trying to implement it into my (NextJS) project.
I had quite a few words to add so I manually added them on the list on "profane-words.js" file, but the filter doesn't seem to work with the added words. Also noticed that profanity.addWords() and profanity.removeWords() only works on the instance where they are triggered, but I may be wrong.
I could use some help here.
Option 1
If you want to modify the profane-words.js file, you are effectively creating a new version of the app. To do this, you'll need to take several steps:
- Uninstall the NPM package from your project:
npm uninstall @2toad/profantiy
- Download the source for this repo
- Make your changes to the source
- Compile your changes:
npm run build
- Add the "dist" folder created by the build process to your project
- Example: rename "dist" to "profanity" and add it to a "libs" folder at the root of your project
- Import the libs/profanity package into your project (instead of the @2toad/profanity npm package)
Option 2
An alternative to Option 1, which doesn't require a custom build, lets you to take advantage of the addwords()
function. You'd want to create a global instance of profanity, and within your project startup add all your custom words using a loop (e.g., forEach
), then reference that global instance of profanity throughout your project.
Hey @JasonPierce
Used the first option, and it did indeed work. Thank you!
Though, I have to ask; why aren't the new added profane words permanent by using the addWords()
function? I will need to add new words in the future and I really don't want to go with option 2, but other than that, is rebuilding the library the only other option I got?
Glad that worked for you @adornerz.
The Profanity library is designed to maintain state within an instance, but not across instances. Nor is it designed to persist state after an instance is destroyed. If you want to persist the state of an instance, you'll need to implement this yourself. This shouldn't be difficult to do, but will differ depending on your use case (e.g., browser vs node).:
- Browser: if you're using the Profanity lib in a browser, you'll want to leverage localStorage
- Node: if you're using the Profanity lib in Node, you can use WriteFile and ReadFile