2
0

diff.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. setup.placeDiffInDom = (title, oldContent, newContent, domSelector) => {
  2. setup.externalCodePromise('diff').then(()=>{
  3. const escapedChars = {
  4. /*'&' : '&',
  5. '<' : '&lt;',
  6. '>' : '&gt;',*/
  7. '"' : '&quot;',
  8. "'" : '&#39;',
  9. '`' : '&#96;'
  10. };
  11. for(const [toBeEscaped, asEscaped] of Object.entries(escapedChars))
  12. oldContent = oldContent.replaceAll(toBeEscaped,asEscaped);
  13. const unifiedDiffPatch = window.Diff.createTwoFilesPatch(title,title,oldContent,newContent,undefined,undefined,{ignoreWhitespace: true, stripTrailingCr: true});
  14. let resultingHTML = "";
  15. unifiedDiffPatch.split('\n').forEach((line)=>{
  16. if(line.startsWith('@@')){
  17. resultingHTML += `<p>${line}</p>`;
  18. }else if(line.startsWith('---')){
  19. }else if(line.startsWith('+++')){
  20. }else if(line.startsWith('-')){
  21. resultingHTML += `<p class="substraction">${line}</p>`;
  22. }else if(line.startsWith('+')){
  23. resultingHTML += `<p class="addition">${line}</p>`;
  24. }
  25. });
  26. $(domSelector).html(resultingHTML);
  27. });
  28. }