Skip to content

Commit 78709e1

Browse files
committed
First take at the readme
1 parent 2f288c7 commit 78709e1

File tree

1 file changed

+94
-202
lines changed

1 file changed

+94
-202
lines changed

README.md

Lines changed: 94 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @humanspeak/svelte-diff-match-patch
22

3-
A powerful, customizable markdown renderer for Svelte with TypeScript support. Built as a successor to the original svelte-diff-match-patch package by Pablo Berganza, now maintained and enhanced by Humanspeak, Inc.
3+
A powerful, customizable diff-match-patch component for Svelte with TypeScript support.
44

55
[![NPM version](https://img.shields.io/npm/v/@humanspeak/svelte-diff-match-patch.svg)](https://www.npmjs.com/package/@humanspeak/svelte-diff-match-patch)
66
[![Build Status](https://github.com/humanspeak/svelte-diff-match-patch/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/humanspeak/svelte-diff-match-patch/actions/workflows/npm-publish.yml)
@@ -16,43 +16,33 @@ A powerful, customizable markdown renderer for Svelte with TypeScript support. B
1616

1717
## Features
1818

19-
- 🚀 Full markdown syntax support through Marked
19+
- 🚀 High-performance diff algorithm implementation
2020
- 💪 Complete TypeScript support with strict typing
21-
- 🎨 Customizable component rendering system
22-
- 🔒 Secure HTML parsing via HTMLParser2
23-
- 🎯 GitHub-style slug generation for headers
24-
- ♿ WCAG 2.1 accessibility compliance
21+
- 🎨 Customizable diff rendering with CSS classes OR svelte snippets
22+
- 🔒 Safe and efficient text comparison
23+
- 🎯 Configurable cleanup algorithms (semantic and efficiency)
2524
- 🧪 Comprehensive test coverage (vitest and playwright)
2625
- 🔄 Svelte 5 runes compatibility
27-
- 🛡️ XSS protection and sanitization
28-
- 🎨 Custom Marked extensions support (e.g., GitHub-style alerts)
29-
- 🔍 Improved attribute handling and component isolation
30-
- 📦 Enhanced token cleanup and nested content support
26+
- ⚡ Configurable timeout for large text comparisons
27+
- 📊 Detailed timing and diff statistics
28+
- 🎨 Customizable diff highlighting styles
29+
- 🔍 Real-time diff updates
3130

3231
## Recent Updates
3332

3433
### New Features
3534

36-
- Improved HTML attribute isolation for nested components
37-
- Enhanced token cleanup for better nested content handling
38-
- Added proper attribute inheritance control
39-
- Implemented strict debugging checks in CI/CD pipeline
35+
- Added detailed timing information for diff operations
36+
- Enhanced cleanup algorithms for better diff results
37+
- Improved performance for large text comparisons
38+
- Added TypeScript types for all component props and events
39+
- Implemented proper state management with Svelte 5 runes
4040

4141
### Testing Improvements
4242

4343
- Enhanced Playwright E2E test coverage
44-
- Added comprehensive tests for custom extensions
44+
- Added comprehensive tests for cleanup algorithms
4545
- Improved test reliability with proper component mounting checks
46-
- Added specific test cases for nested component scenarios
47-
- **Note:** Performance tests use a higher threshold for Firefox due to slower execution in CI environments. See `tests/performance.test.ts` for details.
48-
49-
### CI/CD Enhancements
50-
51-
- Added automated debugging statement detection
52-
- Improved release workflow with GPG signing
53-
- Enhanced PR validation and automated version bumping
54-
- Added manual workflow triggers for better release control
55-
- Implemented monthly cache cleanup
5646

5747
## Installation
5848

@@ -67,49 +57,51 @@ pnpm add @humanspeak/svelte-diff-match-patch
6757
yarn add @humanspeak/svelte-diff-match-patch
6858
```
6959

70-
## External Dependencies
71-
72-
This package carefully selects its dependencies to provide a robust and maintainable solution:
73-
74-
### Core Dependencies
75-
76-
- **marked**
77-
78-
- Industry-standard markdown parser
79-
- Battle-tested in production
80-
- Extensive security features
81-
82-
- **github-slugger**
83-
84-
- GitHub-style heading ID generation
85-
- Unicode support
86-
- Collision handling
87-
88-
- **htmlparser2**
89-
90-
- High-performance HTML parsing
91-
- Streaming capabilities
92-
- Security-focused design
93-
9460
## Basic Usage
9561

9662
```svelte
9763
<script lang="ts">
98-
import SvelteMarkdown from '@humanspeak/svelte-diff-match-patch'
64+
import SvelteDiffMatchPatch from '@humanspeak/svelte-diff-match-patch'
9965
100-
const source = `
101-
# This is a header
66+
let originalText = $state(`I am the very model of a modern Major-General,
67+
I've information vegetable, animal, and mineral,
68+
I know the kings of England, and I quote the fights historical,
69+
From Marathon to Waterloo, in order categorical.`)
10270
103-
This is a paragraph with **bold** and <em>mixed HTML</em>.
71+
let modifiedText = $state(`I am the very model of a cartoon individual,
72+
My animation's comical, unusual, and whimsical,
73+
I'm quite adept at funny gags, comedic theory I have read,
74+
From wicked puns and stupid jokes to anvils that drop on your head.`)
10475
105-
* List item with \`inline code\`
106-
* And a [link](https://svelte.dev)
107-
* With nested items
108-
* Supporting full markdown
109-
`
76+
const onProcessing = (timing, diff) => {
77+
console.log('Diff timing:', timing)
78+
console.log('Diff result:', diff)
79+
}
11080
</script>
11181
112-
<SvelteMarkdown {source} />
82+
<SvelteDiffMatchPatch
83+
{originalText}
84+
{modifiedText}
85+
timeout={1}
86+
cleanupSemantic={false}
87+
cleanupEfficiency={4}
88+
{onProcessing}
89+
rendererClasses={{
90+
remove: 'diff-remove',
91+
insert: 'diff-insert',
92+
equal: 'diff-equal'
93+
}}
94+
/>
95+
96+
<style>
97+
:global(.diff-remove) {
98+
background-color: #ffd7d5;
99+
text-decoration: line-through;
100+
}
101+
:global(.diff-insert) {
102+
background-color: #d4ffd4;
103+
}
104+
</style>
113105
```
114106

115107
## TypeScript Support
@@ -118,169 +110,69 @@ The package is written in TypeScript and includes full type definitions:
118110

119111
```typescript
120112
import type {
121-
Renderers,
122-
Token,
123-
TokensList,
124-
SvelteMarkdownOptions
113+
SvelteDiffMatchPatchTiming,
114+
SvelteDiffMatchPatchDiff,
115+
SvelteDiffMatchPatchProps
125116
} from '@humanspeak/svelte-diff-match-patch'
126117
```
127118

128-
## Custom Renderer Example
119+
## Props
120+
121+
| Prop | Type | Default | Description |
122+
| ----------------- | ---------- | ------- | ---------------------------------------------- |
123+
| originalText | `string` | - | The original text to compare against |
124+
| modifiedText | `string` | - | The modified text to compare with original |
125+
| timeout | `number` | 1 | Timeout in seconds for diff computation |
126+
| cleanupSemantic | `boolean` | false | Enable semantic cleanup for better readability |
127+
| cleanupEfficiency | `number` | 4 | Efficiency cleanup level (0-4) |
128+
| onProcessing | `function` | - | Callback for timing and diff information |
129+
| rendererClasses | `object` | - | CSS classes for diff highlighting |
130+
131+
## Events
129132

130-
Here's a complete example of a custom renderer with TypeScript support:
133+
The component emits a `processing` event with timing and diff information:
131134

132135
```svelte
133136
<script lang="ts">
134-
import type { Snippet } from 'svelte'
135-
136-
interface Props {
137-
children?: Snippet
138-
href?: string
139-
title?: string
137+
import type {
138+
SvelteDiffMatchPatchTiming,
139+
SvelteDiffMatchPatchDiff
140+
} from '@humanspeak/svelte-diff-match-patch'
141+
142+
const onProcessing = (timing: SvelteDiffMatchPatchTiming, diff: SvelteDiffMatchPatchDiff) => {
143+
console.log('Diff computation time:', timing.computeTime)
144+
console.log('Cleanup time:', timing.cleanupTime)
145+
console.log('Total changes:', diff.length)
140146
}
141-
142-
const { href = '', title = '', children }: Props = $props()
143147
</script>
144148
145-
<a {href} {title} class="custom-link">
146-
{@render children?.()}
147-
</a>
148-
```
149-
150-
If you would like to extend other renderers please take a look inside the [renderers folder](https://github.com/humanspeak/svelte-diff-match-patch/tree/main/src/lib/renderers) for the default implentation of them. If you would like feature additions please feel free to open an issue!
151-
152-
## Advanced Features
153-
154-
### Table Support with Mixed Content
155-
156-
The package excels at handling complex nested structures and mixed content:
157-
158-
```markdown
159-
| Type | Content |
160-
| ---------- | --------------------------------------- |
161-
| Nested | <div>**bold** and _italic_</div> |
162-
| Mixed List | <ul><li>Item 1</li><li>Item 2</li></ul> |
163-
| Code | <code>`inline code`</code> |
149+
<SvelteDiffMatchPatch {originalText} {modifiedText} {onProcessing} />
164150
```
165151

166-
### HTML in Markdown
167-
168-
Seamlessly mix HTML and Markdown:
169-
170-
```markdown
171-
<div style="color: blue">
172-
### This is a Markdown heading inside HTML
173-
And here's some **bold** text too!
174-
</div>
175-
176-
<details>
177-
<summary>Click to expand</summary>
152+
## Cleanup Algorithms
178153

179-
- This is a markdown list
180-
- Inside an HTML details element
181-
- Supporting **bold** and _italic_ text
154+
### Semantic Cleanup
182155

183-
</details>
184-
```
185-
186-
## Available Renderers
187-
188-
- `text` - Text within other elements
189-
- `paragraph` - Paragraph (`<p>`)
190-
- `em` - Emphasis (`<em>`)
191-
- `strong` - Strong/bold (`<strong>`)
192-
- `hr` - Horizontal rule (`<hr>`)
193-
- `blockquote` - Block quote (`<blockquote>`)
194-
- `del` - Deleted/strike-through (`<del>`)
195-
- `link` - Link (`<a>`)
196-
- `image` - Image (`<img>`)
197-
- `table` - Table (`<table>`)
198-
- `tablehead` - Table head (`<thead>`)
199-
- `tablebody` - Table body (`<tbody>`)
200-
- `tablerow` - Table row (`<tr>`)
201-
- `tablecell` - Table cell (`<td>`/`<th>`)
202-
- `list` - List (`<ul>`/`<ol>`)
203-
- `listitem` - List item (`<li>`)
204-
- `heading` - Heading (`<h1>`-`<h6>`)
205-
- `codespan` - Inline code (`<code>`)
206-
- `code` - Block of code (`<pre><code>`)
207-
- `html` - HTML node
208-
- `rawtext` - All other text that is going to be included in an object above
209-
210-
### Optional List Renderers
211-
212-
For fine-grained styling:
213-
214-
- `orderedlistitem` - Items in ordered lists
215-
- `unorderedlistitem` - Items in unordered lists
216-
217-
### HTML Renderers
218-
219-
The `html` renderer is special and can be configured separately to handle HTML elements:
220-
221-
| Element | Description |
222-
| -------- | -------------------- |
223-
| `div` | Division element |
224-
| `span` | Inline container |
225-
| `table` | HTML table structure |
226-
| `thead` | Table header group |
227-
| `tbody` | Table body group |
228-
| `tr` | Table row |
229-
| `td` | Table data cell |
230-
| `th` | Table header cell |
231-
| `ul` | Unordered list |
232-
| `ol` | Ordered list |
233-
| `li` | List item |
234-
| `code` | Code block |
235-
| `em` | Emphasized text |
236-
| `strong` | Strong text |
237-
| `a` | Anchor/link |
238-
| `img` | Image |
239-
240-
You can customize HTML rendering by providing your own components:
241-
242-
```typescript
243-
import type { HtmlRenderers } from '@humanspeak/svelte-diff-match-patch'
156+
When `cleanupSemantic` is enabled, the diff algorithm will:
244157

245-
const customHtmlRenderers: Partial<HtmlRenderers> = {
246-
div: YourCustomDivComponent,
247-
span: YourCustomSpanComponent
248-
}
249-
```
158+
- Factor out commonalities that are likely to be coincidental
159+
- Improve human readability of the diff
160+
- May increase computation time for large texts
250161

251-
## Events
252-
253-
The component emits a `parsed` event when tokens are calculated:
254-
255-
```svelte
256-
<script lang="ts">
257-
import SvelteMarkdown from '@humanspeak/svelte-diff-match-patch'
258-
259-
const handleParsed = (tokens: Token[] | TokensList) => {
260-
console.log('Parsed tokens:', tokens)
261-
}
262-
</script>
263-
264-
<SvelteMarkdown {source} parsed={handleParsed} />
265-
```
266-
267-
## Props
162+
### Efficiency Cleanup
268163

269-
| Prop | Type | Description |
270-
| --------- | ----------------------- | ------------------------------------- |
271-
| source | `string \| Token[]` | Markdown content or pre-parsed tokens |
272-
| renderers | `Partial<Renderers>` | Custom component overrides |
273-
| options | `SvelteMarkdownOptions` | Marked parser configuration |
274-
| isInline | `boolean` | Toggle inline parsing mode |
164+
The `cleanupEfficiency` level (0-4) controls how aggressively the algorithm:
275165

276-
## Security
166+
- Factors out short commonalities
167+
- Reduces computational overhead
168+
- Higher values mean more aggressive cleanup
277169

278-
The package includes several security features:
170+
## Performance Considerations
279171

280-
- XSS protection through HTML sanitization
281-
- Secure HTML parsing with HTMLParser2
282-
- Safe handling of HTML entities
283-
- Protection against malicious markdown injection
172+
- For large texts, consider increasing the `timeout` value
173+
- Use `cleanupSemantic` for better readability in small to medium texts
174+
- Use `cleanupEfficiency` for better performance in large texts
175+
- Monitor the `onProcessing` callback for timing information
284176

285177
## License
286178

0 commit comments

Comments
 (0)