A powerful, customizable diff-match-patch library for Svelte with TypeScript support
npm install @humanspeak/svelte-diff-match-patchA powerful, customizable diff-match-patch component for Svelte with TypeScript support.











- ๐ High-performance diff algorithm implementation
- ๐ช Complete TypeScript support with strict typing
- ๐จ Customizable diff rendering with CSS classes OR svelte snippets
- ๐ Safe and efficient text comparison
- ๐ฏ Configurable cleanup algorithms (semantic and efficiency)
- ๐งช Comprehensive test coverage (vitest and playwright)
- ๐ Svelte 5 runes compatibility
- โก Configurable timeout for large text comparisons
- ๐ Detailed timing and diff statistics
- ๐จ Customizable diff highlighting styles
- ๐ Real-time diff updates
- Added detailed timing information for diff operations
- Enhanced cleanup algorithms for better diff results
- Improved performance for large text comparisons
- Added TypeScript types for all component props and events
- Implemented proper state management with Svelte 5 runes
- Enhanced Playwright E2E test coverage
- Added comprehensive tests for cleanup algorithms
- Improved test reliability with proper component mounting checks
``bash`
npm i -S @humanspeak/svelte-diff-match-patch
Or with your preferred package manager:
`bash`
pnpm add @humanspeak/svelte-diff-match-patch
yarn add @humanspeak/svelte-diff-match-patch
`svelte
{modifiedText}
timeout={1}
cleanupSemantic={false}
cleanupEfficiency={4}
{onProcessing}
rendererClasses={{
remove: 'diff-remove',
insert: 'diff-insert',
equal: 'diff-equal'
}}
/>
`
The package is written in TypeScript and includes full type definitions:
`typescript`
import type {
SvelteDiffMatchPatchTiming,
SvelteDiffMatchPatchDiff,
SvelteDiffMatchPatchProps
} from '@humanspeak/svelte-diff-match-patch'
| Prop | Type | Default | Description |
| ----------------- | ---------- | ------- | ---------------------------------------------- |
| originalText | string | - | The original text to compare against |string
| modifiedText | | - | The modified text to compare with original |number
| timeout | | 1 | Timeout in seconds for diff computation |boolean
| cleanupSemantic | | false | Enable semantic cleanup for better readability |number
| cleanupEfficiency | | 4 | Efficiency cleanup level (0-4) |function
| onProcessing | | - | Callback for timing and diff information |object
| rendererClasses | | - | CSS classes for diff highlighting |
You can customize how the diff is rendered using Svelte snippets. This gives you full control over the HTML structure and styling of each diff part.
`svelte
{#snippet remove(text: string)}
{text}
{/snippet}
{#snippet insert(text: string)}
{text}
{/snippet}
{#snippet equal(text: string)}
{text}
{/snippet}
{#snippet lineBreak()}
{/snippet}
`
| Snippet | Parameters | Description |
| --------- | ---------- | -------------------------------------------- |
| remove | text | Renders removed text (in originalText only) |text
| insert | | Renders inserted text (in modifiedText only) |text
| equal | | Renders unchanged text (in both texts) |
| lineBreak | - | Renders line breaks between diff sections |
You can use these snippets to:
- Customize the HTML structure of each diff part
- Apply custom styling to different types of changes
- Add additional elements or attributes
- Implement custom animations or transitions
- Add tooltips or other interactive elements
If you don't provide snippets, the component will use the default rendering with the rendererClasses prop.
The component emits a processing event with timing and diff information:
`svelte
`
When cleanupSemantic is enabled, the diff algorithm will:
- Factor out commonalities that are likely to be coincidental
- Improve human readability of the diff
- May increase computation time for large texts
The cleanupEfficiency level (0-4) controls how aggressively the algorithm:
- Factors out short commonalities
- Reduces computational overhead
- Higher values mean more aggressive cleanup
- For large texts, consider increasing the timeout valuecleanupSemantic
- Use for better readability in small to medium textscleanupEfficiency
- Use for better performance in large textsonProcessing` callback for timing information
- Monitor the
MIT ยฉ Humanspeak, Inc.
Made with โฅ by Humanspeak