From 37dcd37e044f03484bbae6642de6d1416e379098 Mon Sep 17 00:00:00 2001 From: Jean Date: Thu, 26 Jun 2025 19:11:12 -0700 Subject: [PATCH] Adding mirror feature, resolves #31 --- src/replace-words.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/replace-words.js b/src/replace-words.js index fae509e..db322b5 100644 --- a/src/replace-words.js +++ b/src/replace-words.js @@ -41,6 +41,7 @@ function replaceAll() { replaceExceptions(options); replaceCuts(); replaceCapitals(); + replaceMirror(); } }); } @@ -233,12 +234,18 @@ function replaceCuts() { walk(document.body, searchTerm, null, cutMethod); } -/* Replaces cut statements with cuts */ +/* Replaces capital statements statements with requested casing */ function replaceCapitals() { const searchTerm = /(cap|Cap|CAP)\/([^\/]+)\//i; walk(document.body, searchTerm, null, capitalMethod); } +/* Replaces mirror statements with reversed phrases */ +function replaceMirror() { + const searchTerm = /mrr\/([^\/]+)\//i; + walk(document.body, searchTerm, null, mirrorMethod); +} + /* Turns a term into regexp format and uses that to replace it */ function escapeAndReplace(searchTerm, replaceValue, caseSensitive) { let searchTermEscaped = searchTerm.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); @@ -401,6 +408,18 @@ function capitalMethod(node, searchTerm, unused) { capitalMethod(node, searchTerm, unused); } +/* Allows for custom words to be mirrored */ +function mirrorMethod(node, searchTerm, unused) { + const match = node.nodeValue.match(searchTerm); + if (match == null) { return; } + const target = match[1]; + + let replaceValue = target.split('').reverse().join(''); + + node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); + mirrorMethod(node, searchTerm, unused); +} + /* Returns the input word, capitalized */ function capitalize(word) { return word.charAt(0).toUpperCase() + word.slice(1);