feat: compare DjVu and e-books too (extractText via dejaview / foliate)
- DjVuRenderer.extractText(): the dejaview worker's 'text' message returns word zones per page; group them into lines by baseline. Worker setup factored into _spawnWorker() (shared with load()). - EPUBRenderer.extractText(): foliate's makeBook() parses any e-book (epub/mobi/azw3/fb2) into sections; walk each section document block-aware. - _extractText already prefers a renderer's extractText(), so both become comparable with no other changes. Now only image/comic/pptx (no text) show the 'unavailable' message. - Docs: EMBEDDING/README compare format lists updated (PDF + DjVu + e-books). Verified: EPUB diff shows changes across chapters (GPL->MIT, required->optional); DjVu extracts its text layer (757 chars) and renders the diff. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+6
-5
@@ -84,9 +84,10 @@ is not neutered). The host↔iframe handshake is origin-checked and same-origin
|
||||
|
||||
## Comparing two versions (with blame)
|
||||
|
||||
For text documents (txt, md, source code, csv/tsv, docx, rtf, odf, doc, xlsx) you
|
||||
can show a diff of two versions, with each change carrying **blame** the host
|
||||
supplies:
|
||||
For any document with extractable text — **PDF**, **DjVu**, **e-books**
|
||||
(EPUB/MOBI/AZW3/FB2), and the text/Office formats (md, docx, rtf, odf, doc, txt,
|
||||
source code, csv/tsv, xlsx) — you can show a diff of two versions, with each
|
||||
change carrying **blame** the host supplies:
|
||||
|
||||
```js
|
||||
const inst = ReaderJS.mount(container, {})
|
||||
@@ -109,8 +110,8 @@ Or compare straight from `mount`: `ReaderJS.mount(el, { compare: { a, b, blame,
|
||||
- Hover a changed line → a tooltip shows its blame; click 📌 to **pin** it open (multiple pins allowed).
|
||||
- **`blame` must be plain data** — functions don't survive `postMessage`. Keys are
|
||||
the line numbers of the *new* version (matching `git blame`).
|
||||
- Canvas/reflowable formats (pdf, djvu, images, comics, pptx, epub) aren't text-comparable
|
||||
and show an "unavailable" message.
|
||||
- Only image, comic-archive and slide (PPTX) formats have no extractable text and
|
||||
show an "unavailable" message.
|
||||
|
||||
## Content-Security-Policy
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ To give it a try without installing anything, visit the [live demo](https://alpa
|
||||
|
||||
- PDF.js-inspired UI — sidebar thumbnails, page navigation, zoom, print
|
||||
- Drag & drop or browse to open files
|
||||
- Compare two versions of a text document (side-by-side / unified / inline) with pinnable, blame-aware tooltips
|
||||
- Compare two versions of a document — PDF, e-books, Office and text formats (side-by-side / unified / inline) with pinnable, blame-aware tooltips
|
||||
- Client-side only — documents are never uploaded or transmitted
|
||||
- Multilingual UI — auto-detects your browser language with a manual switcher (English, Polish, Spanish, French, German, Portuguese, Chinese, Japanese, Russian)
|
||||
|
||||
|
||||
@@ -32,6 +32,42 @@ export class DjVuRenderer extends BaseRenderer {
|
||||
})
|
||||
}
|
||||
|
||||
// Inline new URL(...) so Vite emits the dejaview worker as its own chunk.
|
||||
_spawnWorker() {
|
||||
this._worker = new Worker(new URL('dejaview/src/worker.js', import.meta.url), { type: 'module' })
|
||||
this._worker.onmessage = (e) => {
|
||||
const { id, error } = e.data
|
||||
const p = this._pending.get(id)
|
||||
if (!p) return
|
||||
this._pending.delete(id)
|
||||
error ? p.reject(new Error(error)) : p.resolve(e.data)
|
||||
}
|
||||
}
|
||||
|
||||
// Plain text of the DjVu for the compare view — the worker returns word zones
|
||||
// ({xmin,ymin,xmax,ymax,str}) per page; group them into lines by baseline.
|
||||
async extractText(buffer) {
|
||||
this._spawnWorker()
|
||||
try {
|
||||
const meta = await this._call('open', { buffer })
|
||||
const lines = []
|
||||
for (let i = 0; i < (meta.pageCount || 1); i++) {
|
||||
const { words } = await this._call('text', { index: i })
|
||||
let line = [], lineY = null
|
||||
for (const w of (words || [])) {
|
||||
const h = (w.ymax - w.ymin) || 10
|
||||
if (lineY != null && Math.abs(w.ymin - lineY) > h * 0.7) { lines.push(line.join(' ')); line = [] }
|
||||
line.push(w.str); lineY = w.ymin
|
||||
}
|
||||
if (line.length) lines.push(line.join(' '))
|
||||
}
|
||||
return lines.join('\n')
|
||||
} finally {
|
||||
this._worker?.terminate()
|
||||
this._worker = null
|
||||
}
|
||||
}
|
||||
|
||||
_logical(info) {
|
||||
const dpi = info?.dpi || 300
|
||||
return {
|
||||
@@ -44,17 +80,7 @@ export class DjVuRenderer extends BaseRenderer {
|
||||
await super.load(buffer, container, viewer)
|
||||
const gen = ++this._gen
|
||||
|
||||
// Inline new URL(...) so Vite emits the dejaview worker as its own chunk.
|
||||
this._worker = new Worker(
|
||||
new URL('dejaview/src/worker.js', import.meta.url),
|
||||
{ type: 'module' })
|
||||
this._worker.onmessage = (e) => {
|
||||
const { id, error } = e.data
|
||||
const p = this._pending.get(id)
|
||||
if (!p) return
|
||||
this._pending.delete(id)
|
||||
error ? p.reject(new Error(error)) : p.resolve(e.data)
|
||||
}
|
||||
this._spawnWorker()
|
||||
|
||||
const meta = await this._call('open', { buffer })
|
||||
if (gen !== this._gen) return
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { BaseRenderer } from './base-renderer.js'
|
||||
// Registers the <foliate-view> custom element. foliate-js (MIT) isn't published
|
||||
// to npm, so it's pinned to a commit as a git dependency in package.json.
|
||||
import 'foliate-js/view.js'
|
||||
// `makeBook` parses any e-book (epub/mobi/azw3/fb2) into sections; importing the
|
||||
// module also registers the <foliate-view> custom element. foliate-js (MIT)
|
||||
// isn't on npm, so it's a pinned git dependency in package.json.
|
||||
import { makeBook } from 'foliate-js/view.js'
|
||||
|
||||
// Reflowable books are styled by injecting CSS into each section document.
|
||||
// "Zoom" maps to font-size since EPUB/MOBI have no fixed page geometry.
|
||||
@@ -10,6 +11,27 @@ const bookCSS = (scale) => `
|
||||
p, li, blockquote, dd { line-height: 1.5; }
|
||||
`
|
||||
|
||||
// Flatten a section document to text, breaking at block boundaries.
|
||||
const BLOCK = new Set(['P','DIV','LI','H1','H2','H3','H4','H5','H6','BLOCKQUOTE',
|
||||
'PRE','TR','SECTION','ARTICLE','FIGCAPTION','DD','DT'])
|
||||
function docText(root) {
|
||||
let out = ''
|
||||
const walk = node => {
|
||||
for (const child of node.childNodes) {
|
||||
if (child.nodeType === 3) out += child.nodeValue
|
||||
else if (child.nodeType === 1) {
|
||||
const tag = child.tagName?.toUpperCase()
|
||||
if (tag === 'BR') { out += '\n'; continue }
|
||||
if (tag === 'SCRIPT' || tag === 'STYLE') continue
|
||||
walk(child)
|
||||
if (BLOCK.has(tag)) out += '\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(root)
|
||||
return out.replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n')
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders reflowable e-books — EPUB, MOBI and KF8/AZW3 — via foliate-js's
|
||||
* <foliate-view> paginator. Page turns and the page counter are driven by
|
||||
@@ -93,6 +115,26 @@ export class EPUBRenderer extends BaseRenderer {
|
||||
getPageWidth() { return null }
|
||||
getPageHeight() { return null }
|
||||
|
||||
// Plain text of the whole book for the compare view — walk every section's
|
||||
// document (works for epub/mobi/azw3/fb2 via foliate's makeBook).
|
||||
async extractText(buffer, name) {
|
||||
const book = await makeBook(new File([buffer], name || 'book.epub'))
|
||||
try {
|
||||
const parts = []
|
||||
for (const section of (book.sections || [])) {
|
||||
if (typeof section.createDocument !== 'function') continue
|
||||
let doc
|
||||
try { doc = await section.createDocument() } catch { continue }
|
||||
const body = doc?.body || doc?.documentElement
|
||||
const text = body ? docText(body).trim() : ''
|
||||
if (text) parts.push(text)
|
||||
}
|
||||
return parts.join('\n\n')
|
||||
} finally {
|
||||
book?.destroy?.()
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (this._onResize) window.removeEventListener('resize', this._onResize)
|
||||
this._onResize = null
|
||||
|
||||
Reference in New Issue
Block a user