Traversing the graph
When the Text is class is used, a complex graph structure is created.
Overview
Section titled “Overview”The Node class is a doubly-linked node that forms a hierarchical tree structure. It supports both lateral traversal (siblings) and vertical traversal (parent-child relationships).
Traversal patterns
Section titled “Traversal patterns”Lateral traversal (siblings)
Section titled “Lateral traversal (siblings)”Navigate left and right within the same level:
const text = new Text("בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים");const firstWord = text.words[0];
// Move forwardconst secondWord = firstWord.next;
// Move backwardconst backToFirst = secondWord?.prev;Vertical traversal (parent-child)
Section titled “Vertical traversal (parent-child)”Navigate up and down the hierarchy:
const text = new Text("בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים");const firstWord = text.words[0];const firstSyllable = firstWord.syllables[0];
// Down: parent → childrenconst syllables = firstWord.syllables;const clusters = firstSyllable.clusters;
// Up: child → parentconst parentWord = firstSyllable.word; // or firstSyllable.parent?.valueconst parentSyllable = clusters[0].syllable; // or clusters[0].parent?.valueCombined traversal
Section titled “Combined traversal”You can combine both patterns to navigate anywhere in the graph:
const text = new Text("בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים");
const thirdWord = text.words[1]; // אֱלֹהִ֑יםconst lastSyllable = thirdWord.syllables[2]; // הִ֑יםconst firstCluster = lastSyllable.clusters[0]; // הִ֑const firstChar = firstCluster.chars[0]; // ה
// Navigate to next cluster's charsconst nextCluster = firstCluster.next;const nextChars = nextCluster?.chars;