Skip to content

Instantly share code, notes, and snippets.

@oziresrds
Last active June 28, 2017 15:36
Show Gist options
  • Save oziresrds/58fe6ec157aaf3ca7236c4e606dc61b5 to your computer and use it in GitHub Desktop.
Save oziresrds/58fe6ec157aaf3ca7236c4e606dc61b5 to your computer and use it in GitHub Desktop.
Remove all blocks in rtf fonttbl code
function removeAllBlocksInRtfFonttblCode(rtf) {
let newRtf = '', firstIndex, lastIndex, count, tagOpen = 1;
firstIndex = rtf.indexOf('\\fonttbl');
count = firstIndex;
while(count < rtf.length && tagOpen > 0) {
if(rtf[count] == '{')
tagOpen++;
else if(rtf[count] == '}') {
if(tagOpen > 1)
tagOpen--;
else {
tagOpen--;
newRtf += rtf[count];
lastIndex = count;
}
} else
newRtf += rtf[count];
count++;
}
return rtf.substring(0, firstIndex) + newRtf + rtf.substring(lastIndex+1);
}
@rdsmartins
Copy link

if(rtf.indexOf("\fonttbl{") == -1){
return rtf;
}

let tokens = rtf.split("");
let tokensReverted = _.reverse(tokens);
if(tokensReverted[0] != "}"){
  let found  = false;
  tokensReverted = _.filter(tokensReverted, el=>{
    if(!found && el == "}") found = true;
    if(found){
      return el;
    }
  });
  tokens = _.reverse(tokensReverted);
  rtf = tokens.join("");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment