author fix

This commit is contained in:
Mateusz Gruszczyński
2026-07-25 18:46:31 +02:00
parent 6d10b9c2fd
commit fdf6c577ee
3 changed files with 30 additions and 9 deletions
+26 -5
View File
@@ -1,4 +1,9 @@
const VERSION = 2;
const OWNER_COLOR_SEPARATOR = "\u001f";
function ownerIdentity(owner) {
return String(owner || "").split(OWNER_COLOR_SEPARATOR, 1)[0];
}
function normalize(spans, length) {
const sorted = (Array.isArray(spans) ? spans : [])
@@ -93,7 +98,12 @@ export function replaceAuthorshipOwner(model, matcher, replacement, contentLengt
export function authorshipOwners(model) {
return [...new Set((model?.spans || []).map(span => span.owner).filter(Boolean))];
const owners = new Map();
for (const span of model?.spans || []) {
const identity = ownerIdentity(span.owner);
if (identity) owners.set(identity, span.owner);
}
return [...owners.values()];
}
export function lineAuthors(content, model) {
@@ -102,11 +112,17 @@ export function lineAuthors(content, model) {
return starts.map((start, index) => {
const end = index + 1 < starts.length ? starts[index + 1] : content.length;
const totals = new Map();
const representatives = new Map();
for (const span of model?.spans || []) {
const overlap = Math.max(0, Math.min(end, span.end) - Math.max(start, span.start));
if (overlap) totals.set(span.owner, (totals.get(span.owner) || 0) + overlap);
if (!overlap) continue;
const identity = ownerIdentity(span.owner);
totals.set(identity, (totals.get(identity) || 0) + overlap);
representatives.set(identity, span.owner);
}
return [...totals.entries()].sort((a, b) => b[1] - a[1]).map(([owner]) => owner);
return [...totals.entries()]
.sort((a, b) => b[1] - a[1])
.map(([identity]) => representatives.get(identity));
});
}
@@ -142,11 +158,16 @@ export function lineOwners(content, model) {
return starts.map((start, index) => {
const end = index + 1 < starts.length ? starts[index + 1] : content.length;
const totals = new Map();
const representatives = new Map();
for (const span of model?.spans || []) {
const overlap = Math.max(0, Math.min(end, span.end) - Math.max(start, span.start));
if (overlap) totals.set(span.owner, (totals.get(span.owner) || 0) + overlap);
if (!overlap) continue;
const identity = ownerIdentity(span.owner);
totals.set(identity, (totals.get(identity) || 0) + overlap);
representatives.set(identity, span.owner);
}
return [...totals.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] || "";
const identity = [...totals.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
return identity ? representatives.get(identity) : "";
});
}