mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-12-05 02:44:46 +03:00
32 lines
735 B
TypeScript
32 lines
735 B
TypeScript
import { diffWordsWithSpace, diffChars } from 'diff';
|
|
import { level } from './types';
|
|
import { escapeMarkup } from 'utils/string';
|
|
|
|
const DIFF_FN = {
|
|
word: diffWordsWithSpace,
|
|
char: diffChars
|
|
} as const;
|
|
|
|
export function compareTextsHtml(
|
|
textA: string,
|
|
textB: string,
|
|
level: level
|
|
): string {
|
|
const diffs = DIFF_FN[level](textA, textB);
|
|
|
|
const html = diffs
|
|
.map((part) => {
|
|
const val = escapeMarkup(part.value).replace(/\n/g, '<br>');
|
|
if (part.added) {
|
|
return `<span class="diff-added">${val}</span>`;
|
|
}
|
|
if (part.removed) {
|
|
return `<span class="diff-removed">${val}</span>`;
|
|
}
|
|
return val;
|
|
})
|
|
.join('');
|
|
|
|
return `<div class="diff-line">${html}</div>`;
|
|
}
|