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!

1.6 KB
Raw Blame
import type { CustomSectionType, ResumeData, StyleIntent, StyleSlot } from "./data";
import { sectionTypeSchema } from "./data";

export type SectionStyleRuleContext = {
	sectionId: string;
	sectionType?: CustomSectionType | undefined;
};

export type ResolveStyleRuleSlotOptions = {
	slot: StyleSlot;
	sectionId?: string | undefined;
	sectionType?: CustomSectionType | undefined;
};

export const getSectionStyleRuleContext = (
	data: ResumeData,
	sectionId: string,
	sectionTypes?: ReadonlyMap<string, CustomSectionType>,
): SectionStyleRuleContext => {
	const type = sectionId as CustomSectionType;
	if (type !== "cover-letter" && sectionTypeSchema.options.includes(type)) return { sectionId, sectionType: type };

	const sectionType = sectionTypes
		? sectionTypes.get(sectionId)
		: data.customSections.find((section) => section.id === sectionId)?.type;
	return { sectionId, sectionType };
};

export const resolveStyleIntentForSlot = (data: ResumeData, options: ResolveStyleRuleSlotOptions): StyleIntent => {
	const matchingRules = (data.metadata.styleRules ?? []).filter((rule) => {
		if (!rule.enabled) return false;
		if (!rule.slots[options.slot]) return false;

		if (rule.target.scope === "global") return true;
		if (rule.target.scope === "sectionType") return rule.target.sectionType === options.sectionType;
		return rule.target.sectionId === options.sectionId;
	});

	const specificity = { global: 0, sectionType: 1, sectionId: 2 };
	matchingRules.sort((a, b) => specificity[a.target.scope] - specificity[b.target.scope]);

	return Object.assign({}, ...matchingRules.map((rule) => rule.slots[options.slot]));
};