Skip to content

Conversation

@ShocKWavEv1
Copy link

No description provided.

…eve the data source from the specific fileName, handle edge case when theres no chart or chart multiple
@vercel
Copy link

vercel bot commented Nov 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
nixtla-blog Ready Ready Preview Comment Nov 21, 2025 6:32am

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

…ns and listen to specific headers to apply this formatting
const processChart = (match, chartJson, type) => {
try {
const chartData = JSON.parse(chartJson.trim());
const chartId = chartData.id || `${type}-${chartIndex++}`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const chartId = chartData.id || `${type}-${chartIndex++}`;
let chartId = chartData.id;
if (!chartId) {
// Find an ID that hasn't been used yet
while (charts[`${type}-${chartIndex}`]) {
chartIndex++;
}
chartId = `${type}-${chartIndex}`;
chartIndex++;
}

The chartIndex counter doesn't increment consistently across all charts, only when a chart lacks an explicit ID. This can cause ID collisions when mixing explicit IDs with auto-generated ones.

View Details

Analysis

Chart ID collision in extractCharts() causes data loss when mixing explicit and auto-generated IDs

What fails: The extractCharts() function in api/[fileName].js at line 207 loses charts when mixing explicit chart IDs with auto-generated ones. When a chart has an explicit ID (e.g., "chart-1") and a subsequent chart requires an auto-generated ID that happens to be the same (e.g., "chart-1"), the second chart overwrites the first in the charts object, causing permanent data loss.

How to reproduce:

// Create a markdown file with three charts:
// 1. Explicit ID "chart-1"
// 2. Auto-generated ID (no explicit id field)
// 3. Auto-generated ID (no explicit id field)

const markdown = `
\`\`\`chart
{"id": "chart-1", "title": "Chart One"}
\`\`\`

\`\`\`chart
{"title": "Chart Two"}
\`\`\`

\`\`\`chart
{"title": "Chart Three"}
\`\`\`
`;

// Process through extractCharts

Result: Only 2 charts are stored in the charts object:

  • chart-0: "Chart Two"
  • chart-1: "Chart Three" (overwrites the explicit "Chart One")

Expected: 3 charts stored with all data preserved:

  • chart-1: "Chart One"
  • chart-0: "Chart Two"
  • chart-2: "Chart Three"

Root cause: The original code only increments chartIndex when generating auto-IDs due to short-circuit evaluation of the || operator:

const chartId = chartData.id || ` 

When chartData.id is truthy (explicit ID provided), chartIndex++ never executes. This causes auto-generated IDs to potentially collide with explicit IDs that follow the type-number naming pattern.

Fix implemented: Added collision detection by checking if a generated ID is already in use before assigning it:

if (!chartId) {
  // Find an ID that hasn't been used yet
  while (charts[` 

This ensures auto-generated IDs skip any numbers that are already occupied by explicit IDs, preventing collisions entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants