UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

49 lines (48 loc) 1.65 kB
export const getDateForNode = (node, lineBlame) => { if (!node.position) { return new Date().toISOString(); } const startLine = node.position.start.line; const blameDate = lineBlame[startLine]; if (blameDate) { const parsedDate = new Date(blameDate); if (!isNaN(parsedDate.getTime())) { return parsedDate.toISOString(); } } return new Date().toISOString(); }; export const getMostRepresentativeDate = (updateComponent, lineBlame, defaultDate) => { const dates = []; const collectDates = (nodes) => { for (const node of nodes) { if (node.type !== 'heading' && node.position) { const nodeDate = getDateForNode(node, lineBlame); dates.push(nodeDate); } if ('children' in node && Array.isArray(node.children) && node.children.length > 0) { collectDates(node.children); } } }; collectDates(updateComponent.children); if (dates.length === 0) { return defaultDate; } const dateCounts = new Map(); for (const date of dates) { dateCounts.set(date, (dateCounts.get(date) || 0) + 1); } let mostCommonDate = ''; let maxCount = 0; for (const [date, count] of dateCounts.entries()) { const isTiedOlder = count === maxCount && mostCommonDate && new Date(date).getTime() < new Date(mostCommonDate).getTime(); if (count > maxCount || isTiedOlder) { maxCount = count; mostCommonDate = date; } } return mostCommonDate || defaultDate; };