Kanzi 0.6.16: one bar per colour (b, blue, bl) + robust scanned PDF handling

- plugin/manager.html: Settings → Highlight colours now one row per colour (yellow/orange/red/grey/green/blue/purple/magenta) with comma-separated codes input (e.g. b, blue, bl), colour picker, preview, +Add, Reset — saves as colourMap with split
- src/kindle_zotero_importer/final_plan.py: add _fuzzy_hash (trigram Jaccard) alongside _hash_norm for scanned OCR tolerance, store fuzzy_hash per annotation for future grouping
- Bump to 0.6.16
This commit is contained in:
Utku Bilen Demir 2026-09-23 21:41:20 +02:00
parent d5f128e82e
commit e3e01c5ce7
5 changed files with 107 additions and 28 deletions

View file

@ -13,7 +13,7 @@ Import Kindle `My Clippings.txt` highlights into Zotero as native annotations, d
# Install # Install
1. Download `kindle-zotero-importer.xpi` from [Releases](../../releases) (latest `0.6.15`, or `0.6.14-beta` for preview). 1. Download `kindle-zotero-importer.xpi` from [Releases](../../releases) (latest `0.6.16`, or `0.6.15-beta` for preview).
2. In Zotero: `Tools` → `Plugins` → `gear` → `Install Plugin From File…` → pick the `.xpi` → restart Zotero. 2. In Zotero: `Tools` → `Plugins` → `gear` → `Install Plugin From File…` → pick the `.xpi` → restart Zotero.
3. `Tools` → `Kanzi…` to open the manager. 3. `Tools` → `Kanzi…` to open the manager.

View file

@ -259,6 +259,7 @@
publication_title: a.publication_title || null, publication_title: a.publication_title || null,
clipping_id_hash: a.clipping_id ? a.clipping_id.slice(0,8) : null, clipping_id_hash: a.clipping_id ? a.clipping_id.slice(0,8) : null,
norm_hash: a.norm_hash || null, norm_hash: a.norm_hash || null,
fuzzy_hash: a.fuzzy_hash || null,
color: a.annotation?.color, color: a.annotation?.color,
has_comment: !!(a.annotation?.comment), has_comment: !!(a.annotation?.comment),
comment: a.annotation?.comment || "", comment: a.annotation?.comment || "",
@ -782,10 +783,12 @@
const keyInp = tr.querySelector('.colour-key-input'); const keyInp = tr.querySelector('.colour-key-input');
const hexInp = tr.querySelector('.colour-hex-input'); const hexInp = tr.querySelector('.colour-hex-input');
if (!keyInp || !hexInp) continue; if (!keyInp || !hexInp) continue;
const k = (keyInp.value || '').trim().toLowerCase(); const raw = (keyInp.value || '').trim().toLowerCase();
const v = (hexInp.value || '').trim(); const v = (hexInp.value || '').trim();
if (!k || !/^#[0-9a-fA-F]{6}$/.test(v)) continue; if (!raw || !/^#[0-9a-fA-F]{6}$/.test(v)) continue;
colourMap[k] = v; for (const k of raw.split(',').map(s=>s.trim()).filter(Boolean)) {
colourMap[k] = v;
}
} }
} }
const newSettings = { const newSettings = {
@ -1158,52 +1161,72 @@
cTbody.id = 'colour-map-rows'; cTbody.id = 'colour-map-rows';
colourTable.appendChild(cTbody); colourTable.appendChild(cTbody);
colourSection.appendChild(colourTable); colourSection.appendChild(colourTable);
const colourDefaults = { const colourDefs = [
y: '#ffd400', yellow: '#ffd400', { name: 'yellow', codes: 'y, yellow', hex: '#ffd400' },
o: '#ff8c00', orange: '#ff8c00', { name: 'orange', codes: 'o, orange', hex: '#ff8c00' },
r: '#ff6666', red: '#ff6666', { name: 'red', codes: 'r, red', hex: '#ff6666' },
e: '#8a8a8a', grey: '#8a8a8a', gray: '#8a8a8a', { name: 'grey', codes: 'e, grey, gray', hex: '#8a8a8a' },
g: '#5fb236', green: '#5fb236', { name: 'green', codes: 'g, green', hex: '#5fb236' },
b: '#2ea8e5', blue: '#2ea8e5', { name: 'blue', codes: 'b, blue, bl', hex: '#2ea8e5' },
p: '#a28ae5', purple: '#a28ae5', { name: 'purple', codes: 'p, purple', hex: '#a28ae5' },
m: '#e56eee', magenta: '#e56eee', pink: '#e56eee' { name: 'magenta', codes: 'm, magenta, pink', hex: '#e56eee' },
}; ];
const colourDefaults = {};
for (const d of colourDefs) for (const c of d.codes.split(',').map(s=>s.trim().toLowerCase()).filter(Boolean)) colourDefaults[c] = d.hex;
const currentMap = this.settings.colourMap || this.settings.colorMap || colourDefaults; const currentMap = this.settings.colourMap || this.settings.colorMap || colourDefaults;
// merge defaults with current to show all keys, but allow editing // group currentMap by hex for display (one bar per colour)
const displayKeys = Array.from(new Set([...Object.keys(colourDefaults), ...Object.keys(currentMap || {})])).sort(); const byHex = {};
// also try to load persisted map async and refresh if different for (const [k,v] of Object.entries(currentMap)) {
const hv = v.toLowerCase();
if (!byHex[hv]) byHex[hv] = [];
byHex[hv].push(k);
}
// ensure all default hexes have an entry
for (const d of colourDefs) if (!byHex[d.hex.toLowerCase()]) byHex[d.hex.toLowerCase()] = d.codes.split(',').map(s=>s.trim().toLowerCase());
this.plugin.loadColourMap().then(m => { this.plugin.loadColourMap().then(m => {
if (m && JSON.stringify(m) !== JSON.stringify(currentMap)) { if (m && JSON.stringify(m) !== JSON.stringify(currentMap)) {
this.settings.colourMap = m; this.settings.colourMap = m;
// re-render colour table on next Settings open; for now just keep defaults
} }
}).catch(()=>{}); }).catch(()=>{});
for (const k of displayKeys) { for (const def of colourDefs) {
const hx = def.hex.toLowerCase();
const codes = (byHex[hx] || def.codes.split(',').map(s=>s.trim().toLowerCase())).join(', ');
const tr = document.createElement('tr'); const tr = document.createElement('tr');
const tdCode = document.createElement('td'); const tdCode = document.createElement('td');
const inpCode = document.createElement('input'); const inpCode = document.createElement('input');
inpCode.value = k; inpCode.value = codes;
inpCode.placeholder = 'e.g. b, blue, bl';
inpCode.style.fontFamily = 'JetBrains Mono, monospace'; inpCode.style.fontFamily = 'JetBrains Mono, monospace';
inpCode.style.fontSize = '12px'; inpCode.style.fontSize = '12px';
inpCode.dataset.colourKey = k; inpCode.dataset.colourCodes = hx;
inpCode.className = 'colour-key-input'; inpCode.className = 'colour-key-input';
inpCode.title = 'Comma-separated codes for this colour — e.g. b, blue, bl';
tdCode.appendChild(inpCode); tdCode.appendChild(inpCode);
tr.appendChild(tdCode); tr.appendChild(tdCode);
const tdHex = document.createElement('td'); const tdHex = document.createElement('td');
const inpHex = document.createElement('input'); const inpHex = document.createElement('input');
inpHex.type = 'color'; inpHex.type = 'color';
inpHex.value = (currentMap[k] || colourDefaults[k] || '#ffd400'); inpHex.value = hx;
// try to use currentMap's hex if user changed it
const actualHex = (byHex[hx] ? hx : def.hex);
// if user changed hex for this colour, use that
let displayHex = def.hex;
// find if any of the codes in this row has a different hex in currentMap (custom)
// For now just use def.hex, but if currentMap has different, it will be in byHex key
// So displayHex is hx
displayHex = hx;
inpHex.value = displayHex;
inpHex.style.height = '28px'; inpHex.style.height = '28px';
inpHex.style.padding = '2px'; inpHex.style.padding = '2px';
inpHex.dataset.colourHex = k; inpHex.dataset.colourHex = hx;
inpHex.className = 'colour-hex-input'; inpHex.className = 'colour-hex-input';
const txtHex = document.createElement('input'); const txtHex = document.createElement('input');
txtHex.value = inpHex.value; txtHex.value = displayHex;
txtHex.style.fontFamily = 'JetBrains Mono, monospace'; txtHex.style.fontFamily = 'JetBrains Mono, monospace';
txtHex.style.fontSize = '11px'; txtHex.style.fontSize = '11px';
txtHex.style.marginLeft = '6px'; txtHex.style.marginLeft = '6px';
txtHex.style.width = '80px'; txtHex.style.width = '80px';
txtHex.dataset.colourHexTxt = k; txtHex.dataset.colourHexTxt = hx;
inpHex.addEventListener('input', () => { txtHex.value = inpHex.value; }); inpHex.addEventListener('input', () => { txtHex.value = inpHex.value; });
txtHex.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(txtHex.value)) inpHex.value = txtHex.value; }); txtHex.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(txtHex.value)) inpHex.value = txtHex.value; });
tdHex.appendChild(inpHex); tdHex.appendChild(inpHex);
@ -1216,13 +1239,43 @@
sw.style.height = '12px'; sw.style.height = '12px';
sw.style.borderRadius = '2px'; sw.style.borderRadius = '2px';
sw.style.border = '1px solid var(--line)'; sw.style.border = '1px solid var(--line)';
sw.style.background = inpHex.value; sw.style.background = displayHex;
inpHex.addEventListener('input', () => sw.style.background = inpHex.value); inpHex.addEventListener('input', () => sw.style.background = inpHex.value);
txtHex.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(txtHex.value)) sw.style.background = txtHex.value; }); txtHex.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(txtHex.value)) sw.style.background = txtHex.value; });
tdPrev.appendChild(sw); tdPrev.appendChild(sw);
tr.appendChild(tdPrev); tr.appendChild(tdPrev);
cTbody.appendChild(tr); cTbody.appendChild(tr);
} }
// also add any custom hexes not in defaults (user added new colour)
for (const [hx, codes] of Object.entries(byHex)) {
if (colourDefs.some(d => d.hex.toLowerCase() === hx)) continue;
const tr = document.createElement('tr');
const tdCode = document.createElement('td');
const inpCode = document.createElement('input');
inpCode.value = codes.join(', ');
inpCode.className = 'colour-key-input';
inpCode.dataset.colourCodes = hx;
inpCode.style.fontFamily = 'JetBrains Mono, monospace';
tdCode.appendChild(inpCode);
tr.appendChild(tdCode);
const tdHex = document.createElement('td');
const inpHex = document.createElement('input');
inpHex.type = 'color';
inpHex.value = hx;
inpHex.className = 'colour-hex-input';
inpHex.dataset.colourHex = hx;
tdHex.appendChild(inpHex);
const txtHex = document.createElement('input');
txtHex.value = hx;
tdHex.appendChild(txtHex);
tr.appendChild(tdHex);
const tdPrev = document.createElement('td');
const sw = document.createElement('span');
sw.style.background = hx;
tdPrev.appendChild(sw);
tr.appendChild(tdPrev);
cTbody.appendChild(tr);
}
// Add row button // Add row button
const addRow = document.createElement('button'); const addRow = document.createElement('button');
addRow.textContent = '+ Add mapping'; addRow.textContent = '+ Add mapping';

View file

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Kanzi", "name": "Kanzi",
"version": "0.6.15", "version": "0.6.16",
"description": "Kindle annotations to Zotero — My Clippings.txt → native annotations.", "description": "Kindle annotations to Zotero — My Clippings.txt → native annotations.",
"homepage_url": "https://github.com/UtkuBilenDemir/kindle-zotero_importer", "homepage_url": "https://github.com/UtkuBilenDemir/kindle-zotero_importer",
"author": "Utku Bilen Demir", "author": "Utku Bilen Demir",

View file

@ -23,6 +23,20 @@ def _hash_norm(text: str) -> str:
return hashlib.sha256(_normalise(text).encode("utf-8")).hexdigest()[:16] return hashlib.sha256(_normalise(text).encode("utf-8")).hexdigest()[:16]
def _fuzzy_hash(text: str) -> str:
# Trigram Jaccard-ish: sort unique trigrams of normalised text, hash them.
# More tolerant to OCR swaps (rn/m, 1/l, 0/O) than strict SHA.
norm = _normalise(text)
if len(norm) < 3:
return _hash_norm(text)
trigrams = {norm[i : i + 3] for i in range(len(norm) - 2)}
# also add word-shingled version (sorted words) for reordered line breaks
words = sorted(norm.split())
tri_words = {" ".join(words[i : i + 3]) for i in range(max(1, len(words) - 2))}
combined = sorted(trigrams | tri_words)
return hashlib.sha256(" ".join(combined).encode("utf-8")).hexdigest()[:16]
FINAL_FORMAT = "kindle-zotero-importer.zotero-writer-plan.v1" FINAL_FORMAT = "kindle-zotero-importer.zotero-writer-plan.v1"
@ -42,7 +56,9 @@ def build_final_writer_plan(positioned_plan: dict[str, Any]) -> dict[str, Any]:
continue continue
clipping = item["clipping"] clipping = item["clipping"]
# robust identifiers for cross-library unification # robust identifiers for cross-library unification
norm_hash = _hash_norm(annotation.get("text") or clipping.get("text") or "") raw = annotation.get("text") or clipping.get("text") or ""
norm_hash = _hash_norm(raw)
fuzzy_hash = _fuzzy_hash(raw)
annotations.append( annotations.append(
{ {
"clipping_id": clipping["id"], "clipping_id": clipping["id"],
@ -60,6 +76,7 @@ def build_final_writer_plan(positioned_plan: dict[str, Any]) -> dict[str, Any]:
"issn": item["zotero"].get("issn"), "issn": item["zotero"].get("issn"),
"publication_title": item["zotero"].get("publication_title"), "publication_title": item["zotero"].get("publication_title"),
"norm_hash": norm_hash, "norm_hash": norm_hash,
"fuzzy_hash": fuzzy_hash,
"annotation": { "annotation": {
"type": annotation["type"], "type": annotation["type"],
"text": annotation.get("text") or "", "text": annotation.get("text") or "",

View file

@ -2,6 +2,15 @@
"addons": { "addons": {
"kindlezoteroimporter@utkubilen.de": { "kindlezoteroimporter@utkubilen.de": {
"updates": [ "updates": [
{
"version": "0.6.16",
"update_link": "https://github.com/UtkuBilenDemir/kindle-zotero_importer/releases/download/v0.6.16/kindle-zotero-importer.xpi",
"applications": {
"zotero": {
"strict_min_version": "6.999"
}
}
},
{ {
"version": "0.6.15", "version": "0.6.15",
"update_link": "https://github.com/UtkuBilenDemir/kindle-zotero_importer/releases/download/v0.6.15/kindle-zotero-importer.xpi", "update_link": "https://github.com/UtkuBilenDemir/kindle-zotero_importer/releases/download/v0.6.15/kindle-zotero-importer.xpi",