Story Data Map (JSON)
Export format for story review. Not a direct Twine import file.
// Map data will be generated here.
Story: Untitled Narrative
No passages defined.
';
return;
}
twineMap.forEach(item => {
const outgoingLinks = (item.links_raw.match(/->/g) || []).length;
const div = document.createElement('div');
div.className = 'twine-list-item';
div.innerHTML = `
${item.title}
Links Out: ${outgoingLinks} | Tags: ${item.tags.join(', ') || 'none'}
`;
container.appendChild(div);
});
};
window.twineUpdateDashboard = function() {
const totalNodes = twineMap.length;
let deadEnds = 0;
let totalLinks = 0;
const linkCounts = {};
// Find all target titles from the raw links
const allLinks = [];
twineMap.forEach(node => {
const links = (node.links_raw.match(/->([A-Za-z0-9_\s]+)]]/g) || []);
const targets = links.map(l => l.replace(/\[\[.*->/g, '').replace(']]', '').trim());
targets.forEach(t => allLinks.push(t));
totalLinks += targets.length;
if (targets.length === 0) deadEnds++;
// For chart data (complexity per node)
linkCounts[node.title] = targets.length;
});
// Refine dead ends: check if node title is a target anywhere
const allNodeTitles = twineMap.map(n => n.title);
deadEnds = allNodeTitles.filter(title => {
// It's a dead end if it has no outgoing links AND it is not the starting node
const node = twineMap.find(n => n.title === title);
return node.links_raw.length === 0 && title !== "Start";
}).length;
// Update Metrics
document.getElementById('twine-total-nodes').innerText = totalNodes;
document.getElementById('twine-dead-ends').innerText = deadEnds;
// Generate JSON/Data Map
twineRenderJSON();
// Update Chart
if(twineChart) {
twineChart.data.labels = Object.keys(linkCounts);
twineChart.data.datasets[0].data = Object.values(linkCounts);
twineChart.update();
}
};
window.twineRenderJSON = function() {
const metadata = {
story_title: document.getElementById('twine-cfg-title').value,
author: document.getElementById('twine-cfg-author').value || "Anonymous",
total_passages: twineMap.length,
date_generated: new Date().toISOString()
};
const structuredMap = twineMap.map(p => {
// Extract link targets and text using a simple regex match
const linkRegex = /\[\[(.+?)->(.+?)\]\]/g;
let match;
let linksParsed = [];
while ((match = linkRegex.exec(p.links_raw)) !== null) {
linksParsed.push({
link_text: match[1].trim(),
target_passage: match[2].trim()
});
}
return {
id: p.id,
title: p.title,
content_excerpt: p.text.substring(0, 80) + '...',
tags: p.tags,
links_out: linksParsed
};
});
const finalData = { metadata, passages: structuredMap };
document.getElementById('twine-output-json').innerText = JSON.stringify(finalData, null, 2);
};
window.twineResetData = function(silent = false) {
if(silent || confirm("Clear all story data and reset to template?")) {
twineMap = [
{ id: currentUniqueId++, title: "Start", text: "Welcome to the story. This is the first decision point.", tags: ["start"], links_raw: "[[Go North->North Path]] [[Go East->East Path]]" }
];
document.getElementById('twine-in-title').value = "Start";
document.getElementById('twine-cfg-title').value = "The Whispering Crossroads";
twineRenderPassageList();
twineUpdateDashboard();
if(!silent) alert("Story map reset.");
}
};
// --- Chart Logic ---
window.twineInitChart = function() {
const ctx = document.getElementById('twine-chart').getContext('2d');
twineChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: 'Outgoing Links',
data: [],
backgroundColor: '#6c5ce7',
borderColor: '#6c5ce7',
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: { beginAtZero: true, ticks: { stepSize: 1 }, grid: { color: '#333' } },
x: { ticks: { autoSkip: true, maxRotation: 45 }, grid: { color: '#333' } }
},
plugins: { legend: { display: false } }
}
});
};
// --- PDF Export ---
window.twineDownloadPDF = function() {
const element = document.getElementById('twine-export-area');
// Get the raw JSON string
const jsonText = document.getElementById('twine-output-json').innerText;
// Create a clean, printable wrapper
const tempDiv = document.createElement('div');
tempDiv.style.padding = '20px';
tempDiv.style.color = '#000'; // Black text for print
tempDiv.style.fontFamily = 'serif';
tempDiv.innerHTML = `
${document.getElementById('twine-cfg-title').value} - Story Flow Report
Author: ${document.getElementById('twine-cfg-author').value || 'Anonymous'}
Data Map (JSON Structure)
${jsonText}
Passage List Summary
${twineMap.map(p => `- ${p.title} (${(p.links_raw.match(/->/g) || []).length} Links) - ${p.text.substring(0, 50)}...
`).join('')}
`;
const opt = {
margin: 0.5,
filename: 'Twine_Story_Map.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
html2pdf().set(opt).from(tempDiv).save();
};
})();