compat(koplugin): make meta_hash extraction more robust for calibre conversions and metadata edits, fixes #1838 (#2154)

This commit is contained in:
Huang Xin
2025-10-01 22:38:29 +08:00
committed by GitHub
parent ccad1ca0d9
commit bc8e419d51
2 changed files with 51 additions and 12 deletions
+31 -6
View File
@@ -251,10 +251,30 @@ const normalizeIdentifier = (identifier: string) => {
return identifier;
};
const getPreferredIdentifier = (identifiers: string[] | Identifier[]) => {
for (const scheme of ['uuid', 'calibre', 'isbn']) {
const found = identifiers.find((identifier) =>
typeof identifier === 'string'
? identifier.toLowerCase().includes(scheme)
: identifier.scheme.toLowerCase() === scheme,
);
if (found) {
return typeof found === 'string' ? normalizeIdentifier(found) : found.value;
}
}
return;
};
const getIdentifiersList = (
identifiers: undefined | string | string[] | Identifier | Identifier[],
) => {
if (!identifiers) return [];
if (Array.isArray(identifiers)) {
const preferred = getPreferredIdentifier(identifiers);
if (preferred) {
return [preferred];
}
}
return Array.isArray(identifiers)
? identifiers
.map((identifier) =>
@@ -267,10 +287,15 @@ const getIdentifiersList = (
};
export const getMetadataHash = (metadata: BookMetadata) => {
const title = getTitleForHash(metadata.title);
const authors = getAuthorsList(metadata.author).join(',');
const identifiers = getIdentifiersList(metadata.altIdentifier || metadata.identifier).join(',');
const hashSource = `${title}|${authors}|${identifiers}`;
const metaHash = md5(hashSource.normalize('NFC'));
return metaHash;
try {
const title = getTitleForHash(metadata.title);
const authors = getAuthorsList(metadata.author).join(',');
const identifiers = getIdentifiersList(metadata.altIdentifier || metadata.identifier).join(',');
const hashSource = `${title}|${authors}|${identifiers}`;
const metaHash = md5(hashSource.normalize('NFC'));
return metaHash;
} catch (error) {
console.error('Error generating metadata hash:', error);
}
return;
};
+20 -6
View File
@@ -312,11 +312,25 @@ function ReadestSync:generateMetadataHash()
local identifiers = doc_props.identifiers or ''
if identifiers:find("\n") then
identifiers = util.splitToArray(identifiers, "\n")
for i, id in ipairs(identifiers) do
identifiers[i] = normalizeIdentifier(id)
local list = util.splitToArray(identifiers, "\n")
local normalized = {}
local priorities = { "uuid", "calibre", "isbn" }
local preferred = nil
for i, id in ipairs(list) do
normalized[i] = normalizeIdentifier(id)
local candidate = id:lower()
for _, p in ipairs(priorities) do
if candidate:find(p, 1, true) then
preferred = normalized[i]
break
end
end
end
if preferred then
identifiers = preferred
else
identifiers = table.concat(normalized, ",")
end
identifiers = table.concat(identifiers, ",")
else
identifiers = normalizeIdentifier(identifiers)
end
@@ -327,10 +341,10 @@ end
function ReadestSync:getMetaHash()
local doc_readest_sync = self.ui.doc_settings:readSetting("readest_sync") or {}
local meta_hash = doc_readest_sync.meta_hash
local meta_hash = doc_readest_sync.meta_hash_v1
if not meta_hash then
meta_hash = self:generateMetadataHash()
doc_readest_sync.meta_hash = meta_hash
doc_readest_sync.meta_hash_v1 = meta_hash
self.ui.doc_settings:saveSetting("readest_sync", doc_readest_sync)
end
return meta_hash