diff --git a/src/replace-words.js b/src/replace-words.js index e2b5859..65b6847 100644 --- a/src/replace-words.js +++ b/src/replace-words.js @@ -33,6 +33,8 @@ function replaceAll() { if (isAlsoSet) { replaceAlso(options); } + + replaceCuts(); }); } @@ -229,6 +231,12 @@ function replaceAlso(options) { }); } +/* Replaces cut statements with cuts */ +function replaceCuts() { + const searchTerm = /[Cc]ut\/([^\/]+)\/(-?[\d]+)\//; + walk(document.body, searchTerm, null, cutMethod); +} + /* Turns a term into regexp format and uses that to replace it */ function escapeAndReplace(searchTerm, replaceValue, caseSensitive) { let searchTermEscaped = searchTerm.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); @@ -393,6 +401,30 @@ function pluralPrnVerbMethod(node, searchTerm, options) { pluralPrnVerbMethod(node, searchTerm, options); } +/* Allows for custom words to be cut off */ +function cutMethod(node, searchTerm, unused) { + let match = node.nodeValue.match(searchTerm); + if (match == null) { return; } + const target = match[1]; + const amount = match[2]; + + if (target.length <= Math.abs(amount)) { + node.nodeValue = node.nodeValue.replace(searchTerm, target); + cutMethod(node, searchTerm, unused); + return; + } + + let replaceValue; + if (amount < 0) { + replaceValue = target.slice(0, amount); + } else { + replaceValue = target.slice(amount); + } + + node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); + cutMethod(node, searchTerm, unused); +} + /* Returns the input word, capitalized */ function capitalize(word) { return word.charAt(0).toUpperCase() + word.slice(1);