/** * SPDX-License-Identifier: Apache-2.0 * Modified from @sanity/diff-match-patch 3.2.1 by TextDiffNext. * Changes: UTF-16 patch coordinates and URI encoding. * See THIRD-PARTY-NOTICES.md for upstream attribution. */ import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT} from '../diff/diff.js' import {encodeUriText} from '../utils/uriText.js' import {type Patch} from './createPatchObject.js' /** * Create a textual representation of a patch list. * * @param patches - Patches to stringify * @returns Text representation of patches * @public */ export function stringify(patches: Patch[]): string { return patches.map(stringifyPatch).join('') } /** * Create a textual representation of a patch. * * @param patch - Patch to stringify * @returns Text representation of patch * @public */ export function stringifyPatch(patch: Patch): string { const {length1, length2, start1, start2, diffs} = patch let coords1: string if (length1 === 0) { coords1 = `${start1},0` } else if (length1 === 1) { coords1 = `${start1 + 1}` } else { coords1 = `${start1 + 1},${length1}` } let coords2: string if (length2 === 0) { coords2 = `${start2},0` } else if (length2 === 1) { coords2 = `${start2 + 1}` } else { coords2 = `${start2 + 1},${length2}` } const text = [`@@ -${coords1} +${coords2} @@\n`] let op // Escape the body of the patch with %xx notation. for (let x = 0; x < diffs.length; x++) { switch (diffs[x][0]) { case DIFF_INSERT: op = '+' break case DIFF_DELETE: op = '-' break case DIFF_EQUAL: op = ' ' break default: throw new Error('Unknown patch operation.') } text[x + 1] = `${op + encodeUriText(diffs[x][1])}\n` } return text.join('').replace(/%20/g, ' ') }