@ -5,6 +5,7 @@
@@ -5,6 +5,7 @@
import { TextPlaceholder } from "flowbite-svelte";
import { getContext } from "svelte";
import type { Asciidoctor , Document } from "asciidoctor";
let {
address,
rootAddress,
@ -20,7 +21,7 @@
@@ -20,7 +21,7 @@
const publicationTree: PublicationTree = getContext('publicationTree');
const asciidoctor: Asciidoctor = getContext('asciidoctor');
let leafEvent: Promise< NDKEvent | null > = $derived.by(async () =>
let leafEvent: Promise< NDKEvent | null > = $derived.by(async () =>
await publicationTree.getEvent(address));
let rootEvent: Promise< NDKEvent | null > = $derived.by(async () =>
await publicationTree.getEvent(rootAddress));
@ -28,6 +29,8 @@
@@ -28,6 +29,8 @@
(await rootEvent)?.getMatchingTags('type')[0]?.[1]);
let leafHierarchy: Promise< NDKEvent [ ] > = $derived.by(async () =>
await publicationTree.getHierarchy(address));
let leafTitle: Promise< string | undefined > = $derived.by(async () =>
(await leafEvent)?.getMatchingTags('title')[0]?.[1]);
let leafContent: Promise< string | Document > = $derived.by(async () =>
asciidoctor.convert((await leafEvent)?.content ?? ''));
@ -39,40 +42,40 @@
@@ -39,40 +42,40 @@
return leaves[index - 1];
});
let previousLeafHierarchy: Promise< NDKEvent [] | null > = $derived.by(async () => {
const previousLeaf = await previousLeafEvent ;
if (!previousLeaf) {
console.debug('Finding previous leaf hierarchy for ', address) ;
if (!previousLeafEvent ) {
return null;
}
return await publicationTree.getHierarchy(previousLeafEvent? .tagAddress() ?? '' )
return await publicationTree.getHierarchy(previousLeafEvent.tagAddress());
});
let divergingBranches = $derived.by(async () => {
const currentHierarchy = await leafHierarchy ;
const previousHierarchy = await previousLeafHierarchy ;
console.debug('Finding diverging branches for ', address) ;
let [leafHierarchyValue, previousLeafHierarchyValue] = await Promise.all([leafHierarchy, previousLeafHierarchy]) ;
const branches: [NDKEvent, number][] = [];
if (!previousHierarchy) {
for (let i = 0; i < currentHierarchy .length - 1 ; i ++) {
branches.push([currentHierarchy [i], i]);
if (!previousLeaf HierarchyValue ) {
for (let i = 0; i < leafHierarchyValue .length - 1 ; i ++) {
branches.push([leafHierarchyValue [i], i]);
}
return branches;
}
const minLength = Math.min(currentHierarchy.length, previousHierarchy .length);
const minLength = Math.min(leafHierarchyValue.length, previousLeafHierarchyValue .length);
// Find the first diverging node.
let divergingIndex = 0;
while (
divergingIndex < minLength & &
currentHierarchy[divergingIndex].tagAddress() === previousHierarchy [divergingIndex].tagAddress()
leafHierarchyValue[divergingIndex].tagAddress() === previousLeafHierarchyValue [divergingIndex].tagAddress()
) {
divergingIndex++;
}
// Add all branches from the first diverging node to the current leaf.
for (let i = divergingIndex; i < currentHierarchy .length - 1 ; i ++) {
branches.push([currentHierarchy [i], i]);
for (let i = divergingIndex; i < leafHierarchyValue .length - 1 ; i ++) {
branches.push([leafHierarchyValue [i], i]);
}
return branches;
@ -90,15 +93,18 @@
@@ -90,15 +93,18 @@
< / script >
<!-- TODO: Correctly handle events that are the start of a content section. -->
< section bind:this = { sectionRef } >
{ #await Promise . all ([ leafContent , publicationType , divergingBranches ])}
< section bind:this = { sectionRef } class='publication-leather' >
{ #await Promise . all ([ leafTitle , leaf Content , leafHierarchy , publicationType , divergingBranches ])}
< TextPlaceholder size = 'xxl' / >
{ :then [ leafContent , publicationType , divergingBranches ]}
{ :then [ leafTitle , leaf Content , leafHierarchy , publicationType , divergingBranches ]}
<!-- TODO: Ensure we render all headings, not just the first one. -->
{ #each divergingBranches as [ branch , depth ]}
{ @render sectionHeading ( branch . getMatchingTags ( 'title' )[ 0 ] ? .[ 1 ] ?? '' , depth )}
{ /each }
{ #if leafTitle }
{ @const leafDepth = leafHierarchy . length - 1 }
{ @render sectionHeading ( leafTitle , leafDepth )}
{ /if }
{ @render contentParagraph ( leafContent . toString (), publicationType ?? 'article' , false )}
{ /await }
< / section >