Skip to content

Commit 02401fe

Browse files
committed
Persist story order in URL hash fragment
When stories are reordered via drag-and-drop, the new order is saved to the URL as #order=id1,id2,id3 using entry IDs. On page load, the hash is read and applied - invalid IDs (no longer on the page) are ignored, and valid IDs from the hash are placed first with remaining stories appended in their default order. https://claude.ai/code/session_01C19UgCaTpUpdknig1mnMfj
1 parent 8d8bac8 commit 02401fe

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

blog-to-newsletter.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,37 @@ <h2>Links sent in previous newsletters</h2>
423423
// URL replacements map (old URL -> new URL)
424424
let urlReplacements = new Map();
425425

426+
// Persist story order in URL hash fragment
427+
function updateUrlHash() {
428+
const ids = storyOrder.map(s => parseInt(s.split(':')[0], 10));
429+
history.replaceState(null, '', '#order=' + ids.join(','));
430+
}
431+
432+
// Read story order from URL hash and reorder storyOrder accordingly
433+
function applyOrderFromHash() {
434+
const hash = location.hash;
435+
if (!hash.startsWith('#order=')) return;
436+
437+
const hashIds = hash.slice('#order='.length).split(',')
438+
.map(s => parseInt(s, 10))
439+
.filter(id => !isNaN(id));
440+
441+
if (hashIds.length === 0) return;
442+
443+
const storyMap = new Map(
444+
storyOrder.map(s => [parseInt(s.split(':')[0], 10), s])
445+
);
446+
const currentIds = Array.from(storyMap.keys());
447+
448+
// Keep only IDs that exist in current entries
449+
const validHashIds = hashIds.filter(id => storyMap.has(id));
450+
// Remaining IDs not mentioned in the hash, in their default order
451+
const remainingIds = currentIds.filter(id => !validHashIds.includes(id));
452+
453+
const newIdOrder = [...validHashIds, ...remainingIds];
454+
storyOrder = newIdOrder.map(id => storyMap.get(id));
455+
}
456+
426457
// SQL query
427458
const sql = `
428459
with content as (
@@ -723,6 +754,7 @@ <h2>Links sent in previous newsletters</h2>
723754

724755
// Initialize story order
725756
storyOrder = entries.map(e => `${e.id}: ${e.title}`).reverse();
757+
applyOrderFromHash();
726758

727759
renderStoryOrder();
728760
generateNewsletter();
@@ -818,6 +850,7 @@ <h2>Links sent in previous newsletters</h2>
818850

819851
// Update storyOrder array
820852
storyOrder = Array.from(storyOrderList.querySelectorAll('li')).map(el => el.textContent);
853+
updateUrlHash();
821854
generateNewsletter();
822855

823856
draggedItem = null;

0 commit comments

Comments
 (0)