Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

fix(import): keep Education records apart when a degree wraps over several lines (RX-158) (#3804)

Amruth Pillaid772b75dee64
diff --git a/packages/api/src/features/resume/import-education-wrapped.test.ts b/packages/api/src/features/resume/import-education-wrapped.test.ts
new file mode 100644
index 000000000..2db9d3011
--- /dev/null
+++ b/packages/api/src/features/resume/import-education-wrapped.test.ts
@@ -0,0 +1,89 @@
+import type { ResumeData } from "@reactive-resume/schema/resume/data";
+import { expect, it, vi } from "vite-plus/test";
+import { call } from "@orpc/server";
+import { createResumePdfFile } from "@reactive-resume/pdf/server";
+import { defaultResumeData } from "@reactive-resume/schema/resume/default";
+import { templateSchema } from "@reactive-resume/schema/templates";
+import { generateId } from "@reactive-resume/utils/string";
+const create = vi.hoisted(() => vi.fn());
+vi.mock("./service", () => ({ resumeService: { create } }));
+vi.mock("@reactive-resume/auth/config", () => ({
+ auth: { api: { getSession: vi.fn().mockResolvedValue({ user: { id: "owner" } }) } },
+ verifyOAuthToken: vi.fn(),
+}));
+vi.mock("@reactive-resume/db/client", () => ({
+ db: {
+ select: () => ({
+ from: () => ({
+ where: () => Object.assign(Promise.resolve([]), { limit: async () => [{ id: "owner", banned: false }] }),
+ }),
+ }),
+ },
+}));
+const { importResumeFile } = await import("./imports");
+
+type Education = ResumeData["sections"]["education"]["items"][number];
+const record = (fields: Partial<Education>): Education => ({
+ id: generateId(),
+ hidden: false,
+ school: "",
+ degree: "",
+ grade: "",
+ area: "",
+ location: "",
+ period: "",
+ dates: { start: "", end: "", present: false },
+ website: { url: "", label: "", inlineLink: false },
+ description: "",
+ ...fields,
+});
+
+// RX-158: a degree long enough to wrap over several lines beside the school must stay in its own record.
+it.each(templateSchema.options)(
+ "keeps two Education records apart when the first degree wraps, in %s",
+ async (template) => {
+ const source = structuredClone(defaultResumeData);
+ source.basics.name = "Jordan Sample";
+ source.picture.hidden = true;
+ source.metadata.layout.pages = [{ fullWidth: true, main: ["education"], sidebar: [] }];
+ source.metadata.design.entry = { layout: "stacked", lead: "organization", accent: "none" };
+ source.metadata.typography.body = { ...source.metadata.typography.body, fontSize: 11.5, lineHeight: 1.9 };
+ source.sections.education.items = [
+ record({
+ school: "University of California, Berkeley",
+ degree: "Bachelor of Engineering (Honours) in Electrical Engineering and Computer Science",
+ area: "Electrical Engineering",
+ location: "Berkeley, CA",
+ period: "May 2014",
+ dates: { start: "2014-05", end: "", present: false },
+ }),
+ record({
+ school: "Imperial College London",
+ degree: "MSc",
+ area: "Data Science",
+ grade: "Distinction",
+ location: "London, UK",
+ period: "2015 – 2016",
+ dates: { start: "2015", end: "2016", present: false },
+ }),
+ ];
+ const file = await createResumePdfFile({ data: source, template, filename: `education-${template}.pdf` });
+ create.mockClear();
+ await call(importResumeFile, { file, format: "PDF" }, { context: { locale: "en-US", reqHeaders: new Headers() } });
+ const saved = create.mock.calls[0]?.[0] as { data: ResumeData };
+ const items = saved.data.sections.education.items;
+ expect(items.map((item) => item.school)).toEqual(["University of California, Berkeley", "Imperial College London"]);
+ for (const [index, original] of source.sections.education.items.entries()) {
+ expect(items[index]).toMatchObject({
+ degree: original.degree,
+ grade: original.grade,
+ area: original.area,
+ // Every word went to a field: nothing is left over as a description.
+ description: "",
+ location: original.location,
+ period: original.period,
+ });
+ }
+ },
+ 60000,
+);
diff --git a/packages/import/src/pdf-lines.ts b/packages/import/src/pdf-lines.ts
index d0bc28898..470602fd9 100644
--- a/packages/import/src/pdf-lines.ts
+++ b/packages/import/src/pdf-lines.ts
@@ -1638,6 +1638,60 @@ function educationCells(row: TextLine) {
: fields;
}
+// A long degree wraps over a few lines beside its school; more than this reads as prose, not one cell.
+const MAX_WRAPPED_CELL_LINES = 4;
+
+/**
+ * The lines a right cell wraps onto when it's too long to sit beside its school: a school alone on its row, then two
+ * or more lines in the cell's font, inset from the school's edge and ending on the right edge that the next row's
+ * right cell (location and period) ends on, whose left cell starts under the school. Null when the rows don't line up.
+ */
+function wrappedRightCell(source: ImportLine[], index: number): ImportLine[] | null {
+ const row = source[index];
+ const lead = row ? educationCells(row) : [];
+ const school = lead[0];
+ if (!row || !school || lead.length !== 1) return null;
+ const size = school.fontSize;
+ const lines: ImportLine[] = [];
+ for (let at = index + 1; at < source.length && lines.length <= MAX_WRAPPED_CELL_LINES; at++) {
+ const line = source[at];
+ const cells = line ? educationCells(line) : [];
+ const cell = cells[0];
+ const first = lines[0] ? educationCells(lines[0])[0] : cell;
+ if (!line || !cell || !first || cells.length !== 1) break;
+ if (
+ cell.fontRef === school.fontRef ||
+ cell.fontRef !== first.fontRef ||
+ cell.x <= school.x + size * 0.25 ||
+ Math.abs(cell.x + cell.width - first.x - first.width) >= size * 0.25 ||
+ line.page !== row.page ||
+ line.stream !== row.stream
+ )
+ break;
+ lines.push(line);
+ }
+ const degree = lines[0] ? educationCells(lines[0])[0] : undefined;
+ const below = source[index + 1 + lines.length];
+ const cells = below ? educationCells(below) : [];
+ const [area, tail] = cells;
+ if (
+ lines.length < 2 ||
+ lines.length > MAX_WRAPPED_CELL_LINES ||
+ !degree ||
+ !below ||
+ !area ||
+ !tail ||
+ cells.length !== 2 ||
+ area.fontRef !== degree.fontRef ||
+ Math.abs(area.x - school.x) >= size * 0.25 ||
+ Math.abs(degree.x + degree.width - tail.x - tail.width) >= size * 0.25 ||
+ below.page !== row.page ||
+ below.stream !== row.stream
+ )
+ return null;
+ return lines;
+}
+
/** Decode only corroborated stacked records; every unassigned span stays in the record description. */
function educationRecords(
source: ImportLine[],
@@ -1646,6 +1700,8 @@ function educationRecords(
): ResumeData["sections"]["education"]["items"] | null {
work.charge(source.reduce((n, row) => n + row.spans.length, 0) * 4);
const run: ImportLine[] = [];
+ // Rows folded from a wrapped right cell: the cell's first line spans the row, so it starts just past the school.
+ const folded = new Set<ImportLine>();
for (let index = 0; index < source.length; index++) {
const row = source[index];
if (!row) continue;
@@ -1679,6 +1735,18 @@ function educationRecords(
) {
run.push({ ...row, ...lineWithSpans(row, [...row.spans, ...next.spans]) });
index++;
+ continue;
+ }
+ const wrapped = wrappedRightCell(source, index);
+ if (wrapped) {
+ // Each wrapped line starts a new word: the lines' spans overlap on the page, so no gap would add the space.
+ const lines = wrapped.map((line, at) =>
+ line.spans.map((span, position) => (at > 0 && position === 0 ? { ...span, spaceBefore: true } : span)),
+ );
+ const merged = { ...row, ...lineWithSpans(row, [...row.spans, ...lines.flat()]) };
+ folded.add(merged);
+ run.push(merged);
+ index += wrapped.length;
} else run.push(row);
}
const rows = run.map(educationCells);
@@ -1703,7 +1771,7 @@ function educationRecords(
lead.fontRef !== pair.fontRef &&
Math.abs(area.x - lead.x) < lead.fontSize * 0.25 &&
area.fontRef !== lead.fontRef &&
- pair.x > lead.x + lead.fontSize &&
+ pair.x > lead.x + lead.fontSize * (run[index] && folded.has(run[index]) ? 0.25 : 1) &&
tail.x > area.x + area.fontSize &&
Math.abs(pair.x + pair.width - tail.x - tail.width) < lead.fontSize * 0.25 &&
run[index]?.page === run[index + 1]?.page &&